@@ -675,3 +675,153 @@ def coord_or_list_to_dict(data: Union[Coordinates, list, dict]) -> dict:
675675
676676 # Return the dictionary
677677 return data_copy
678+
679+
680+ def polygon_feature_to_bbox_list (
681+ polygon : dict , is_bbox_list_required : bool = True
682+ ) -> typing .Union [list , dict ]:
683+ """
684+ Converts a polygon to a bounding box
685+
686+ The polygon below has been obtained from https://geojson.io/. If you have a polygon,
687+ with only 4 array elements, then simply take the first element and append it to the
688+ coordinates to obtain the below example.
689+
690+ Usage::
691+
692+ >>> from mapillary.utils.format import polygon_feature_to_bbox_list
693+ >>> bbox = polygon_feature_to_bbox_list(polygon={
694+ ... "type": "Feature",
695+ ... "properties": {},
696+ ... "geometry": {
697+ ... "type": "Polygon",
698+ ... "coordinates": [
699+ ... [
700+ ... [
701+ ... 48.1640625,
702+ ... 38.41055825094609
703+ ... ],
704+ ... [
705+ ... 62.22656249999999,
706+ ... 38.41055825094609
707+ ... ],
708+ ... [
709+ ... 62.22656249999999,
710+ ... 45.336701909968134
711+ ... ],
712+ ... [
713+ ... 48.1640625,
714+ ... 45.336701909968134
715+ ... ],
716+ ... [
717+ ... 48.1640625,
718+ ... 38.41055825094609
719+ ... ]
720+ ... ]
721+ ... ]
722+ ... })
723+ >>> bbox
724+ ... [62.22656249999999, 48.1640625, 38.41055825094609, 45.336701909968134]
725+
726+ :param polygon: The polygon to convert
727+ :type polygon: dict
728+
729+ :param is_bbox_list_required: Flag if bbox is required as a list. If true, returns a list,
730+ else returns a dict
731+ :type is_bbox_list_required: bool
732+ :default is_bbox_list_required: True
733+
734+ :return: The bounding box
735+ :rtype: typing.Union[list, dict]
736+ """
737+
738+ west = polygon ["geometry" ]["coordinates" ][0 ][1 ][0 ]
739+ south = polygon ["geometry" ]["coordinates" ][0 ][0 ][0 ]
740+ east = polygon ["geometry" ]["coordinates" ][0 ][0 ][1 ]
741+ north = polygon ["geometry" ]["coordinates" ][0 ][2 ][1 ]
742+
743+ if is_bbox_list_required :
744+ return [west , south , east , north ]
745+
746+ return {"west" , west , "south" , south , "east" , east , "north" , north }
747+
748+
749+ def bbox_to_polygon (bbox : typing .Union [list , dict ]) -> dict :
750+ """
751+ Converts a bounding box dictionary to a polygon
752+
753+ Usage::
754+
755+ >>> from mapillary.utils.format import bbox_to_polygon
756+ >>> bbox = [62.22656249999999, 48.1640625, 38.41055825094609, 45.336701909968134]
757+ >>> polygon = bbox_to_polygon(bbox=bbox)
758+ >>> polygon
759+ ... {
760+ ... "type": "Feature",
761+ ... "properties": {},
762+ ... "geometry": {
763+ ... "type": "Polygon",
764+ ... "coordinates": [
765+ ... [
766+ ... [
767+ ... 48.1640625,
768+ ... 38.41055825094609
769+ ... ],
770+ ... [
771+ ... 62.22656249999999,
772+ ... 38.41055825094609
773+ ... ],
774+ ... [
775+ ... 62.22656249999999,
776+ ... 45.336701909968134
777+ ... ],
778+ ... [
779+ ... 48.1640625,
780+ ... 45.336701909968134
781+ ... ],
782+ ... [
783+ ... 48.1640625,
784+ ... 38.41055825094609
785+ ... ]
786+ ... ]
787+ ... ]
788+ ... })
789+
790+ :param bbox: The bounding box to convert
791+ :type bbox: dict
792+
793+ :return: The polygon
794+ :rtype: dict
795+ """
796+
797+ # Initializing the polygon
798+ polygon = {
799+ "type" : "Feature" ,
800+ "properties" : {},
801+ "geometry" : {
802+ "type" : "Polygon" ,
803+ "coordinates" : [[[0 , 0 ], [0 , 0 ], [0 , 0 ], [0 , 0 ], [0 , 0 ]]],
804+ },
805+ }
806+
807+ # If bbox is a list, convert to dict
808+ if isinstance (bbox , list ):
809+ bbox = {"west" : bbox [0 ], "south" : bbox [1 ], "east" : bbox [2 ], "north" : bbox [3 ]}
810+
811+ # For the top left to the bottom left
812+ polygon ["geometry" ]["coordinates" ][0 ][0 ] = [bbox ["south" ], bbox ["east" ]]
813+
814+ # from the bottom left to the bottom right
815+ polygon ["geometry" ]["coordinates" ][0 ][1 ] = [bbox ["west" ], bbox ["east" ]]
816+
817+ # from the bottom right to the top right
818+ polygon ["geometry" ]["coordinates" ][0 ][2 ] = [bbox ["west" ], bbox ["north" ]]
819+
820+ # from the top right to the top left
821+ polygon ["geometry" ]["coordinates" ][0 ][3 ] = [bbox ["south" ], bbox ["north" ]]
822+
823+ # from the top left to the top left
824+ polygon ["geometry" ]["coordinates" ][0 ][4 ] = [bbox ["south" ], bbox ["east" ]]
825+
826+ # Returning the results
827+ return polygon
0 commit comments