diff --git a/django/contrib/gis/gdal/Layer.py b/django/contrib/gis/gdal/Layer.py index ea787ccc98..6e5ccb201b 100644 --- a/django/contrib/gis/gdal/Layer.py +++ b/django/contrib/gis/gdal/Layer.py @@ -41,19 +41,25 @@ class Layer(object): end = self.num_feat if not isinstance(index, (slice, int)): raise TypeError + if isinstance(index,int): + # An integer index was given if index < 0: index = end - index if index < 0 or index >= self.num_feat: raise IndexError, 'index out of range' yield make_feature(index) - else: #isinstance(index,slice) + else: + # A slice was given start, stop, stride = index.indices(end) for offset in xrange(start,stop,stride): yield make_feature(offset) def __iter__(self): "Iterates over each Feature in the Layer." + # Resetting the Layer before beginning iteration + lgdal.OGR_L_ResetReading(self._layer) + return self.__getitem__(slice(self.num_feat)) def __len__(self): diff --git a/django/contrib/gis/utils/LayerMapping.py b/django/contrib/gis/utils/LayerMapping.py index b768c52a07..0b0fcdd977 100644 --- a/django/contrib/gis/utils/LayerMapping.py +++ b/django/contrib/gis/utils/LayerMapping.py @@ -28,7 +28,13 @@ Usage: is a geographic then it should correspond to the OGR geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'. - Example: +Keyword Args: + layer -- The index of the layer to use from the Data Source (defaults to 0) + + source_srs -- Use this to specify the source SRS manually (for example, + some shapefiles don't come with a '.prj' file) + +Example: 1. You need a GDAL-supported data source, like a shapefile. @@ -68,7 +74,6 @@ Usage: } # The mapping is a dictionary >>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping) >>> lm.save(verbose=True) # Save the layermap, imports the data. - >>> lm.save(verbose=True) Saved: Name: 1 Saved: Name: 2 Saved: Name: 3 @@ -187,10 +192,23 @@ def check_layer(layer, fields, mapping): for feat in layer: check_feature(feat, fields, mapping) +def check_srs(layer, source_srs): + "Checks the compatibility of the given spatial reference object." + if isinstance(source_srs, SpatialReference): + sr = source_srs + elif isinstance(source_srs, SpatialRefSys): + sr = source_srs.srs + else: + sr = layer.srs + if not sr: + raise Exception, 'No source reference system defined.' + else: + return sr + class LayerMapping: "A class that maps OGR Layers to Django Models." - def __init__(self, model, ogr_file, mapping, layer=0): + def __init__(self, model, ogr_file, mapping, layer=0, source_srs=None): "Takes the Django model, the mapping (dictionary), and the SHP file." # Getting the field names and types from the model @@ -208,6 +226,7 @@ class LayerMapping: self.fields = fields self.mapping = mapping self.model = model + self.source_srs = check_srs(self.layer, source_srs) def save(self, verbose=False): "Runs the layer mapping on the given SHP file, and saves to the database." @@ -220,10 +239,12 @@ class LayerMapping: # Getting the coordinate system needed for transformation (with CoordTransform) try: - source_srs = self.layer.srs + # Getting the target spatial reference system target_srs = SpatialRefSys.objects.get(srid=geo_col.srid).srs - ct = CoordTransform(source_srs, target_srs) - except: + + # Creating the CoordTransform object + ct = CoordTransform(self.source_srs, target_srs) + except Exception, msg: raise Exception, 'Could not translate between the data source and model geometry.' for feat in self.layer: diff --git a/django/contrib/gis/utils/__init__.py b/django/contrib/gis/utils/__init__.py index ce1b521332..2c5a3391b3 100644 --- a/django/contrib/gis/utils/__init__.py +++ b/django/contrib/gis/utils/__init__.py @@ -1 +1,2 @@ from LayerMapping import LayerMapping +from inspect_data import sample