Debian Codegraph @

django/contrib/gis/gdal/geomtype.py

raw
1 from django.contrib.gis.gdal.error import GDALException
2
3
4 class OGRGeomType:
5 "Encapsulate OGR Geometry Types."
6
7 wkb25bit = -2147483648
8
9 # Dictionary of acceptable OGRwkbGeometryType s and their string names.
10 _types = {
11 0: "Unknown",
12 1: "Point",
13 2: "LineString",
14 3: "Polygon",
15 4: "MultiPoint",
16 5: "MultiLineString",
17 6: "MultiPolygon",
18 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",
27 100: "None",
28 101: "LinearRing",
29 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",
70 1 + wkb25bit: "Point25D",
71 2 + wkb25bit: "LineString25D",
72 3 + wkb25bit: "Polygon25D",
73 4 + wkb25bit: "MultiPoint25D",
74 5 + wkb25bit: "MultiLineString25D",
75 6 + wkb25bit: "MultiPolygon25D",
76 7 + wkb25bit: "GeometryCollection25D",
77 }
78 # Reverse type dictionary, keyed by lowercase of the name.
79 _str_types = {v.lower(): k for k, v in _types.items()}
80
81 def __init__(self, type_input):
82 "Figure out the correct OGR Type based upon the input."
83 if isinstance(type_input, OGRGeomType):
84 num = type_input.num
85 elif isinstance(type_input, str):
86 type_input = type_input.lower()
87 if type_input == "geometry":
88 type_input = "unknown"
89 num = self._str_types.get(type_input)
90 if num is None:
91 raise GDALException('Invalid OGR String Type "%s"' % type_input)
92 elif isinstance(type_input, int):
93 if type_input not in self._types:
94 raise GDALException("Invalid OGR Integer Type: %d" % type_input)
95 num = type_input
96 else:
97 raise TypeError("Invalid OGR input type given.")
98
99 # Setting the OGR geometry type number.
100 self.num = num
101
102 def __str__(self):
103 "Return the value of the name property."
104 return self.name
105
106 def __repr__(self):
107 return f"<{self.__class__.__qualname__}: {self.name}>"
108
109 def __eq__(self, other):
110 """
111 Do an equivalence test on the OGR type with the given
112 other OGRGeomType, the short-hand string, or the integer.
113 """
114 if isinstance(other, OGRGeomType):
115 return self.num == other.num
116 elif isinstance(other, str):
117 return self.name.lower() == other.lower()
118 elif isinstance(other, int):
119 return self.num == other
120 else:
121 return False
122
123 @property
124 def name(self):
125 "Return a short-hand string form of the OGR Geometry type."
126 return self._types[self.num]
127
128 @property
129 def django(self):
130 "Return the Django GeometryField for this OGR Type."
131 s = self.name.replace("25D", "")
132 if s in ("LinearRing", "None"):
133 return None
134 elif s == "Unknown":
135 s = "Geometry"
136 elif s == "PointZ":
137 s = "Point"
138 return s + "Field"
139
140 def to_multi(self):
141 """
142 Transform Point, LineString, Polygon, and their 25D equivalents
143 to their Multi... counterpart.
144 """
145 if self.name.startswith(("Point", "LineString", "Polygon")):
146 self.num += 3