diff --git a/docs/docs/mapillary.models/mapillary.models.geojson.md b/docs/docs/mapillary.models/mapillary.models.geojson.md index 63cc320a..a7256a05 100644 --- a/docs/docs/mapillary.models/mapillary.models.geojson.md +++ b/docs/docs/mapillary.models/mapillary.models.geojson.md @@ -62,7 +62,9 @@ Return the list representation of the Coordinates ### class mapillary.models.geojson.Feature(feature: dict) Bases: `object` -Representation for a feature in a feature list +Representation for a feature in a feature list. + +Unlocated features have `geometry=None` and serialize with a null geometry. * **Parameters** diff --git a/src/mapillary/models/geojson.py b/src/mapillary/models/geojson.py index 1730f2d7..1794311c 100644 --- a/src/mapillary/models/geojson.py +++ b/src/mapillary/models/geojson.py @@ -232,7 +232,9 @@ def __repr__(self): class Feature: - """Representation for a feature in a feature list + """Representation for a feature in a feature list. + + Unlocated features have `geometry=None` and serialize with a null geometry. :param feature: The GeoJSON as the input :type feature: dict @@ -267,7 +269,10 @@ def __init__(self, feature: dict) -> None: self.type = "Feature" # Setting the `geometry` property - self.geometry = Geometry(feature["geometry"]) + geometry = feature["geometry"] + self.geometry = ( + None if geometry is None or geometry == {} else Geometry(geometry) + ) # Setting the `properties` property self.properties = Properties(feature["properties"]) @@ -277,7 +282,7 @@ def to_dict(self) -> dict: return { "type": self.type, - "geometry": self.geometry.to_dict(), + "geometry": self.geometry.to_dict() if self.geometry is not None else None, "properties": self.properties.to_dict(), } diff --git a/src/mapillary/utils/format.py b/src/mapillary/utils/format.py index 8085fc75..9d516bc9 100644 --- a/src/mapillary/utils/format.py +++ b/src/mapillary/utils/format.py @@ -224,13 +224,13 @@ def detection_features_to_geojson(feature_list: list) -> dict: { # Type is 'Feature' "type": "Feature", - # Let 'geometry' be the `image` key, defaults to {} is `image` not in feature + # Unlocated detections have a null GeoJSON geometry. "geometry": { "type": "Point", "coordinates": feature["image"]["geometry"]["coordinates"], } - if "image" in feature - else {}, + if feature.get("image") and feature["image"].get("geometry") + else None, # Property list "possible_none_properties": { # Only if "image" was specified in the `fields` of the endpoint, else None diff --git a/tests/utils/test_detection_geometry.py b/tests/utils/test_detection_geometry.py new file mode 100644 index 00000000..e4e713e3 --- /dev/null +++ b/tests/utils/test_detection_geometry.py @@ -0,0 +1,33 @@ +from mapillary.models.geojson import GeoJSON +from mapillary.utils.format import detection_features_to_geojson + + +def test_detections_with_and_without_image_geometry(): + point = {"type": "Point", "coordinates": [12.5, 48.25]} + detections = [ + {"id": "1", "image": {"geometry": point}}, + {"id": "2", "geometry": "pixel-shape", "value": "object--catch-basin"}, + ] + + collection = GeoJSON(detection_features_to_geojson(detections)) + located, unlocated = collection.to_dict()["features"] + + assert located["geometry"] == point + assert unlocated["geometry"] is None + assert unlocated["properties"] == { + "id": "2", + "pixel_geometry": "pixel-shape", + "value": "object--catch-basin", + } + + +def test_previous_empty_geometry_serializes_as_null(): + collection = GeoJSON( + { + "type": "FeatureCollection", + "features": [ + {"type": "Feature", "geometry": {}, "properties": {"id": "2"}} + ], + } + ) + assert collection.to_dict()["features"][0]["geometry"] is None