Django

Code

Changeset 6465

Show
Ignore:
Timestamp:
10/07/07 17:42:26 (1 year ago)
Author:
jbronn
Message:

gis: geos: Fixed #5630 with patch from rcoup; added the ogr and srs properties.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/geos/base.py

    r6315 r6465  
    1515from django.contrib.gis.geos.libgeos import lgeos, HAS_NUMPY, ISQLQuote 
    1616from django.contrib.gis.geos.pointer import GEOSPointer, NULL_GEOM 
     17 
     18# Trying to import GDAL libraries, if available.  Have to place in 
     19# try/except since this package may be used outside GeoDjango. 
     20try: 
     21    from django.contrib.gis.gdal import OGRGeometry, SpatialReference 
     22    HAS_GDAL=True 
     23except: 
     24    HAS_GDAL=False 
    1725 
    1826# Regular expression for recognizing HEXEWKB and WKT.  A prophylactic measure 
     
    436444        return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype) 
    437445 
     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 
    438466    #### Topology Routines #### 
    439467    def _unary_topology(self, func, *args): 
  • django/branches/gis/django/contrib/gis/geos/geometries.py

    r6024 r6465  
    5454 
    5555    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 
    5758        if self.hasz: return 3 
    5859        else: return 2 
  • django/branches/gis/django/contrib/gis/geos/pointer.py

    r6024 r6465  
    187187 
    188188        # 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)): 
    190190            self._child_cs = True 
    191191        else: 
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r6314 r6465  
    55    MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, \ 
    66    fromstr, HAS_NUMPY 
     7from django.contrib.gis.geos.base import HAS_GDAL 
    78from geometries import * 
    89 
    910if HAS_NUMPY: from numpy import array 
     11if HAS_GDAL: from django.contrib.gis.gdal import OGRGeometry, SpatialReference 
    1012 
    1113class GEOSTest(unittest.TestCase): 
     
    723725        self.assertEqual(8.0, mpoly.length) 
    724726 
     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         
    725778def suite(): 
    726779    s = unittest.TestSuite()