Django

Code

Changeset 5613

Show
Ignore:
Timestamp:
07/04/07 10:27:01 (1 year ago)
Author:
jbronn
Message:

gis: made sure to reset the Layer before iteration; LayerMapping? now supports specifying a source SRS.

Files:

Legend:

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

    r5606 r5613  
    4242        if not isinstance(index, (slice, int)): 
    4343            raise TypeError 
     44        
    4445        if isinstance(index,int): 
     46            # An integer index was given 
    4547            if index < 0: 
    4648                index = end - index 
     
    4850                raise IndexError, 'index out of range' 
    4951            yield make_feature(index) 
    50         else: #isinstance(index,slice) 
     52        else:  
     53            # A slice was given 
    5154            start, stop, stride = index.indices(end) 
    5255            for offset in xrange(start,stop,stride): 
     
    5558    def __iter__(self): 
    5659        "Iterates over each Feature in the Layer." 
     60        # Resetting the Layer before beginning iteration 
     61        lgdal.OGR_L_ResetReading(self._layer) 
     62 
    5763        return self.__getitem__(slice(self.num_feat)) 
    5864 
  • django/branches/gis/django/contrib/gis/utils/__init__.py

    r5529 r5613  
    11from LayerMapping import LayerMapping 
     2from inspect_data import sample 
  • django/branches/gis/django/contrib/gis/utils/LayerMapping.py

    r5586 r5613  
    2929             geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'. 
    3030 
    31  Example: 
     31Keyword Args: 
     32  layer -- The index of the layer to use from the Data Source (defaults to 0) 
     33 
     34  source_srs -- Use this to specify the source SRS manually (for example,  
     35                 some shapefiles don't come with a '.prj' file) 
     36 
     37Example: 
    3238 
    3339 1. You need a GDAL-supported data source, like a shapefile. 
     
    6975  >>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping)  
    7076  >>> lm.save(verbose=True) # Save the layermap, imports the data.  
    71   >>> lm.save(verbose=True) 
    7277  Saved: Name: 1 
    7378  Saved: Name: 2 
     
    188193        check_feature(feat, fields, mapping) 
    189194 
     195def check_srs(layer, source_srs): 
     196    "Checks the compatibility of the given spatial reference object." 
     197    if isinstance(source_srs, SpatialReference): 
     198        sr = source_srs 
     199    elif isinstance(source_srs, SpatialRefSys): 
     200        sr = source_srs.srs 
     201    else: 
     202        sr = layer.srs 
     203    if not sr: 
     204        raise Exception, 'No source reference system defined.' 
     205    else: 
     206        return sr 
     207 
    190208class LayerMapping: 
    191209    "A class that maps OGR Layers to Django Models." 
    192210 
    193     def __init__(self, model, ogr_file, mapping, layer=0): 
     211    def __init__(self, model, ogr_file, mapping, layer=0, source_srs=None): 
    194212        "Takes the Django model, the mapping (dictionary), and the SHP file." 
    195213 
     
    209227        self.mapping = mapping 
    210228        self.model = model 
     229        self.source_srs = check_srs(self.layer, source_srs) 
    211230         
    212231    def save(self, verbose=False): 
     
    221240        # Getting the coordinate system needed for transformation (with CoordTransform)   
    222241        try: 
    223             source_srs = self.layer.srs 
     242            # Getting the target spatial reference system 
    224243            target_srs = SpatialRefSys.objects.get(srid=geo_col.srid).srs 
    225             ct = CoordTransform(source_srs, target_srs) 
    226         except: 
     244 
     245            # Creating the CoordTransform object 
     246            ct = CoordTransform(self.source_srs, target_srs) 
     247        except Exception, msg: 
    227248            raise Exception, 'Could not translate between the data source and model geometry.' 
    228249