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 |
|
|
|
def __init__(self, points): |
|
|
self.points = points |
|
|
#self.compute_bbox() |
|
|
self.bbox = None |
|
48 |
|
|
49 |
def compute_bbox(self): |
class ShapefileShape: |
50 |
if self.bbox is not None: |
|
51 |
return self.bbox |
"""Represent one shape of a shapefile""" |
52 |
|
|
53 |
|
def __init__(self, shapefile, shapeid): |
54 |
|
self.shapefile = shapefile |
55 |
|
self.shapeid = shapeid |
56 |
|
|
57 |
|
def compute_bbox(self): |
58 |
|
""" |
59 |
|
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy) |
60 |
|
""" |
61 |
xs = [] |
xs = [] |
62 |
ys = [] |
ys = [] |
63 |
for part in self.points: |
for part in self.Points(): |
64 |
for x, y in part: |
for x, y in part: |
65 |
xs.append(x) |
xs.append(x) |
66 |
ys.append(y) |
ys.append(y) |
67 |
self.llx = min(xs) |
return (min(xs), min(ys), max(xs), max(ys)) |
|
self.lly = min(ys) |
|
|
self.urx = max(xs) |
|
|
self.ury = max(ys) |
|
68 |
|
|
69 |
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
def ShapeID(self): |
70 |
|
return self.shapeid |
|
return self.bbox |
|
71 |
|
|
72 |
def Points(self): |
def Points(self): |
73 |
return self.points |
"""Return the coordinates of the shape as a list of lists of pairs""" |
74 |
|
shape = self.shapefile.read_object(self.shapeid) |
75 |
|
points = shape.vertices() |
76 |
|
if self.shapefile.info()[1] == shapelib.SHPT_POINT: |
77 |
|
points = [points] |
78 |
|
return points |
79 |
|
|
80 |
|
def RawData(self): |
81 |
|
"""Return the shape id to use with the shapefile""" |
82 |
|
return self.shapeid |
83 |
|
|
84 |
|
def Shapefile(self): |
85 |
|
"""Return the shapefile object""" |
86 |
|
return self.shapefile |
87 |
|
|
88 |
|
|
89 |
class ShapeTable(transientdb.AutoTransientTable): |
class ShapeTable(transientdb.AutoTransientTable): |
166 |
""" |
""" |
167 |
return self.shapetype |
return self.shapetype |
168 |
|
|
169 |
|
def RawShapeFormat(self): |
170 |
|
"""Return the raw data format of the shape data, i.e. RAW_SHAPEFILE""" |
171 |
|
return RAW_SHAPEFILE |
172 |
|
|
173 |
def NumShapes(self): |
def NumShapes(self): |
174 |
"""Return the number of shapes in the shape store""" |
"""Return the number of shapes in the shape store""" |
175 |
return self.numshapes |
return self.numshapes |
196 |
""" |
""" |
197 |
return self.bbox |
return self.bbox |
198 |
|
|
199 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, bbox): |
200 |
"""Return the ids of the shapes that overlap the box. |
"""Return an iterable over the shapes that overlap the bounding box. |
201 |
|
|
202 |
Box is a tuple (left, bottom, right, top) in the coordinate |
The bbox parameter should be the bounding box as a tuple in the |
203 |
system used used in the shapefile. |
form (minx, miny, maxx, maxy) in the coordinate system of the |
204 |
""" |
shape store. |
205 |
left, bottom, right, top = box |
""" |
206 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
left, bottom, right, top = bbox |
207 |
|
for i in self.shapetree.find_shapes((left, bottom), (right, top)): |
208 |
|
yield ShapefileShape(self.shapefile, i) |
209 |
|
|
210 |
|
def AllShapes(self): |
211 |
|
"""Return an iterable over the shapes in the shape store.""" |
212 |
|
for i in xrange(self.NumShapes()): |
213 |
|
yield ShapefileShape(self.shapefile, i) |
214 |
|
|
215 |
def Shape(self, index): |
def Shape(self, index): |
216 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
217 |
shape = self.shapefile.read_object(index) |
return ShapefileShape(self.shapefile, index) |
|
|
|
|
if self.ShapeType() == SHAPETYPE_POINT: |
|
|
points = [shape.vertices()] |
|
|
else: |
|
|
points = [] |
|
|
for poly in shape.vertices(): |
|
|
part = [] |
|
|
for x, y in poly: |
|
|
part.append((x, y)) |
|
|
points.append(part) |
|
|
|
|
|
return Shape(points) |
|
218 |
|
|
219 |
|
|
220 |
|
|
269 |
""" |
""" |
270 |
return self.shapestore.ShapesInRegion(bbox) |
return self.shapestore.ShapesInRegion(bbox) |
271 |
|
|
272 |
|
def AllShapes(self): |
273 |
|
"""Return an iterable over the shapes in the shape store. |
274 |
|
|
275 |
|
This method is simply delegated to the shapestore the |
276 |
|
DerivedShapeStore was instantiated with. |
277 |
|
""" |
278 |
|
return self.shapestore.AllShapes() |
279 |
|
|
280 |
def ShapeType(self): |
def ShapeType(self): |
281 |
"""Return the type of the shapes in the layer. |
"""Return the type of the shapes in the layer. |
282 |
|
|
285 |
""" |
""" |
286 |
return self.shapestore.ShapeType() |
return self.shapestore.ShapeType() |
287 |
|
|
288 |
|
def RawShapeFormat(self): |
289 |
|
"""Return the raw data format of the shapes. |
290 |
|
|
291 |
|
This method is simply delegated to the shapestore the |
292 |
|
DerivedShapeStore was instantiated with. |
293 |
|
""" |
294 |
|
return self.shapestore.RawShapeFormat() |
295 |
|
|
296 |
def NumShapes(self): |
def NumShapes(self): |
297 |
"""Return the number of shapes in the shapestore.""" |
"""Return the number of shapes in the shapestore.""" |
298 |
return self.shapestore.NumShapes() |
return self.shapestore.NumShapes() |