Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

gis: geos: Fixed #5630 with patch from rcoup; added the `ogr` and `sr…

…s` properties.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6465 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5d7fd63423e631773880265fab82749189932c0a 1 parent ddc9361
Justin Bronn authored October 07, 2007
28  django/contrib/gis/geos/base.py
@@ -15,6 +15,14 @@
15 15
 from django.contrib.gis.geos.libgeos import lgeos, HAS_NUMPY, ISQLQuote
16 16
 from django.contrib.gis.geos.pointer import GEOSPointer, NULL_GEOM
17 17
 
  18
+# Trying to import GDAL libraries, if available.  Have to place in
  19
+# try/except since this package may be used outside GeoDjango.
  20
+try:
  21
+    from django.contrib.gis.gdal import OGRGeometry, SpatialReference
  22
+    HAS_GDAL=True
  23
+except:
  24
+    HAS_GDAL=False
  25
+
18 26
 # Regular expression for recognizing HEXEWKB and WKT.  A prophylactic measure
19 27
 #  to prevent potentially malicious input from reaching the underlying C
20 28
 #  library.  Not a substitute for good web security programming practices.
@@ -435,6 +443,26 @@ def kml(self):
435 443
         gtype = self.geom_type
436 444
         return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype)
437 445
 
  446
+    #### GDAL-specific output routines ####
  447
+    @property
  448
+    def ogr(self):
  449
+        "Returns the OGR Geometry for this Geometry."
  450
+        if HAS_GDAL:
  451
+            if self.srid:
  452
+                return OGRGeometry(self.wkb, self.srid)
  453
+            else:
  454
+                return OGRGeometry(self.wkb)
  455
+        else:
  456
+            return None
  457
+
  458
+    @property
  459
+    def srs(self):
  460
+        "Returns the OSR SpatialReference for SRID of this Geometry."
  461
+        if HAS_GDAL and self.srid:
  462
+            return SpatialReference(self.srid)
  463
+        else:
  464
+            return None
  465
+
438 466
     #### Topology Routines ####
439 467
     def _unary_topology(self, func, *args):
440 468
         """
3  django/contrib/gis/geos/geometries.py
@@ -53,7 +53,8 @@ def __init__(self, x, y=None, z=None, srid=None):
53 53
         super(Point, self).__init__(lgeos.GEOSGeom_createPoint(cs), srid=srid)
54 54
 
55 55
     def __len__(self):
56  
-        "Returns the number of dimensions for this Point (either 2 or 3)."
  56
+        "Returns the number of dimensions for this Point (either 0, 2 or 3)."
  57
+        if self.empty: return 0
57 58
         if self.hasz: return 3
58 59
         else: return 2
59 60
         
2  django/contrib/gis/geos/pointer.py
@@ -186,7 +186,7 @@ def set_children(self, ptr_list):
186 186
         n_child = len(ptr_list)
187 187
 
188 188
         # Determining whether coordinate sequences pointers were passed in.
189  
-        if isinstance(ptr_list[0], (tuple, list)):
  189
+        if n_child and isinstance(ptr_list[0], (tuple, list)):
190 190
             self._child_cs = True
191 191
         else:
192 192
             self._child_cs = False
53  django/contrib/gis/tests/test_geos.py
@@ -4,9 +4,11 @@
4 4
     GEOSGeometry, Point, LineString, LinearRing, Polygon, \
5 5
     MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, \
6 6
     fromstr, HAS_NUMPY
  7
+from django.contrib.gis.geos.base import HAS_GDAL
7 8
 from geometries import *
8 9
 
9 10
 if HAS_NUMPY: from numpy import array
  11
+if HAS_GDAL: from django.contrib.gis.gdal import OGRGeometry, SpatialReference
10 12
 
11 13
 class GEOSTest(unittest.TestCase):
12 14
 
@@ -722,6 +724,57 @@ def test19_length(self):
722 724
         mpoly = MultiPolygon(poly.clone(), poly)
723 725
         self.assertEqual(8.0, mpoly.length)
724 726
 
  727
+    def test20_emptyCollections(self):
  728
+        "Testing empty geometries and collections."
  729
+        gc1 = GeometryCollection([])
  730
+        gc2 = fromstr('GEOMETRYCOLLECTION EMPTY')
  731
+        pnt = fromstr('POINT EMPTY')
  732
+        ls = fromstr('LINESTRING EMPTY')
  733
+        poly = fromstr('POLYGON EMPTY')
  734
+        mls = fromstr('MULTILINESTRING EMPTY')
  735
+        mpoly1 = fromstr('MULTIPOLYGON EMPTY')
  736
+        mpoly2 = MultiPolygon(())
  737
+
  738
+        for g in [gc1, gc2, pnt, ls, poly, mls, mpoly1, mpoly2]:
  739
+            self.assertEqual(True, g.empty)
  740
+
  741
+            # Testing len() and num_geom.
  742
+            if isinstance(g, Polygon):
  743
+                self.assertEqual(1, len(g)) # Has one empty linear ring
  744
+                self.assertEqual(1, g.num_geom)
  745
+                self.assertEqual(0, len(g[0]))
  746
+            elif isinstance(g, (Point, LineString)):
  747
+                self.assertEqual(1, g.num_geom)
  748
+                self.assertEqual(0, len(g))
  749
+            else:
  750
+                self.assertEqual(0, g.num_geom)
  751
+                self.assertEqual(0, len(g))
  752
+
  753
+            # Testing __getitem__ (doesn't work on Point or Polygon)
  754
+            if isinstance(g, Point):
  755
+                self.assertRaises(GEOSGeometryIndexError, g.get_x)
  756
+            elif isinstance(g, Polygon):
  757
+                lr = g.shell
  758
+                self.assertEqual('LINEARRING EMPTY', lr.wkt)
  759
+                self.assertEqual(0, len(lr))
  760
+                self.assertEqual(True, lr.empty)
  761
+                self.assertRaises(GEOSGeometryIndexError, lr.__getitem__, 0)
  762
+            else:
  763
+                self.assertRaises(GEOSGeometryIndexError, g.__getitem__, 0)
  764
+
  765
+    def test21_test_gdal(self):
  766
+        "Testing `ogr` and `srs` properties."
  767
+        if not HAS_GDAL: return
  768
+        g1 = fromstr('POINT(5 23)')
  769
+        self.assertEqual(True, isinstance(g1.ogr, OGRGeometry))
  770
+        self.assertEqual(g1.srs, None)
  771
+        
  772
+        g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
  773
+        self.assertEqual(True, isinstance(g2.ogr, OGRGeometry))
  774
+        self.assertEqual(True, isinstance(g2.srs, SpatialReference))
  775
+        self.assertEqual(g2.hex, g2.ogr.hex)
  776
+        self.assertEqual('WGS 84', g2.srs.name)
  777
+        
725 778
 def suite():
726 779
     s = unittest.TestSuite()
727 780
     s.addTest(unittest.makeSuite(GEOSTest))

0 notes on commit 5d7fd63

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