9 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
10 |
|
|
11 |
from math import log, ceil |
from math import log, ceil |
12 |
|
import warnings |
13 |
|
|
14 |
from Thuban import _ |
from Thuban import _ |
|
|
|
15 |
import shapelib, shptree |
import shapelib, shptree |
16 |
|
|
|
import gdal |
|
|
from gdalconst import GA_ReadOnly |
|
|
|
|
17 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
18 |
LAYER_CHANGED |
LAYER_CHANGED, LAYER_SHAPESTORE_REPLACED |
|
|
|
|
from color import Color |
|
19 |
|
|
20 |
import classification |
import classification |
21 |
|
|
22 |
|
from color import Color |
23 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
24 |
|
|
25 |
|
import resource |
26 |
|
|
27 |
|
|
28 |
class Shape: |
class Shape: |
29 |
|
|
99 |
"""Determine if this layer support classifications.""" |
"""Determine if this layer support classifications.""" |
100 |
return False |
return False |
101 |
|
|
102 |
|
def HasShapes(self): |
103 |
|
"""Determine if this layer supports shapes.""" |
104 |
|
return False |
105 |
|
|
106 |
def GetProjection(self): |
def GetProjection(self): |
107 |
"""Return the layer's projection.""" |
"""Return the layer's projection.""" |
108 |
return self.projection |
return self.projection |
170 |
|
|
171 |
self.UnsetModified() |
self.UnsetModified() |
172 |
|
|
173 |
|
def __getattr__(self, attr): |
174 |
|
"""Access to some attributes for backwards compatibility |
175 |
|
|
176 |
|
The attributes implemented here are now held by the shapestore |
177 |
|
if at all. For backwards compatibility pretend that they are |
178 |
|
still there but issue a DeprecationWarning when they are |
179 |
|
accessed. |
180 |
|
""" |
181 |
|
if attr in ("table", "shapetable"): |
182 |
|
value = self.store.Table() |
183 |
|
elif attr == "shapefile": |
184 |
|
value = self.store.Shapefile() |
185 |
|
elif attr == "filename": |
186 |
|
value = self.store.FileName() |
187 |
|
else: |
188 |
|
raise AttributeError, attr |
189 |
|
warnings.warn("The Layer attribute %r is deprecated." |
190 |
|
" It's value can be accessed through the shapestore" |
191 |
|
% attr, DeprecationWarning, stacklevel = 2) |
192 |
|
return value |
193 |
|
|
194 |
def SetShapeStore(self, store): |
def SetShapeStore(self, store): |
195 |
self.store = store |
self.store = store |
196 |
self.shapefile = self.store.Shapefile() |
shapefile = self.store.Shapefile() |
|
self.shapetable = self.store.Table() |
|
|
if hasattr(self.store, "FileName"): |
|
|
self.filename = self.store.FileName() |
|
|
self.table = self.shapetable |
|
197 |
|
|
198 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
numshapes, shapetype, mins, maxs = shapefile.info() |
199 |
self.numshapes = numshapes |
self.numshapes = numshapes |
200 |
self.shapetype = shapelib_shapetypes[shapetype] |
self.shapetype = shapelib_shapetypes[shapetype] |
201 |
|
|
215 |
else: |
else: |
216 |
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
217 |
|
|
218 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(shapefile.cobject(), 2, |
219 |
maxdepth) |
maxdepth) |
220 |
# Set the classification to None if there is a classification |
# Set the classification to None if there is a classification |
221 |
# and the new shapestore doesn't have a table with a suitable |
# and the new shapestore doesn't have a table with a suitable |
230 |
and (not table.HasColumn(fieldname) |
and (not table.HasColumn(fieldname) |
231 |
or table.Column(fieldname).type != fieldtype)): |
or table.Column(fieldname).type != fieldtype)): |
232 |
self.SetClassification(None) |
self.SetClassification(None) |
233 |
self.changed(LAYER_CHANGED, self) |
self.changed(LAYER_SHAPESTORE_REPLACED, self) |
234 |
|
|
235 |
def ShapeStore(self): |
def ShapeStore(self): |
236 |
return self.store |
return self.store |
294 |
return (min(llx), min(lly), max(urx), max(ury)) |
return (min(llx), min(lly), max(urx), max(ury)) |
295 |
|
|
296 |
def GetFieldType(self, fieldName): |
def GetFieldType(self, fieldName): |
297 |
if self.table.HasColumn(fieldName): |
table = self.store.Table() |
298 |
return self.table.Column(fieldName).type |
if table.HasColumn(fieldName): |
299 |
|
return table.Column(fieldName).type |
300 |
return None |
return None |
301 |
|
|
302 |
|
def HasShapes(self): |
303 |
|
return True |
304 |
|
|
305 |
def NumShapes(self): |
def NumShapes(self): |
306 |
"""Return the number of shapes in the layer""" |
"""Return the number of shapes in the layer""" |
307 |
return self.numshapes |
return self.numshapes |
314 |
|
|
315 |
def Shape(self, index): |
def Shape(self, index): |
316 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
317 |
shape = self.shapefile.read_object(index) |
shape = self.store.Shapefile().read_object(index) |
318 |
|
|
319 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
320 |
points = shape.vertices() |
points = shape.vertices() |
405 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
406 |
|
|
407 |
|
|
408 |
|
if resource.has_gdal_support(): |
409 |
|
import gdal |
410 |
|
from gdalconst import GA_ReadOnly |
411 |
|
|
412 |
class RasterLayer(BaseLayer): |
class RasterLayer(BaseLayer): |
413 |
|
|
414 |
def __init__(self, title, filename, projection = None, visible = True): |
def __init__(self, title, filename, projection = None, visible = True): |
434 |
|
|
435 |
self.bbox = -1 |
self.bbox = -1 |
436 |
|
|
437 |
# |
if resource.has_gdal_support(): |
438 |
# temporarily open the file so that GDAL can test if it's valid. |
# |
439 |
# |
# temporarily open the file so that GDAL can test if it's valid. |
440 |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
# |
441 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
442 |
|
|
443 |
if dataset is None: |
if dataset is None: |
444 |
raise IOError() |
raise IOError() |
445 |
|
|
446 |
self.UnsetModified() |
self.UnsetModified() |
447 |
|
|
450 |
|
|
451 |
If the layer has no shapes, return None. |
If the layer has no shapes, return None. |
452 |
""" |
""" |
453 |
|
if not resource.has_gdal_support(): |
454 |
|
return None |
455 |
|
|
456 |
if self.bbox == -1: |
if self.bbox == -1: |
457 |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
458 |
if dataset is None: |
if dataset is None: |