Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/docs/mapillary.models/mapillary.models.geojson.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
11 changes: 8 additions & 3 deletions src/mapillary/models/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand All @@ -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(),
}

Expand Down
6 changes: 3 additions & 3 deletions src/mapillary/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/utils/test_detection_geometry.py
Original file line number Diff line number Diff line change
@@ -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