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 |
|
|
166 |
|
|
167 |
self.UnsetModified() |
self.UnsetModified() |
168 |
|
|
169 |
|
def __getattr__(self, attr): |
170 |
|
"""Access to some attributes for backwards compatibility |
171 |
|
|
172 |
|
The attributes implemented here are now held by the shapestore |
173 |
|
if at all. For backwards compatibility pretend that they are |
174 |
|
still there but issue a DeprecationWarning when they are |
175 |
|
accessed. |
176 |
|
""" |
177 |
|
if attr in ("table", "shapetable"): |
178 |
|
value = self.store.Table() |
179 |
|
elif attr == "shapefile": |
180 |
|
value = self.store.Shapefile() |
181 |
|
elif attr == "filename": |
182 |
|
value = self.store.FileName() |
183 |
|
else: |
184 |
|
raise AttributeError, attr |
185 |
|
warnings.warn("The Layer attribute %r is deprecated." |
186 |
|
" It's value can be accessed through the shapestore" |
187 |
|
% attr, DeprecationWarning, stacklevel = 2) |
188 |
|
return value |
189 |
|
|
190 |
def SetShapeStore(self, store): |
def SetShapeStore(self, store): |
191 |
self.store = store |
self.store = store |
192 |
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 |
|
193 |
|
|
194 |
numshapes, shapetype, mins, maxs = self.shapefile.info() |
numshapes, shapetype, mins, maxs = shapefile.info() |
195 |
self.numshapes = numshapes |
self.numshapes = numshapes |
196 |
self.shapetype = shapelib_shapetypes[shapetype] |
self.shapetype = shapelib_shapetypes[shapetype] |
197 |
|
|
211 |
else: |
else: |
212 |
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4))) |
213 |
|
|
214 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(shapefile.cobject(), 2, |
215 |
maxdepth) |
maxdepth) |
216 |
|
# Set the classification to None if there is a classification |
217 |
|
# and the new shapestore doesn't have a table with a suitable |
218 |
|
# column, i.e one with the same name and type as before |
219 |
|
# FIXME: Maybe we should keep it the same if the type is |
220 |
|
# compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT |
221 |
if self.__classification is not None: |
if self.__classification is not None: |
222 |
fieldname = self.__classification.GetField() |
fieldname = self.__classification.GetField() |
223 |
if fieldname is not None and \ |
fieldtype = self.__classification.GetFieldType() |
224 |
not self.store.Table().HasColumn(fieldname): |
table = self.store.Table() |
225 |
|
if (fieldname is not None |
226 |
|
and (not table.HasColumn(fieldname) |
227 |
|
or table.Column(fieldname).type != fieldtype)): |
228 |
self.SetClassification(None) |
self.SetClassification(None) |
229 |
self.changed(LAYER_CHANGED, self) |
self.changed(LAYER_SHAPESTORE_REPLACED, self) |
230 |
|
|
231 |
def ShapeStore(self): |
def ShapeStore(self): |
232 |
return self.store |
return self.store |
290 |
return (min(llx), min(lly), max(urx), max(ury)) |
return (min(llx), min(lly), max(urx), max(ury)) |
291 |
|
|
292 |
def GetFieldType(self, fieldName): |
def GetFieldType(self, fieldName): |
293 |
if self.table.HasColumn(fieldName): |
table = self.store.Table() |
294 |
return self.table.Column(fieldName).type |
if table.HasColumn(fieldName): |
295 |
|
return table.Column(fieldName).type |
296 |
return None |
return None |
297 |
|
|
298 |
def NumShapes(self): |
def NumShapes(self): |
307 |
|
|
308 |
def Shape(self, index): |
def Shape(self, index): |
309 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
310 |
shape = self.shapefile.read_object(index) |
shape = self.store.Shapefile().read_object(index) |
311 |
|
|
312 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
313 |
points = shape.vertices() |
points = shape.vertices() |
398 |
return (_("Layer '%s'") % self.Title(), items) |
return (_("Layer '%s'") % self.Title(), items) |
399 |
|
|
400 |
|
|
401 |
|
if resource.has_gdal_support(): |
402 |
|
import gdal |
403 |
|
from gdalconst import GA_ReadOnly |
404 |
|
|
405 |
class RasterLayer(BaseLayer): |
class RasterLayer(BaseLayer): |
406 |
|
|
407 |
def __init__(self, title, filename, projection = None, visible = True): |
def __init__(self, title, filename, projection = None, visible = True): |
427 |
|
|
428 |
self.bbox = -1 |
self.bbox = -1 |
429 |
|
|
430 |
# |
if resource.has_gdal_support(): |
431 |
# temporarily open the file so that GDAL can test if it's valid. |
# |
432 |
# |
# temporarily open the file so that GDAL can test if it's valid. |
433 |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
# |
434 |
|
dataset = gdal.Open(self.filename, GA_ReadOnly) |
435 |
|
|
436 |
if dataset is None: |
if dataset is None: |
437 |
raise IOError() |
raise IOError() |
438 |
|
|
439 |
self.UnsetModified() |
self.UnsetModified() |
440 |
|
|
443 |
|
|
444 |
If the layer has no shapes, return None. |
If the layer has no shapes, return None. |
445 |
""" |
""" |
446 |
|
if not resource.has_gdal_support(): |
447 |
|
return None |
448 |
|
|
449 |
if self.bbox == -1: |
if self.bbox == -1: |
450 |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
dataset = gdal.Open(self.filename, GA_ReadOnly) |
451 |
if dataset is None: |
if dataset is None: |