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:
parent
c703452841
commit
40e0a964ea
@ -27,7 +27,7 @@
|
|||||||
NAD83 / Texas South Central
|
NAD83 / Texas South Central
|
||||||
"""
|
"""
|
||||||
import re
|
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
|
from ctypes import byref, c_char_p, c_int, c_void_p
|
||||||
|
|
||||||
# Getting the error checking routine and exceptions
|
# Getting the error checking routine and exceptions
|
||||||
@ -44,26 +44,35 @@ class SpatialReference(object):
|
|||||||
|
|
||||||
# Well-Known Geographical Coordinate System Name
|
# Well-Known Geographical Coordinate System Name
|
||||||
_well_known = {'WGS84':4326, 'WGS72':4322, 'NAD27':4267, 'NAD83':4269}
|
_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 ####
|
#### Python 'magic' routines ####
|
||||||
def __init__(self, srs_input='', srs_type='wkt'):
|
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.
|
# Intializing pointer and string buffer.
|
||||||
self._ptr = None
|
self._ptr = None
|
||||||
buf = c_char_p('')
|
buf = c_char_p('')
|
||||||
|
|
||||||
# Encoding to ASCII if unicode passed in.
|
if isinstance(srs_input, basestring):
|
||||||
if isinstance(srs_input, UnicodeType):
|
# Encoding to ASCII if unicode passed in.
|
||||||
srs_input = srs_input.encode('ascii')
|
if isinstance(srs_input, UnicodeType):
|
||||||
|
srs_input = srs_input.encode('ascii')
|
||||||
|
|
||||||
if isinstance(srs_input, StringType):
|
epsg_m = self._epsg_regex.match(srs_input)
|
||||||
m = self._epsg_regex.match(srs_input)
|
proj_m = self._proj_regex.match(srs_input)
|
||||||
if m:
|
if epsg_m:
|
||||||
# Is this an EPSG well known name?
|
# Is this an EPSG well known name?
|
||||||
srs_type = 'epsg'
|
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:
|
elif srs_input in self._well_known:
|
||||||
# Is this a short-hand well known name?
|
# Is this a short-hand well known name?
|
||||||
srs_type = 'epsg'
|
srs_type = 'epsg'
|
||||||
|
@ -77,17 +77,19 @@ class SpatialRefTest(unittest.TestCase):
|
|||||||
for s in srlist:
|
for s in srlist:
|
||||||
if s.proj:
|
if s.proj:
|
||||||
srs1 = SpatialReference(s.wkt)
|
srs1 = SpatialReference(s.wkt)
|
||||||
srs2 = SpatialReference(s.proj, 'proj')
|
srs2 = SpatialReference(s.proj)
|
||||||
self.assertEqual(srs1.proj, srs2.proj)
|
self.assertEqual(srs1.proj, srs2.proj)
|
||||||
|
|
||||||
def test05_epsg(self):
|
def test05_epsg(self):
|
||||||
"Test EPSG import."
|
"Test EPSG import."
|
||||||
for s in srlist:
|
for s in srlist:
|
||||||
if s.epsg:
|
if s.epsg:
|
||||||
srs1 = SpatialReference(s.wkt)
|
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)
|
#self.assertEqual(srs1.wkt, srs2.wkt)
|
||||||
for srs in (srs1, srs2):
|
for srs in (srs1, srs2, srs3, srs4):
|
||||||
for attr, expected in s.attr:
|
for attr, expected in s.attr:
|
||||||
self.assertEqual(expected, srs[attr])
|
self.assertEqual(expected, srs[attr])
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user