Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19171 -- Allowed coordinate transforms with custom SRIDs

Thanks reidpr at lanl.gov for the report.
  • Loading branch information...
commit 360217fc87db82575c8b07704143ced07c2a234c 1 parent e6f5b7e
Claude Paroz authored
28  django/contrib/gis/geos/geometry.py
@@ -11,6 +11,8 @@
11 11
 # super-class for mutable list behavior
12 12
 from django.contrib.gis.geos.mutable_list import ListMixin
13 13
 
  14
+from django.contrib.gis.gdal.error import SRSException
  15
+
14 16
 # GEOS-related dependencies.
15 17
 from django.contrib.gis.geos.base import GEOSBase, gdal
16 18
 from django.contrib.gis.geos.coordseq import GEOSCoordSeq
@@ -460,24 +462,26 @@ def prepared(self):
460 462
     @property
461 463
     def ogr(self):
462 464
         "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:
469 466
             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)
470 473
 
471 474
     @property
472 475
     def srs(self):
473 476
         "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:
480 478
             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
481 485
 
482 486
     @property
483 487
     def crs(self):
16  django/contrib/gis/geos/tests/test_geos.py
@@ -662,6 +662,22 @@ def test_srid(self):
662 662
         p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
663 663
         self.assertEqual(-1, p3.srid)
664 664
 
  665
+    def test_custom_srid(self):
  666
+        """ Test with a srid unknown from GDAL """
  667
+        pnt = Point(111200, 220900, srid=999999)
  668
+        self.assertTrue(pnt.ewkt.startswith("SRID=999999;POINT (111200.0"))
  669
+        self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
  670
+        self.assertIsNone(pnt.srs)
  671
+
  672
+        # Test conversion from custom to a known srid
  673
+        c2w = gdal.CoordTransform(
  674
+            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'),
  675
+            gdal.SpatialReference(4326))
  676
+        new_pnt = pnt.transform(c2w, clone=True)
  677
+        self.assertEqual(new_pnt.srid, 4326)
  678
+        self.assertAlmostEqual(new_pnt.x, 1, 3)
  679
+        self.assertAlmostEqual(new_pnt.y, 2, 3)
  680
+
665 681
     def test_mutable_geometries(self):
666 682
         "Testing the mutability of Polygons and Geometry Collections."
667 683
         ### Testing the mutability of Polygons ###

0 notes on commit 360217f

Please sign in to comment.
Something went wrong with that request. Please try again.