Code

Ticket #19171: 19171-1.diff

File 19171-1.diff, 3.1 KB (added by claudep, 18 months ago)

Transform should work with custom srid

Line 
1diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py
2index df396bd..ceb41aa 100644
3--- a/django/contrib/gis/geos/geometry.py
4+++ b/django/contrib/gis/geos/geometry.py
5@@ -11,6 +11,8 @@ from django.contrib.gis import memoryview
6 # super-class for mutable list behavior
7 from django.contrib.gis.geos.mutable_list import ListMixin
8 
9+from django.contrib.gis.gdal.error import SRSException
10+
11 # GEOS-related dependencies.
12 from django.contrib.gis.geos.base import GEOSBase, gdal
13 from django.contrib.gis.geos.coordseq import GEOSCoordSeq
14@@ -460,24 +462,26 @@ class GEOSGeometry(GEOSBase, ListMixin):
15     @property
16     def ogr(self):
17         "Returns the OGR Geometry for this Geometry."
18-        if gdal.HAS_GDAL:
19-            if self.srid:
20-                return gdal.OGRGeometry(self.wkb, self.srid)
21-            else:
22-                return gdal.OGRGeometry(self.wkb)
23-        else:
24+        if not gdal.HAS_GDAL:
25             raise GEOSException('GDAL required to convert to an OGRGeometry.')
26+        if self.srid:
27+            try:
28+                return gdal.OGRGeometry(self.wkb, self.srid)
29+            except SRSException:
30+                pass
31+        return gdal.OGRGeometry(self.wkb)
32 
33     @property
34     def srs(self):
35         "Returns the OSR SpatialReference for SRID of this Geometry."
36-        if gdal.HAS_GDAL:
37-            if self.srid:
38-                return gdal.SpatialReference(self.srid)
39-            else:
40-                return None
41-        else:
42+        if not gdal.HAS_GDAL:
43             raise GEOSException('GDAL required to return a SpatialReference object.')
44+        if self.srid:
45+            try:
46+                return gdal.SpatialReference(self.srid)
47+            except SRSException:
48+                pass
49+        return None
50 
51     @property
52     def crs(self):
53diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
54index 283daa4..ad1404f 100644
55--- a/django/contrib/gis/geos/tests/test_geos.py
56+++ b/django/contrib/gis/geos/tests/test_geos.py
57@@ -646,6 +646,22 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
58         p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
59         self.assertEqual(-1, p3.srid)
60 
61+    def test_custom_srid(self):
62+        """ Test with a srid unknown from GDAL """
63+        pnt = Point(111200, 220900, srid=999999)
64+        self.assertTrue(pnt.ewkt.startswith("SRID=999999;POINT (111200.0"))
65+        self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
66+        self.assertIsNone(pnt.srs)
67+
68+        # Test conversion from custom to a known srid
69+        c2w = gdal.CoordTransform(
70+            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'),
71+            gdal.SpatialReference(4326))
72+        new_pnt = pnt.transform(c2w, clone=True)
73+        self.assertEqual(new_pnt.srid, 4326)
74+        self.assertAlmostEqual(new_pnt.x, 1, 3)
75+        self.assertAlmostEqual(new_pnt.y, 2, 3)
76+
77     def test_mutable_geometries(self):
78         "Testing the mutability of Polygons and Geometry Collections."
79         ### Testing the mutability of Polygons ###