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$ |
34 |
shapelib.SHPT_ARC: SHAPETYPE_ARC, |
shapelib.SHPT_ARC: SHAPETYPE_ARC, |
35 |
shapelib.SHPT_POINT: SHAPETYPE_POINT} |
shapelib.SHPT_POINT: SHAPETYPE_POINT} |
36 |
|
|
37 |
|
# |
38 |
|
# Raw shape data formats |
39 |
|
# |
40 |
|
|
41 |
class Shape: |
# Raw data is the same as that returned by the points method. |
42 |
|
RAW_PYTHON = "RAW_PYTHON" |
43 |
|
|
44 |
"""Represent one shape""" |
# Raw data is a shapefile. The Shape object will use the shapeid as the |
45 |
|
# raw data. |
46 |
|
RAW_SHAPEFILE = "RAW_SHAPEFILE" |
47 |
|
|
48 |
def __init__(self, points): |
# Raw data in well-known text format |
49 |
self.points = points |
RAW_WKT = "RAW_WKT" |
|
#self.compute_bbox() |
|
|
self.bbox = None |
|
50 |
|
|
|
def compute_bbox(self): |
|
|
if self.bbox is not None: |
|
|
return self.bbox |
|
51 |
|
|
52 |
|
class ShapefileShape: |
53 |
|
|
54 |
|
"""Represent one shape of a shapefile""" |
55 |
|
|
56 |
|
def __init__(self, shapefile, shapeid): |
57 |
|
self.shapefile = shapefile |
58 |
|
self.shapeid = shapeid |
59 |
|
|
60 |
|
def compute_bbox(self): |
61 |
|
""" |
62 |
|
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy) |
63 |
|
""" |
64 |
xs = [] |
xs = [] |
65 |
ys = [] |
ys = [] |
66 |
for x, y in self.points: |
for part in self.Points(): |
67 |
xs.append(x) |
for x, y in part: |
68 |
ys.append(y) |
xs.append(x) |
69 |
self.llx = min(xs) |
ys.append(y) |
70 |
self.lly = min(ys) |
return (min(xs), min(ys), max(xs), max(ys)) |
|
self.urx = max(xs) |
|
|
self.ury = max(ys) |
|
71 |
|
|
72 |
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
def ShapeID(self): |
73 |
|
return self.shapeid |
|
return self.bbox |
|
74 |
|
|
75 |
def Points(self): |
def Points(self): |
76 |
return self.points |
"""Return the coordinates of the shape as a list of lists of pairs""" |
77 |
|
shape = self.shapefile.read_object(self.shapeid) |
78 |
|
points = shape.vertices() |
79 |
|
if self.shapefile.info()[1] == shapelib.SHPT_POINT: |
80 |
|
points = [points] |
81 |
|
return points |
82 |
|
|
83 |
|
def RawData(self): |
84 |
|
"""Return the shape id to use with the shapefile""" |
85 |
|
return self.shapeid |
86 |
|
|
87 |
|
def Shapefile(self): |
88 |
|
"""Return the shapefile object""" |
89 |
|
return self.shapefile |
90 |
|
|
91 |
|
|
92 |
class ShapeTable(transientdb.AutoTransientTable): |
class ShapeTable(transientdb.AutoTransientTable): |
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] |
171 |
""" |
""" |
172 |
return self.shapetype |
return self.shapetype |
173 |
|
|
174 |
|
def RawShapeFormat(self): |
175 |
|
"""Return the raw data format of the shape data, i.e. RAW_SHAPEFILE""" |
176 |
|
return RAW_SHAPEFILE |
177 |
|
|
178 |
def NumShapes(self): |
def NumShapes(self): |
179 |
"""Return the number of shapes in the shape store""" |
"""Return the number of shapes in the shape store""" |
180 |
return self.numshapes |
return self.numshapes |
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""" |
226 |
shape = self.shapefile.read_object(index) |
return ShapefileShape(self.shapefile, index) |
|
|
|
|
if self.ShapeType() == SHAPETYPE_POINT: |
|
|
points = shape.vertices() |
|
|
else: |
|
|
#for poly in shape.vertices(): |
|
|
poly = shape.vertices()[0] |
|
|
points = [] |
|
|
for x, y in poly: |
|
|
points.append((x, y)) |
|
|
|
|
|
return Shape(points) |
|
227 |
|
|
228 |
|
|
229 |
|
|
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() |