Django

Code

Changeset 6639

Show
Ignore:
Timestamp:
11/03/07 19:48:10 (8 months ago)
Author:
jbronn
Message:

gis: gdal:

(1) Added the field_widths and field_precision properties to Layer with patch from tlp.
(2) Field definition is now passed into the Feature from the Layer.
(3) Fixed memory leak issue with Feature not being properly deleted.
(4) OGR_L_GetNextFeature() now used for iteration on Layer.
(5) Added the gdal_version, gdal_full_version, and gdal_release_date functions.
(6) Made all initial pointers None instead of 0.

Files:

Legend:

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

    r6436 r6639  
    5656    def __init__(self, ds_input, ds_driver=False): 
    5757 
    58         self._ds = 0 # Initially NULL 
     58        self._ds = None # Initially NULL 
    5959 
    6060        # Registering all the drivers, this needs to be done 
  • django/branches/gis/django/contrib/gis/gdal/driver.py

    r6436 r6639  
    2828        if isinstance(input, StringType): 
    2929            # If a string name of the driver was passed in 
    30             self._dr = 0 # Initially NULL 
     30            self._dr = None # Initially NULL 
    3131            self._register() 
    3232 
  • django/branches/gis/django/contrib/gis/gdal/feature.py

    r6464 r6639  
    1818 
    1919    #### Python 'magic' routines #### 
    20     def __init__(self, f): 
     20    def __init__(self, feat, fdefn): 
    2121        "Needs a C pointer (Python integer in ctypes) in order to initialize." 
    22         self._feat = 0 # Initially NULL 
    23         self._fdefn = 0  
    24         if not f
     22        self._feat = None # Initially NULL 
     23        self._fdefn = None  
     24        if not feat or not fdefn
    2525            raise OGRException('Cannot create OGR Feature, invalid pointer given.') 
    26         self._feat = f 
    27         self._fdefn = lgdal.OGR_F_GetDefnRef(f) 
     26        self._feat = feat 
     27        self._fdefn = fdefn 
    2828 
    2929    def __del__(self): 
    3030        "Releases a reference to this object." 
    31         if self._fdefn: lgdal.OGR_FD_Release(self._fdefn
     31        if self._feat: lgdal.OGR_F_Destroy(self._feat
    3232 
    3333    def __getitem__(self, index): 
     
    9292 
    9393        # Attempting to retrieve the Spatial Reference for the geometry. 
    94         srs_ptr  = lgdal.OGR_G_GetSpatialReference(geom_ptr
     94        srs_ptr  = lgdal.OSRClone(lgdal.OGR_G_GetSpatialReference(geom_ptr)
    9595        if srs_ptr: 
    9696            srs = SpatialReference(srs_ptr, 'ogr') 
  • django/branches/gis/django/contrib/gis/gdal/field.py

    r6436 r6639  
    1313    def __init__(self, fld, val=''): 
    1414        "Needs a C pointer (Python integer in ctypes) in order to initialize." 
    15         self._fld = 0 # Initially NULL 
     15        self._fld = None # Initially NULL 
    1616 
    1717        if not fld: 
  • django/branches/gis/django/contrib/gis/gdal/__init__.py

    r6436 r6639  
    2727    from django.contrib.gis.gdal.driver import Driver 
    2828    from django.contrib.gis.gdal.datasource import DataSource 
     29    from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, gdal_release_date 
    2930    from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
    3031    from django.contrib.gis.gdal.geometries import OGRGeometry 
  • django/branches/gis/django/contrib/gis/gdal/layer.py

    r6421 r6639  
    2828    def __init__(self, l): 
    2929        "Needs a C pointer (Python/ctypes integer) in order to initialize." 
    30         self._layer = 0 # Initially NULL 
    31         self._ldefn = 0 
     30        self._layer = None # Initially NULL 
     31        self._ldefn = None 
    3232        if not l: 
    3333            raise OGRException, 'Cannot create Layer, invalid pointer given' 
     
    3737    def __getitem__(self, index): 
    3838        "Gets the Feature at the specified index." 
    39         def make_feature(offset): 
    40             return Feature(lgdal.OGR_L_GetFeature(self._layer, 
    41                                                   c_long(offset))) 
    42         end = self.num_feat 
    4339        if not isinstance(index, (slice, int)): 
    4440            raise TypeError 
    45         
     41        end = self.num_feat 
    4642        if isinstance(index,int): 
    4743            # An integer index was given 
     
    5046            if index < 0 or index >= self.num_feat: 
    5147                raise OGRIndexError, 'index out of range' 
    52             return make_feature(index) 
     48            return self._make_feature(index) 
    5349        else:  
    5450            # A slice was given 
    5551            start, stop, stride = index.indices(end) 
    56             return [make_feature(offset) for offset in range(start,stop,stride)] 
     52            return [self._make_feature(offset) for offset in range(start,stop,stride)] 
    5753 
    5854    def __iter__(self): 
    5955        "Iterates over each Feature in the Layer." 
    60         #TODO: is OGR's GetNextFeature faster here? 
     56        # ResetReading() must be called before iteration is to begin. 
     57        lgdal.OGR_L_ResetReading(self._layer) 
    6158        for i in range(self.num_feat): 
    62             yield self.__getitem__(i
     59            yield Feature(lgdal.OGR_L_GetNextFeature(self._layer), self._ldefn
    6360 
    6461    def __len__(self): 
     
    6966        "The string name of the layer." 
    7067        return self.name 
     68 
     69    def _make_feature(self, offset): 
     70        "Helper routine for __getitem__ that makes a feature from an offset." 
     71        return Feature(lgdal.OGR_L_GetFeature(self._layer, c_long(offset)), self._ldefn) 
    7172 
    7273    #### Layer properties #### 
     
    113114                 for i in xrange(self.num_fields) ] 
    114115     
     116    @property  
     117    def field_widths(self): 
     118        "Returns a list of the maximum field widths for the features." 
     119        return [ int(lgdal.OGR_Fld_GetWidth(lgdal.OGR_FD_GetFieldDefn(self._ldefn, i))) 
     120                 for i in xrange(self.num_fields) ] 
     121 
     122    @property  
     123    def field_precisions(self): 
     124        "Returns the field precisions for the features." 
     125        return [ int(lgdal.OGR_Fld_GetPrecision(lgdal.OGR_FD_GetFieldDefn(self._ldefn, i))) 
     126                 for i in xrange(self.num_fields) ] 
     127 
    115128    #### Layer Methods #### 
    116129    def get_fields(self, field_name): 
  • django/branches/gis/django/contrib/gis/gdal/libgdal.py

    r5749 r6639  
    11import os, sys 
    2 from ctypes import CDLL 
     2from ctypes import CDLL, string_at 
    33from django.contrib.gis.gdal.error import OGRException 
    44 
     
    2222lgdal = CDLL(lib_name) 
    2323 
     24#### Version-information functions. #### 
     25def _version_info(key): 
     26    "Returns GDAL library version information with the given key." 
     27    buf = lgdal.GDALVersionInfo(key) 
     28    if buf: return string_at(buf) 
     29 
     30def gdal_version(): 
     31    "Returns only the GDAL version number information." 
     32    return _version_info('RELEASE_NAME') 
     33 
     34def gdal_full_version():  
     35    "Returns the full GDAL version information." 
     36    return _version_info('') 
     37 
     38def gdal_release_date(date=False):  
     39    """ 
     40    Returns the release date in a string format, e.g, "2007/06/27". 
     41    If the date keyword argument is set to True, a Python datetime object 
     42    will be returned instead. 
     43    """ 
     44    from datetime import datetime 
     45    rel = _version_info('RELEASE_DATE') 
     46    yy, mm, dd = map(int, (rel[0:4], rel[4:6], rel[6:8])) 
     47    d = datetime(yy, mm, dd) 
     48    if date: return d 
     49    else: return d.strftime('%Y/%m/%d') 
  • django/branches/gis/django/contrib/gis/gdal/srs.py

    r6574 r6639  
    8383        "Creates a spatial reference object from the given OGC Well Known Text (WKT)." 
    8484 
    85         self._srs = 0 # Initially NULL 
     85        self._srs = None # Initially NULL 
    8686 
    8787        # Creating an initial empty string buffer.