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() |
199 |
|
|
200 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
201 |
maxdepth) |
maxdepth) |
202 |
|
# Set the classification to None if there is a classification |
203 |
|
# and the new shapestore doesn't have a table with a suitable |
204 |
|
# column, i.e one with the same name and type as before |
205 |
|
# FIXME: Maybe we should keep it the same if the type is |
206 |
|
# compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT |
207 |
if self.__classification is not None: |
if self.__classification is not None: |
208 |
fieldname = self.__classification.GetField() |
fieldname = self.__classification.GetField() |
209 |
if fieldname is not None and \ |
fieldtype = self.__classification.GetFieldType() |
210 |
not self.store.Table().HasColumn(fieldname): |
table = self.store.Table() |
211 |
|
if (fieldname is not None |
212 |
|
and (not table.HasColumn(fieldname) |
213 |
|
or table.Column(fieldname).type != fieldtype)): |
214 |
self.SetClassification(None) |
self.SetClassification(None) |
215 |
self.changed(LAYER_CHANGED, self) |
self.changed(LAYER_CHANGED, self) |
216 |
|
|
318 |
|
|
319 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
320 |
|
|
321 |
def GetProjection(self): |
def HasClassification(self): |
322 |
return self.projection |
return True |
|
|
|
|
def SetProjection(self, projection): |
|
|
"""Set the layer's projection""" |
|
|
self.projection = projection |
|
|
self.changed(LAYER_PROJECTION_CHANGED, self) |
|
323 |
|
|
324 |
def GetClassification(self): |
def GetClassification(self): |
325 |
return self.__classification |
return self.__classification |
358 |
def TreeInfo(self): |
def TreeInfo(self): |
359 |
items = [] |
items = [] |
360 |
|
|
361 |
|
if hasattr(self, 'filename'): |
362 |
|
items.append(_("Filename: %s") % self.filename) |
363 |
|
|
364 |
if self.Visible(): |
if self.Visible(): |
365 |
items.append(_("Shown")) |
items.append(_("Shown")) |
366 |
else: |
else: |
383 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
384 |
|
|
385 |
|
|
386 |
|
class RasterLayer(BaseLayer): |
387 |
|
|
388 |
|
def __init__(self, title, filename, projection = None, visible = True): |
389 |
|
"""Initialize the Raster Layer. |
390 |
|
|
391 |
|
title -- title for the layer. |
392 |
|
|
393 |
|
filename -- file name of the source image. |
394 |
|
|
395 |
|
projection -- Projection object describing the projection which |
396 |
|
the source image is in. |
397 |
|
|
398 |
|
visible -- True is the layer should initially be visible. |
399 |
|
|
400 |
|
Throws IOError if the filename is invalid or points to a file that |
401 |
|
is not in a format GDAL can use. |
402 |
|
""" |
403 |
|
|
404 |
|
BaseLayer.__init__(self, title, visible = visible) |
405 |
|
|
406 |
|
self.projection = projection |
407 |
|
self.filename = filename |
408 |
|
|
409 |
|
self.bbox = -1 |
410 |
|
|
411 |
|
# |
412 |
|
# temporarily open the file so that GDAL can test if it's valid. |
413 |
|
# |
414 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
415 |
|
|
416 |
|
if dataset is None: |
417 |
|
raise IOError() |
418 |
|
|
419 |
|
self.UnsetModified() |
420 |
|
|
421 |
|
def BoundingBox(self): |
422 |
|
"""Return the layer's bounding box in the intrinsic coordinate system. |
423 |
|
|
424 |
|
If the layer has no shapes, return None. |
425 |
|
""" |
426 |
|
if self.bbox == -1: |
427 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
428 |
|
if dataset is None: |
429 |
|
self.bbox = None |
430 |
|
else: |
431 |
|
geotransform = dataset.GetGeoTransform() |
432 |
|
if geotransform is None: |
433 |
|
return None |
434 |
|
|
435 |
|
x = 0 |
436 |
|
y = dataset.RasterYSize |
437 |
|
left = geotransform[0] + \ |
438 |
|
geotransform[1] * x + \ |
439 |
|
geotransform[2] * y |
440 |
|
|
441 |
|
bottom = geotransform[3] + \ |
442 |
|
geotransform[4] * x + \ |
443 |
|
geotransform[5] * y |
444 |
|
|
445 |
|
x = dataset.RasterXSize |
446 |
|
y = 0 |
447 |
|
right = geotransform[0] + \ |
448 |
|
geotransform[1] * x + \ |
449 |
|
geotransform[2] * y |
450 |
|
|
451 |
|
top = geotransform[3] + \ |
452 |
|
geotransform[4] * x + \ |
453 |
|
geotransform[5] * y |
454 |
|
|
455 |
|
self.bbox = (left, bottom, right, top) |
456 |
|
|
457 |
|
return self.bbox |
458 |
|
|
459 |
|
def LatLongBoundingBox(self): |
460 |
|
bbox = self.BoundingBox() |
461 |
|
if bbox is None: |
462 |
|
return None |
463 |
|
|
464 |
|
llx, lly, urx, ury = bbox |
465 |
|
if self.projection is not None: |
466 |
|
llx, lly = self.projection.Inverse(llx, lly) |
467 |
|
urx, ury = self.projection.Inverse(urx, ury) |
468 |
|
|
469 |
|
return llx, lly, urx, ury |
470 |
|
|
471 |
|
def GetImageFilename(self): |
472 |
|
return self.filename |
473 |
|
|
474 |
|
def TreeInfo(self): |
475 |
|
items = [] |
476 |
|
|
477 |
|
if self.Visible(): |
478 |
|
items.append(_("Shown")) |
479 |
|
else: |
480 |
|
items.append(_("Hidden")) |
481 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
482 |
|
|
483 |
|
bbox = self.LatLongBoundingBox() |
484 |
|
if bbox is not None: |
485 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
486 |
|
else: |
487 |
|
items.append(_("Extent (lat-lon):")) |
488 |
|
|
489 |
|
if self.projection and len(self.projection.params) > 0: |
490 |
|
items.append((_("Projection"), |
491 |
|
[str(param) for param in self.projection.params])) |
492 |
|
|
493 |
|
return (_("Layer '%s'") % self.Title(), items) |
494 |
|
|