12 |
# $Id$ |
# $Id$ |
13 |
|
|
14 |
import os |
import os |
|
import warnings |
|
15 |
import weakref |
import weakref |
16 |
|
from math import ceil, log |
17 |
|
|
18 |
import shapelib |
import shapelib |
19 |
|
import shptree |
20 |
import table |
import table |
21 |
import transientdb |
import transientdb |
22 |
|
|
23 |
from Thuban import _ |
from Thuban import _ |
24 |
|
|
25 |
|
# Shape type constants |
26 |
|
SHAPETYPE_POLYGON = "polygon" |
27 |
|
SHAPETYPE_ARC = "arc" |
28 |
|
SHAPETYPE_POINT = "point" |
29 |
|
|
30 |
|
# mapping from shapelib shapetype constants to our constants |
31 |
|
shapelib_shapetypes = {shapelib.SHPT_POLYGON: SHAPETYPE_POLYGON, |
32 |
|
shapelib.SHPT_ARC: SHAPETYPE_ARC, |
33 |
|
shapelib.SHPT_POINT: SHAPETYPE_POINT} |
34 |
|
|
35 |
|
|
36 |
|
class Shape: |
37 |
|
|
38 |
|
"""Represent one shape""" |
39 |
|
|
40 |
|
def __init__(self, points): |
41 |
|
self.points = points |
42 |
|
#self.compute_bbox() |
43 |
|
self.bbox = None |
44 |
|
|
45 |
|
def compute_bbox(self): |
46 |
|
if self.bbox is not None: |
47 |
|
return self.bbox |
48 |
|
|
49 |
|
xs = [] |
50 |
|
ys = [] |
51 |
|
for x, y in self.points: |
52 |
|
xs.append(x) |
53 |
|
ys.append(y) |
54 |
|
self.llx = min(xs) |
55 |
|
self.lly = min(ys) |
56 |
|
self.urx = max(xs) |
57 |
|
self.ury = max(ys) |
58 |
|
|
59 |
|
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
60 |
|
|
61 |
|
return self.bbox |
62 |
|
|
63 |
|
def Points(self): |
64 |
|
return self.points |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
class ShapeTable(transientdb.AutoTransientTable): |
class ShapeTable(transientdb.AutoTransientTable): |
69 |
|
|
104 |
self.dbftable = table.DBFTable(filename) |
self.dbftable = table.DBFTable(filename) |
105 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
106 |
|
|
107 |
|
self.numshapes, shapetype, mins, maxs = self.shapefile.info() |
108 |
|
if self.numshapes: |
109 |
|
self.bbox = mins[:2] + maxs[:2] |
110 |
|
else: |
111 |
|
self.bbox = None |
112 |
|
self.shapetype = shapelib_shapetypes[shapetype] |
113 |
|
|
114 |
|
# estimate a good depth for the quad tree. Each depth multiplies |
115 |
|
# the number of nodes by four, therefore we basically take the |
116 |
|
# base 4 logarithm of the number of shapes. |
117 |
|
if self.numshapes < 4: |
118 |
|
maxdepth = 1 |
119 |
|
else: |
120 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
121 |
|
|
122 |
|
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
123 |
|
maxdepth) |
124 |
|
|
125 |
def Table(self): |
def Table(self): |
126 |
"""Return the table containing the attribute data""" |
"""Return the table containing the attribute data""" |
127 |
return self.table |
return self.table |
138 |
"""Return the filetype. This is always the string 'shapefile'""" |
"""Return the filetype. This is always the string 'shapefile'""" |
139 |
return "shapefile" |
return "shapefile" |
140 |
|
|
141 |
|
def ShapeType(self): |
142 |
|
"""Return the type of the shapes in the shapestore. |
143 |
|
|
144 |
|
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
145 |
|
""" |
146 |
|
return self.shapetype |
147 |
|
|
148 |
|
def NumShapes(self): |
149 |
|
"""Return the number of shapes in the shape store""" |
150 |
|
return self.numshapes |
151 |
|
|
152 |
def Dependencies(self): |
def Dependencies(self): |
153 |
"""Return the empty tuple. |
"""Return the empty tuple. |
154 |
|
|
163 |
""" |
""" |
164 |
return None |
return None |
165 |
|
|
166 |
|
def BoundingBox(self): |
167 |
|
"""Return the bounding box of the shapes in the shapestore. |
168 |
|
|
169 |
|
The coordinate system used is whatever was used in the shapefile. |
170 |
|
If the shapefile is empty, return None. |
171 |
|
""" |
172 |
|
return self.bbox |
173 |
|
|
174 |
|
def ShapesInRegion(self, box): |
175 |
|
"""Return the ids of the shapes that overlap the box. |
176 |
|
|
177 |
|
Box is a tuple (left, bottom, right, top) in the coordinate |
178 |
|
system used used in the shapefile. |
179 |
|
""" |
180 |
|
left, bottom, right, top = box |
181 |
|
return self.shapetree.find_shapes((left, bottom), (right, top)) |
182 |
|
|
183 |
|
def Shape(self, index): |
184 |
|
"""Return the shape with index index""" |
185 |
|
shape = self.shapefile.read_object(index) |
186 |
|
|
187 |
|
if self.ShapeType() == SHAPETYPE_POINT: |
188 |
|
points = shape.vertices() |
189 |
|
else: |
190 |
|
#for poly in shape.vertices(): |
191 |
|
poly = shape.vertices()[0] |
192 |
|
points = [] |
193 |
|
for x, y in poly: |
194 |
|
points.append((x, y)) |
195 |
|
|
196 |
|
return Shape(points) |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
class DerivedShapeStore: |
class DerivedShapeStore: |
201 |
|
|
235 |
Return the original shapestore the derived store was instantiated with |
Return the original shapestore the derived store was instantiated with |
236 |
""" |
""" |
237 |
return self.shapestore |
return self.shapestore |
238 |
|
|
239 |
|
def Shape(self, index): |
240 |
|
"""Return the shape with index index""" |
241 |
|
return self.shapestore.Shape(index) |
242 |
|
|
243 |
|
def ShapesInRegion(self, bbox): |
244 |
|
"""Return the ids of the shapes that overlap the box. |
245 |
|
|
246 |
|
This method is simply delegated to the shapestore the |
247 |
|
DerivedShapeStore was instantiated with. |
248 |
|
""" |
249 |
|
return self.shapestore.ShapesInRegion(bbox) |
250 |
|
|
251 |
|
def ShapeType(self): |
252 |
|
"""Return the type of the shapes in the layer. |
253 |
|
|
254 |
|
This method is simply delegated to the shapestore the |
255 |
|
DerivedShapeStore was instantiated with. |
256 |
|
""" |
257 |
|
return self.shapestore.ShapeType() |
258 |
|
|
259 |
|
def NumShapes(self): |
260 |
|
"""Return the number of shapes in the shapestore.""" |
261 |
|
return self.shapestore.NumShapes() |
262 |
|
|
263 |
|
def BoundingBox(self): |
264 |
|
"""Return the bounding box of the shapes in the shapestore. |
265 |
|
|
266 |
|
This method is simply delegated to the shapestore the |
267 |
|
DerivedShapeStore was instantiated with. |
268 |
|
""" |
269 |
|
return self.shapestore.BoundingBox() |