Skip to content

Commit c359b02

Browse files
committed
✨ feat: add util functions, interface for config
Signed-off-by: saif <[email protected]>
1 parent 7f8a19f commit c359b02

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

src/mapillary/interface.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
# Models
2525
from mapillary.models.geojson import Coordinates, GeoJSON
26+
from mapillary.models.config import Config
2627

2728
# Exception classes
2829
from mapillary.models.exceptions import InvalidOptionError
@@ -34,6 +35,35 @@
3435
import mapillary.controller.save as save
3536

3637

38+
def configure_mapillary_settings(**kwargs):
39+
"""
40+
A function allowing the user to configure the Mapillary settings for the session. Takes no
41+
arguments and sets a global variable used by other functions making API requests. For more
42+
information what the details of authentication, please check out the blog post at Mapillary.
43+
https://blog.mapillary.com/update/2021/06/23/getting-started-with-the-new-mapillary-api-v4.html
44+
45+
:return: None
46+
:rtype: None
47+
48+
Usage::
49+
50+
>>> import mapillary as mly
51+
>>> mly.interface.configure_mapillary_settings()
52+
>>> mly.interface.configure_mapillary_settings(use_strict=True)
53+
54+
:param kwargs: Keyword arguments for the configuration
55+
:type kwargs: dict
56+
57+
:param kwargs.use_strict: Whether to use strict mode or not
58+
:type kwargs.use_strict: bool
59+
60+
:return: None
61+
:rtype: None
62+
"""
63+
64+
return Config(kwargs)
65+
66+
3767
def set_access_token(token: str):
3868
"""
3969
A function allowing the user to set an access token for the session, which they can create at

src/mapillary/utils/format.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/mapillary/utils/time.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# Package imports
1616
import datetime
17+
import re
1718

1819

1920
def date_to_unix_timestamp(date: str) -> int:
@@ -43,3 +44,20 @@ def date_to_unix_timestamp(date: str) -> int:
4344

4445
# Return the epoch timestamp in miliseconds
4546
return int(datetime.datetime.fromisoformat(date).timestamp()) * 1000
47+
48+
49+
def is_iso8601_datetime_format(date_time: str) -> bool:
50+
"""
51+
Checks if the date time is in ISO 8601 format
52+
53+
:param date_time: The date time to be checked
54+
:type date_time: str
55+
56+
:return: True if the date time is in ISO 8601 format, else False
57+
:rtype: bool
58+
"""
59+
60+
return (
61+
re.match(r"(\d{4})\-(\d{2})\-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})Z", date_time)
62+
is not None
63+
)

0 commit comments

Comments
 (0)