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
15 changes: 12 additions & 3 deletions src/mapillary/models/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def __init__(self, geometry: dict) -> None:
options=["dict"],
)

if not geometry:
self.type = None
self.coordinates = None
return

# Setting the type of the selected geometry
self.type: str = geometry["type"]

Expand All @@ -218,7 +223,10 @@ def __init__(self, geometry: dict) -> None:
def to_dict(self):
"""Return dictionary representation of the geometry"""

return {"type": self.type, "coordinates": self.coordinates.to_list()}
return {
"type": self.type,
"coordinates": self.coordinates.to_list() if self.coordinates else None,
}

def __str__(self):
"""Return the informal string representation of the Geometry"""
Expand Down Expand Up @@ -267,7 +275,8 @@ def __init__(self, feature: dict) -> None:
self.type = "Feature"

# Setting the `geometry` property
self.geometry = Geometry(feature["geometry"])
geom = feature.get("geometry")
self.geometry = Geometry(geom) if geom else None

# Setting the `properties` property
self.properties = Properties(feature["properties"])
Expand All @@ -277,7 +286,7 @@ def to_dict(self) -> dict:

return {
"type": self.type,
"geometry": self.geometry.to_dict(),
"geometry": self.geometry.to_dict() if self.geometry else None,
"properties": self.properties.to_dict(),
}

Expand Down
60 changes: 60 additions & 0 deletions tests/models/test_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2022 Facebook, Inc. and its affiliates. (http://www.facebook.com)
# -*- coding: utf-8 -*-

"""
tests.models.test_geojson

This module tests the GeoJSON model classes defined in src/mapillary/models/geojson.py.
"""

from mapillary.models.geojson import Feature, GeoJSON, Geometry


def test_geometry_empty_dict_does_not_raise_key_error():
geom = Geometry({})
assert geom.type is None
assert geom.coordinates is None
assert geom.to_dict() == {
"type": None,
"coordinates": None,
}


def test_geometry_none_sets_none_fields():
geom = Geometry(None)
assert geom.type is None
assert geom.coordinates is None
assert geom.to_dict() == {
"type": None,
"coordinates": None,
}


def test_feature_with_empty_geometry():
feature = Feature(
{
"type": "Feature",
"geometry": {},
"properties": {"id": "123"},
}
)
assert feature.geometry is None
assert feature.to_dict()["geometry"] is None


def test_geojson_with_empty_feature_geometry():
geojson = GeoJSON(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {},
"properties": {},
}
],
}
)
assert len(geojson.features) == 1
assert geojson.features[0].geometry is None
assert geojson.to_dict()["features"][0]["geometry"] is None