Django

Code

Changeset 7113

Show
Ignore:
Timestamp:
02/14/08 10:05:44 (5 months ago)
Author:
jbronn
Message:

gis: gdal: Fixed GDAL version string parsing for development versions; GEOJSON constant now in root module; added extent property and coords property alias for tuple; added x,y,z properties for OGR LineStrings?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/gdal/geometries.py

    r7107 r7113  
    217217    def envelope(self): 
    218218        "Returns the envelope for this Geometry." 
     219        # TODO: Fix Envelope() for Point geometries. 
    219220        return Envelope(get_envelope(self._ptr, byref(OGREnvelope()))) 
     221 
     222    @property 
     223    def extent(self): 
     224        "Returns the envelope as a 4-tuple, instead of as an Envelope object." 
     225        return self.envelope.tuple 
    220226 
    221227    #### SpatialReference-related Properties #### 
     
    447453    def z(self): 
    448454        "Returns the Z coordinate for this Point." 
    449         return getz(self._ptr, 0) 
     455        if self.coord_dim == 3: 
     456            return getz(self._ptr, 0) 
    450457 
    451458    @property 
     
    456463        elif self.coord_dim == 3: 
    457464            return (self.x, self.y, self.z) 
     465    coords = tuple 
    458466 
    459467class LineString(OGRGeometry): 
     
    486494    def tuple(self): 
    487495        "Returns the tuple representation of this LineString." 
    488         return tuple(self[i] for i in xrange(len(self))) 
     496        return tuple([self[i] for i in xrange(len(self))]) 
     497    coords = tuple 
     498 
     499    def _listarr(self, func): 
     500        """ 
     501        Internal routine that returns a sequence (list) corresponding with 
     502        the given function. 
     503        """ 
     504        return [func(self._ptr, i) for i in xrange(len(self))] 
     505 
     506    @property 
     507    def x(self): 
     508        "Returns the X coordinates in a list." 
     509        return self._listarr(getx) 
     510 
     511    @property 
     512    def y(self): 
     513        "Returns the Y coordinates in a list." 
     514        return self._listarr(gety) 
     515     
     516    @property 
     517    def z(self): 
     518        "Returns the Z coordinates in a list." 
     519        if self.coord_dim == 3: 
     520            return self._listarr(getz) 
    489521 
    490522# LinearRings are used in Polygons. 
     
    514546        "Returns the shell of this Polygon." 
    515547        return self[0] # First ring is the shell 
     548    exterior_ring = shell 
    516549 
    517550    @property 
    518551    def tuple(self): 
    519552        "Returns a tuple of LinearRing coordinate tuples." 
    520         return tuple(self[i].tuple for i in xrange(self.geom_count)) 
     553        return tuple([self[i].tuple for i in xrange(self.geom_count)]) 
     554    coords = tuple 
    521555 
    522556    @property 
     
    576610    def tuple(self): 
    577611        "Returns a tuple representation of this Geometry Collection." 
    578         return tuple(self[i].tuple for i in xrange(self.geom_count))  
     612        return tuple([self[i].tuple for i in xrange(self.geom_count)]) 
     613    coords = tuple 
    579614 
    580615# Multiple Geometry types. 
  • django/branches/gis/django/contrib/gis/gdal/__init__.py

    r6686 r7113  
    2929    from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, gdal_release_date 
    3030    from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
    31     from django.contrib.gis.gdal.geometries import OGRGeometry 
     31    from django.contrib.gis.gdal.geometries import OGRGeometry, GEOJSON 
    3232    HAS_GDAL = True 
    3333except: 
    34     HAS_GDAL = False 
     34    HAS_GDAL, GEOJSON = False, False 
    3535 
    3636# The envelope, error, and geomtype modules do not actually require the 
  • django/branches/gis/django/contrib/gis/gdal/prototypes/geom.py

    r7107 r7113  
    99 
    1010# Some prototypes need to be aware of what version GDAL we have. 
    11 major, minor1, minor2 = map(int, gdal_version().split('.')
    12 if major <= 1 and minor1 <= 4: 
     11major, minor = map(int, gdal_version().split('.')[:2]
     12if major <= 1 and minor <= 4: 
    1313    GEOJSON = False 
    1414else: 
  • django/branches/gis/django/contrib/gis/tests/test_gdal_geom.py

    r7107 r7113  
    117117            self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr)) 
    118118            prev = linestr 
     119 
     120            # Testing the x, y properties. 
     121            x = [tmpx for tmpx, tmpy in ls.tup] 
     122            y = [tmpy for tmpx, tmpy in ls.tup] 
     123            self.assertEqual(x, linestr.x) 
     124            self.assertEqual(y, linestr.y) 
    119125 
    120126    def test05_multilinestring(self): 
     
    355361            for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp) 
    356362 
     363    def test15_extent(self): 
     364        "Testing `extent` property." 
     365        # The xmin, ymin, xmax, ymax of the MultiPoint should be returned. 
     366        mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)') 
     367        self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) 
     368        # Testing on the 'real world' Polygon. 
     369        poly = OGRGeometry(polygons[3].wkt) 
     370        ring = poly.shell 
     371        x, y = ring.x, ring.y 
     372        xmin, ymin = min(x), min(y) 
     373        xmax, ymax = max(x), max(y) 
     374        self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) 
     375 
    357376def suite(): 
    358377    s = unittest.TestSuite()