1
0
mirror of https://github.com/django/django.git synced 2025-07-04 01:39:20 +00:00

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
This commit is contained in:
Justin Bronn 2007-10-07 04:21:31 +00:00
parent acbc766894
commit ad3e9e99a2
3 changed files with 69 additions and 19 deletions

View File

@ -39,7 +39,7 @@
True True
""" """
# types & ctypes # 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 from ctypes import byref, string_at, c_char_p, c_double, c_int, c_void_p
# Getting GDAL prerequisites # Getting GDAL prerequisites
@ -181,8 +181,8 @@ class OGRGeometry(object):
"Returns the number of Points in this Geometry." "Returns the number of Points in this Geometry."
return self.point_count return self.point_count
@property # The SRS property
def srs(self): def get_srs(self):
"Returns the Spatial Reference for this Geometry." "Returns the Spatial Reference for this Geometry."
srs_ptr = lgdal.OGR_G_GetSpatialReference(self._g) srs_ptr = lgdal.OGR_G_GetSpatialReference(self._g)
if srs_ptr: if srs_ptr:
@ -190,6 +190,32 @@ class OGRGeometry(object):
else: else:
return None 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 @property
def geom_type(self): def geom_type(self):
"Returns the Type for this Geometry." "Returns the Type for this Geometry."

View File

@ -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. 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 >>> print srs.name
NAD83 / Texas South Central 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 #### #### ctypes function prototypes ####
def ellipsis_func(f): def ellipsis_func(f):
""" """
@ -204,7 +204,10 @@ class SpatialReference(object):
Returns the EPSG SRID of this Spatial Reference, will be None if Returns the EPSG SRID of this Spatial Reference, will be None if
if undefined. if undefined.
""" """
return self.srs['AUTHORITY', 1] try:
return int(self.attr_value('AUTHORITY', 1))
except ValueError:
return None
#### Unit Properties #### #### Unit Properties ####
def _cache_linear(self): def _cache_linear(self):
@ -327,21 +330,21 @@ class SpatialReference(object):
"Returns the WKT representation of this Spatial Reference." "Returns the WKT representation of this Spatial Reference."
w = c_char_p() w = c_char_p()
check_err(lgdal.OSRExportToWkt(self._srs, byref(w))) check_err(lgdal.OSRExportToWkt(self._srs, byref(w)))
return string_at(w) if w: return string_at(w)
@property @property
def pretty_wkt(self, simplify=0): def pretty_wkt(self, simplify=0):
"Returns the 'pretty' representation of the WKT." "Returns the 'pretty' representation of the WKT."
w = c_char_p() w = c_char_p()
check_err(lgdal.OSRExportToPrettyWkt(self._srs, byref(w), c_int(simplify))) check_err(lgdal.OSRExportToPrettyWkt(self._srs, byref(w), c_int(simplify)))
return string_at(w) if w: return string_at(w)
@property @property
def proj(self): def proj(self):
"Returns the PROJ.4 representation for this Spatial Reference." "Returns the PROJ.4 representation for this Spatial Reference."
w = c_char_p() w = c_char_p()
check_err(lgdal.OSRExportToProj4(self._srs, byref(w))) check_err(lgdal.OSRExportToProj4(self._srs, byref(w)))
return string_at(w) if w: return string_at(w)
@property @property
def proj4(self): def proj4(self):

View File

@ -148,6 +148,27 @@ class OGRGeomTest(unittest.TestCase):
for ring in poly: for ring in poly:
self.assertEqual(sr.wkt, ring.srs.wkt) 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(): def suite():
s = unittest.TestSuite() s = unittest.TestSuite()
s.addTest(unittest.makeSuite(OGRGeomTest)) s.addTest(unittest.makeSuite(OGRGeomTest))