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 |
|
|
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 = 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 |
201 |
if self.__classification is not None: |
if self.__classification is not None: |
202 |
fieldname = self.__classification.GetField() |
fieldname = self.__classification.GetField() |
203 |
if fieldname is not None and \ |
if fieldname is not None and \ |
204 |
not self.store.Table().field_info_by_name(fieldname): |
not self.store.Table().HasColumn(fieldname): |
205 |
self.SetClassification(None) |
self.SetClassification(None) |
206 |
self.changed(LAYER_CHANGED, self) |
self.changed(LAYER_CHANGED, self) |
207 |
|
|
234 |
else: |
else: |
235 |
return None |
return None |
236 |
|
|
237 |
def GetFieldType(self, fieldName): |
def ShapesBoundingBox(self, shapes): |
238 |
info = self.table.field_info_by_name(fieldName) |
"""Return a bounding box in lat/long coordinates for the given |
239 |
if info is not None: |
list of shape ids. |
240 |
return info[0] |
|
241 |
|
If shapes is None or empty, return None. |
242 |
|
""" |
243 |
|
|
244 |
|
if shapes is None or len(shapes) == 0: return None |
245 |
|
|
246 |
|
llx = [] |
247 |
|
lly = [] |
248 |
|
urx = [] |
249 |
|
ury = [] |
250 |
|
|
251 |
|
if self.projection is not None: |
252 |
|
inverse = lambda x, y: self.projection.Inverse(x, y) |
253 |
else: |
else: |
254 |
return None |
inverse = lambda x, y: (x, y) |
255 |
|
|
256 |
|
for id in shapes: |
257 |
|
left, bottom, right, top = self.Shape(id).compute_bbox() |
258 |
|
|
259 |
|
left, bottom = inverse(left, bottom) |
260 |
|
right, top = inverse(right, top) |
261 |
|
|
262 |
|
llx.append(left) |
263 |
|
lly.append(bottom) |
264 |
|
urx.append(right) |
265 |
|
ury.append(top) |
266 |
|
|
267 |
|
return (min(llx), min(lly), max(urx), max(ury)) |
268 |
|
|
269 |
|
def GetFieldType(self, fieldName): |
270 |
|
if self.table.HasColumn(fieldName): |
271 |
|
return self.table.Column(fieldName).type |
272 |
|
return None |
273 |
|
|
274 |
def NumShapes(self): |
def NumShapes(self): |
275 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
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 |
|
Throws IOError if the filename is invalid or points to a file that |
389 |
|
is not in a format GDAL can use. |
390 |
|
""" |
391 |
|
|
392 |
|
BaseLayer.__init__(self, title, visible = visible) |
393 |
|
|
394 |
|
self.projection = projection |
395 |
|
self.filename = filename |
396 |
|
|
397 |
|
self.bbox = -1 |
398 |
|
|
399 |
|
# |
400 |
|
# temporarily open the file so that GDAL can test if it's valid. |
401 |
|
# |
402 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
403 |
|
|
404 |
|
if dataset is None: |
405 |
|
raise IOError() |
406 |
|
|
407 |
|
self.UnsetModified() |
408 |
|
|
409 |
|
def BoundingBox(self): |
410 |
|
"""Return the layer's bounding box in the intrinsic coordinate system. |
411 |
|
|
412 |
|
If the layer has no shapes, return None. |
413 |
|
""" |
414 |
|
if self.bbox == -1: |
415 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
416 |
|
if dataset is None: |
417 |
|
self.bbox = None |
418 |
|
else: |
419 |
|
geotransform = dataset.GetGeoTransform() |
420 |
|
if geotransform is None: |
421 |
|
return None |
422 |
|
|
423 |
|
x = 0 |
424 |
|
y = dataset.RasterYSize |
425 |
|
left = geotransform[0] + \ |
426 |
|
geotransform[1] * x + \ |
427 |
|
geotransform[2] * y |
428 |
|
|
429 |
|
bottom = geotransform[3] + \ |
430 |
|
geotransform[4] * x + \ |
431 |
|
geotransform[5] * y |
432 |
|
|
433 |
|
x = dataset.RasterXSize |
434 |
|
y = 0 |
435 |
|
right = geotransform[0] + \ |
436 |
|
geotransform[1] * x + \ |
437 |
|
geotransform[2] * y |
438 |
|
|
439 |
|
top = geotransform[3] + \ |
440 |
|
geotransform[4] * x + \ |
441 |
|
geotransform[5] * y |
442 |
|
|
443 |
|
self.bbox = (left, bottom, right, top) |
444 |
|
|
445 |
|
return self.bbox |
446 |
|
|
447 |
|
def LatLongBoundingBox(self): |
448 |
|
bbox = self.BoundingBox() |
449 |
|
if bbox is None: |
450 |
|
return None |
451 |
|
|
452 |
|
llx, lly, urx, ury = bbox |
453 |
|
if self.projection is not None: |
454 |
|
llx, lly = self.projection.Inverse(llx, lly) |
455 |
|
urx, ury = self.projection.Inverse(urx, ury) |
456 |
|
|
457 |
|
return llx, lly, urx, ury |
458 |
|
|
459 |
|
def GetImageFilename(self): |
460 |
|
return self.filename |
461 |
|
|
462 |
|
def TreeInfo(self): |
463 |
|
items = [] |
464 |
|
|
465 |
|
if self.Visible(): |
466 |
|
items.append(_("Shown")) |
467 |
|
else: |
468 |
|
items.append(_("Hidden")) |
469 |
|
items.append(_("Shapes: %d") % self.NumShapes()) |
470 |
|
|
471 |
|
bbox = self.LatLongBoundingBox() |
472 |
|
if bbox is not None: |
473 |
|
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox) |
474 |
|
else: |
475 |
|
items.append(_("Extent (lat-lon):")) |
476 |
|
|
477 |
|
if self.projection and len(self.projection.params) > 0: |
478 |
|
items.append((_("Projection"), |
479 |
|
[str(param) for param in self.projection.params])) |
480 |
|
|
481 |
|
return (_("Layer '%s'") % self.Title(), items) |
482 |
|
|