Ticket #19171: 19171-1.diff

File 19171-1.diff, 3.1 KB (added by Claude Paroz, 12 years ago)

Transform should work with custom srid

  • django/contrib/gis/geos/geometry.py

    diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py
    index df396bd..ceb41aa 100644
    a b from django.contrib.gis import memoryview  
    1111# super-class for mutable list behavior
    1212from django.contrib.gis.geos.mutable_list import ListMixin
    1313
     14from django.contrib.gis.gdal.error import SRSException
     15
    1416# GEOS-related dependencies.
    1517from django.contrib.gis.geos.base import GEOSBase, gdal
    1618from django.contrib.gis.geos.coordseq import GEOSCoordSeq
    class GEOSGeometry(GEOSBase, ListMixin):  
    460462    @property
    461463    def ogr(self):
    462464        "Returns the OGR Geometry for this Geometry."
    463         if gdal.HAS_GDAL:
    464             if self.srid:
    465                 return gdal.OGRGeometry(self.wkb, self.srid)
    466             else:
    467                 return gdal.OGRGeometry(self.wkb)
    468         else:
     465        if not gdal.HAS_GDAL:
    469466            raise GEOSException('GDAL required to convert to an OGRGeometry.')
     467        if self.srid:
     468            try:
     469                return gdal.OGRGeometry(self.wkb, self.srid)
     470            except SRSException:
     471                pass
     472        return gdal.OGRGeometry(self.wkb)
    470473
    471474    @property
    472475    def srs(self):
    473476        "Returns the OSR SpatialReference for SRID of this Geometry."
    474         if gdal.HAS_GDAL:
    475             if self.srid:
    476                 return gdal.SpatialReference(self.srid)
    477             else:
    478                 return None
    479         else:
     477        if not gdal.HAS_GDAL:
    480478            raise GEOSException('GDAL required to return a SpatialReference object.')
     479        if self.srid:
     480            try:
     481                return gdal.SpatialReference(self.srid)
     482            except SRSException:
     483                pass
     484        return None
    481485
    482486    @property
    483487    def crs(self):
  • django/contrib/gis/geos/tests/test_geos.py

    diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
    index 283daa4..ad1404f 100644
    a b class GEOSTest(unittest.TestCase, TestDataMixin):  
    646646        p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
    647647        self.assertEqual(-1, p3.srid)
    648648
     649    def test_custom_srid(self):
     650        """ Test with a srid unknown from GDAL """
     651        pnt = Point(111200, 220900, srid=999999)
     652        self.assertTrue(pnt.ewkt.startswith("SRID=999999;POINT (111200.0"))
     653        self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
     654        self.assertIsNone(pnt.srs)
     655
     656        # Test conversion from custom to a known srid
     657        c2w = gdal.CoordTransform(
     658            gdal.SpatialReference('+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R_A +ellps=WGS84 +datum=WGS84 +units=m +no_defs'),
     659            gdal.SpatialReference(4326))
     660        new_pnt = pnt.transform(c2w, clone=True)
     661        self.assertEqual(new_pnt.srid, 4326)
     662        self.assertAlmostEqual(new_pnt.x, 1, 3)
     663        self.assertAlmostEqual(new_pnt.y, 2, 3)
     664
    649665    def test_mutable_geometries(self):
    650666        "Testing the mutability of Polygons and Geometry Collections."
    651667        ### Testing the mutability of Polygons ###
Back to Top