mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
gis: made sure to reset the Layer before iteration; LayerMapping now supports specifying a source SRS.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5613 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f34300e57a
commit
5f7c6f758b
@ -41,19 +41,25 @@ class Layer(object):
|
|||||||
end = self.num_feat
|
end = self.num_feat
|
||||||
if not isinstance(index, (slice, int)):
|
if not isinstance(index, (slice, int)):
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
|
||||||
if isinstance(index,int):
|
if isinstance(index,int):
|
||||||
|
# An integer index was given
|
||||||
if index < 0:
|
if index < 0:
|
||||||
index = end - index
|
index = end - index
|
||||||
if index < 0 or index >= self.num_feat:
|
if index < 0 or index >= self.num_feat:
|
||||||
raise IndexError, 'index out of range'
|
raise IndexError, 'index out of range'
|
||||||
yield make_feature(index)
|
yield make_feature(index)
|
||||||
else: #isinstance(index,slice)
|
else:
|
||||||
|
# A slice was given
|
||||||
start, stop, stride = index.indices(end)
|
start, stop, stride = index.indices(end)
|
||||||
for offset in xrange(start,stop,stride):
|
for offset in xrange(start,stop,stride):
|
||||||
yield make_feature(offset)
|
yield make_feature(offset)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"Iterates over each Feature in the Layer."
|
"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))
|
return self.__getitem__(slice(self.num_feat))
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
|
@ -28,7 +28,13 @@ Usage:
|
|||||||
is a geographic then it should correspond to the OGR
|
is a geographic then it should correspond to the OGR
|
||||||
geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'.
|
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.
|
1. You need a GDAL-supported data source, like a shapefile.
|
||||||
|
|
||||||
@ -68,7 +74,6 @@ Usage:
|
|||||||
} # The mapping is a dictionary
|
} # The mapping is a dictionary
|
||||||
>>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping)
|
>>> lm = LayerMapping(TestGeo, 'test_poly.shp', mapping)
|
||||||
>>> lm.save(verbose=True) # Save the layermap, imports the data.
|
>>> lm.save(verbose=True) # Save the layermap, imports the data.
|
||||||
>>> lm.save(verbose=True)
|
|
||||||
Saved: Name: 1
|
Saved: Name: 1
|
||||||
Saved: Name: 2
|
Saved: Name: 2
|
||||||
Saved: Name: 3
|
Saved: Name: 3
|
||||||
@ -187,10 +192,23 @@ def check_layer(layer, fields, mapping):
|
|||||||
for feat in layer:
|
for feat in layer:
|
||||||
check_feature(feat, fields, mapping)
|
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:
|
class LayerMapping:
|
||||||
"A class that maps OGR Layers to Django Models."
|
"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."
|
"Takes the Django model, the mapping (dictionary), and the SHP file."
|
||||||
|
|
||||||
# Getting the field names and types from the model
|
# Getting the field names and types from the model
|
||||||
@ -208,6 +226,7 @@ class LayerMapping:
|
|||||||
self.fields = fields
|
self.fields = fields
|
||||||
self.mapping = mapping
|
self.mapping = mapping
|
||||||
self.model = model
|
self.model = model
|
||||||
|
self.source_srs = check_srs(self.layer, source_srs)
|
||||||
|
|
||||||
def save(self, verbose=False):
|
def save(self, verbose=False):
|
||||||
"Runs the layer mapping on the given SHP file, and saves to the database."
|
"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)
|
# Getting the coordinate system needed for transformation (with CoordTransform)
|
||||||
try:
|
try:
|
||||||
source_srs = self.layer.srs
|
# Getting the target spatial reference system
|
||||||
target_srs = SpatialRefSys.objects.get(srid=geo_col.srid).srs
|
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.'
|
raise Exception, 'Could not translate between the data source and model geometry.'
|
||||||
|
|
||||||
for feat in self.layer:
|
for feat in self.layer:
|
||||||
|
@ -1 +1,2 @@
|
|||||||
from LayerMapping import LayerMapping
|
from LayerMapping import LayerMapping
|
||||||
|
from inspect_data import sample
|
||||||
|
Loading…
x
Reference in New Issue
Block a user