2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
The GDAL/OGR library uses an Envelope structure to hold the bounding
|
|
|
|
box information for a geometry. The envelope (bounding box) contains
|
|
|
|
two pairs of coordinates, one for the lower left coordinate and one
|
|
|
|
for the upper right coordinate:
|
|
|
|
|
|
|
|
+----------o Upper right; (max_x, max_y)
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
Lower left (min_x, min_y) o----------+
|
|
|
|
"""
|
|
|
|
from ctypes import Structure, c_double
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2014-12-18 05:00:05 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# The OGR definition of an Envelope is a C structure containing four doubles.
|
|
|
|
# See the 'ogr_core.h' source file for more information:
|
2016-06-16 09:20:23 +08:00
|
|
|
# http://www.gdal.org/ogr__core_8h_source.html
|
2008-08-06 02:13:06 +08:00
|
|
|
class OGREnvelope(Structure):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Represent the OGREnvelope C Structure."
|
2008-08-06 02:13:06 +08:00
|
|
|
_fields_ = [("MinX", c_double),
|
|
|
|
("MaxX", c_double),
|
|
|
|
("MinY", c_double),
|
|
|
|
("MaxY", c_double),
|
|
|
|
]
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Envelope:
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
The Envelope object is a C structure that contains the minimum and
|
|
|
|
maximum X, Y coordinates for a rectangle bounding box. The naming
|
|
|
|
of the variables is compatible with the OGR Envelope structure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
"""
|
|
|
|
The initialization function may take an OGREnvelope structure, 4-element
|
|
|
|
tuple or list, or 4 individual arguments.
|
|
|
|
"""
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
if len(args) == 1:
|
|
|
|
if isinstance(args[0], OGREnvelope):
|
|
|
|
# OGREnvelope (a ctypes Structure) was passed in.
|
|
|
|
self._envelope = args[0]
|
2009-03-08 07:02:48 +08:00
|
|
|
elif isinstance(args[0], (tuple, list)):
|
2008-08-06 02:13:06 +08:00
|
|
|
# A tuple was passed in.
|
|
|
|
if len(args[0]) != 4:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Incorrect number of tuple elements (%d).' % len(args[0]))
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
self._from_sequence(args[0])
|
|
|
|
else:
|
2017-01-20 17:20:53 +08:00
|
|
|
raise TypeError('Incorrect type of argument: %s' % type(args[0]))
|
2008-08-06 02:13:06 +08:00
|
|
|
elif len(args) == 4:
|
2011-08-12 22:14:15 +08:00
|
|
|
# Individual parameters passed in.
|
2008-08-06 02:13:06 +08:00
|
|
|
# Thanks to ww for the help
|
2012-09-24 01:59:27 +08:00
|
|
|
self._from_sequence([float(a) for a in args])
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Incorrect number (%d) of arguments.' % len(args))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Checking the x,y coordinates
|
2009-03-08 07:02:48 +08:00
|
|
|
if self.min_x > self.max_x:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Envelope minimum X > maximum X.')
|
2009-03-08 07:02:48 +08:00
|
|
|
if self.min_y > self.max_y:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Envelope minimum Y > maximum Y.')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return True if the envelopes are equivalent; can compare against
|
2008-08-06 02:13:06 +08:00
|
|
|
other Envelopes and 4-tuples.
|
|
|
|
"""
|
|
|
|
if isinstance(other, Envelope):
|
|
|
|
return (self.min_x == other.min_x) and (self.min_y == other.min_y) and \
|
|
|
|
(self.max_x == other.max_x) and (self.max_y == other.max_y)
|
2009-03-08 07:02:48 +08:00
|
|
|
elif isinstance(other, tuple) and len(other) == 4:
|
2008-08-06 02:13:06 +08:00
|
|
|
return (self.min_x == other[0]) and (self.min_y == other[1]) and \
|
|
|
|
(self.max_x == other[2]) and (self.max_y == other[3])
|
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Equivalence testing only works with other Envelopes.')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a string representation of the tuple."
|
2008-08-06 02:13:06 +08:00
|
|
|
return str(self.tuple)
|
|
|
|
|
|
|
|
def _from_sequence(self, seq):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Initialize the C OGR Envelope structure from the given sequence."
|
2008-08-06 02:13:06 +08:00
|
|
|
self._envelope = OGREnvelope()
|
|
|
|
self._envelope.MinX = seq[0]
|
|
|
|
self._envelope.MinY = seq[1]
|
|
|
|
self._envelope.MaxX = seq[2]
|
|
|
|
self._envelope.MaxY = seq[3]
|
2013-10-11 04:42:30 +08:00
|
|
|
|
|
|
|
def expand_to_include(self, *args):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Modify the envelope to expand to include the boundaries of
|
2013-10-11 04:42:30 +08:00
|
|
|
the passed-in 2-tuple (a point), 4-tuple (an extent) or
|
|
|
|
envelope.
|
|
|
|
"""
|
|
|
|
# We provide a number of different signatures for this method,
|
|
|
|
# and the logic here is all about converting them into a
|
|
|
|
# 4-tuple single parameter which does the actual work of
|
|
|
|
# expanding the envelope.
|
|
|
|
if len(args) == 1:
|
|
|
|
if isinstance(args[0], Envelope):
|
|
|
|
return self.expand_to_include(args[0].tuple)
|
2009-03-08 07:02:48 +08:00
|
|
|
elif hasattr(args[0], 'x') and hasattr(args[0], 'y'):
|
2013-10-11 04:42:30 +08:00
|
|
|
return self.expand_to_include(args[0].x, args[0].y, args[0].x, args[0].y)
|
|
|
|
elif isinstance(args[0], (tuple, list)):
|
|
|
|
# A tuple was passed in.
|
|
|
|
if len(args[0]) == 2:
|
|
|
|
return self.expand_to_include((args[0][0], args[0][1], args[0][0], args[0][1]))
|
|
|
|
elif len(args[0]) == 4:
|
|
|
|
(minx, miny, maxx, maxy) = args[0]
|
|
|
|
if minx < self._envelope.MinX:
|
|
|
|
self._envelope.MinX = minx
|
|
|
|
if miny < self._envelope.MinY:
|
|
|
|
self._envelope.MinY = miny
|
|
|
|
if maxx > self._envelope.MaxX:
|
|
|
|
self._envelope.MaxX = maxx
|
|
|
|
if maxy > self._envelope.MaxY:
|
|
|
|
self._envelope.MaxY = maxy
|
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Incorrect number of tuple elements (%d).' % len(args[0]))
|
2013-10-11 04:42:30 +08:00
|
|
|
else:
|
2017-01-20 17:20:53 +08:00
|
|
|
raise TypeError('Incorrect type of argument: %s' % type(args[0]))
|
2013-10-11 04:42:30 +08:00
|
|
|
elif len(args) == 2:
|
|
|
|
# An x and an y parameter were passed in
|
|
|
|
return self.expand_to_include((args[0], args[1], args[0], args[1]))
|
|
|
|
elif len(args) == 4:
|
2011-08-12 22:14:15 +08:00
|
|
|
# Individual parameters passed in.
|
2013-10-11 04:42:30 +08:00
|
|
|
return self.expand_to_include(args)
|
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Incorrect number (%d) of arguments.' % len(args[0]))
|
2009-03-08 07:02:48 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def min_x(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value of the minimum X coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._envelope.MinX
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_y(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value of the minimum Y coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._envelope.MinY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_x(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value of the maximum X coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._envelope.MaxX
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_y(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value of the maximum Y coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._envelope.MaxY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ur(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the upper-right coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return (self.max_x, self.max_y)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ll(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the lower-left coordinate."
|
2008-08-06 02:13:06 +08:00
|
|
|
return (self.min_x, self.min_y)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tuple(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a tuple representing the envelope."
|
2008-08-06 02:13:06 +08:00
|
|
|
return (self.min_x, self.min_y, self.max_x, self.max_y)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def wkt(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return WKT representing a Polygon for this envelope."
|
2008-08-06 02:13:06 +08:00
|
|
|
# TODO: Fix significant figures.
|
|
|
|
return 'POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))' % \
|
|
|
|
(self.min_x, self.min_y, self.min_x, self.max_y,
|
|
|
|
self.max_x, self.max_y, self.max_x, self.min_y,
|
|
|
|
self.min_x, self.min_y)
|