Django

Code

Changeset 6596

Show
Ignore:
Timestamp:
10/22/07 18:37:26 (11 months ago)
Author:
jbronn
Message:

gis: added get_srid() spatial backend utility that takes into account -1 SRID values; removed from_hex flag from GEOS initialization; added tests for SRID values, and changed test_gdal module docstring.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/db/backend/oracle/field.py

    r6524 r6596  
    55from django.db.models.fields import Field # Django base Field class 
    66from django.contrib.gis.geos import GEOSGeometry 
    7 from django.contrib.gis.db.backend.util import GeoFieldSQL 
     7from django.contrib.gis.db.backend.util import get_srid, GeoFieldSQL 
    88from django.contrib.gis.db.backend.oracle.adaptor import OracleSpatialAdaptor 
    99from django.contrib.gis.db.backend.oracle.query import ORACLE_SPATIAL_TERMS, TRANSFORM 
     
    119119            # Getting the SRID of the geometry, or defaulting to that of the field if 
    120120            # it is None. 
    121             if value.srid is None: srid = self._srid 
    122             else: srid = value.srid 
     121            srid = get_srid(self, value) 
    123122             
    124123            # The adaptor will be used by psycopg2 for quoting the WKT. 
  • django/branches/gis/django/contrib/gis/db/backend/postgis/field.py

    r6524 r6596  
    33from django.db.models.fields import Field # Django base Field class 
    44from django.contrib.gis.geos import GEOSGeometry, GEOSException  
    5 from django.contrib.gis.db.backend.util import GeoFieldSQL 
     5from django.contrib.gis.db.backend.util import get_srid, GeoFieldSQL 
    66from django.contrib.gis.db.backend.postgis.adaptor import PostGISAdaptor 
    77from django.contrib.gis.db.backend.postgis.query import POSTGIS_TERMS, TRANSFORM 
     
    110110            # Getting the SRID of the geometry, or defaulting to that of the field if 
    111111            # it is None. 
    112             if value.srid is None: srid = self._srid 
    113             else: srid = value.srid 
     112            srid = get_srid(self, value) 
    114113 
    115114            # The adaptor will be used by psycopg2 for quoting the WKB. 
  • django/branches/gis/django/contrib/gis/db/backend/util.py

    r6508 r6596  
    77        self.where = where 
    88        self.params = params 
     9 
     10def get_srid(field, geom): 
     11    """ 
     12    Gets the SRID depending on the value of the SRID setting of the field 
     13    and that of the given geometry. 
     14    """ 
     15    if geom.srid is None or (geom.srid == -1 and field._srid != -1): 
     16        return field._srid 
     17    else: 
     18        return geom.srid 
  • django/branches/gis/django/contrib/gis/geos/base.py

    r6508 r6596  
    4949         (SRID) number for this Geometry.  If not set, the SRID will be None. 
    5050        """  
    51         from_hex = False 
    5251        if isinstance(geo_input, UnicodeType): 
    5352            # Encoding to ASCII, WKT or HEXEWKB doesn't need any more. 
     
    5655            if hex_regex.match(geo_input): 
    5756                # If the regex matches, the geometry is in HEX form. 
    58                 from_hex = True 
    5957                sz = c_size_t(len(geo_input)) 
    6058                buf = create_string_buffer(geo_input) 
     
    8785        if srid and isinstance(srid, int): self.srid = srid 
    8886 
    89         # Exported HEX from other GEOS geometries will have -1 SRID --  
    90         # set here to 0, when the SRID is not explicitly given. 
    91         if not srid and from_hex: self.srid = 0 
    92  
    9387        # Setting the class type (e.g., 'Point', 'Polygon', etc.) 
    9488        self.__class__ = GEOS_CLASSES[self.geom_type] 
     
    423417        -1 by default, even if the SRID is set. 
    424418        """ 
     419        # A possible faster, all-python, implementation:  
     420        #  str(self.wkb).encode('hex') 
    425421        sz = c_size_t() 
    426422        h = lgeos.GEOSGeomToHEX_buf(self._ptr(), byref(sz)) 
  • django/branches/gis/django/contrib/gis/tests/geoapp/tests.py

    r6524 r6596  
    139139        if not oracle: 
    140140            h = City.objects.transform('point', srid=htown.srid).get(name='Houston') 
     141            self.assertEqual(3084, h.point.srid) 
    141142            self.assertAlmostEqual(htown.x, h.point.x, 8) 
    142143            self.assertAlmostEqual(htown.y, h.point.y, 8) 
    143144 
    144145        p = City.objects.transform('point', srid=ptown.srid).get(name='Pueblo') 
     146        self.assertEqual(2774, p.point.srid) 
    145147        self.assertAlmostEqual(ptown.x, p.point.x, 7) 
    146148        self.assertAlmostEqual(ptown.y, p.point.y, 7) 
  • django/branches/gis/django/contrib/gis/tests/test_gdal.py

    r6436 r6596  
     1""" 
     2Module for executing all of the GDAL tests.  None 
     3of these tests require the use of the database. 
     4""" 
    15from unittest import TestSuite, TextTestRunner 
    26 
     
    2125 
    2226def run(verbosity=1): 
    23     "Runs the tests that do not require geographic (GEOS, GDAL, etc.) models." 
     27    "Runs the GDAL tests." 
    2428    TextTestRunner(verbosity=verbosity).run(suite()) 
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r6465 r6596  
    608608        for i in range(len(gc)): self.assertEqual(32021, gc[i].srid) 
    609609 
     610        # GEOS may get the SRID from HEXEWKB 
     611        # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS 
     612        # using `SELECT GeomFromText('POINT (5 23)', 4326);`. 
     613        hex = '0101000020E610000000000000000014400000000000003740' 
     614        p1 = fromstr(hex) 
     615        self.assertEqual(4326, p1.srid) 
     616 
     617        # However, when HEX is exported, the SRID information is lost 
     618        # and set to -1.  Essentially, the 'E' of the EWKB is not 
     619        # encoded in HEX by the GEOS C library for some reason. 
     620        p2 = fromstr(p1.hex) 
     621        self.assertEqual(-1, p2.srid) 
     622        p3 = fromstr(p1.hex, srid=-1) # -1 is intended. 
     623        self.assertEqual(-1, p3.srid) 
     624 
    610625    def test16_mutable_geometries(self): 
    611626        "Testing the mutability of Polygons and Geometry Collections."