1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 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 |
|
|
|
import os |
|
11 |
from math import log, ceil |
from math import log, ceil |
12 |
|
|
13 |
from Thuban import _ |
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_VISIBILITY_CHANGED, \ |
18 |
LAYER_VISIBILITY_CHANGED |
LAYER_CHANGED |
19 |
|
|
20 |
from color import Color |
from color import Color |
|
# Some predefined colors for internal use |
|
|
_black = Color(0, 0, 0) |
|
21 |
|
|
22 |
from classification import Classification |
import classification |
|
|
|
|
from table import Table |
|
23 |
|
|
24 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
25 |
|
|
26 |
|
|
27 |
class Shape: |
class Shape: |
28 |
|
|
29 |
"""Represent one shape""" |
"""Represent one shape""" |
100 |
|
|
101 |
TITLE_CHANGED -- The title has changed. |
TITLE_CHANGED -- The title has changed. |
102 |
LAYER_PROJECTION_CHANGED -- the projection has changed. |
LAYER_PROJECTION_CHANGED -- the projection has changed. |
|
LAYER_LEGEND_CHANGED -- the fill or stroke attributes have changed |
|
|
|
|
103 |
""" |
""" |
104 |
|
|
105 |
def __init__(self, title, filename, projection = None, |
def __init__(self, title, data, projection = None, |
106 |
fill = None, stroke = _black, stroke_width = 1, visible = 1): |
fill = Color.Transparent, |
107 |
|
stroke = Color.Black, |
108 |
|
lineWidth = 1, |
109 |
|
visible = 1): |
110 |
"""Initialize the layer. |
"""Initialize the layer. |
111 |
|
|
112 |
title -- the title |
title -- the title |
113 |
filename -- the name of the shapefile |
data -- datastore object for the shape data shown by the layer |
114 |
projection -- the projection object. Its Inverse method is |
projection -- the projection object. Its Inverse method is |
115 |
assumed to map the layer's coordinates to lat/long |
assumed to map the layer's coordinates to lat/long |
116 |
coordinates |
coordinates |
117 |
fill -- the fill color or None if the shapes are not filled |
fill -- the fill color or Color.Transparent if the shapes are |
118 |
stroke -- the stroke color or None if the shapes are not stroked |
not filled |
119 |
|
stroke -- the stroke color or Color.Transparent if the shapes |
120 |
|
are not stroked |
121 |
visible -- boolean. If true the layer is visible. |
visible -- boolean. If true the layer is visible. |
122 |
|
|
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 |
|
|
|
# Make the filename absolute. The filename will be |
|
|
# interpreted relative to that anyway, but when saving a |
|
|
# session we need to compare absolute paths and it's usually |
|
|
# safer to always work with absolute paths. |
|
|
self.filename = os.path.abspath(filename) |
|
|
|
|
127 |
self.projection = projection |
self.projection = projection |
128 |
self.fill = fill |
|
129 |
self.stroke = stroke |
# |
130 |
self.stroke_width = stroke_width |
# this is really important so that when the classification class |
131 |
self.shapefile = None |
# tries to set its parent layer the variable will exist |
132 |
self.shapetree = None |
# |
133 |
self.open_shapefile() |
self.__classification = None |
134 |
# shapetable is the table associated with the shapefile, while |
self.__setClassLock = False |
135 |
# table is the default table used to look up attributes for |
|
136 |
# display |
self.SetShapeStore(data) |
137 |
self.shapetable = Table(filename) |
|
138 |
|
self.SetClassification(None) |
139 |
|
|
140 |
|
self.__classification.SetDefaultLineColor(stroke) |
141 |
|
self.__classification.SetDefaultLineWidth(lineWidth) |
142 |
|
self.__classification.SetDefaultFill(fill) |
143 |
|
self.__classification.SetLayer(self) |
144 |
|
|
145 |
|
self.UnsetModified() |
146 |
|
|
147 |
|
|
148 |
|
def SetShapeStore(self, store): |
149 |
|
self.store = store |
150 |
|
self.shapefile = self.store.Shapefile() |
151 |
|
self.shapetable = self.store.Table() |
152 |
|
self.filename = self.store.filename |
153 |
self.table = self.shapetable |
self.table = self.shapetable |
154 |
|
|
155 |
self.classification = Classification() |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
156 |
self.classification.setNull( |
self.numshapes = numshapes |
157 |
{'stroke':stroke, 'stroke_width':stroke_width, 'fill':fill}) |
self.shapetype = shapelib_shapetypes[shapetype] |
158 |
|
|
159 |
def open_shapefile(self): |
# if there are shapes, set the bbox accordingly. Otherwise |
160 |
if self.shapefile is None: |
# set it to None. |
161 |
self.shapefile = shapelib.ShapeFile(self.filename) |
if self.numshapes: |
162 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
self.bbox = mins[:2] + maxs[:2] |
163 |
self.numshapes = numshapes |
else: |
164 |
self.shapetype = shapelib_shapetypes[shapetype] |
self.bbox = None |
|
|
|
|
# if there are shapes, set the bbox accordinly. Otherwise |
|
|
# set it to None. |
|
|
if self.numshapes: |
|
|
self.bbox = mins[:2] + maxs[:2] |
|
|
else: |
|
|
self.bbox = None |
|
|
|
|
|
# estimate a good depth for the quad tree. Each depth |
|
|
# multiplies the number of nodes by four, therefore we |
|
|
# basically take the base 4 logarithm of the number of |
|
|
# shapes. |
|
|
if self.numshapes < 4: |
|
|
maxdepth = 1 |
|
|
else: |
|
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
|
165 |
|
|
166 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
# estimate a good depth for the quad tree. Each depth |
167 |
maxdepth) |
# multiplies the number of nodes by four, therefore we |
168 |
|
# basically take the base 4 logarithm of the number of |
169 |
|
# shapes. |
170 |
|
if self.numshapes < 4: |
171 |
|
maxdepth = 1 |
172 |
|
else: |
173 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
174 |
|
|
175 |
|
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
176 |
|
maxdepth) |
177 |
|
if self.__classification is not None: |
178 |
|
fieldname = self.__classification.GetField() |
179 |
|
if not self.store.Table().field_info_by_name(fieldname): |
180 |
|
self.SetClassification(None) |
181 |
|
self.changed(LAYER_CHANGED, self) |
182 |
|
|
183 |
|
def ShapeStore(self): |
184 |
|
return self.store |
185 |
|
|
186 |
def Destroy(self): |
def Destroy(self): |
187 |
BaseLayer.Destroy(self) |
BaseLayer.Destroy(self) |
188 |
if self.shapefile is not None: |
self.SetClassification(None) |
|
self.shapefile.close() |
|
|
self.shapefile = None |
|
|
self.shapetree = None |
|
|
self.table.Destroy() |
|
189 |
|
|
190 |
def BoundingBox(self): |
def BoundingBox(self): |
191 |
"""Return the layer's bounding box in the intrinsic coordinate system. |
"""Return the layer's bounding box in the intrinsic coordinate system. |
192 |
|
|
193 |
If the layer has no shapes, return None. |
If the layer has no shapes, return None. |
194 |
""" |
""" |
|
# The bbox will be set by open_shapefile just as we need it |
|
|
# here. |
|
|
self.open_shapefile() |
|
195 |
return self.bbox |
return self.bbox |
196 |
|
|
197 |
def LatLongBoundingBox(self): |
def LatLongBoundingBox(self): |
209 |
else: |
else: |
210 |
return None |
return None |
211 |
|
|
212 |
|
def GetFieldType(self, fieldName): |
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""" |
|
self.open_shapefile() |
|
221 |
return self.numshapes |
return self.numshapes |
222 |
|
|
223 |
def ShapeType(self): |
def ShapeType(self): |
224 |
"""Return the type of the shapes in the layer. |
"""Return the type of the shapes in the layer. |
225 |
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
226 |
""" |
""" |
|
self.open_shapefile() |
|
227 |
return self.shapetype |
return self.shapetype |
228 |
|
|
229 |
def Shape(self, index): |
def Shape(self, index): |
230 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
|
self.open_shapefile() |
|
231 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
232 |
|
|
233 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
255 |
self.projection = projection |
self.projection = projection |
256 |
self.changed(LAYER_PROJECTION_CHANGED, self) |
self.changed(LAYER_PROJECTION_CHANGED, self) |
257 |
|
|
258 |
def SetFill(self, fill): |
def GetClassification(self): |
259 |
"""Set the layer's fill color. None means the shapes are not filled""" |
return self.__classification |
260 |
self.fill = fill |
|
261 |
self.changed(LAYER_LEGEND_CHANGED, self) |
def SetClassification(self, clazz): |
262 |
|
"""Set the classification to 'clazz' |
263 |
def SetStroke(self, stroke): |
|
264 |
"""Set the layer's stroke color. None means the shapes are not |
If 'clazz' is None a default classification is created |
265 |
stroked.""" |
""" |
266 |
self.stroke = stroke |
|
267 |
self.changed(LAYER_LEGEND_CHANGED, self) |
# prevent infinite recursion when calling SetLayer() |
268 |
|
if self.__setClassLock: return |
|
def SetStrokeWidth(self, width): |
|
|
"""Set the layer's stroke width.""" |
|
|
self.stroke_width = width |
|
|
self.changed(LAYER_LEGEND_CHANGED, self) |
|
269 |
|
|
270 |
|
self.__setClassLock = True |
271 |
|
|
272 |
|
if clazz is None: |
273 |
|
if self.__classification is not None: |
274 |
|
self.__classification.SetLayer(None) |
275 |
|
self.__classification = classification.Classification() |
276 |
|
else: |
277 |
|
self.__classification = clazz |
278 |
|
try: |
279 |
|
self.__classification.SetLayer(self) |
280 |
|
except ValueError: |
281 |
|
self.__setClassLock = False |
282 |
|
raise ValueError |
283 |
|
|
284 |
|
self.changed(LAYER_CHANGED, self) |
285 |
|
|
286 |
|
self.__setClassLock = False |
287 |
|
|
288 |
|
def ClassChanged(self): |
289 |
|
"""Called from the classification object when it has changed.""" |
290 |
|
self.changed(LAYER_CHANGED, self) |
291 |
|
|
292 |
def TreeInfo(self): |
def TreeInfo(self): |
293 |
items = [] |
items = [] |
294 |
|
|
305 |
items.append(_("Extent (lat-lon):")) |
items.append(_("Extent (lat-lon):")) |
306 |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
307 |
|
|
308 |
def color_string(color): |
items.append(self.__classification) |
|
if color is None: |
|
|
return "None" |
|
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
|
|
|
|
|
# layers will always have a classification with at least a NULL data set |
|
|
|
|
|
#items.append((_("Fill: %s") % color_string(self.fill), self.fill)) |
|
|
#items.append((_("Outline: %s") % color_string(self.stroke), self.stroke)) |
|
|
|
|
|
items.append(self.classification) |
|
309 |
|
|
310 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
311 |
|
|