Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 claudep authored
28 django/contrib/gis/geos/geometry.py
View
@@ -11,6 +11,8 @@
# 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 @@ def prepared(self):
@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):
16 django/contrib/gis/geos/tests/test_geos.py
View
@@ -662,6 +662,22 @@ def test_srid(self):
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 ###
Please sign in to comment.
Something went wrong with that request. Please try again.