Changeset 5613
- Timestamp:
- 07/04/07 10:27:01 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/gdal/Layer.py
r5606 r5613 42 42 if not isinstance(index, (slice, int)): 43 43 raise TypeError 44 44 45 if isinstance(index,int): 46 # An integer index was given 45 47 if index < 0: 46 48 index = end - index … … 48 50 raise IndexError, 'index out of range' 49 51 yield make_feature(index) 50 else: #isinstance(index,slice) 52 else: 53 # A slice was given 51 54 start, stop, stride = index.indices(end) 52 55 for offset in xrange(start,stop,stride): … … 55 58 def __iter__(self): 56 59 "Iterates over each Feature in the Layer." 60 # Resetting the Layer before beginning iteration 61 lgdal.OGR_L_ResetReading(self._layer) 62 57 63 return self.__getitem__(slice(self.num_feat)) 58 64 django/branches/gis/django/contrib/gis/utils/__init__.py
r5529 r5613 1 1 from LayerMapping import LayerMapping 2 from inspect_data import sample django/branches/gis/django/contrib/gis/utils/LayerMapping.py
r5586 r5613 29 29 geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'. 30 30 31 Example: 31 Keyword 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 37 Example: 32 38 33 39 1. You need a GDAL-supported data source, like a shapefile. … … 69 75 >>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping) 70 76 >>> lm.save(verbose=True) # Save the layermap, imports the data. 71 >>> lm.save(verbose=True)72 77 Saved: Name: 1 73 78 Saved: Name: 2 … … 188 193 check_feature(feat, fields, mapping) 189 194 195 def 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 190 208 class LayerMapping: 191 209 "A class that maps OGR Layers to Django Models." 192 210 193 def __init__(self, model, ogr_file, mapping, layer=0 ):211 def __init__(self, model, ogr_file, mapping, layer=0, source_srs=None): 194 212 "Takes the Django model, the mapping (dictionary), and the SHP file." 195 213 … … 209 227 self.mapping = mapping 210 228 self.model = model 229 self.source_srs = check_srs(self.layer, source_srs) 211 230 212 231 def save(self, verbose=False): … … 221 240 # Getting the coordinate system needed for transformation (with CoordTransform) 222 241 try: 223 source_srs = self.layer.srs242 # Getting the target spatial reference system 224 243 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: 227 248 raise Exception, 'Could not translate between the data source and model geometry.' 228 249
