14 |
|
|
15 |
import shapelib, shptree |
import shapelib, shptree |
16 |
|
|
17 |
|
import gdal |
18 |
|
from gdalconst import GA_ReadOnly |
19 |
|
|
20 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
21 |
LAYER_CHANGED |
LAYER_CHANGED |
22 |
|
|
77 |
|
|
78 |
"""Base class for the layers.""" |
"""Base class for the layers.""" |
79 |
|
|
80 |
def __init__(self, title, visible = True): |
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 |
|
|
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 |
self.projection = projection |
projection = projection) |
152 |
|
|
153 |
# |
# |
154 |
# this is really important so that when the classification class |
# this is really important so that when the classification class |
309 |
|
|
310 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
311 |
|
|
312 |
def GetProjection(self): |
def HasClassification(self): |
313 |
return self.projection |
return True |
|
|
|
|
def SetProjection(self, projection): |
|
|
"""Set the layer's projection""" |
|
|
self.projection = projection |
|
|
self.changed(LAYER_PROJECTION_CHANGED, self) |
|
314 |
|
|
315 |
def GetClassification(self): |
def GetClassification(self): |
316 |
return self.__classification |
return self.__classification |
371 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
372 |
|
|
373 |
|
|
374 |
|
class RasterLayer(BaseLayer): |
375 |
|
|
376 |
|
def __init__(self, title, filename, projection = None, visible = True): |
377 |
|
"""Initialize the Raster Layer. |
378 |
|
|
379 |
|
title -- title for the layer. |
380 |
|
|
381 |
|
filename -- file name of the source image. |
382 |
|
|
383 |
|
projection -- Projection object describing the projection which |
384 |
|
the source image is in. |
385 |
|
|
386 |
|
visible -- True is the layer should initially be visible. |
387 |
|
""" |
388 |
|
|
389 |
|
BaseLayer.__init__(self, title, visible = visible) |
390 |
|
|
391 |
|
self.projection = projection |
392 |
|
self.filename = filename |
393 |
|
|
394 |
|
self.bbox = -1 |
395 |
|
|
396 |
|
self.UnsetModified() |
397 |
|
|
398 |
|
|
399 |
|
def BoundingBox(self): |
400 |
|
"""Return the layer's bounding box in the intrinsic coordinate system. |
401 |
|
|
402 |
|
If the layer has no shapes, return None. |
403 |
|
""" |
404 |
|
if self.bbox == -1: |
405 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
406 |
|
if dataset is None: |
407 |
|
self.bbox = None |
408 |
|
else: |
409 |
|
left, bottom = self.__CalcCorner(dataset, |
410 |
|
0, dataset.RasterYSize) |
411 |
|
right, top = self.__CalcCorner(dataset, |
412 |
|
dataset.RasterXSize, 0) |
413 |
|
self.bbox = (left, bottom, right, top) |
414 |
|
|
415 |
|
return self.bbox |
416 |
|
|
417 |
|
def __CalcCorner(self, dataset, x, y): |
418 |
|
geotransform = dataset.GetGeoTransform() |
419 |
|
return geotransform[0] + geotransform[1] * x + geotransform[2] * y, \ |
420 |
|
geotransform[3] + geotransform[4] * x + geotransform[5] * y |
421 |
|
|
422 |
|
def LatLongBoundingBox(self): |
423 |
|
bbox = self.BoundingBox() |
424 |
|
if bbox is not None: |
425 |
|
llx, lly, urx, ury = bbox |
426 |
|
if self.projection is not None: |
427 |
|
llx, lly = self.projection.Inverse(llx, lly) |
428 |
|
urx, ury = self.projection.Inverse(urx, ury) |
429 |
|
return llx, lly, urx, ury |
430 |
|
else: |
431 |
|
return None |
432 |
|
|
433 |
|
def GetImageFilename(self): |
434 |
|
return self.filename |
435 |
|
|
436 |
|
def TreeInfo(self): |
437 |
|
items = [] |
438 |
|
|
439 |
|
if self.Visible(): |
440 |
|
items.append(_("Shown")) |
441 |
|
else: |
442 |
|
items.append(_("Hidden")) |
443 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
444 |
|
|
445 |
|
bbox = self.LatLongBoundingBox() |
446 |
|
if bbox is not None: |
447 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
448 |
|
else: |
449 |
|
items.append(_("Extent (lat-lon):")) |
450 |
|
|
451 |
|
if self.projection and len(self.projection.params) > 0: |
452 |
|
items.append((_("Projection"), |
453 |
|
[str(param) for param in self.projection.params])) |
454 |
|
|
455 |
|
return (_("Layer '%s'") % self.Title(), items) |
456 |
|
|