web/lib/django/contrib/gis/gdal/layer.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
     1 # Needed ctypes routines
     1 # Needed ctypes routines
     2 from ctypes import byref
     2 from ctypes import c_double, byref
     3 
     3 
     4 # Other GDAL imports.
     4 # Other GDAL imports.
     5 from django.contrib.gis.gdal.base import GDALBase
     5 from django.contrib.gis.gdal.base import GDALBase
     6 from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope
     6 from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope
     7 from django.contrib.gis.gdal.error import OGRException, OGRIndexError, SRSException
     7 from django.contrib.gis.gdal.error import OGRException, OGRIndexError, SRSException
     8 from django.contrib.gis.gdal.feature import Feature
     8 from django.contrib.gis.gdal.feature import Feature
     9 from django.contrib.gis.gdal.field import OGRFieldTypes
     9 from django.contrib.gis.gdal.field import OGRFieldTypes
    10 from django.contrib.gis.gdal.geometries import OGRGeomType
    10 from django.contrib.gis.gdal.geomtype import OGRGeomType
       
    11 from django.contrib.gis.gdal.geometries import OGRGeometry
    11 from django.contrib.gis.gdal.srs import SpatialReference
    12 from django.contrib.gis.gdal.srs import SpatialReference
    12 
    13 
    13 # GDAL ctypes function prototypes.
    14 # GDAL ctypes function prototypes.
    14 from django.contrib.gis.gdal.prototypes import ds as capi, srs as srs_api
    15 from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api, srs as srs_api
    15 
    16 
    16 # For more information, see the OGR C API source code:
    17 # For more information, see the OGR C API source code:
    17 #  http://www.gdal.org/ogr/ogr__api_8h.html
    18 #  http://www.gdal.org/ogr/ogr__api_8h.html
    18 #
    19 #
    19 # The OGR_L_* routines are relevant here.
    20 # The OGR_L_* routines are relevant here.
   154     def field_precisions(self):
   155     def field_precisions(self):
   155         "Returns the field precisions for the features."
   156         "Returns the field precisions for the features."
   156         return [capi.get_field_precision(capi.get_field_defn(self._ldefn, i))
   157         return [capi.get_field_precision(capi.get_field_defn(self._ldefn, i))
   157                 for i in xrange(self.num_fields)]
   158                 for i in xrange(self.num_fields)]
   158 
   159 
       
   160     def _get_spatial_filter(self):
       
   161         try:
       
   162             return OGRGeometry(geom_api.clone_geom(capi.get_spatial_filter(self.ptr)))
       
   163         except OGRException:
       
   164             return None
       
   165 
       
   166     def _set_spatial_filter(self, filter):
       
   167         if isinstance(filter, OGRGeometry):
       
   168             capi.set_spatial_filter(self.ptr, filter.ptr)
       
   169         elif isinstance(filter, (tuple, list)):
       
   170             if not len(filter) == 4:
       
   171                 raise ValueError('Spatial filter list/tuple must have 4 elements.')
       
   172             # Map c_double onto params -- if a bad type is passed in it
       
   173             # will be caught here.
       
   174             xmin, ymin, xmax, ymax = map(c_double, filter)
       
   175             capi.set_spatial_filter_rect(self.ptr, xmin, ymin, xmax, ymax)
       
   176         elif filter is None:
       
   177             capi.set_spatial_filter(self.ptr, None)
       
   178         else:
       
   179             raise TypeError('Spatial filter must be either an OGRGeometry instance, a 4-tuple, or None.')
       
   180 
       
   181     spatial_filter = property(_get_spatial_filter, _set_spatial_filter)
       
   182 
   159     #### Layer Methods ####
   183     #### Layer Methods ####
   160     def get_fields(self, field_name):
   184     def get_fields(self, field_name):
   161         """
   185         """
   162         Returns a list containing the given field name for every Feature
   186         Returns a list containing the given field name for every Feature
   163         in the Layer.
   187         in the Layer.