Author: jbronn
Date: 2007-07-04 10:27:01 -0500 (Wed, 04 Jul 2007)
New Revision: 5613

Modified:
   django/branches/gis/django/contrib/gis/gdal/Layer.py
   django/branches/gis/django/contrib/gis/utils/LayerMapping.py
   django/branches/gis/django/contrib/gis/utils/__init__.py
Log:
gis: made sure to reset the Layer before iteration; LayerMapping now supports 
specifying a source SRS.

Modified: django/branches/gis/django/contrib/gis/gdal/Layer.py
===================================================================
--- django/branches/gis/django/contrib/gis/gdal/Layer.py        2007-07-04 
12:48:12 UTC (rev 5612)
+++ django/branches/gis/django/contrib/gis/gdal/Layer.py        2007-07-04 
15:27:01 UTC (rev 5613)
@@ -41,19 +41,25 @@
         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):

Modified: django/branches/gis/django/contrib/gis/utils/LayerMapping.py
===================================================================
--- django/branches/gis/django/contrib/gis/utils/LayerMapping.py        
2007-07-04 12:48:12 UTC (rev 5612)
+++ django/branches/gis/django/contrib/gis/utils/LayerMapping.py        
2007-07-04 15:27:01 UTC (rev 5613)
@@ -28,8 +28,14 @@
              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.
 
   Assume we're using the test_poly SHP file:
@@ -68,7 +74,6 @@
                  } # 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 @@
     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 @@
         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 @@
         
         # 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:

Modified: django/branches/gis/django/contrib/gis/utils/__init__.py
===================================================================
--- django/branches/gis/django/contrib/gis/utils/__init__.py    2007-07-04 
12:48:12 UTC (rev 5612)
+++ django/branches/gis/django/contrib/gis/utils/__init__.py    2007-07-04 
15:27:01 UTC (rev 5613)
@@ -1 +1,2 @@
 from LayerMapping import LayerMapping
+from inspect_data import sample


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to