7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
|
import os |
11 |
from math import log, ceil |
from math import log, ceil |
12 |
|
|
13 |
|
from Thuban import _ |
14 |
|
|
15 |
import shapelib, shptree |
import shapelib, shptree |
16 |
|
|
17 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_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) |
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 |
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) |
174 |
maxdepth) |
maxdepth) |
175 |
|
|
176 |
def Destroy(self): |
def Destroy(self): |
177 |
BaseLayer.Destroy() |
BaseLayer.Destroy(self) |
178 |
if self.shapefile is not None: |
if self.shapefile is not None: |
179 |
self.shapefile.close() |
self.shapefile.close() |
180 |
self.shapefile = None |
self.shapefile = None |
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: |
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): |
def ShapesInRegion(self, box): |
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) |
self.changed(LAYER_LEGEND_CHANGED, self) |
265 |
|
|
266 |
def SetStrokeWidth(self, width): |
def SetStrokeWidth(self, width): |
267 |
"""Set the layer's stroke width.""" |
"""Set the layer's stroke width.""" |
268 |
self.stroke_width = width |
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): |
def TreeInfo(self): |
274 |
items = [] |
items = [] |
275 |
|
|
276 |
if self.Visible(): |
if self.Visible(): |
277 |
items.append("Shown") |
items.append(_("Shown")) |
278 |
else: |
else: |
279 |
items.append("Hidden") |
items.append(_("Hidden")) |
280 |
items.append("Shapes: %d" % self.NumShapes()) |
items.append(_("Shapes: %d") % self.NumShapes()) |
281 |
|
|
282 |
bbox = self.LatLongBoundingBox() |
bbox = self.LatLongBoundingBox() |
283 |
if bbox is not None: |
if bbox is not None: |
284 |
items.append("Extent (lat-lon): (%g, %g, %g, %g)" % bbox) |
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
285 |
else: |
else: |
286 |
items.append("Extent (lat-lon):") |
items.append(_("Extent (lat-lon):")) |
287 |
items.append("Shapetype: %s" % shapetype_names[self.ShapeType()]) |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
288 |
|
|
289 |
def color_string(color): |
def color_string(color): |
290 |
if color is None: |
if color is None: |
291 |
return "None" |
return "None" |
292 |
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
|
items.append("Fill: " + color_string(self.fill)) |
|
|
items.append("Outline: " + color_string(self.stroke)) |
|
293 |
|
|
294 |
return ("Layer '%s'" % self.Title(), items) |
# 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 |
|
|