1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
|
# Jonathan Coles <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
9 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
10 |
|
|
11 |
|
import os |
12 |
from math import log, ceil |
from math import log, ceil |
13 |
|
|
14 |
|
from Thuban import _ |
15 |
|
|
16 |
import shapelib, shptree |
import shapelib, shptree |
17 |
|
|
18 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
19 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED |
20 |
|
|
21 |
from color import Color |
from color import Color |
|
# Some predefined colors for internal use |
|
|
_black = Color(0, 0, 0) |
|
22 |
|
|
23 |
|
import classification |
24 |
|
|
25 |
from table import Table |
from table import Table |
26 |
|
|
86 |
"""Set the layer's visibility.""" |
"""Set the layer's visibility.""" |
87 |
self.visible = visible |
self.visible = visible |
88 |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
89 |
|
|
90 |
|
|
91 |
class Layer(BaseLayer): |
class Layer(BaseLayer): |
92 |
|
|
93 |
"""Represent the information of one geodata file (currently a shapefile) |
"""Represent the information of one geodata file (currently a shapefile) |
107 |
""" |
""" |
108 |
|
|
109 |
def __init__(self, title, filename, projection = None, |
def __init__(self, title, filename, projection = None, |
110 |
fill = None, stroke = _black, stroke_width = 1, visible = 1): |
fill = Color.None, |
111 |
|
stroke = Color.Black, |
112 |
|
lineWidth = 1, |
113 |
|
visible = 1): |
114 |
"""Initialize the layer. |
"""Initialize the layer. |
115 |
|
|
116 |
title -- the title |
title -- the title |
118 |
projection -- the projection object. Its Inverse method is |
projection -- the projection object. Its Inverse method is |
119 |
assumed to map the layer's coordinates to lat/long |
assumed to map the layer's coordinates to lat/long |
120 |
coordinates |
coordinates |
121 |
fill -- the fill color or None if the shapes are not filled |
fill -- the fill color or Color.None if the shapes are not filled |
122 |
stroke -- the stroke color or None if the shapes are not stroked |
stroke -- the stroke color or Color.None if the shapes are not stroked |
123 |
visible -- boolean. If true the layer is visible. |
visible -- boolean. If true the layer is visible. |
124 |
|
|
125 |
colors are expected to be instances of Color class |
colors are expected to be instances of Color class |
126 |
""" |
""" |
127 |
BaseLayer.__init__(self, title, visible = visible) |
BaseLayer.__init__(self, title, visible = visible) |
128 |
self.filename = filename |
|
129 |
|
# Make the filename absolute. The filename will be |
130 |
|
# interpreted relative to that anyway, but when saving a |
131 |
|
# session we need to compare absolute paths and it's usually |
132 |
|
# safer to always work with absolute paths. |
133 |
|
self.filename = os.path.abspath(filename) |
134 |
|
|
135 |
self.projection = projection |
self.projection = projection |
|
self.fill = fill |
|
|
self.stroke = stroke |
|
|
self.stroke_width = stroke_width |
|
136 |
self.shapefile = None |
self.shapefile = None |
137 |
self.shapetree = None |
self.shapetree = None |
138 |
self.open_shapefile() |
self.open_shapefile() |
142 |
self.shapetable = Table(filename) |
self.shapetable = Table(filename) |
143 |
self.table = self.shapetable |
self.table = self.shapetable |
144 |
|
|
145 |
|
self.__classification = classification.Classification(self) |
146 |
|
self.__classification.SetDefaultLineColor(stroke) |
147 |
|
self.__classification.SetDefaultLineWidth(lineWidth) |
148 |
|
self.__classification.SetDefaultFill(fill) |
149 |
|
|
150 |
|
self.UnsetModified() |
151 |
|
|
152 |
def open_shapefile(self): |
def open_shapefile(self): |
153 |
if self.shapefile is None: |
if self.shapefile is None: |
154 |
self.shapefile = shapelib.ShapeFile(self.filename) |
self.shapefile = shapelib.ShapeFile(self.filename) |
208 |
else: |
else: |
209 |
return None |
return None |
210 |
|
|
211 |
|
def GetFieldType(self, fieldName): |
212 |
|
self.open_shapefile() |
213 |
|
info = self.table.field_info_by_name(fieldName) |
214 |
|
if info is not None: |
215 |
|
return info[0] |
216 |
|
else: |
217 |
|
return None |
218 |
|
|
219 |
def NumShapes(self): |
def NumShapes(self): |
220 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
221 |
self.open_shapefile() |
self.open_shapefile() |
232 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
233 |
self.open_shapefile() |
self.open_shapefile() |
234 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
235 |
|
|
236 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
237 |
points = shape.vertices() |
points = shape.vertices() |
238 |
else: |
else: |
241 |
points = [] |
points = [] |
242 |
for x, y in poly: |
for x, y in poly: |
243 |
points.append((x, y)) |
points.append((x, y)) |
244 |
|
|
245 |
return Shape(points) |
return Shape(points) |
246 |
|
|
247 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, box): |
258 |
self.projection = projection |
self.projection = projection |
259 |
self.changed(LAYER_PROJECTION_CHANGED, self) |
self.changed(LAYER_PROJECTION_CHANGED, self) |
260 |
|
|
261 |
def SetFill(self, fill): |
def GetClassification(self): |
262 |
"""Set the layer's fill color. None means the shapes are not filled""" |
return self.__classification |
|
self.fill = fill |
|
|
self.changed(LAYER_LEGEND_CHANGED, self) |
|
|
|
|
|
def SetStroke(self, stroke): |
|
|
"""Set the layer's stroke color. None means the shapes are not |
|
|
stroked.""" |
|
|
self.stroke = stroke |
|
|
self.changed(LAYER_LEGEND_CHANGED, self) |
|
263 |
|
|
264 |
def SetStrokeWidth(self, width): |
def SetClassification(self, clazz): |
265 |
"""Set the layer's stroke width.""" |
self.__classification = clazz |
|
self.stroke_width = width |
|
266 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_LEGEND_CHANGED, self) |
267 |
|
|
268 |
def TreeInfo(self): |
def TreeInfo(self): |
269 |
items = [] |
items = [] |
270 |
|
|
271 |
if self.Visible(): |
if self.Visible(): |
272 |
items.append("Shown") |
items.append(_("Shown")) |
273 |
else: |
else: |
274 |
items.append("Hidden") |
items.append(_("Hidden")) |
275 |
items.append("Shapes: %d" % self.NumShapes()) |
items.append(_("Shapes: %d") % self.NumShapes()) |
276 |
|
|
277 |
bbox = self.LatLongBoundingBox() |
bbox = self.LatLongBoundingBox() |
278 |
if bbox is not None: |
if bbox is not None: |
279 |
items.append("Extent (lat-lon): (%g, %g, %g, %g)" % bbox) |
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
280 |
else: |
else: |
281 |
items.append("Extent (lat-lon):") |
items.append(_("Extent (lat-lon):")) |
282 |
items.append("Shapetype: %s" % shapetype_names[self.ShapeType()]) |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
283 |
|
|
284 |
|
items.append(self.__classification) |
285 |
|
|
286 |
def color_string(color): |
return (_("Layer '%s'") % self.Title(), items) |
|
if color is None: |
|
|
return "None" |
|
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
|
|
items.append("Fill: " + color_string(self.fill)) |
|
|
items.append("Outline: " + color_string(self.stroke)) |
|
287 |
|
|
|
return ("Layer '%s'" % self.Title(), items) |
|