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 |
173 |
self.store = store |
self.store = store |
174 |
self.shapefile = self.store.Shapefile() |
self.shapefile = self.store.Shapefile() |
175 |
self.shapetable = self.store.Table() |
self.shapetable = self.store.Table() |
176 |
self.filename = self.store.filename |
if hasattr(self.store, "FileName"): |
177 |
|
self.filename = self.store.FileName() |
178 |
self.table = self.shapetable |
self.table = self.shapetable |
179 |
|
|
180 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
310 |
|
|
311 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
312 |
|
|
313 |
def GetProjection(self): |
def HasClassification(self): |
314 |
return self.projection |
return True |
|
|
|
|
def SetProjection(self, projection): |
|
|
"""Set the layer's projection""" |
|
|
self.projection = projection |
|
|
self.changed(LAYER_PROJECTION_CHANGED, self) |
|
315 |
|
|
316 |
def GetClassification(self): |
def GetClassification(self): |
317 |
return self.__classification |
return self.__classification |
372 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
373 |
|
|
374 |
|
|
375 |
|
class RasterLayer(BaseLayer): |
376 |
|
|
377 |
|
def __init__(self, title, filename, projection = None, visible = True): |
378 |
|
"""Initialize the Raster Layer. |
379 |
|
|
380 |
|
title -- title for the layer. |
381 |
|
|
382 |
|
filename -- file name of the source image. |
383 |
|
|
384 |
|
projection -- Projection object describing the projection which |
385 |
|
the source image is in. |
386 |
|
|
387 |
|
visible -- True is the layer should initially be visible. |
388 |
|
|
389 |
|
Throws IOError if the filename is invalid or points to a file that |
390 |
|
is not in a format GDAL can use. |
391 |
|
""" |
392 |
|
|
393 |
|
BaseLayer.__init__(self, title, visible = visible) |
394 |
|
|
395 |
|
self.projection = projection |
396 |
|
self.filename = filename |
397 |
|
|
398 |
|
self.bbox = -1 |
399 |
|
|
400 |
|
# |
401 |
|
# temporarily open the file so that GDAL can test if it's valid. |
402 |
|
# |
403 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
404 |
|
|
405 |
|
if dataset is None: |
406 |
|
raise IOError() |
407 |
|
|
408 |
|
self.UnsetModified() |
409 |
|
|
410 |
|
def BoundingBox(self): |
411 |
|
"""Return the layer's bounding box in the intrinsic coordinate system. |
412 |
|
|
413 |
|
If the layer has no shapes, return None. |
414 |
|
""" |
415 |
|
if self.bbox == -1: |
416 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
417 |
|
if dataset is None: |
418 |
|
self.bbox = None |
419 |
|
else: |
420 |
|
geotransform = dataset.GetGeoTransform() |
421 |
|
if geotransform is None: |
422 |
|
return None |
423 |
|
|
424 |
|
x = 0 |
425 |
|
y = dataset.RasterYSize |
426 |
|
left = geotransform[0] + \ |
427 |
|
geotransform[1] * x + \ |
428 |
|
geotransform[2] * y |
429 |
|
|
430 |
|
bottom = geotransform[3] + \ |
431 |
|
geotransform[4] * x + \ |
432 |
|
geotransform[5] * y |
433 |
|
|
434 |
|
x = dataset.RasterXSize |
435 |
|
y = 0 |
436 |
|
right = geotransform[0] + \ |
437 |
|
geotransform[1] * x + \ |
438 |
|
geotransform[2] * y |
439 |
|
|
440 |
|
top = geotransform[3] + \ |
441 |
|
geotransform[4] * x + \ |
442 |
|
geotransform[5] * y |
443 |
|
|
444 |
|
self.bbox = (left, bottom, right, top) |
445 |
|
|
446 |
|
return self.bbox |
447 |
|
|
448 |
|
def LatLongBoundingBox(self): |
449 |
|
bbox = self.BoundingBox() |
450 |
|
if bbox is None: |
451 |
|
return None |
452 |
|
|
453 |
|
llx, lly, urx, ury = bbox |
454 |
|
if self.projection is not None: |
455 |
|
llx, lly = self.projection.Inverse(llx, lly) |
456 |
|
urx, ury = self.projection.Inverse(urx, ury) |
457 |
|
|
458 |
|
return llx, lly, urx, ury |
459 |
|
|
460 |
|
def GetImageFilename(self): |
461 |
|
return self.filename |
462 |
|
|
463 |
|
def TreeInfo(self): |
464 |
|
items = [] |
465 |
|
|
466 |
|
if self.Visible(): |
467 |
|
items.append(_("Shown")) |
468 |
|
else: |
469 |
|
items.append(_("Hidden")) |
470 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
471 |
|
|
472 |
|
bbox = self.LatLongBoundingBox() |
473 |
|
if bbox is not None: |
474 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
475 |
|
else: |
476 |
|
items.append(_("Extent (lat-lon):")) |
477 |
|
|
478 |
|
if self.projection and len(self.projection.params) > 0: |
479 |
|
items.append((_("Projection"), |
480 |
|
[str(param) for param in self.projection.params])) |
481 |
|
|
482 |
|
return (_("Layer '%s'") % self.Title(), items) |
483 |
|
|