Debian Codegraph @

django/contrib/gis/gdal/envelope.py

raw
1 """
2 The GDAL/OGR library uses an Envelope structure to hold the bounding
3 box information for a geometry. The envelope (bounding box) contains
4 two pairs of coordinates, one for the lower left coordinate and one
5 for the upper right coordinate:
6
7 +----------o Upper right; (max_x, max_y)
8 | |
9 | |
10 | |
11 Lower left (min_x, min_y) o----------+
12 """
13
14 from ctypes import Structure, c_double
15
16 from django.contrib.gis.gdal.error import GDALException
17
18
19 # The OGR definition of an Envelope is a C structure containing four doubles.
20 # See the 'ogr_core.h' source file for more information:
21 # https://gdal.org/doxygen/ogr__core_8h_source.html
22 class OGREnvelope(Structure):
23 "Represent the OGREnvelope C Structure."
24
25 _fields_ = [
26 ("MinX", c_double),
27 ("MaxX", c_double),
28 ("MinY", c_double),
29 ("MaxY", c_double),
30 ]
31
32
33 class Envelope:
34 """
35 The Envelope object is a C structure that contains the minimum and
36 maximum X, Y coordinates for a rectangle bounding box. The naming
37 of the variables is compatible with the OGR Envelope structure.
38 """
39
40 def __init__(self, *args):
41 """
42 The initialization function may take an OGREnvelope structure, 4-element
43 tuple or list, or 4 individual arguments.
44 """
45
46 if len(args) == 1:
47 if isinstance(args[0], OGREnvelope):
48 # OGREnvelope (a ctypes Structure) was passed in.
49 self._envelope = args[0]
50 elif isinstance(args[0], (tuple, list)):
51 # A tuple was passed in.
52 if len(args[0]) != 4:
53 raise GDALException(
54 "Incorrect number of tuple elements (%d)." % len(args[0])
55 )
56 else:
57 self._from_sequence(args[0])
58 else:
59 raise TypeError("Incorrect type of argument: %s" % type(args[0]))
60 elif len(args) == 4:
61 # Individual parameters passed in.
62 # Thanks to ww for the help
63 self._from_sequence([float(a) for a in args])
64 else:
65 raise GDALException("Incorrect number (%d) of arguments." % len(args))
66
67 # Checking the x,y coordinates
68 if self.min_x > self.max_x:
69 raise GDALException("Envelope minimum X > maximum X.")
70 if self.min_y > self.max_y:
71 raise GDALException("Envelope minimum Y > maximum Y.")
72
73 def __eq__(self, other):
74 """
75 Return True if the envelopes are equivalent; can compare against
76 other Envelopes and 4-tuples.
77 """
78 if isinstance(other, Envelope):
79 return (
80 (self.min_x == other.min_x)
81 and (self.min_y == other.min_y)
82 and (self.max_x == other.max_x)
83 and (self.max_y == other.max_y)
84 )
85 elif isinstance(other, tuple) and len(other) == 4:
86 return (
87 (self.min_x == other[0])
88 and (self.min_y == other[1])
89 and (self.max_x == other[2])
90 and (self.max_y == other[3])
91 )
92 else:
93 raise GDALException("Equivalence testing only works with other Envelopes.")
94
95 def __str__(self):
96 "Return a string representation of the tuple."
97 return str(self.tuple)
98
99 def _from_sequence(self, seq):
100 "Initialize the C OGR Envelope structure from the given sequence."
101 self._envelope = OGREnvelope()
102 self._envelope.MinX = seq[0]
103 self._envelope.MinY = seq[1]
104 self._envelope.MaxX = seq[2]
105 self._envelope.MaxY = seq[3]
106
107 def expand_to_include(self, *args):
108 """
109 Modify the envelope to expand to include the boundaries of
110 the passed-in 2-tuple (a point), 4-tuple (an extent) or
111 envelope.
112 """
113 # We provide a number of different signatures for this method,
114 # and the logic here is all about converting them into a
115 # 4-tuple single parameter which does the actual work of
116 # expanding the envelope.
117 if len(args) == 1:
118 if isinstance(args[0], Envelope):
119 return self.expand_to_include(args[0].tuple)
120 elif hasattr(args[0], "x") and hasattr(args[0], "y"):
121 return self.expand_to_include(
122 args[0].x, args[0].y, args[0].x, args[0].y
123 )
124 elif isinstance(args[0], (tuple, list)):
125 # A tuple was passed in.
126 if len(args[0]) == 2:
127 return self.expand_to_include(
128 (args[0][0], args[0][1], args[0][0], args[0][1])
129 )
130 elif len(args[0]) == 4:
131 minx, miny, maxx, maxy = args[0]
132 if minx < self._envelope.MinX:
133 self._envelope.MinX = minx
134 if miny < self._envelope.MinY:
135 self._envelope.MinY = miny
136 if maxx > self._envelope.MaxX:
137 self._envelope.MaxX = maxx
138 if maxy > self._envelope.MaxY:
139 self._envelope.MaxY = maxy
140 else:
141 raise GDALException(
142 "Incorrect number of tuple elements (%d)." % len(args[0])
143 )
144 else:
145 raise TypeError("Incorrect type of argument: %s" % type(args[0]))
146 elif len(args) == 2:
147 # An x and an y parameter were passed in
148 return self.expand_to_include((args[0], args[1], args[0], args[1]))
149 elif len(args) == 4:
150 # Individual parameters passed in.
151 return self.expand_to_include(args)
152 else:
153 raise GDALException("Incorrect number (%d) of arguments." % len(args[0]))
154
155 @property
156 def min_x(self):
157 "Return the value of the minimum X coordinate."
158 return self._envelope.MinX
159
160 @property
161 def min_y(self):
162 "Return the value of the minimum Y coordinate."
163 return self._envelope.MinY
164
165 @property
166 def max_x(self):
167 "Return the value of the maximum X coordinate."
168 return self._envelope.MaxX
169
170 @property
171 def max_y(self):
172 "Return the value of the maximum Y coordinate."
173 return self._envelope.MaxY
174
175 @property
176 def ur(self):
177 "Return the upper-right coordinate."
178 return (self.max_x, self.max_y)
179
180 @property
181 def ll(self):
182 "Return the lower-left coordinate."
183 return (self.min_x, self.min_y)
184
185 @property
186 def tuple(self):
187 "Return a tuple representing the envelope."
188 return (self.min_x, self.min_y, self.max_x, self.max_y)
189
190 @property
191 def wkt(self):
192 "Return WKT representing a Polygon for this envelope."
193 # TODO: Fix significant figures.
194 return "POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))" % (
195 self.min_x,
196 self.min_y,
197 self.min_x,
198 self.max_y,
199 self.max_x,
200 self.max_y,
201 self.max_x,
202 self.min_y,
203 self.min_x,
204 self.min_y,
205 )