Cleaned up initialization of `SpatialReference` objects -- now take advantage of OGR's `SetFromUserInput` function. Refs #9855. trunk
authorJustin Bronn <jbronn@geodjango.org>
Mon Dec 15 09:07:54 2008 -0600 (14 months ago)
branchtrunk
changeset 11edd876f3bc87
parent 10 49b0e3f72c0b
child 12 44c7cd6df7a3
Cleaned up initialization of `SpatialReference` objects -- now take advantage of OGR's `SetFromUserInput` function. Refs #9855.
django/contrib/gis/gdal/prototypes/srs.py
django/contrib/gis/gdal/srs.py
     1.1 --- a/django/contrib/gis/gdal/prototypes/srs.py	Mon Dec 15 08:40:26 2008 -0600
     1.2 +++ b/django/contrib/gis/gdal/prototypes/srs.py	Mon Dec 15 09:07:54 2008 -0600
     1.3 @@ -36,6 +36,7 @@
     1.4  from_proj = void_output(lgdal.OSRImportFromProj4, [c_void_p, c_char_p])
     1.5  from_epsg = void_output(std_call('OSRImportFromEPSG'), [c_void_p, c_int])
     1.6  from_xml = void_output(lgdal.OSRImportFromXML, [c_void_p, c_char_p])
     1.7 +from_user_input = void_output(std_call('OSRSetFromUserInput'), [c_void_p, c_char_p])
     1.8  
     1.9  # Morphing to/from ESRI WKT.
    1.10  morph_to_esri = void_output(lgdal.OSRMorphToESRI, [c_void_p])
     2.1 --- a/django/contrib/gis/gdal/srs.py	Mon Dec 15 08:40:26 2008 -0600
     2.2 +++ b/django/contrib/gis/gdal/srs.py	Mon Dec 15 09:07:54 2008 -0600
     2.3 @@ -27,7 +27,6 @@
     2.4    NAD83 / Texas South Central
     2.5  """
     2.6  import re
     2.7 -from types import UnicodeType, TupleType
     2.8  from ctypes import byref, c_char_p, c_int, c_void_p
     2.9  
    2.10  # Getting the error checking routine and exceptions
    2.11 @@ -43,13 +42,8 @@
    2.12      systems (projections and datums) and to transform between them."
    2.13      """
    2.14  
    2.15 -    # Well-Known Geographical Coordinate System Name
    2.16 -    _well_known = {'WGS84':4326, 'WGS72':4322, 'NAD27':4267, 'NAD83':4269}
    2.17 -    _epsg_regex = re.compile('^(EPSG:)?(?P<epsg>\d+)$', re.I)
    2.18 -    _proj_regex = re.compile(r'^\+proj')
    2.19 -
    2.20      #### Python 'magic' routines ####
    2.21 -    def __init__(self, srs_input='', srs_type='wkt'):
    2.22 +    def __init__(self, srs_input=''):
    2.23          """
    2.24          Creates a GDAL OSR Spatial Reference object from the given input.
    2.25          The input may be string of OGC Well Known Text (WKT), an integer 
    2.26 @@ -57,43 +51,33 @@
    2.27          string (one of 'WGS84', 'WGS72', 'NAD27', 'NAD83').
    2.28          """
    2.29          buf = c_char_p('')
    2.30 +        srs_type = 'user'
    2.31  
    2.32          if isinstance(srs_input, basestring):
    2.33              # Encoding to ASCII if unicode passed in.
    2.34 -            if isinstance(srs_input, UnicodeType):
    2.35 +            if isinstance(srs_input, unicode):
    2.36                  srs_input = srs_input.encode('ascii')
    2.37 -
    2.38 -            epsg_m = self._epsg_regex.match(srs_input)
    2.39 -            proj_m = self._proj_regex.match(srs_input)
    2.40 -            if epsg_m:
    2.41 -                # Is this an EPSG well known name?    
    2.42 -                srs_type = 'epsg'
    2.43 -                srs_input = int(epsg_m.group('epsg'))
    2.44 -            elif proj_m:
    2.45 -                # Is the string a PROJ.4 string?
    2.46 -                srs_type = 'proj'
    2.47 -            elif srs_input in self._well_known:
    2.48 -                # Is this a short-hand well known name?  
    2.49 -                srs_type = 'epsg'
    2.50 -                srs_input = self._well_known[srs_input]
    2.51 -            elif srs_type == 'proj':
    2.52 +            try:
    2.53 +                # If SRID is a string, e.g., '4326', then make acceptable
    2.54 +                # as user input.
    2.55 +                srid = int(srs_input)
    2.56 +                srs_input = 'EPSG:%d' % srid
    2.57 +            except ValueError:
    2.58                  pass
    2.59 -            else:
    2.60 -                # Setting the buffer with WKT, PROJ.4 string, etc.
    2.61 -                buf = c_char_p(srs_input)
    2.62 -        elif isinstance(srs_input, int):
    2.63 +        elif isinstance(srs_input, (int, long)):
    2.64              # EPSG integer code was input.
    2.65 -            if srs_type != 'epsg': srs_type = 'epsg'
    2.66 -        elif isinstance(srs_input, c_void_p):
    2.67 +            srs_type = 'epsg'
    2.68 +        elif isinstance(srs_input, self.ptr_type):
    2.69 +            srs = srs_input
    2.70              srs_type = 'ogr'
    2.71          else:
    2.72              raise TypeError('Invalid SRS type "%s"' % srs_type)
    2.73  
    2.74          if srs_type == 'ogr':
    2.75 -            # SRS input is OGR pointer
    2.76 +            # Input is already an SRS pointer.
    2.77              srs = srs_input
    2.78          else:
    2.79 -            # Creating a new pointer, using the string buffer.
    2.80 +            # Creating a new SRS pointer, using the string buffer.
    2.81              srs = capi.new_srs(buf)
    2.82  
    2.83          # If the pointer is NULL, throw an exception.
    2.84 @@ -102,9 +86,11 @@
    2.85          else:
    2.86              self.ptr = srs
    2.87  
    2.88 -        # Post-processing if in PROJ.4 or EPSG formats.
    2.89 -        if srs_type == 'proj': self.import_proj(srs_input)
    2.90 -        elif srs_type == 'epsg': self.import_epsg(srs_input)
    2.91 +        # Importing from either the user input string or an integer SRID.
    2.92 +        if srs_type == 'user':
    2.93 +            self.import_user_input(srs_input)
    2.94 +        elif srs_type == 'epsg':
    2.95 +            self.import_epsg(srs_input)
    2.96  
    2.97      def __del__(self):
    2.98          "Destroys this spatial reference."
    2.99 @@ -286,17 +272,21 @@
   2.100          return bool(capi.isprojected(self.ptr))
   2.101  
   2.102      #### Import Routines #####
   2.103 -    def import_wkt(self, wkt):
   2.104 -        "Imports the Spatial Reference from OGC WKT (string)"
   2.105 -        capi.from_wkt(self.ptr, byref(c_char_p(wkt)))
   2.106 +    def import_epsg(self, epsg):
   2.107 +        "Imports the Spatial Reference from the EPSG code (an integer)."
   2.108 +        capi.from_epsg(self.ptr, epsg)
   2.109  
   2.110      def import_proj(self, proj):
   2.111          "Imports the Spatial Reference from a PROJ.4 string."
   2.112          capi.from_proj(self.ptr, proj)
   2.113  
   2.114 -    def import_epsg(self, epsg):
   2.115 -        "Imports the Spatial Reference from the EPSG code (an integer)."
   2.116 -        capi.from_epsg(self.ptr, epsg)
   2.117 +    def import_user_input(self, user_input):
   2.118 +        "Imports the Spatial Reference from the given user input string."
   2.119 +        capi.from_user_input(self.ptr, user_input)
   2.120 +
   2.121 +    def import_wkt(self, wkt):
   2.122 +        "Imports the Spatial Reference from OGC WKT (string)"
   2.123 +        capi.from_wkt(self.ptr, byref(c_char_p(wkt)))
   2.124  
   2.125      def import_xml(self, xml):
   2.126          "Imports the Spatial Reference from an XML string."