1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

gis: gdal: SpatialReference objects may now be constructed with PROJ.4 strings without specifying a keyword.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7102 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2008-02-08 19:10:00 +00:00
parent c703452841
commit 40e0a964ea
2 changed files with 26 additions and 15 deletions

View File

@ -27,7 +27,7 @@
NAD83 / Texas South Central
"""
import re
from types import StringType, UnicodeType, TupleType
from types import UnicodeType, TupleType
from ctypes import byref, c_char_p, c_int, c_void_p
# Getting the error checking routine and exceptions
@ -44,26 +44,35 @@ class SpatialReference(object):
# Well-Known Geographical Coordinate System Name
_well_known = {'WGS84':4326, 'WGS72':4322, 'NAD27':4267, 'NAD83':4269}
_epsg_regex = re.compile('^EPSG:(?P<epsg>\d+)$', re.I)
_epsg_regex = re.compile('^(EPSG:)?(?P<epsg>\d+)$', re.I)
_proj_regex = re.compile(r'^\+proj')
#### Python 'magic' routines ####
def __init__(self, srs_input='', srs_type='wkt'):
"Creates a spatial reference object from the given OGC Well Known Text (WKT)."
"""
Creates a GDAL OSR Spatial Reference object from the given input.
The input may be string of OGC Well Known Text (WKT), an integer
EPSG code, a PROJ.4 string, and/or a projection "well known" shorthand
string (one of 'WGS84', 'WGS72', 'NAD27', 'NAD83').
"""
# Intializing pointer and string buffer.
self._ptr = None
buf = c_char_p('')
if isinstance(srs_input, basestring):
# Encoding to ASCII if unicode passed in.
if isinstance(srs_input, UnicodeType):
srs_input = srs_input.encode('ascii')
if isinstance(srs_input, StringType):
m = self._epsg_regex.match(srs_input)
if m:
epsg_m = self._epsg_regex.match(srs_input)
proj_m = self._proj_regex.match(srs_input)
if epsg_m:
# Is this an EPSG well known name?
srs_type = 'epsg'
srs_input = int(m.group('epsg'))
srs_input = int(epsg_m.group('epsg'))
elif proj_m:
# Is the string a PROJ.4 string?
srs_type = 'proj'
elif srs_input in self._well_known:
# Is this a short-hand well known name?
srs_type = 'epsg'

View File

@ -77,7 +77,7 @@ class SpatialRefTest(unittest.TestCase):
for s in srlist:
if s.proj:
srs1 = SpatialReference(s.wkt)
srs2 = SpatialReference(s.proj, 'proj')
srs2 = SpatialReference(s.proj)
self.assertEqual(srs1.proj, srs2.proj)
def test05_epsg(self):
@ -85,9 +85,11 @@ class SpatialRefTest(unittest.TestCase):
for s in srlist:
if s.epsg:
srs1 = SpatialReference(s.wkt)
srs2 = SpatialReference(s.epsg, 'epsg')
srs2 = SpatialReference(s.epsg)
srs3 = SpatialReference(str(s.epsg))
srs4 = SpatialReference('EPSG:%d' % s.epsg)
#self.assertEqual(srs1.wkt, srs2.wkt)
for srs in (srs1, srs2):
for srs in (srs1, srs2, srs3, srs4):
for attr, expected in s.attr:
self.assertEqual(expected, srs[attr])