Skip to content

Commit a6b6796

Browse files
committed
fix GeometryCollection initialization
1 parent 3020173 commit a6b6796

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

django_mongodb_backend_gis/adapter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ def __init__(self, obj, geography=False):
66
"""
77
Initialize on the spatial object.
88
"""
9-
self.data = {
9+
if obj.__class__.__name__ == "GeometryCollection":
10+
self.data = {
11+
"type": obj.__class__.__name__,
12+
"geometries": [self.get_data(x) for x in obj],
13+
}
14+
else:
15+
self.data = self.get_data(obj)
16+
17+
def get_data(self, obj):
18+
return {
1019
"type": obj.__class__.__name__,
1120
"coordinates": obj.coords[0] if obj.__class__.__name__ == "Polygon" else obj.coords,
1221
}

django_mongodb_backend_gis/features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ def django_test_expected_failures(self):
2525
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_geometry_field_option",
2626
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_serialization_base",
2727
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_srid_option",
28-
# Crash initializing GeometryCollection: 'tuple' object is not callable
29-
"gis_tests.geoapp.tests.GeoModelTest.test_geometryfield",
3028
# KeyError: 'within' connection.ops.gis_operators[self.lookup_name]
3129
"gis_tests.geoapp.tests.GeoModelTest.test_gis_query_as_string",
3230
# No lookups are supported (yet?)

django_mongodb_backend_gis/operations.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ def converter(value, expression, connection): # noqa: ARG001
7272
if value is None:
7373
return None
7474
geom_class = getattr(geos, value["type"])
75+
if geom_class.__name__ == "GeometryCollection":
76+
return geom_class(
77+
[getattr(geos, v["type"])(v["coordinates"]) for v in value["geometries"]],
78+
srid=4326,
79+
)
7580
if issubclass(geom_class, geos.GeometryCollection):
7681
# TODO: confirm this is correct.
7782
return geom_class(
7883
[
84+
# TODO: For MultiLineString, geom_class._allowed is a
85+
# tuple so this will crash.
7986
geom_class._allowed(value["coordinates"][x][0])
8087
for x in range(len(value["coordinates"]))
8188
],

0 commit comments

Comments
 (0)