Django

Code

Changeset 5527

Show
Ignore:
Timestamp:
06/24/07 20:29:01 (1 year ago)
Author:
jbronn
Message:

gis: added the Envelope class, used in getting layer and geometry extents; also added boundary and convex_hull properties.

Files:

Legend:

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

    r5478 r5527  
    11from Driver import Driver 
     2from Envelope import Envelope 
    23from DataSource import DataSource 
    34from SpatialReference import SpatialReference, CoordTransform 
  • django/branches/gis/django/contrib/gis/gdal/Layer.py

    r5397 r5527  
    11# Needed ctypes routines 
    2 from ctypes import c_int, c_long, c_void_p, string_at 
     2from ctypes import c_int, c_long, c_void_p, byref, string_at 
    33 
    44# The GDAL C Library 
     
    66 
    77# Other GDAL imports. 
     8from django.contrib.gis.gdal.Envelope import Envelope, OGREnvelope 
    89from django.contrib.gis.gdal.Feature import Feature 
    9 from django.contrib.gis.gdal.OGRError import OGRException 
     10from django.contrib.gis.gdal.OGRError import OGRException, check_err 
    1011from django.contrib.gis.gdal.SpatialReference import SpatialReference 
    1112 
     
    5960    #### Layer properties #### 
    6061    @property 
     62    def extent(self): 
     63        "Returns the extent (an Envelope) of this layer." 
     64        env = OGREnvelope() 
     65        check_err(lgdal.OGR_L_GetExtent(self._layer, byref(env), c_int(1))) 
     66        return Envelope(env) 
     67 
     68    @property 
    6169    def name(self): 
    6270        "Returns the name of this layer in the Data Source." 
  • django/branches/gis/django/contrib/gis/gdal/OGRGeometry.py

    r5478 r5527  
    55# Getting the GDAL C library and error checking facilities 
    66from django.contrib.gis.gdal.libgdal import lgdal 
     7from django.contrib.gis.gdal.Envelope import Envelope, OGREnvelope 
    78from django.contrib.gis.gdal.OGRError import check_err, OGRException 
    89from django.contrib.gis.gdal.SpatialReference import SpatialReference, CoordTransform 
     
    249250        a = lgdal.OGR_G_GetArea(self._g) 
    250251        return a.value 
     252 
     253    @property 
     254    def envelope(self): 
     255        "Returns the envelope for this Geometry." 
     256        env = OGREnvelope() 
     257        lgdal.OGR_G_GetEnvelope(self._g, byref(env)) 
     258        return Envelope(env) 
    251259     
    252260    #### Geometry Methods #### 
     
    323331 
    324332    #### Geometry-generation Methods #### 
    325     def _geomgen(self, gen_func, other): 
    326         if not isinstance(other, OGRGeometry): 
    327             raise OGRException, 'Must use another OGRGeometry object for geometry-generating operations!' 
    328         return OGRGeometry(gen_func(self._g, other._g)) 
     333    def _geomgen(self, gen_func, other=None): 
     334        "A helper routine for the OGR routines that generate geometries." 
     335        if isinstance(other, OGRGeometry): 
     336            return OGRGeometry(gen_func(self._g, other._g)) 
     337        else: 
     338            return OGRGeometry(gen_func(self._g)) 
     339 
     340    @property 
     341    def boundary(self): 
     342        "Returns the boundary of this geometry." 
     343        return self._geomgen(lgdal.OGR_G_GetBoundary) 
     344 
     345    @property 
     346    def convex_hull(self): 
     347        "Returns the smallest convex Polygon that contains all the points in the Geometry." 
     348        return self._geomgen(lgdal.OGR_G_ConvexHull) 
    329349 
    330350    def union(self, other): 
  • django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py

    r5478 r5527  
    11import os, os.path, unittest 
    22from django.contrib.gis.gdal import DataSource, OGRException 
     3from django.contrib.gis.gdal.Envelope import Envelope 
    34from django.contrib.gis.gdal.Field import OFTReal, OFTInteger, OFTString 
    45 
     
    1718# List of acceptable data sources. 
    1819ds_list = (TestSHP('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, fields={'dbl' : OFTReal, 'int' : OFTReal, 'str' : OFTString,}, 
     20                   extent=(-1.35011,0.166623,-0.524093,0.824508), # Got extent from QGIS 
    1921                   srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'), 
    2022           TestSHP('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3, fields={'float' : OFTReal, 'int' : OFTReal, 'str' : OFTString,}, 
     23                   extent=(-1.01513,-0.558245,0.161876,0.839637), # Got extent from QGIS 
    2124                   srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'), 
    2225           ) 
     
    6871 
    6972                # Making sure we get the number of fields we expect 
    70                 self.assertEqual(layer.num_fields, source.nfld) 
    71                 self.assertEqual(len(layer.fields), source.nfld) 
     73                self.assertEqual(source.nfld, layer.num_fields) 
     74                self.assertEqual(source.nfld, len(layer.fields)) 
     75 
     76                # Testing the layer's extent (an Envelope), and it's properties 
     77                self.assertEqual(True, isinstance(layer.extent, Envelope)) 
     78                self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5) 
     79                self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5) 
     80                self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5) 
     81                self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5) 
    7282 
    7383                # Now checking the field names. 
     
    114124                    # Making sure the SpatialReference is as expected. 
    115125                    self.assertEqual(source.srs_wkt, g.srs.wkt) 
    116                      
     126                         
    117127def suite(): 
    118128    s = unittest.TestSuite()