From 360217fc87db82575c8b07704143ced07c2a234c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 9 Mar 2013 17:46:20 +0100 Subject: [PATCH] Fixed #19171 -- Allowed coordinate transforms with custom SRIDs Thanks reidpr at lanl.gov for the report. --- django/contrib/gis/geos/geometry.py | 28 ++++++++++++---------- django/contrib/gis/geos/tests/test_geos.py | 16 +++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py index 7ae57883e3..01719c21d4 100644 --- a/django/contrib/gis/geos/geometry.py +++ b/django/contrib/gis/geos/geometry.py @@ -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 self.srid: - return gdal.OGRGeometry(self.wkb, self.srid) - else: - return gdal.OGRGeometry(self.wkb) - else: + 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) + except SRSException: + pass + return gdal.OGRGeometry(self.wkb) @property def srs(self): "Returns the OSR SpatialReference for SRID of this Geometry." - if gdal.HAS_GDAL: - if self.srid: - return gdal.SpatialReference(self.srid) - else: - return None - else: + if not gdal.HAS_GDAL: raise GEOSException('GDAL required to return a SpatialReference object.') + if self.srid: + try: + return gdal.SpatialReference(self.srid) + except SRSException: + pass + return None @property def crs(self): diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index 0081c7f728..de9471211e 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -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 ###