mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
gis: gdal: Fixed GDAL version string parsing for development versions; GEOJSON constant now in root module; added extent
property and coords
property alias for tuple
; added x,y,z properties for OGR LineStrings.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7113 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d88c9ac644
commit
6a692e8031
@ -28,10 +28,10 @@ try:
|
|||||||
from django.contrib.gis.gdal.datasource import DataSource
|
from django.contrib.gis.gdal.datasource import DataSource
|
||||||
from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, gdal_release_date
|
from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, gdal_release_date
|
||||||
from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
|
from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
|
||||||
from django.contrib.gis.gdal.geometries import OGRGeometry
|
from django.contrib.gis.gdal.geometries import OGRGeometry, GEOJSON
|
||||||
HAS_GDAL = True
|
HAS_GDAL = True
|
||||||
except:
|
except:
|
||||||
HAS_GDAL = False
|
HAS_GDAL, GEOJSON = False, False
|
||||||
|
|
||||||
# The envelope, error, and geomtype modules do not actually require the
|
# The envelope, error, and geomtype modules do not actually require the
|
||||||
# GDAL library.
|
# GDAL library.
|
||||||
|
@ -216,8 +216,14 @@ class OGRGeometry(object):
|
|||||||
@property
|
@property
|
||||||
def envelope(self):
|
def envelope(self):
|
||||||
"Returns the envelope for this Geometry."
|
"Returns the envelope for this Geometry."
|
||||||
|
# TODO: Fix Envelope() for Point geometries.
|
||||||
return Envelope(get_envelope(self._ptr, byref(OGREnvelope())))
|
return Envelope(get_envelope(self._ptr, byref(OGREnvelope())))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def extent(self):
|
||||||
|
"Returns the envelope as a 4-tuple, instead of as an Envelope object."
|
||||||
|
return self.envelope.tuple
|
||||||
|
|
||||||
#### SpatialReference-related Properties ####
|
#### SpatialReference-related Properties ####
|
||||||
|
|
||||||
# The SRS property
|
# The SRS property
|
||||||
@ -446,7 +452,8 @@ class Point(OGRGeometry):
|
|||||||
@property
|
@property
|
||||||
def z(self):
|
def z(self):
|
||||||
"Returns the Z coordinate for this Point."
|
"Returns the Z coordinate for this Point."
|
||||||
return getz(self._ptr, 0)
|
if self.coord_dim == 3:
|
||||||
|
return getz(self._ptr, 0)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tuple(self):
|
def tuple(self):
|
||||||
@ -455,6 +462,7 @@ class Point(OGRGeometry):
|
|||||||
return (self.x, self.y)
|
return (self.x, self.y)
|
||||||
elif self.coord_dim == 3:
|
elif self.coord_dim == 3:
|
||||||
return (self.x, self.y, self.z)
|
return (self.x, self.y, self.z)
|
||||||
|
coords = tuple
|
||||||
|
|
||||||
class LineString(OGRGeometry):
|
class LineString(OGRGeometry):
|
||||||
|
|
||||||
@ -485,7 +493,31 @@ class LineString(OGRGeometry):
|
|||||||
@property
|
@property
|
||||||
def tuple(self):
|
def tuple(self):
|
||||||
"Returns the tuple representation of this LineString."
|
"Returns the tuple representation of this LineString."
|
||||||
return tuple(self[i] for i in xrange(len(self)))
|
return tuple([self[i] for i in xrange(len(self))])
|
||||||
|
coords = tuple
|
||||||
|
|
||||||
|
def _listarr(self, func):
|
||||||
|
"""
|
||||||
|
Internal routine that returns a sequence (list) corresponding with
|
||||||
|
the given function.
|
||||||
|
"""
|
||||||
|
return [func(self._ptr, i) for i in xrange(len(self))]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def x(self):
|
||||||
|
"Returns the X coordinates in a list."
|
||||||
|
return self._listarr(getx)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def y(self):
|
||||||
|
"Returns the Y coordinates in a list."
|
||||||
|
return self._listarr(gety)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def z(self):
|
||||||
|
"Returns the Z coordinates in a list."
|
||||||
|
if self.coord_dim == 3:
|
||||||
|
return self._listarr(getz)
|
||||||
|
|
||||||
# LinearRings are used in Polygons.
|
# LinearRings are used in Polygons.
|
||||||
class LinearRing(LineString): pass
|
class LinearRing(LineString): pass
|
||||||
@ -513,11 +545,13 @@ class Polygon(OGRGeometry):
|
|||||||
def shell(self):
|
def shell(self):
|
||||||
"Returns the shell of this Polygon."
|
"Returns the shell of this Polygon."
|
||||||
return self[0] # First ring is the shell
|
return self[0] # First ring is the shell
|
||||||
|
exterior_ring = shell
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tuple(self):
|
def tuple(self):
|
||||||
"Returns a tuple of LinearRing coordinate tuples."
|
"Returns a tuple of LinearRing coordinate tuples."
|
||||||
return tuple(self[i].tuple for i in xrange(self.geom_count))
|
return tuple([self[i].tuple for i in xrange(self.geom_count)])
|
||||||
|
coords = tuple
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def point_count(self):
|
def point_count(self):
|
||||||
@ -575,7 +609,8 @@ class GeometryCollection(OGRGeometry):
|
|||||||
@property
|
@property
|
||||||
def tuple(self):
|
def tuple(self):
|
||||||
"Returns a tuple representation of this Geometry Collection."
|
"Returns a tuple representation of this Geometry Collection."
|
||||||
return tuple(self[i].tuple for i in xrange(self.geom_count))
|
return tuple([self[i].tuple for i in xrange(self.geom_count)])
|
||||||
|
coords = tuple
|
||||||
|
|
||||||
# Multiple Geometry types.
|
# Multiple Geometry types.
|
||||||
class MultiPoint(GeometryCollection): pass
|
class MultiPoint(GeometryCollection): pass
|
||||||
|
@ -8,8 +8,8 @@ from django.contrib.gis.gdal.prototypes.generation import \
|
|||||||
srs_output, string_output, void_output
|
srs_output, string_output, void_output
|
||||||
|
|
||||||
# Some prototypes need to be aware of what version GDAL we have.
|
# Some prototypes need to be aware of what version GDAL we have.
|
||||||
major, minor1, minor2 = map(int, gdal_version().split('.'))
|
major, minor = map(int, gdal_version().split('.')[:2])
|
||||||
if major <= 1 and minor1 <= 4:
|
if major <= 1 and minor <= 4:
|
||||||
GEOJSON = False
|
GEOJSON = False
|
||||||
else:
|
else:
|
||||||
GEOJSON = True
|
GEOJSON = True
|
||||||
|
@ -117,6 +117,12 @@ class OGRGeomTest(unittest.TestCase):
|
|||||||
self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
|
self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
|
||||||
prev = linestr
|
prev = linestr
|
||||||
|
|
||||||
|
# Testing the x, y properties.
|
||||||
|
x = [tmpx for tmpx, tmpy in ls.tup]
|
||||||
|
y = [tmpy for tmpx, tmpy in ls.tup]
|
||||||
|
self.assertEqual(x, linestr.x)
|
||||||
|
self.assertEqual(y, linestr.y)
|
||||||
|
|
||||||
def test05_multilinestring(self):
|
def test05_multilinestring(self):
|
||||||
"Testing MultiLineString objects."
|
"Testing MultiLineString objects."
|
||||||
prev = OGRGeometry('POINT(0 0)')
|
prev = OGRGeometry('POINT(0 0)')
|
||||||
@ -354,6 +360,19 @@ class OGRGeomTest(unittest.TestCase):
|
|||||||
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
|
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
|
||||||
for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
|
for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
|
||||||
|
|
||||||
|
def test15_extent(self):
|
||||||
|
"Testing `extent` property."
|
||||||
|
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
|
||||||
|
mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
|
||||||
|
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
|
||||||
|
# Testing on the 'real world' Polygon.
|
||||||
|
poly = OGRGeometry(polygons[3].wkt)
|
||||||
|
ring = poly.shell
|
||||||
|
x, y = ring.x, ring.y
|
||||||
|
xmin, ymin = min(x), min(y)
|
||||||
|
xmax, ymax = max(x), max(y)
|
||||||
|
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
s = unittest.TestSuite()
|
s = unittest.TestSuite()
|
||||||
s.addTest(unittest.makeSuite(OGRGeomTest))
|
s.addTest(unittest.makeSuite(OGRGeomTest))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user