summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2007-10-07 04:21:31 +0000
committerJustin Bronn <jbronn@gmail.com>2007-10-07 04:21:31 +0000
commitad3e9e99a2c6bd640571bb1d6f2e92d74e255188 (patch)
tree5a216650b0661237387e827fd9b8b9030787677a
parentacbc76689444ac62b84cf88529b6792e9ae0595b (diff)
gis: gdal: The OGRGeometry `srs` property is now mutable, and added the `srid` property.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6460 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/gis/gdal/geometries.py32
-rw-r--r--django/contrib/gis/gdal/srs.py35
-rw-r--r--django/contrib/gis/tests/test_gdal_geom.py21
3 files changed, 69 insertions, 19 deletions
diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py
index 4818a8fa1f..c997718b8c 100644
--- a/django/contrib/gis/gdal/geometries.py
+++ b/django/contrib/gis/gdal/geometries.py
@@ -39,7 +39,7 @@
True
"""
# types & ctypes
-from types import IntType, StringType
+from types import IntType, StringType, UnicodeType
from ctypes import byref, string_at, c_char_p, c_double, c_int, c_void_p
# Getting GDAL prerequisites
@@ -181,8 +181,8 @@ class OGRGeometry(object):
"Returns the number of Points in this Geometry."
return self.point_count
- @property
- def srs(self):
+ # The SRS property
+ def get_srs(self):
"Returns the Spatial Reference for this Geometry."
srs_ptr = lgdal.OGR_G_GetSpatialReference(self._g)
if srs_ptr:
@@ -190,6 +190,32 @@ class OGRGeometry(object):
else:
return None
+ def set_srs(self, srs):
+ "Sets the SpatialReference for this geometry."
+ if isinstance(srs, SpatialReference):
+ srs_ptr = lgdal.OSRClone(srs._srs)
+ elif isinstance(srs, (StringType, UnicodeType, IntType)):
+ sr = SpatialReference(srs)
+ srs_ptr = lgdal.OSRClone(sr._srs)
+ else:
+ raise TypeError('Cannot assign spatial reference with object of type: %s' % type(srs))
+ lgdal.OGR_G_AssignSpatialReference(self._g, srs_ptr)
+
+ srs = property(get_srs, set_srs)
+
+ # The SRID property
+ def get_srid(self):
+ if self.srs: return self.srs.srid
+ else: return None
+
+ def set_srid(self, srid):
+ if isinstance(srid, IntType):
+ self.srs = srid
+ else:
+ raise TypeError('SRID must be set with an integer.')
+
+ srid = property(get_srid, set_srid)
+
@property
def geom_type(self):
"Returns the Type for this Geometry."
diff --git a/django/contrib/gis/gdal/srs.py b/django/contrib/gis/gdal/srs.py
index fea050afba..243df6e4bb 100644
--- a/django/contrib/gis/gdal/srs.py
+++ b/django/contrib/gis/gdal/srs.py
@@ -1,15 +1,3 @@
-import re
-from types import StringType, UnicodeType, TupleType
-from ctypes import \
- c_char_p, c_int, c_double, c_void_p, POINTER, \
- byref, string_at, create_string_buffer
-
-# Getting the GDAL C Library
-from django.contrib.gis.gdal.libgdal import lgdal
-
-# Getting the error checking routine and exceptions
-from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
-
"""
The Spatial Reference class, represensents OGR Spatial Reference objects.
@@ -38,6 +26,18 @@ from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
>>> print srs.name
NAD83 / Texas South Central
"""
+import re
+from types import StringType, UnicodeType, TupleType
+from ctypes import \
+ c_char_p, c_int, c_double, c_void_p, POINTER, \
+ byref, string_at, create_string_buffer
+
+# Getting the GDAL C Library
+from django.contrib.gis.gdal.libgdal import lgdal
+
+# Getting the error checking routine and exceptions
+from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
+
#### ctypes function prototypes ####
def ellipsis_func(f):
"""
@@ -204,7 +204,10 @@ class SpatialReference(object):
Returns the EPSG SRID of this Spatial Reference, will be None if
if undefined.
"""
- return self.srs['AUTHORITY', 1]
+ try:
+ return int(self.attr_value('AUTHORITY', 1))
+ except ValueError:
+ return None
#### Unit Properties ####
def _cache_linear(self):
@@ -327,21 +330,21 @@ class SpatialReference(object):
"Returns the WKT representation of this Spatial Reference."
w = c_char_p()
check_err(lgdal.OSRExportToWkt(self._srs, byref(w)))
- return string_at(w)
+ if w: return string_at(w)
@property
def pretty_wkt(self, simplify=0):
"Returns the 'pretty' representation of the WKT."
w = c_char_p()
check_err(lgdal.OSRExportToPrettyWkt(self._srs, byref(w), c_int(simplify)))
- return string_at(w)
+ if w: return string_at(w)
@property
def proj(self):
"Returns the PROJ.4 representation for this Spatial Reference."
w = c_char_p()
check_err(lgdal.OSRExportToProj4(self._srs, byref(w)))
- return string_at(w)
+ if w: return string_at(w)
@property
def proj4(self):
diff --git a/django/contrib/gis/tests/test_gdal_geom.py b/django/contrib/gis/tests/test_gdal_geom.py
index 99631aa732..4b5c73b36d 100644
--- a/django/contrib/gis/tests/test_gdal_geom.py
+++ b/django/contrib/gis/tests/test_gdal_geom.py
@@ -148,6 +148,27 @@ class OGRGeomTest(unittest.TestCase):
for ring in poly:
self.assertEqual(sr.wkt, ring.srs.wkt)
+ mpoly = OGRGeometry(mp.wkt, 4326)
+ self.assertEqual(4326, mpoly.srid)
+ mpoly.srs = SpatialReference(4269)
+ self.assertEqual(4269, mpoly.srid)
+ self.assertEqual('NAD83', mpoly.srs.name)
+ for poly in mpoly:
+ self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
+ poly.srs = 32140
+ for ring in poly:
+ self.assertEqual(32140, ring.srs.srid)
+ self.assertEqual('NAD83 / Texas South Central', ring.srs.name)
+ ring.srs = str(SpatialReference(4326)) # back to WGS84
+ self.assertEqual(4326, ring.srs.srid)
+
+ # Using the `srid` property.
+ ring.srid = 4322
+ self.assertEqual('WGS 72', ring.srs.name)
+ self.assertEqual(4322, ring.srid)
+
+
+
def suite():
s = unittest.TestSuite()
s.addTest(unittest.makeSuite(OGRGeomTest))