Skip to content

Commit f26523d

Browse files
shmoon-krEvan-Dabeeopre-commit-ci[bot]bellini666
authored
Add support for the the general Geometry type (#709)
* Add Geometry type * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor isinstance statement to test multiple types * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove RELEASE.md file * Refactor Geometry scalar * fix typing issues --------- Co-authored-by: Evan Moon <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thiago Bellini Ribeiro <[email protected]>
1 parent 95dc43b commit f26523d

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

poetry.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

strawberry_django/fields/types.py

+12
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def __hash__(self):
282282
MultiPoint = None
283283
MultilineString = None
284284
MultiPolygon = None
285+
Geometry = None
285286
else:
286287
Point = strawberry.scalar(
287288
NewType("Point", tuple[float, float, Optional[float]]),
@@ -344,6 +345,16 @@ def __hash__(self):
344345
description="A geographical object that contains multiple polygons.",
345346
)
346347

348+
Geometry = strawberry.scalar(
349+
NewType("Geometry", geos.GEOSGeometry),
350+
serialize=lambda v: v.tuple if isinstance(v, geos.GEOSGeometry) else v, # type: ignore
351+
parse_value=lambda v: geos.GeometryCollection,
352+
description=(
353+
"An arbitrary geographical object. One of Point, "
354+
"LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon."
355+
),
356+
)
357+
347358
field_type_map.update(
348359
{
349360
geos_fields.PointField: Point,
@@ -352,6 +363,7 @@ def __hash__(self):
352363
geos_fields.MultiPointField: MultiPoint,
353364
geos_fields.MultiLineStringField: MultiLineString,
354365
geos_fields.MultiPolygonField: MultiPolygon,
366+
geos_fields.GeometryField: Geometry,
355367
},
356368
)
357369

tests/fields/test_types.py

+2
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ class GeoFieldType:
387387
multi_point: auto
388388
multi_line_string: auto
389389
multi_polygon: auto
390+
geometry: auto
390391

391392
object_definition = get_object_definition(GeoFieldType, strict=True)
392393
assert [
@@ -399,6 +400,7 @@ class GeoFieldType:
399400
("multi_point", types.MultiPoint),
400401
("multi_line_string", types.MultiLineString),
401402
("multi_polygon", types.MultiPolygon),
403+
("geometry", types.Geometry),
402404
]
403405

404406

tests/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class GeosFieldsModel(models.Model):
133133
multi_point = geos_fields.MultiPointField(null=True, blank=True)
134134
multi_line_string = geos_fields.MultiLineStringField(null=True, blank=True)
135135
multi_polygon = geos_fields.MultiPolygonField(null=True, blank=True)
136+
geometry = geos_fields.GeometryField(null=True, blank=True)
136137

137138
except ImproperlyConfigured:
138139
GEOS_IMPORTED = False

0 commit comments

Comments
 (0)