@@ -68,36 +68,41 @@ def geo_db_type(self, f):
6868 return "object"
6969
7070 def get_geometry_converter (self , expression ):
71+ srid = expression .output_field .srid
72+
7173 def geom_from_coordinates (geom_class , coordinates ):
7274 is_polygon = geom_class .__name__ == "Polygon"
73- return geom_class (* coordinates if is_polygon else coordinates , srid = 4326 )
75+ return geom_class (* coordinates if is_polygon else coordinates , srid = srid )
7476
7577 def converter (value , expression , connection ): # noqa: ARG001
7678 if value is None :
7779 return None
80+
7881 geom_class = getattr (geos , value ["type" ])
7982 if geom_class .__name__ == "GeometryCollection" :
8083 return geom_class (
8184 [
8285 geom_from_coordinates (getattr (geos , v ["type" ]), v ["coordinates" ])
8386 for v in value ["geometries" ]
8487 ],
85- srid = 4326 ,
88+ srid = srid ,
8689 )
8790 if issubclass (geom_class , geos .GeometryCollection ):
88- # TODO: confirm this is correct.
91+ sub_geom_class = geom_class ._allowed
92+ # MultiLineString allows both LineString and LinearRing but should be
93+ # initialized with LineString.
94+ if isinstance (sub_geom_class , tuple ):
95+ sub_geom_class = sub_geom_class [0 ]
8996 return geom_class (
9097 [
91- # TODO: For MultiLineString, geom_class._allowed is a
92- # tuple so this will crash.
93- geom_class ._allowed (
98+ sub_geom_class (
9499 * value ["coordinates" ][x ]
95100 if geom_class .__name__ == "MultiPolygon"
96101 else value ["coordinates" ][x ]
97102 )
98103 for x in range (len (value ["coordinates" ]))
99104 ],
100- srid = 4326 ,
105+ srid = srid ,
101106 )
102107 return geom_from_coordinates (geom_class , value ["coordinates" ])
103108
0 commit comments