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 part in self.points: |
52 |
|
for x, y in part: |
53 |
|
xs.append(x) |
54 |
|
ys.append(y) |
55 |
|
self.llx = min(xs) |
56 |
|
self.lly = min(ys) |
57 |
|
self.urx = max(xs) |
58 |
|
self.ury = max(ys) |
59 |
|
|
60 |
|
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
61 |
|
|
62 |
|
return self.bbox |
63 |
|
|
64 |
|
def Points(self): |
65 |
|
return self.points |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
class ShapeTable(transientdb.AutoTransientTable): |
class ShapeTable(transientdb.AutoTransientTable): |
70 |
|
|
105 |
self.dbftable = table.DBFTable(filename) |
self.dbftable = table.DBFTable(filename) |
106 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
107 |
|
|
108 |
|
self.numshapes, shapetype, mins, maxs = self.shapefile.info() |
109 |
|
if self.numshapes: |
110 |
|
self.bbox = mins[:2] + maxs[:2] |
111 |
|
else: |
112 |
|
self.bbox = None |
113 |
|
self.shapetype = shapelib_shapetypes[shapetype] |
114 |
|
|
115 |
|
# estimate a good depth for the quad tree. Each depth multiplies |
116 |
|
# the number of nodes by four, therefore we basically take the |
117 |
|
# base 4 logarithm of the number of shapes. |
118 |
|
if self.numshapes < 4: |
119 |
|
maxdepth = 1 |
120 |
|
else: |
121 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
122 |
|
|
123 |
|
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
124 |
|
maxdepth) |
125 |
|
|
126 |
def Table(self): |
def Table(self): |
127 |
"""Return the table containing the attribute data""" |
"""Return the table containing the attribute data""" |
128 |
return self.table |
return self.table |
139 |
"""Return the filetype. This is always the string 'shapefile'""" |
"""Return the filetype. This is always the string 'shapefile'""" |
140 |
return "shapefile" |
return "shapefile" |
141 |
|
|
142 |
|
def ShapeType(self): |
143 |
|
"""Return the type of the shapes in the shapestore. |
144 |
|
|
145 |
|
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
146 |
|
""" |
147 |
|
return self.shapetype |
148 |
|
|
149 |
|
def NumShapes(self): |
150 |
|
"""Return the number of shapes in the shape store""" |
151 |
|
return self.numshapes |
152 |
|
|
153 |
def Dependencies(self): |
def Dependencies(self): |
154 |
"""Return the empty tuple. |
"""Return the empty tuple. |
155 |
|
|
164 |
""" |
""" |
165 |
return None |
return None |
166 |
|
|
167 |
|
def BoundingBox(self): |
168 |
|
"""Return the bounding box of the shapes in the shapestore. |
169 |
|
|
170 |
|
The coordinate system used is whatever was used in the shapefile. |
171 |
|
If the shapefile is empty, return None. |
172 |
|
""" |
173 |
|
return self.bbox |
174 |
|
|
175 |
|
def ShapesInRegion(self, box): |
176 |
|
"""Return the ids of the shapes that overlap the box. |
177 |
|
|
178 |
|
Box is a tuple (left, bottom, right, top) in the coordinate |
179 |
|
system used used in the shapefile. |
180 |
|
""" |
181 |
|
left, bottom, right, top = box |
182 |
|
return self.shapetree.find_shapes((left, bottom), (right, top)) |
183 |
|
|
184 |
|
def Shape(self, index): |
185 |
|
"""Return the shape with index index""" |
186 |
|
shape = self.shapefile.read_object(index) |
187 |
|
|
188 |
|
if self.ShapeType() == SHAPETYPE_POINT: |
189 |
|
points = [shape.vertices()] |
190 |
|
else: |
191 |
|
points = [] |
192 |
|
for poly in shape.vertices(): |
193 |
|
part = [] |
194 |
|
for x, y in poly: |
195 |
|
part.append((x, y)) |
196 |
|
points.append(part) |
197 |
|
|
198 |
|
return Shape(points) |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
class DerivedShapeStore: |
class DerivedShapeStore: |
203 |
|
|
238 |
""" |
""" |
239 |
return self.shapestore |
return self.shapestore |
240 |
|
|
241 |
|
def Shape(self, index): |
242 |
|
"""Return the shape with index index""" |
243 |
|
return self.shapestore.Shape(index) |
244 |
|
|
245 |
class SimpleStore: |
def ShapesInRegion(self, bbox): |
246 |
|
"""Return the ids of the shapes that overlap the box. |
247 |
|
|
248 |
"""Combine a shapefile and a table object""" |
This method is simply delegated to the shapestore the |
249 |
|
DerivedShapeStore was instantiated with. |
250 |
|
""" |
251 |
|
return self.shapestore.ShapesInRegion(bbox) |
252 |
|
|
253 |
def __init__(self, shapefile, table): |
def ShapeType(self): |
254 |
warnings.warn("The SimpleStore is deprecated." |
"""Return the type of the shapes in the layer. |
|
" Use DerivedShapeStore instead", |
|
|
DeprecationWarning, stacklevel = 2) |
|
|
self.shapefile = shapefile |
|
|
self.table = table |
|
|
self.filename = None |
|
255 |
|
|
256 |
def Table(self): |
This method is simply delegated to the shapestore the |
257 |
return self.table |
DerivedShapeStore was instantiated with. |
258 |
|
""" |
259 |
|
return self.shapestore.ShapeType() |
260 |
|
|
261 |
def Shapefile(self): |
def NumShapes(self): |
262 |
return self.shapefile |
"""Return the number of shapes in the shapestore.""" |
263 |
|
return self.shapestore.NumShapes() |
264 |
|
|
265 |
|
def BoundingBox(self): |
266 |
|
"""Return the bounding box of the shapes in the shapestore. |
267 |
|
|
268 |
|
This method is simply delegated to the shapestore the |
269 |
|
DerivedShapeStore was instantiated with. |
270 |
|
""" |
271 |
|
return self.shapestore.BoundingBox() |