Skip to content

Commit 21b0b23

Browse files
smithdc1felixxm
authored andcommitted
Refs #35058 -- Made OGRGeomType aware of additional WKB geometry types.
This commit increases OGRGeomType's knowledge of WKB types and allows for improved error messages when Django doesn't yet have a corresponding class to wrap a given type.
1 parent 7c26dbf commit 21b0b23

File tree

4 files changed

+134
-3
lines changed

4 files changed

+134
-3
lines changed

django/contrib/gis/gdal/geometries.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def __init__(self, geom_input, srs=None):
123123
self.srs = srs
124124

125125
# Setting the class depending upon the OGR Geometry Type
126-
self.__class__ = GEO_CLASSES[self.geom_type.num]
126+
if (geo_class := GEO_CLASSES.get(self.geom_type.num)) is None:
127+
raise TypeError(f"Unsupported geometry type: {self.geom_type}")
128+
self.__class__ = geo_class
127129

128130
# Pickle routines
129131
def __getstate__(self):

django/contrib/gis/gdal/geomtype.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,57 @@ class OGRGeomType:
1616
5: "MultiLineString",
1717
6: "MultiPolygon",
1818
7: "GeometryCollection",
19+
8: "CircularString",
20+
9: "CompoundCurve",
21+
10: "CurvePolygon",
22+
11: "MultiCurve",
23+
12: "MultiSurface",
24+
15: "PolyhedralSurface",
25+
16: "TIN",
26+
17: "Triangle",
1927
100: "None",
2028
101: "LinearRing",
2129
102: "PointZ",
30+
1008: "CircularStringZ",
31+
1009: "CompoundCurveZ",
32+
1010: "CurvePolygonZ",
33+
1011: "MultiCurveZ",
34+
1012: "MultiSurfaceZ",
35+
1013: "CurveZ",
36+
1014: "SurfaceZ",
37+
1015: "PolyhedralSurfaceZ",
38+
1016: "TINZ",
39+
1017: "TriangleZ",
40+
2001: "PointM",
41+
2002: "LineStringM",
42+
2003: "PolygonM",
43+
2004: "MultiPointM",
44+
2005: "MultiLineStringM",
45+
2006: "MultiPolygonM",
46+
2007: "GeometryCollectionM",
47+
2008: "CircularStringM",
48+
2009: "CompoundCurveM",
49+
2010: "CurvePolygonM",
50+
2011: "MultiCurveM",
51+
2012: "MultiSurfaceM",
52+
2015: "PolyhedralSurfaceM",
53+
2016: "TINM",
54+
2017: "TriangleM",
55+
3001: "PointZM",
56+
3002: "LineStringZM",
57+
3003: "PolygonZM",
58+
3004: "MultiPointZM",
59+
3005: "MultiLineStringZM",
60+
3006: "MultiPolygonZM",
61+
3007: "GeometryCollectionZM",
62+
3008: "CircularStringZM",
63+
3009: "CompoundCurveZM",
64+
3010: "CurvePolygonZM",
65+
3011: "MultiCurveZM",
66+
3012: "MultiSurfaceZM",
67+
3015: "PolyhedralSurfaceZM",
68+
3016: "TINZM",
69+
3017: "TriangleZM",
2270
1 + wkb25bit: "Point25D",
2371
2 + wkb25bit: "LineString25D",
2472
3 + wkb25bit: "Polygon25D",

django/contrib/gis/geometry.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
r"^(SRID=(?P<srid>\-?[0-9]+);)?"
1111
r"(?P<wkt>"
1212
r"(?P<type>POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|"
13-
r"MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)"
13+
r"MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION|CIRCULARSTRING|COMPOUNDCURVE|"
14+
r"CURVEPOLYGON|MULTICURVE|MULTISURFACE|CURVE|SURFACE|POLYHEDRALSURFACE|TIN|"
15+
r"TRIANGLE)"
1416
r"[ACEGIMLONPSRUTYZ0-9,.+() -]+)$",
1517
re.I,
1618
)

tests/gis_tests/gdal_tests/test_geom.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_geomtype(self):
3535
with self.assertRaises(GDALException):
3636
OGRGeomType("fooD")
3737
with self.assertRaises(GDALException):
38-
OGRGeomType(9)
38+
OGRGeomType(4001)
3939

4040
# Equivalence can take strings, ints, and other OGRGeomTypes
4141
self.assertEqual(OGRGeomType(1), OGRGeomType(1))
@@ -635,3 +635,82 @@ def test_empty(self):
635635
def test_empty_point_to_geos(self):
636636
p = OGRGeometry("POINT EMPTY", srs=4326)
637637
self.assertEqual(p.geos.ewkt, p.ewkt)
638+
639+
def test_geometry_types(self):
640+
tests = [
641+
("Point", 1, True),
642+
("LineString", 2, True),
643+
("Polygon", 3, True),
644+
("MultiPoint", 4, True),
645+
("Multilinestring", 5, True),
646+
("MultiPolygon", 6, True),
647+
("GeometryCollection", 7, True),
648+
("CircularString", 8, False),
649+
("CompoundCurve", 9, False),
650+
("CurvePolygon", 10, False),
651+
("MultiCurve", 11, False),
652+
("MultiSurface", 12, False),
653+
# 13 (Curve) and 14 (Surface) are abstract types.
654+
("PolyhedralSurface", 15, False),
655+
("TIN", 16, False),
656+
("Triangle", 17, False),
657+
("Linearring", 2, True),
658+
# Types 1 - 7 with Z dimension have 2.5D enums.
659+
("Point Z", -2147483647, True), # 1001
660+
("LineString Z", -2147483646, True), # 1002
661+
("Polygon Z", -2147483645, True), # 1003
662+
("MultiPoint Z", -2147483644, True), # 1004
663+
("Multilinestring Z", -2147483643, True), # 1005
664+
("MultiPolygon Z", -2147483642, True), # 1006
665+
("GeometryCollection Z", -2147483641, True), # 1007
666+
("CircularString Z", 1008, False),
667+
("CompoundCurve Z", 1009, False),
668+
("CurvePolygon Z", 1010, False),
669+
("MultiCurve Z", 1011, False),
670+
("MultiSurface Z", 1012, False),
671+
("PolyhedralSurface Z", 1015, False),
672+
("TIN Z", 1016, False),
673+
("Triangle Z", 1017, False),
674+
("Point M", 2001, False),
675+
("LineString M", 2002, False),
676+
("Polygon M", 2003, False),
677+
("MultiPoint M", 2004, False),
678+
("MultiLineString M", 2005, False),
679+
("MultiPolygon M", 2006, False),
680+
("GeometryCollection M", 2007, False),
681+
("CircularString M", 2008, False),
682+
("CompoundCurve M", 2009, False),
683+
("CurvePolygon M", 2010, False),
684+
("MultiCurve M", 2011, False),
685+
("MultiSurface M", 2012, False),
686+
("PolyhedralSurface M", 2015, False),
687+
("TIN M", 2016, False),
688+
("Triangle M", 2017, False),
689+
("Point ZM", 3001, False),
690+
("LineString ZM", 3002, False),
691+
("Polygon ZM", 3003, False),
692+
("MultiPoint ZM", 3004, False),
693+
("MultiLineString ZM", 3005, False),
694+
("MultiPolygon ZM", 3006, False),
695+
("GeometryCollection ZM", 3007, False),
696+
("CircularString ZM", 3008, False),
697+
("CompoundCurve ZM", 3009, False),
698+
("CurvePolygon ZM", 3010, False),
699+
("MultiCurve ZM", 3011, False),
700+
("MultiSurface ZM", 3012, False),
701+
("PolyhedralSurface ZM", 3015, False),
702+
("TIN ZM", 3016, False),
703+
("Triangle ZM", 3017, False),
704+
]
705+
706+
for test in tests:
707+
geom_type, num, supported = test
708+
with self.subTest(geom_type=geom_type, num=num, supported=supported):
709+
if supported:
710+
g = OGRGeometry(f"{geom_type} EMPTY")
711+
self.assertEqual(g.geom_type.num, num)
712+
else:
713+
type_ = geom_type.replace(" ", "")
714+
msg = f"Unsupported geometry type: {type_}"
715+
with self.assertRaisesMessage(TypeError, msg):
716+
OGRGeometry(f"{geom_type} EMPTY")

0 commit comments

Comments
 (0)