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, LAYER_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 |
|
# |
146 |
|
# this is really important so that when the classification class |
147 |
|
# tries to set its parent layer the variable will exist |
148 |
|
# |
149 |
|
self.__classification = None |
150 |
|
self.__setClassLock = False |
151 |
|
|
152 |
|
|
153 |
|
self.SetClassification(None) |
154 |
|
|
155 |
|
self.__classification.SetDefaultLineColor(stroke) |
156 |
|
self.__classification.SetDefaultLineWidth(lineWidth) |
157 |
|
self.__classification.SetDefaultFill(fill) |
158 |
|
self.__classification.SetLayer(self) |
159 |
|
|
160 |
|
self.UnsetModified() |
161 |
|
|
162 |
def open_shapefile(self): |
def open_shapefile(self): |
163 |
if self.shapefile is None: |
if self.shapefile is None: |
164 |
self.shapefile = shapelib.ShapeFile(self.filename) |
self.shapefile = shapelib.ShapeFile(self.filename) |
185 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
186 |
maxdepth) |
maxdepth) |
187 |
|
|
188 |
|
def Destroy(self): |
189 |
|
BaseLayer.Destroy(self) |
190 |
|
if self.shapefile is not None: |
191 |
|
self.shapefile.close() |
192 |
|
self.shapefile = None |
193 |
|
self.shapetree = None |
194 |
|
self.SetClassification(None) |
195 |
|
self.table.Destroy() |
196 |
|
|
197 |
def BoundingBox(self): |
def BoundingBox(self): |
198 |
"""Return the layer's bounding box in the intrinsic coordinate system. |
"""Return the layer's bounding box in the intrinsic coordinate system. |
199 |
|
|
219 |
else: |
else: |
220 |
return None |
return None |
221 |
|
|
222 |
|
def GetFieldType(self, fieldName): |
223 |
|
self.open_shapefile() |
224 |
|
info = self.table.field_info_by_name(fieldName) |
225 |
|
if info is not None: |
226 |
|
return info[0] |
227 |
|
else: |
228 |
|
return None |
229 |
|
|
230 |
def NumShapes(self): |
def NumShapes(self): |
231 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
232 |
self.open_shapefile() |
self.open_shapefile() |
243 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
244 |
self.open_shapefile() |
self.open_shapefile() |
245 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
246 |
|
|
247 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
248 |
points = shape.vertices() |
points = shape.vertices() |
249 |
else: |
else: |
252 |
points = [] |
points = [] |
253 |
for x, y in poly: |
for x, y in poly: |
254 |
points.append((x, y)) |
points.append((x, y)) |
255 |
|
|
256 |
return Shape(points) |
return Shape(points) |
257 |
|
|
258 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, box): |
269 |
self.projection = projection |
self.projection = projection |
270 |
self.changed(LAYER_PROJECTION_CHANGED, self) |
self.changed(LAYER_PROJECTION_CHANGED, self) |
271 |
|
|
272 |
def SetFill(self, fill): |
def GetClassification(self): |
273 |
"""Set the layer's fill color. None means the shapes are not filled""" |
return self.__classification |
274 |
self.fill = fill |
|
275 |
self.changed(LAYER_LEGEND_CHANGED, self) |
def SetClassification(self, clazz): |
276 |
|
"""Set the classification to 'clazz' |
277 |
def SetStroke(self, stroke): |
|
278 |
"""Set the layer's stroke color. None means the shapes are not |
If 'clazz' is None a default classification is created |
279 |
stroked.""" |
""" |
280 |
self.stroke = stroke |
|
281 |
self.changed(LAYER_LEGEND_CHANGED, self) |
# prevent infinite recursion when calling SetLayer() |
282 |
|
if self.__setClassLock: return |
283 |
def SetStrokeWidth(self, width): |
|
284 |
"""Set the layer's stroke width.""" |
self.__setClassLock = True |
285 |
self.stroke_width = width |
|
286 |
self.changed(LAYER_LEGEND_CHANGED, self) |
if clazz is None: |
287 |
|
if self.__classification is not None: |
288 |
|
self.__classification.SetLayer(None) |
289 |
|
self.__classification = classification.Classification() |
290 |
|
else: |
291 |
|
self.__classification = clazz |
292 |
|
try: |
293 |
|
self.__classification.SetLayer(self) |
294 |
|
except ValueError: |
295 |
|
self.__setClassLock = False |
296 |
|
raise ValueError |
297 |
|
|
298 |
|
self.changed(LAYER_CHANGED, self) |
299 |
|
|
300 |
|
self.__setClassLock = False |
301 |
|
|
302 |
|
def ClassChanged(self): |
303 |
|
"""Called from the classification object when it has changed.""" |
304 |
|
self.changed(LAYER_CHANGED, self) |
305 |
|
|
306 |
|
def TreeInfo(self): |
307 |
|
items = [] |
308 |
|
|
309 |
|
if self.Visible(): |
310 |
|
items.append(_("Shown")) |
311 |
|
else: |
312 |
|
items.append(_("Hidden")) |
313 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
314 |
|
|
315 |
|
bbox = self.LatLongBoundingBox() |
316 |
|
if bbox is not None: |
317 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
318 |
|
else: |
319 |
|
items.append(_("Extent (lat-lon):")) |
320 |
|
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
321 |
|
|
322 |
|
items.append(self.__classification) |
323 |
|
|
324 |
|
return (_("Layer '%s'") % self.Title(), items) |
325 |
|
|