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]> |
# Jonathan Coles <[email protected]> |
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, \ |
import gdal |
18 |
LAYER_VISIBILITY_CHANGED |
from gdalconst import GA_ReadOnly |
19 |
|
|
20 |
|
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
21 |
|
LAYER_CHANGED |
22 |
|
|
23 |
from color import Color |
from color import Color |
24 |
|
|
25 |
import classification |
import classification |
26 |
|
|
|
from table import Table |
|
|
|
|
27 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
28 |
|
|
29 |
|
|
30 |
class Shape: |
class Shape: |
31 |
|
|
32 |
"""Represent one shape""" |
"""Represent one shape""" |
34 |
def __init__(self, points): |
def __init__(self, points): |
35 |
self.points = points |
self.points = points |
36 |
#self.compute_bbox() |
#self.compute_bbox() |
37 |
|
self.bbox = None |
38 |
|
|
39 |
def compute_bbox(self): |
def compute_bbox(self): |
40 |
|
if self.bbox is not None: |
41 |
|
return self.bbox |
42 |
|
|
43 |
xs = [] |
xs = [] |
44 |
ys = [] |
ys = [] |
45 |
for x, y in self.points: |
for x, y in self.points: |
50 |
self.urx = max(xs) |
self.urx = max(xs) |
51 |
self.ury = max(ys) |
self.ury = max(ys) |
52 |
|
|
53 |
|
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
54 |
|
|
55 |
|
return self.bbox |
56 |
|
|
57 |
def Points(self): |
def Points(self): |
58 |
return self.points |
return self.points |
59 |
|
|
77 |
|
|
78 |
"""Base class for the layers.""" |
"""Base class for the layers.""" |
79 |
|
|
80 |
def __init__(self, title, visible = 1): |
def __init__(self, title, visible = True, projection = None): |
81 |
"""Initialize the layer. |
"""Initialize the layer. |
82 |
|
|
83 |
title -- the title |
title -- the title |
86 |
TitledObject.__init__(self, title) |
TitledObject.__init__(self, title) |
87 |
Modifiable.__init__(self) |
Modifiable.__init__(self) |
88 |
self.visible = visible |
self.visible = visible |
89 |
|
self.projection = projection |
90 |
|
|
91 |
def Visible(self): |
def Visible(self): |
92 |
"""Return true if layer is visible""" |
"""Return true if layer is visible""" |
97 |
self.visible = visible |
self.visible = visible |
98 |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
99 |
|
|
100 |
|
def HasClassification(self): |
101 |
|
"""Determine if this layer support classifications.""" |
102 |
|
return False |
103 |
|
|
104 |
|
def GetProjection(self): |
105 |
|
"""Return the layer's projection.""" |
106 |
|
return self.projection |
107 |
|
|
108 |
|
def SetProjection(self, projection): |
109 |
|
"""Set the layer's projection""" |
110 |
|
self.projection = projection |
111 |
|
self.changed(LAYER_PROJECTION_CHANGED, self) |
112 |
|
|
113 |
class Layer(BaseLayer): |
class Layer(BaseLayer): |
114 |
|
|
124 |
|
|
125 |
TITLE_CHANGED -- The title has changed. |
TITLE_CHANGED -- The title has changed. |
126 |
LAYER_PROJECTION_CHANGED -- the projection has changed. |
LAYER_PROJECTION_CHANGED -- the projection has changed. |
|
LAYER_LEGEND_CHANGED -- the fill or stroke attributes have changed |
|
|
|
|
127 |
""" |
""" |
128 |
|
|
129 |
def __init__(self, title, filename, projection = None, |
def __init__(self, title, data, projection = None, |
130 |
fill = Color.None, |
fill = Color.Transparent, |
131 |
stroke = Color.Black, |
stroke = Color.Black, |
132 |
lineWidth = 1, |
lineWidth = 1, |
133 |
visible = 1): |
visible = True): |
134 |
"""Initialize the layer. |
"""Initialize the layer. |
135 |
|
|
136 |
title -- the title |
title -- the title |
137 |
filename -- the name of the shapefile |
data -- datastore object for the shape data shown by the layer |
138 |
projection -- the projection object. Its Inverse method is |
projection -- the projection object. Its Inverse method is |
139 |
assumed to map the layer's coordinates to lat/long |
assumed to map the layer's coordinates to lat/long |
140 |
coordinates |
coordinates |
141 |
fill -- the fill color or Color.None if the shapes are not filled |
fill -- the fill color or Color.Transparent if the shapes are |
142 |
stroke -- the stroke color or Color.None if the shapes are not stroked |
not filled |
143 |
|
stroke -- the stroke color or Color.Transparent if the shapes |
144 |
|
are not stroked |
145 |
visible -- boolean. If true the layer is visible. |
visible -- boolean. If true the layer is visible. |
146 |
|
|
147 |
colors are expected to be instances of Color class |
colors are expected to be instances of Color class |
148 |
""" |
""" |
149 |
BaseLayer.__init__(self, title, visible = visible) |
BaseLayer.__init__(self, title, |
150 |
|
visible = visible, |
151 |
# Make the filename absolute. The filename will be |
projection = projection) |
|
# 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) |
|
|
|
|
|
self.projection = projection |
|
|
self.shapefile = None |
|
|
self.shapetree = None |
|
|
self.open_shapefile() |
|
|
# shapetable is the table associated with the shapefile, while |
|
|
# table is the default table used to look up attributes for |
|
|
# display |
|
|
self.shapetable = Table(filename) |
|
|
self.table = self.shapetable |
|
152 |
|
|
153 |
# |
# |
154 |
# this is really important so that when the classification class |
# this is really important so that when the classification class |
155 |
# tries to set its parent layer the variable will exist |
# tries to set its parent layer the variable will exist |
156 |
# |
# |
157 |
self.__classification = None |
self.__classification = None |
158 |
self.__setClassLock = False |
self.__setClassLock = False |
159 |
|
|
160 |
|
self.SetShapeStore(data) |
161 |
|
|
162 |
self.SetClassification(None) |
self.SetClassification(None) |
163 |
|
|
168 |
|
|
169 |
self.UnsetModified() |
self.UnsetModified() |
170 |
|
|
|
def open_shapefile(self): |
|
|
if self.shapefile is None: |
|
|
self.shapefile = shapelib.ShapeFile(self.filename) |
|
|
numshapes, shapetype, mins, maxs = self.shapefile.info() |
|
|
self.numshapes = numshapes |
|
|
self.shapetype = shapelib_shapetypes[shapetype] |
|
|
|
|
|
# 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 |
|
171 |
|
|
172 |
# estimate a good depth for the quad tree. Each depth |
def SetShapeStore(self, store): |
173 |
# multiplies the number of nodes by four, therefore we |
self.store = store |
174 |
# basically take the base 4 logarithm of the number of |
self.shapefile = self.store.Shapefile() |
175 |
# shapes. |
self.shapetable = self.store.Table() |
176 |
if self.numshapes < 4: |
if hasattr(self.store, "FileName"): |
177 |
maxdepth = 1 |
self.filename = self.store.FileName() |
178 |
else: |
self.table = self.shapetable |
179 |
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
|
180 |
|
numshapes, shapetype, mins, maxs = self.shapefile.info() |
181 |
|
self.numshapes = numshapes |
182 |
|
self.shapetype = shapelib_shapetypes[shapetype] |
183 |
|
|
184 |
|
# if there are shapes, set the bbox accordingly. Otherwise |
185 |
|
# set it to None. |
186 |
|
if self.numshapes: |
187 |
|
self.bbox = mins[:2] + maxs[:2] |
188 |
|
else: |
189 |
|
self.bbox = None |
190 |
|
|
191 |
|
# estimate a good depth for the quad tree. Each depth |
192 |
|
# multiplies the number of nodes by four, therefore we |
193 |
|
# basically take the base 4 logarithm of the number of |
194 |
|
# shapes. |
195 |
|
if self.numshapes < 4: |
196 |
|
maxdepth = 1 |
197 |
|
else: |
198 |
|
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
199 |
|
|
200 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
201 |
maxdepth) |
maxdepth) |
202 |
|
if self.__classification is not None: |
203 |
|
fieldname = self.__classification.GetField() |
204 |
|
if fieldname is not None and \ |
205 |
|
not self.store.Table().HasColumn(fieldname): |
206 |
|
self.SetClassification(None) |
207 |
|
self.changed(LAYER_CHANGED, self) |
208 |
|
|
209 |
|
def ShapeStore(self): |
210 |
|
return self.store |
211 |
|
|
212 |
def Destroy(self): |
def Destroy(self): |
213 |
BaseLayer.Destroy(self) |
BaseLayer.Destroy(self) |
|
if self.shapefile is not None: |
|
|
self.shapefile.close() |
|
|
self.shapefile = None |
|
|
self.shapetree = None |
|
214 |
self.SetClassification(None) |
self.SetClassification(None) |
|
self.table.Destroy() |
|
215 |
|
|
216 |
def BoundingBox(self): |
def BoundingBox(self): |
217 |
"""Return the layer's bounding box in the intrinsic coordinate system. |
"""Return the layer's bounding box in the intrinsic coordinate system. |
218 |
|
|
219 |
If the layer has no shapes, return None. |
If the layer has no shapes, return None. |
220 |
""" |
""" |
|
# The bbox will be set by open_shapefile just as we need it |
|
|
# here. |
|
|
self.open_shapefile() |
|
221 |
return self.bbox |
return self.bbox |
222 |
|
|
223 |
def LatLongBoundingBox(self): |
def LatLongBoundingBox(self): |
235 |
else: |
else: |
236 |
return None |
return None |
237 |
|
|
238 |
def GetFieldType(self, fieldName): |
def ShapesBoundingBox(self, shapes): |
239 |
self.open_shapefile() |
"""Return a bounding box in lat/long coordinates for the given |
240 |
info = self.table.field_info_by_name(fieldName) |
list of shape ids. |
241 |
if info is not None: |
|
242 |
return info[0] |
If shapes is None or empty, return None. |
243 |
|
""" |
244 |
|
|
245 |
|
if shapes is None or len(shapes) == 0: return None |
246 |
|
|
247 |
|
llx = [] |
248 |
|
lly = [] |
249 |
|
urx = [] |
250 |
|
ury = [] |
251 |
|
|
252 |
|
if self.projection is not None: |
253 |
|
inverse = lambda x, y: self.projection.Inverse(x, y) |
254 |
else: |
else: |
255 |
return None |
inverse = lambda x, y: (x, y) |
256 |
|
|
257 |
|
for id in shapes: |
258 |
|
left, bottom, right, top = self.Shape(id).compute_bbox() |
259 |
|
|
260 |
|
left, bottom = inverse(left, bottom) |
261 |
|
right, top = inverse(right, top) |
262 |
|
|
263 |
|
llx.append(left) |
264 |
|
lly.append(bottom) |
265 |
|
urx.append(right) |
266 |
|
ury.append(top) |
267 |
|
|
268 |
|
return (min(llx), min(lly), max(urx), max(ury)) |
269 |
|
|
270 |
|
def GetFieldType(self, fieldName): |
271 |
|
if self.table.HasColumn(fieldName): |
272 |
|
return self.table.Column(fieldName).type |
273 |
|
return None |
274 |
|
|
275 |
def NumShapes(self): |
def NumShapes(self): |
276 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
|
self.open_shapefile() |
|
277 |
return self.numshapes |
return self.numshapes |
278 |
|
|
279 |
def ShapeType(self): |
def ShapeType(self): |
280 |
"""Return the type of the shapes in the layer. |
"""Return the type of the shapes in the layer. |
281 |
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON. |
282 |
""" |
""" |
|
self.open_shapefile() |
|
283 |
return self.shapetype |
return self.shapetype |
284 |
|
|
285 |
def Shape(self, index): |
def Shape(self, index): |
286 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
|
self.open_shapefile() |
|
287 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
288 |
|
|
289 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
300 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, box): |
301 |
"""Return the ids of the shapes that overlap the box. |
"""Return the ids of the shapes that overlap the box. |
302 |
|
|
303 |
Box is a tuple (left, bottom, right, top) in the coordinate |
Box is a tuple (left, bottom, right, top) in unprojected coordinates. |
|
system used by the layer's shapefile. |
|
304 |
""" |
""" |
305 |
left, bottom, right, top = box |
left, bottom, right, top = box |
306 |
|
|
307 |
|
if self.projection is not None: |
308 |
|
left, bottom = self.projection.Forward(left, bottom) |
309 |
|
right, top = self.projection.Forward(right, top) |
310 |
|
|
311 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
312 |
|
|
313 |
def SetProjection(self, projection): |
def HasClassification(self): |
314 |
"""Set the layer's projection""" |
return True |
|
self.projection = projection |
|
|
self.changed(LAYER_PROJECTION_CHANGED, self) |
|
315 |
|
|
316 |
def GetClassification(self): |
def GetClassification(self): |
317 |
return self.__classification |
return self.__classification |
339 |
self.__setClassLock = False |
self.__setClassLock = False |
340 |
raise ValueError |
raise ValueError |
341 |
|
|
342 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_CHANGED, self) |
343 |
|
|
344 |
self.__setClassLock = False |
self.__setClassLock = False |
345 |
|
|
346 |
def ClassChanged(self): |
def ClassChanged(self): |
347 |
"""Called from the classification object when it has changed.""" |
"""Called from the classification object when it has changed.""" |
348 |
self.changed(LAYER_LEGEND_CHANGED, self) |
self.changed(LAYER_CHANGED, self) |
349 |
|
|
350 |
def TreeInfo(self): |
def TreeInfo(self): |
351 |
items = [] |
items = [] |
352 |
|
|
353 |
|
if hasattr(self, 'filename'): |
354 |
|
items.append(_("Filename: %s") % self.filename) |
355 |
|
|
356 |
if self.Visible(): |
if self.Visible(): |
357 |
items.append(_("Shown")) |
items.append(_("Shown")) |
358 |
else: |
else: |
366 |
items.append(_("Extent (lat-lon):")) |
items.append(_("Extent (lat-lon):")) |
367 |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()]) |
368 |
|
|
369 |
|
if self.projection and len(self.projection.params) > 0: |
370 |
|
items.append((_("Projection"), |
371 |
|
[str(param) for param in self.projection.params])) |
372 |
|
|
373 |
items.append(self.__classification) |
items.append(self.__classification) |
374 |
|
|
375 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
376 |
|
|
377 |
|
|
378 |
|
class RasterLayer(BaseLayer): |
379 |
|
|
380 |
|
def __init__(self, title, filename, projection = None, visible = True): |
381 |
|
"""Initialize the Raster Layer. |
382 |
|
|
383 |
|
title -- title for the layer. |
384 |
|
|
385 |
|
filename -- file name of the source image. |
386 |
|
|
387 |
|
projection -- Projection object describing the projection which |
388 |
|
the source image is in. |
389 |
|
|
390 |
|
visible -- True is the layer should initially be visible. |
391 |
|
|
392 |
|
Throws IOError if the filename is invalid or points to a file that |
393 |
|
is not in a format GDAL can use. |
394 |
|
""" |
395 |
|
|
396 |
|
BaseLayer.__init__(self, title, visible = visible) |
397 |
|
|
398 |
|
self.projection = projection |
399 |
|
self.filename = filename |
400 |
|
|
401 |
|
self.bbox = -1 |
402 |
|
|
403 |
|
# |
404 |
|
# temporarily open the file so that GDAL can test if it's valid. |
405 |
|
# |
406 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
407 |
|
|
408 |
|
if dataset is None: |
409 |
|
raise IOError() |
410 |
|
|
411 |
|
self.UnsetModified() |
412 |
|
|
413 |
|
def BoundingBox(self): |
414 |
|
"""Return the layer's bounding box in the intrinsic coordinate system. |
415 |
|
|
416 |
|
If the layer has no shapes, return None. |
417 |
|
""" |
418 |
|
if self.bbox == -1: |
419 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
420 |
|
if dataset is None: |
421 |
|
self.bbox = None |
422 |
|
else: |
423 |
|
geotransform = dataset.GetGeoTransform() |
424 |
|
if geotransform is None: |
425 |
|
return None |
426 |
|
|
427 |
|
x = 0 |
428 |
|
y = dataset.RasterYSize |
429 |
|
left = geotransform[0] + \ |
430 |
|
geotransform[1] * x + \ |
431 |
|
geotransform[2] * y |
432 |
|
|
433 |
|
bottom = geotransform[3] + \ |
434 |
|
geotransform[4] * x + \ |
435 |
|
geotransform[5] * y |
436 |
|
|
437 |
|
x = dataset.RasterXSize |
438 |
|
y = 0 |
439 |
|
right = geotransform[0] + \ |
440 |
|
geotransform[1] * x + \ |
441 |
|
geotransform[2] * y |
442 |
|
|
443 |
|
top = geotransform[3] + \ |
444 |
|
geotransform[4] * x + \ |
445 |
|
geotransform[5] * y |
446 |
|
|
447 |
|
self.bbox = (left, bottom, right, top) |
448 |
|
|
449 |
|
return self.bbox |
450 |
|
|
451 |
|
def LatLongBoundingBox(self): |
452 |
|
bbox = self.BoundingBox() |
453 |
|
if bbox is None: |
454 |
|
return None |
455 |
|
|
456 |
|
llx, lly, urx, ury = bbox |
457 |
|
if self.projection is not None: |
458 |
|
llx, lly = self.projection.Inverse(llx, lly) |
459 |
|
urx, ury = self.projection.Inverse(urx, ury) |
460 |
|
|
461 |
|
return llx, lly, urx, ury |
462 |
|
|
463 |
|
def GetImageFilename(self): |
464 |
|
return self.filename |
465 |
|
|
466 |
|
def TreeInfo(self): |
467 |
|
items = [] |
468 |
|
|
469 |
|
if self.Visible(): |
470 |
|
items.append(_("Shown")) |
471 |
|
else: |
472 |
|
items.append(_("Hidden")) |
473 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
474 |
|
|
475 |
|
bbox = self.LatLongBoundingBox() |
476 |
|
if bbox is not None: |
477 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
478 |
|
else: |
479 |
|
items.append(_("Extent (lat-lon):")) |
480 |
|
|
481 |
|
if self.projection and len(self.projection.params) > 0: |
482 |
|
items.append((_("Projection"), |
483 |
|
[str(param) for param in self.projection.params])) |
484 |
|
|
485 |
|
return (_("Layer '%s'") % self.Title(), items) |
486 |
|
|