1 |
# Copyright (c) 2001 by Intevation GmbH |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
import shapelib |
import os |
11 |
|
from math import log, ceil |
12 |
|
|
13 |
|
from Thuban import _ |
14 |
|
|
15 |
|
import shapelib, shptree |
16 |
|
|
17 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
18 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED |
21 |
# Some predefined colors for internal use |
# Some predefined colors for internal use |
22 |
_black = Color(0, 0, 0) |
_black = Color(0, 0, 0) |
23 |
|
|
24 |
|
from classification import Classification |
25 |
|
|
26 |
from table import Table |
from table import Table |
27 |
|
|
87 |
"""Set the layer's visibility.""" |
"""Set the layer's visibility.""" |
88 |
self.visible = visible |
self.visible = visible |
89 |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
90 |
|
|
91 |
|
|
92 |
class Layer(BaseLayer): |
class Layer(BaseLayer): |
93 |
|
|
94 |
"""Represent the information of one geodata file (currently a shapefile) |
"""Represent the information of one geodata file (currently a shapefile) |
108 |
""" |
""" |
109 |
|
|
110 |
def __init__(self, title, filename, projection = None, |
def __init__(self, title, filename, projection = None, |
111 |
fill = None, stroke = _black, visible = 1): |
fill = None, stroke = _black, stroke_width = 1, visible = 1): |
112 |
"""Initialize the layer. |
"""Initialize the layer. |
113 |
|
|
114 |
title -- the title |
title -- the title |
123 |
colors are expected to be instances of Color class |
colors are expected to be instances of Color class |
124 |
""" |
""" |
125 |
BaseLayer.__init__(self, title, visible = visible) |
BaseLayer.__init__(self, title, visible = visible) |
126 |
self.filename = filename |
|
127 |
|
# Make the filename absolute. The filename will be |
128 |
|
# interpreted relative to that anyway, but when saving a |
129 |
|
# session we need to compare absolute paths and it's usually |
130 |
|
# safer to always work with absolute paths. |
131 |
|
self.filename = os.path.abspath(filename) |
132 |
|
|
133 |
self.projection = projection |
self.projection = projection |
134 |
self.fill = fill |
self.fill = fill |
135 |
self.stroke = stroke |
self.stroke = stroke |
136 |
|
self.stroke_width = stroke_width |
137 |
self.shapefile = None |
self.shapefile = None |
138 |
|
self.shapetree = None |
139 |
self.open_shapefile() |
self.open_shapefile() |
140 |
# shapetable is the table associated with the shapefile, while |
# shapetable is the table associated with the shapefile, while |
141 |
# table is the default table used to look up attributes for |
# table is the default table used to look up attributes for |
143 |
self.shapetable = Table(filename) |
self.shapetable = Table(filename) |
144 |
self.table = self.shapetable |
self.table = self.shapetable |
145 |
|
|
146 |
|
self.classification = Classification() |
147 |
|
self.classification.setNull( |
148 |
|
{'stroke':stroke, 'stroke_width':stroke_width, 'fill':fill}) |
149 |
|
|
150 |
def open_shapefile(self): |
def open_shapefile(self): |
151 |
if self.shapefile is None: |
if self.shapefile is None: |
152 |
self.shapefile = shapelib.ShapeFile(self.filename) |
self.shapefile = shapelib.ShapeFile(self.filename) |
153 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
154 |
self.numshapes = numshapes |
self.numshapes = numshapes |
155 |
self.shapetype = shapelib_shapetypes[shapetype] |
self.shapetype = shapelib_shapetypes[shapetype] |
156 |
self.bbox = mins[:2] + maxs[:2] |
|
157 |
|
# if there are shapes, set the bbox accordinly. Otherwise |
158 |
|
# set it to None. |
159 |
|
if self.numshapes: |
160 |
|
self.bbox = mins[:2] + maxs[:2] |
161 |
|
else: |
162 |
|
self.bbox = None |
163 |
|
|
164 |
|
# estimate a good depth for the quad tree. Each depth |
165 |
|
# multiplies the number of nodes by four, therefore we |
166 |
|
# basically take the base 4 logarithm of the number of |
167 |
|
# shapes. |
168 |
|
if self.numshapes < 4: |
169 |
|
maxdepth = 1 |
170 |
|
else: |
171 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
172 |
|
|
173 |
|
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
174 |
|
maxdepth) |
175 |
|
|
176 |
|
def Destroy(self): |
177 |
|
BaseLayer.Destroy(self) |
178 |
|
if self.shapefile is not None: |
179 |
|
self.shapefile.close() |
180 |
|
self.shapefile = None |
181 |
|
self.shapetree = None |
182 |
|
self.table.Destroy() |
183 |
|
|
184 |
def BoundingBox(self): |
def BoundingBox(self): |
185 |
"""Return the bounding box of the layer's shapes in their default |
"""Return the layer's bounding box in the intrinsic coordinate system. |
186 |
coordinate system""" |
|
187 |
|
If the layer has no shapes, return None. |
188 |
|
""" |
189 |
|
# The bbox will be set by open_shapefile just as we need it |
190 |
|
# here. |
191 |
self.open_shapefile() |
self.open_shapefile() |
192 |
return self.bbox |
return self.bbox |
193 |
|
|
194 |
def LatLongBoundingBox(self): |
def LatLongBoundingBox(self): |
195 |
"""Return the layer's bounding box in lat/long coordinates""" |
"""Return the layer's bounding box in lat/long coordinates. |
196 |
llx, lly, urx, ury = self.BoundingBox() |
|
197 |
if self.projection is not None: |
Return None, if the layer doesn't contain any shapes. |
198 |
llx, lly = self.projection.Inverse(llx, lly) |
""" |
199 |
urx, ury = self.projection.Inverse(urx, ury) |
bbox = self.BoundingBox() |
200 |
return llx, lly, urx, ury |
if bbox is not None: |
201 |
|
llx, lly, urx, ury = bbox |
202 |
|
if self.projection is not None: |
203 |
|
llx, lly = self.projection.Inverse(llx, lly) |
204 |
|
urx, ury = self.projection.Inverse(urx, ury) |
205 |
|
return llx, lly, urx, ury |
206 |
|
else: |
207 |
|
return None |
208 |
|
|
209 |
def NumShapes(self): |
def NumShapes(self): |
210 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
222 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
223 |
self.open_shapefile() |
self.open_shapefile() |
224 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
225 |
|
|
226 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
227 |
points = shape.vertices() |
points = shape.vertices() |
228 |
else: |
else: |
230 |
poly = shape.vertices()[0] |
poly = shape.vertices()[0] |
231 |
points = [] |
points = [] |
232 |
for x, y in poly: |
for x, y in poly: |
233 |
points.append(x, y) |
points.append((x, y)) |
234 |
|
|
235 |
return Shape(points) |
return Shape(points) |
236 |
|
|
237 |
|
def ShapesInRegion(self, box): |
238 |
|
"""Return the ids of the shapes that overlap the box. |
239 |
|
|
240 |
|
Box is a tuple (left, bottom, right, top) in the coordinate |
241 |
|
system used by the layer's shapefile. |
242 |
|
""" |
243 |
|
left, bottom, right, top = box |
244 |
|
return self.shapetree.find_shapes((left, bottom), (right, top)) |
245 |
|
|
246 |
def SetProjection(self, projection): |
def SetProjection(self, projection): |
247 |
"""Set the layer's projection""" |
"""Set the layer's projection""" |
248 |
self.projection = projection |
self.projection = projection |
250 |
|
|
251 |
def SetFill(self, fill): |
def SetFill(self, fill): |
252 |
"""Set the layer's fill color. None means the shapes are not filled""" |
"""Set the layer's fill color. None means the shapes are not filled""" |
253 |
self.fill = fill |
null = self.classification.getNull() |
254 |
|
null['fill'] = fill |
255 |
|
self.classification.setNull(null) |
256 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_LEGEND_CHANGED, self) |
257 |
|
|
258 |
def SetStroke(self, stroke): |
def SetStroke(self, stroke): |
259 |
"""Set the layer's stroke color. None means the shapes are not |
"""Set the layer's stroke color. None means the shapes are not |
260 |
stroked.""" |
stroked.""" |
261 |
self.stroke = stroke |
null = self.classification.getNull() |
262 |
|
null['stroke'] = stroke |
263 |
|
self.classification.setNull(null) |
264 |
|
self.changed(LAYER_LEGEND_CHANGED, self) |
265 |
|
|
266 |
|
def SetStrokeWidth(self, width): |
267 |
|
"""Set the layer's stroke width.""" |
268 |
|
null = self.classification.getNull() |
269 |
|
null['stroke_width'] = width |
270 |
|
self.classification.setNull(null) |
271 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_LEGEND_CHANGED, self) |
272 |
|
|
273 |
|
def TreeInfo(self): |
274 |
|
items = [] |
275 |
|
|
276 |
|
if self.Visible(): |
277 |
|
items.append(_("Shown")) |
278 |
|
else: |
279 |
|
items.append(_("Hidden")) |
280 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
281 |
|
|
282 |
|
bbox = self.LatLongBoundingBox() |
283 |
|
if bbox is not None: |
284 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
285 |
|
else: |
286 |
|
items.append(_("Extent (lat-lon):")) |
287 |
|
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
288 |
|
|
289 |
|
def color_string(color): |
290 |
|
if color is None: |
291 |
|
return "None" |
292 |
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
293 |
|
|
294 |
|
# layers will always have a classification with at least a NULL data set |
295 |
|
|
296 |
|
#items.append((_("Fill: %s") % color_string(self.fill), self.fill)) |
297 |
|
#items.append((_("Outline: %s") % color_string(self.stroke), self.stroke)) |
298 |
|
|
299 |
|
items.append(self.classification) |
300 |
|
|
301 |
|
return (_("Layer '%s'") % self.Title(), items) |
302 |
|
|