7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
import shapelib |
from math import log, ceil |
11 |
|
|
12 |
|
import shapelib, shptree |
13 |
|
|
14 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
15 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED |
125 |
self.stroke = stroke |
self.stroke = stroke |
126 |
self.stroke_width = stroke_width |
self.stroke_width = stroke_width |
127 |
self.shapefile = None |
self.shapefile = None |
128 |
|
self.shapetree = None |
129 |
self.open_shapefile() |
self.open_shapefile() |
130 |
# shapetable is the table associated with the shapefile, while |
# shapetable is the table associated with the shapefile, while |
131 |
# table is the default table used to look up attributes for |
# table is the default table used to look up attributes for |
139 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
140 |
self.numshapes = numshapes |
self.numshapes = numshapes |
141 |
self.shapetype = shapelib_shapetypes[shapetype] |
self.shapetype = shapelib_shapetypes[shapetype] |
142 |
self.bbox = mins[:2] + maxs[:2] |
|
143 |
|
# if there are shapes, set the bbox accordinly. Otherwise |
144 |
|
# set it to None. |
145 |
|
if self.numshapes: |
146 |
|
self.bbox = mins[:2] + maxs[:2] |
147 |
|
else: |
148 |
|
self.bbox = None |
149 |
|
|
150 |
|
# estimate a good depth for the quad tree. Each depth |
151 |
|
# multiplies the number of nodes by four, therefore we |
152 |
|
# basically take the base 4 logarithm of the number of |
153 |
|
# shapes. |
154 |
|
if self.numshapes < 4: |
155 |
|
maxdepth = 1 |
156 |
|
else: |
157 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
158 |
|
|
159 |
|
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
160 |
|
maxdepth) |
161 |
|
|
162 |
def BoundingBox(self): |
def BoundingBox(self): |
163 |
"""Return the bounding box of the layer's shapes in their default |
"""Return the layer's bounding box in the intrinsic coordinate system. |
164 |
coordinate system""" |
|
165 |
|
If the layer has no shapes, return None. |
166 |
|
""" |
167 |
|
# The bbox will be set by open_shapefile just as we need it |
168 |
|
# here. |
169 |
self.open_shapefile() |
self.open_shapefile() |
170 |
return self.bbox |
return self.bbox |
171 |
|
|
172 |
def LatLongBoundingBox(self): |
def LatLongBoundingBox(self): |
173 |
"""Return the layer's bounding box in lat/long coordinates""" |
"""Return the layer's bounding box in lat/long coordinates. |
174 |
llx, lly, urx, ury = self.BoundingBox() |
|
175 |
if self.projection is not None: |
Return None, if the layer doesn't contain any shapes. |
176 |
llx, lly = self.projection.Inverse(llx, lly) |
""" |
177 |
urx, ury = self.projection.Inverse(urx, ury) |
bbox = self.BoundingBox() |
178 |
return llx, lly, urx, ury |
if bbox is not None: |
179 |
|
llx, lly, urx, ury = bbox |
180 |
|
if self.projection is not None: |
181 |
|
llx, lly = self.projection.Inverse(llx, lly) |
182 |
|
urx, ury = self.projection.Inverse(urx, ury) |
183 |
|
return llx, lly, urx, ury |
184 |
|
else: |
185 |
|
return None |
186 |
|
|
187 |
def NumShapes(self): |
def NumShapes(self): |
188 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
207 |
poly = shape.vertices()[0] |
poly = shape.vertices()[0] |
208 |
points = [] |
points = [] |
209 |
for x, y in poly: |
for x, y in poly: |
210 |
points.append(x, y) |
points.append((x, y)) |
211 |
return Shape(points) |
return Shape(points) |
212 |
|
|
213 |
|
def ShapesInRegion(self, box): |
214 |
|
"""Return the ids of the shapes that overlap the box. |
215 |
|
|
216 |
|
Box is a tuple (left, bottom, right, top) in the coordinate |
217 |
|
system used by the layer's shapefile. |
218 |
|
""" |
219 |
|
left, bottom, right, top = box |
220 |
|
return self.shapetree.find_shapes((left, bottom), (right, top)) |
221 |
|
|
222 |
def SetProjection(self, projection): |
def SetProjection(self, projection): |
223 |
"""Set the layer's projection""" |
"""Set the layer's projection""" |
224 |
self.projection = projection |
self.projection = projection |
239 |
"""Set the layer's stroke width.""" |
"""Set the layer's stroke width.""" |
240 |
self.stroke_width = width |
self.stroke_width = width |
241 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_LEGEND_CHANGED, self) |
242 |
|
|
243 |
|
def TreeInfo(self): |
244 |
|
items = [] |
245 |
|
|
246 |
|
if self.Visible(): |
247 |
|
items.append("Shown") |
248 |
|
else: |
249 |
|
items.append("Hidden") |
250 |
|
items.append("Shapes: %d" % self.NumShapes()) |
251 |
|
|
252 |
|
bbox = self.LatLongBoundingBox() |
253 |
|
if bbox is not None: |
254 |
|
items.append("Extent (lat-lon): (%g, %g, %g, %g)" % bbox) |
255 |
|
else: |
256 |
|
items.append("Extent (lat-lon):") |
257 |
|
items.append("Shapetype: %s" % shapetype_names[self.ShapeType()]) |
258 |
|
|
259 |
|
def color_string(color): |
260 |
|
if color is None: |
261 |
|
return "None" |
262 |
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
263 |
|
items.append("Fill: " + color_string(self.fill)) |
264 |
|
items.append("Outline: " + color_string(self.stroke)) |
265 |
|
|
266 |
|
return ("Layer '%s'" % self.Title(), items) |