@@ -68,36 +68,41 @@ def geo_db_type(self, f):
68
68
return "object"
69
69
70
70
def get_geometry_converter (self , expression ):
71
+ srid = expression .output_field .srid
72
+
71
73
def geom_from_coordinates (geom_class , coordinates ):
72
74
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 )
74
76
75
77
def converter (value , expression , connection ): # noqa: ARG001
76
78
if value is None :
77
79
return None
80
+
78
81
geom_class = getattr (geos , value ["type" ])
79
82
if geom_class .__name__ == "GeometryCollection" :
80
83
return geom_class (
81
84
[
82
85
geom_from_coordinates (getattr (geos , v ["type" ]), v ["coordinates" ])
83
86
for v in value ["geometries" ]
84
87
],
85
- srid = 4326 ,
88
+ srid = srid ,
86
89
)
87
90
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 ]
89
96
return geom_class (
90
97
[
91
- # TODO: For MultiLineString, geom_class._allowed is a
92
- # tuple so this will crash.
93
- geom_class ._allowed (
98
+ sub_geom_class (
94
99
* value ["coordinates" ][x ]
95
100
if geom_class .__name__ == "MultiPolygon"
96
101
else value ["coordinates" ][x ]
97
102
)
98
103
for x in range (len (value ["coordinates" ]))
99
104
],
100
- srid = 4326 ,
105
+ srid = srid ,
101
106
)
102
107
return geom_from_coordinates (geom_class , value ["coordinates" ])
103
108
0 commit comments