7 |
|
|
8 |
"""Data source abstractions""" |
"""Data source abstractions""" |
9 |
|
|
10 |
|
from __future__ import generators |
11 |
|
|
12 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
13 |
# $Source$ |
# $Source$ |
14 |
# $Id$ |
# $Id$ |
45 |
# raw data. |
# raw data. |
46 |
RAW_SHAPEFILE = "RAW_SHAPEFILE" |
RAW_SHAPEFILE = "RAW_SHAPEFILE" |
47 |
|
|
48 |
|
# Raw data in well-known text format |
49 |
|
RAW_WKT = "RAW_WKT" |
50 |
|
|
51 |
|
|
52 |
class ShapefileShape: |
class ShapefileShape: |
53 |
|
|
69 |
ys.append(y) |
ys.append(y) |
70 |
return (min(xs), min(ys), max(xs), max(ys)) |
return (min(xs), min(ys), max(xs), max(ys)) |
71 |
|
|
72 |
|
def ShapeID(self): |
73 |
|
return self.shapeid |
74 |
|
|
75 |
def Points(self): |
def Points(self): |
76 |
"""Return the coordinates of the shape as a list of lists of pairs""" |
"""Return the coordinates of the shape as a list of lists of pairs""" |
77 |
shape = self.shapefile.read_object(self.shapeid) |
shape = self.shapefile.read_object(self.shapeid) |
124 |
# safer to always work with absolute paths. |
# safer to always work with absolute paths. |
125 |
self.filename = os.path.abspath(filename) |
self.filename = os.path.abspath(filename) |
126 |
|
|
|
self.shapefile = shapelib.ShapeFile(self.filename) |
|
127 |
self.dbftable = table.DBFTable(filename) |
self.dbftable = table.DBFTable(filename) |
128 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
129 |
|
self._open_shapefile() |
130 |
|
|
131 |
|
def _open_shapefile(self): |
132 |
|
self.shapefile = shapelib.ShapeFile(self.filename) |
133 |
self.numshapes, shapetype, mins, maxs = self.shapefile.info() |
self.numshapes, shapetype, mins, maxs = self.shapefile.info() |
134 |
if self.numshapes: |
if self.numshapes: |
135 |
self.bbox = mins[:2] + maxs[:2] |
self.bbox = mins[:2] + maxs[:2] |
201 |
""" |
""" |
202 |
return self.bbox |
return self.bbox |
203 |
|
|
204 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, bbox): |
205 |
"""Return the ids of the shapes that overlap the box. |
"""Return an iterable over the shapes that overlap the bounding box. |
206 |
|
|
207 |
Box is a tuple (left, bottom, right, top) in the coordinate |
The bbox parameter should be the bounding box as a tuple in the |
208 |
system used used in the shapefile. |
form (minx, miny, maxx, maxy) in the coordinate system of the |
209 |
""" |
shape store. |
210 |
left, bottom, right, top = box |
""" |
211 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
# Bind a few globals to locals to make it a bit faster |
212 |
|
cls = ShapefileShape |
213 |
|
shapefile = self.shapefile |
214 |
|
|
215 |
|
left, bottom, right, top = bbox |
216 |
|
for i in self.shapetree.find_shapes((left, bottom), (right, top)): |
217 |
|
yield cls(shapefile, i) |
218 |
|
|
219 |
|
def AllShapes(self): |
220 |
|
"""Return an iterable over the shapes in the shape store.""" |
221 |
|
for i in xrange(self.NumShapes()): |
222 |
|
yield ShapefileShape(self.shapefile, i) |
223 |
|
|
224 |
def Shape(self, index): |
def Shape(self, index): |
225 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
278 |
""" |
""" |
279 |
return self.shapestore.ShapesInRegion(bbox) |
return self.shapestore.ShapesInRegion(bbox) |
280 |
|
|
281 |
|
def AllShapes(self): |
282 |
|
"""Return an iterable over the shapes in the shape store. |
283 |
|
|
284 |
|
This method is simply delegated to the shapestore the |
285 |
|
DerivedShapeStore was instantiated with. |
286 |
|
""" |
287 |
|
return self.shapestore.AllShapes() |
288 |
|
|
289 |
def ShapeType(self): |
def ShapeType(self): |
290 |
"""Return the type of the shapes in the layer. |
"""Return the type of the shapes in the layer. |
291 |
|
|
294 |
""" |
""" |
295 |
return self.shapestore.ShapeType() |
return self.shapestore.ShapeType() |
296 |
|
|
297 |
|
def RawShapeFormat(self): |
298 |
|
"""Return the raw data format of the shapes. |
299 |
|
|
300 |
|
This method is simply delegated to the shapestore the |
301 |
|
DerivedShapeStore was instantiated with. |
302 |
|
""" |
303 |
|
return self.shapestore.RawShapeFormat() |
304 |
|
|
305 |
def NumShapes(self): |
def NumShapes(self): |
306 |
"""Return the number of shapes in the shapestore.""" |
"""Return the number of shapes in the shapestore.""" |
307 |
return self.shapestore.NumShapes() |
return self.shapestore.NumShapes() |