Django

Code

Changeset 6460

Show
Ignore:
Timestamp:
10/06/07 23:21:31 (1 year ago)
Author:
jbronn
Message:

gis: gdal: The OGRGeometry srs property is now mutable, and added the srid property.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/gdal/geometries.py

    r6440 r6460  
    4040""" 
    4141# types & ctypes 
    42 from types import IntType, StringType 
     42from types import IntType, StringType, UnicodeType 
    4343from ctypes import byref, string_at, c_char_p, c_double, c_int, c_void_p 
    4444 
     
    182182        return self.point_count 
    183183 
    184     @property 
    185     def srs(self): 
     184    # The SRS property 
     185    def get_srs(self): 
    186186        "Returns the Spatial Reference for this Geometry." 
    187187        srs_ptr = lgdal.OGR_G_GetSpatialReference(self._g) 
     
    190190        else: 
    191191            return None 
     192 
     193    def set_srs(self, srs): 
     194        "Sets the SpatialReference for this geometry." 
     195        if isinstance(srs, SpatialReference): 
     196            srs_ptr = lgdal.OSRClone(srs._srs) 
     197        elif isinstance(srs, (StringType, UnicodeType, IntType)): 
     198            sr = SpatialReference(srs) 
     199            srs_ptr = lgdal.OSRClone(sr._srs) 
     200        else: 
     201            raise TypeError('Cannot assign spatial reference with object of type: %s' % type(srs)) 
     202        lgdal.OGR_G_AssignSpatialReference(self._g, srs_ptr) 
     203 
     204    srs = property(get_srs, set_srs) 
     205 
     206    # The SRID property 
     207    def get_srid(self): 
     208        if self.srs: return self.srs.srid 
     209        else: return None 
     210 
     211    def set_srid(self, srid): 
     212        if isinstance(srid, IntType): 
     213            self.srs = srid 
     214        else: 
     215            raise TypeError('SRID must be set with an integer.') 
     216 
     217    srid = property(get_srid, set_srid) 
    192218 
    193219    @property 
  • django/branches/gis/django/contrib/gis/gdal/srs.py

    r6436 r6460  
    1 import re 
    2 from types import StringType, UnicodeType, TupleType 
    3 from ctypes import \ 
    4      c_char_p, c_int, c_double, c_void_p, POINTER, \ 
    5      byref, string_at, create_string_buffer 
    6  
    7 # Getting the GDAL C Library 
    8 from django.contrib.gis.gdal.libgdal import lgdal 
    9  
    10 # Getting the error checking routine and exceptions 
    11 from django.contrib.gis.gdal.error import check_err, OGRException, SRSException 
    12  
    131""" 
    142  The Spatial Reference class, represensents OGR Spatial Reference objects. 
     
    3927  NAD83 / Texas South Central 
    4028""" 
     29import re 
     30from types import StringType, UnicodeType, TupleType 
     31from ctypes import \ 
     32     c_char_p, c_int, c_double, c_void_p, POINTER, \ 
     33     byref, string_at, create_string_buffer 
     34 
     35# Getting the GDAL C Library 
     36from django.contrib.gis.gdal.libgdal import lgdal 
     37 
     38# Getting the error checking routine and exceptions 
     39from django.contrib.gis.gdal.error import check_err, OGRException, SRSException 
     40 
    4141#### ctypes function prototypes #### 
    4242def ellipsis_func(f): 
     
    205205        if undefined. 
    206206        """ 
    207         return self.srs['AUTHORITY', 1] 
     207        try: 
     208            return int(self.attr_value('AUTHORITY', 1)) 
     209        except ValueError: 
     210            return None 
    208211         
    209212    #### Unit Properties #### 
     
    328331        w = c_char_p() 
    329332        check_err(lgdal.OSRExportToWkt(self._srs, byref(w))) 
    330         return string_at(w) 
     333        if w: return string_at(w) 
    331334 
    332335    @property 
     
    335338        w = c_char_p() 
    336339        check_err(lgdal.OSRExportToPrettyWkt(self._srs, byref(w), c_int(simplify))) 
    337         return string_at(w) 
     340        if w: return string_at(w) 
    338341 
    339342    @property 
     
    342345        w = c_char_p() 
    343346        check_err(lgdal.OSRExportToProj4(self._srs, byref(w))) 
    344         return string_at(w) 
     347        if w: return string_at(w) 
    345348 
    346349    @property 
  • django/branches/gis/django/contrib/gis/tests/test_gdal_geom.py

    r6440 r6460  
    149149                    self.assertEqual(sr.wkt, ring.srs.wkt) 
    150150 
     151            mpoly = OGRGeometry(mp.wkt, 4326) 
     152            self.assertEqual(4326, mpoly.srid) 
     153            mpoly.srs = SpatialReference(4269) 
     154            self.assertEqual(4269, mpoly.srid) 
     155            self.assertEqual('NAD83', mpoly.srs.name) 
     156            for poly in mpoly: 
     157                self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) 
     158                poly.srs = 32140 
     159                for ring in poly: 
     160                    self.assertEqual(32140, ring.srs.srid) 
     161                    self.assertEqual('NAD83 / Texas South Central', ring.srs.name) 
     162                    ring.srs = str(SpatialReference(4326)) # back to WGS84 
     163                    self.assertEqual(4326, ring.srs.srid) 
     164 
     165                    # Using the `srid` property. 
     166                    ring.srid = 4322 
     167                    self.assertEqual('WGS 72', ring.srs.name) 
     168                    self.assertEqual(4322, ring.srid) 
     169             
     170 
     171 
    151172def suite(): 
    152173    s = unittest.TestSuite()