Commit 360217fc authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #19171 -- Allowed coordinate transforms with custom SRIDs

Thanks reidpr at lanl.gov for the report.
parent e6f5b7ea
Loading
Loading
Loading
Loading
+16 −12
Original line number Diff line number Diff line
@@ -11,6 +11,8 @@ from django.contrib.gis import memoryview
# super-class for mutable list behavior
from django.contrib.gis.geos.mutable_list import ListMixin

from django.contrib.gis.gdal.error import SRSException

# GEOS-related dependencies.
from django.contrib.gis.geos.base import GEOSBase, gdal
from django.contrib.gis.geos.coordseq import GEOSCoordSeq
@@ -460,24 +462,26 @@ class GEOSGeometry(GEOSBase, ListMixin):
    @property
    def ogr(self):
        "Returns the OGR Geometry for this Geometry."
        if gdal.HAS_GDAL:
        if not gdal.HAS_GDAL:
            raise GEOSException('GDAL required to convert to an OGRGeometry.')
        if self.srid:
            try:
                return gdal.OGRGeometry(self.wkb, self.srid)
            else:
            except SRSException:
                pass
        return gdal.OGRGeometry(self.wkb)
        else:
            raise GEOSException('GDAL required to convert to an OGRGeometry.')

    @property
    def srs(self):
        "Returns the OSR SpatialReference for SRID of this Geometry."
        if gdal.HAS_GDAL:
        if not gdal.HAS_GDAL:
            raise GEOSException('GDAL required to return a SpatialReference object.')
        if self.srid:
            try:
                return gdal.SpatialReference(self.srid)
            else:
            except SRSException:
                pass
        return None
        else:
            raise GEOSException('GDAL required to return a SpatialReference object.')

    @property
    def crs(self):
+16 −0
Original line number Diff line number Diff line
@@ -662,6 +662,22 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
        p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
        self.assertEqual(-1, p3.srid)

    def test_custom_srid(self):
        """ Test with a srid unknown from GDAL """
        pnt = Point(111200, 220900, srid=999999)
        self.assertTrue(pnt.ewkt.startswith("SRID=999999;POINT (111200.0"))
        self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
        self.assertIsNone(pnt.srs)

        # Test conversion from custom to a known srid
        c2w = gdal.CoordTransform(
            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'),
            gdal.SpatialReference(4326))
        new_pnt = pnt.transform(c2w, clone=True)
        self.assertEqual(new_pnt.srid, 4326)
        self.assertAlmostEqual(new_pnt.x, 1, 3)
        self.assertAlmostEqual(new_pnt.y, 2, 3)

    def test_mutable_geometries(self):
        "Testing the mutability of Polygons and Geometry Collections."
        ### Testing the mutability of Polygons ###