/[thuban]/trunk/thuban/Thuban/Model/layer.py
ViewVC logotype

Diff of /trunk/thuban/Thuban/Model/layer.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 929 by jonathan, Tue May 20 15:22:42 2003 UTC revision 1273 by jonathan, Fri Jun 20 17:45:49 2003 UTC
# Line 9  Line 9 
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    
# Line 101  class BaseLayer(TitledObject, Modifiable Line 99  class BaseLayer(TitledObject, Modifiable
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
# Line 168  class Layer(BaseLayer): Line 170  class Layer(BaseLayer):
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()  
         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    
# Line 196  class Layer(BaseLayer): Line 215  class Layer(BaseLayer):
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
221            # and the new shapestore doesn't have a table with a suitable
222            # column, i.e one with the same name and type as before
223            # FIXME: Maybe we should keep it the same if the type is
224            # compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT
225          if self.__classification is not None:          if self.__classification is not None:
226              fieldname = self.__classification.GetField()              fieldname = self.__classification.GetField()
227              if fieldname is not None and \              fieldtype = self.__classification.GetFieldType()
228                 not self.store.Table().HasColumn(fieldname):              table = self.store.Table()
229                if (fieldname is not None
230                    and (not table.HasColumn(fieldname)
231                         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
# Line 267  class Layer(BaseLayer): Line 294  class Layer(BaseLayer):
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
# Line 283  class Layer(BaseLayer): Line 314  class Layer(BaseLayer):
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()
# Line 349  class Layer(BaseLayer): Line 380  class Layer(BaseLayer):
380      def TreeInfo(self):      def TreeInfo(self):
381          items = []          items = []
382    
383            if hasattr(self, 'filename'):
384                items.append(_("Filename: %s") % self.filename)
385    
386          if self.Visible():          if self.Visible():
387              items.append(_("Shown"))              items.append(_("Shown"))
388          else:          else:
# Line 371  class Layer(BaseLayer): Line 405  class Layer(BaseLayer):
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):
# Line 384  class RasterLayer(BaseLayer): Line 422  class RasterLayer(BaseLayer):
422                        the source image is in.                        the source image is in.
423    
424          visible -- True is the layer should initially be visible.          visible -- True is the layer should initially be visible.
425    
426            Throws IOError if the filename is invalid or points to a file that
427            is not in a format GDAL can use.
428          """          """
429    
430          BaseLayer.__init__(self, title, visible = visible)          BaseLayer.__init__(self, title, visible = visible)
# Line 393  class RasterLayer(BaseLayer): Line 434  class RasterLayer(BaseLayer):
434    
435          self.bbox = -1          self.bbox = -1
436    
437          self.UnsetModified()          if resource.has_gdal_support():
438                #
439                # temporarily open the file so that GDAL can test if it's valid.
440                #
441                dataset = gdal.Open(self.filename, GA_ReadOnly)
442    
443                if dataset is None:
444                    raise IOError()
445    
446            self.UnsetModified()
447    
448      def BoundingBox(self):      def BoundingBox(self):
449          """Return the layer's bounding box in the intrinsic coordinate system.          """Return the layer's bounding box in the intrinsic coordinate system.
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:
459                  self.bbox = None                  self.bbox = None
460              else:              else:
461                  left, bottom = self.__CalcCorner(dataset,                  geotransform = dataset.GetGeoTransform()
462                                                   0, dataset.RasterYSize)                  if geotransform is None:
463                  right, top   = self.__CalcCorner(dataset,                      return None
464                                                   dataset.RasterXSize, 0)  
465                    x = 0
466                    y = dataset.RasterYSize
467                    left = geotransform[0] +        \
468                           geotransform[1] * x +    \
469                           geotransform[2] * y
470    
471                    bottom = geotransform[3] +      \
472                             geotransform[4] * x +  \
473                             geotransform[5] * y
474    
475                    x = dataset.RasterXSize
476                    y = 0
477                    right = geotransform[0] +       \
478                            geotransform[1] * x +   \
479                            geotransform[2] * y
480    
481                    top = geotransform[3] +         \
482                          geotransform[4] * x +     \
483                          geotransform[5] * y
484    
485                  self.bbox = (left, bottom, right, top)                  self.bbox = (left, bottom, right, top)
486    
487          return self.bbox          return self.bbox
488    
     def __CalcCorner(self, dataset, x, y):  
         geotransform = dataset.GetGeoTransform()  
         return geotransform[0] + geotransform[1] * x + geotransform[2] * y, \  
                geotransform[3] + geotransform[4] * x + geotransform[5] * y  
   
489      def LatLongBoundingBox(self):      def LatLongBoundingBox(self):
490          bbox = self.BoundingBox()          bbox = self.BoundingBox()
491          if bbox is not None:          if bbox is None:
             llx, lly, urx, ury = bbox  
             if self.projection is not None:  
                 llx, lly = self.projection.Inverse(llx, lly)  
                 urx, ury = self.projection.Inverse(urx, ury)  
             return llx, lly, urx, ury  
         else:  
492              return None              return None
493    
494            llx, lly, urx, ury = bbox
495            if self.projection is not None:
496                llx, lly = self.projection.Inverse(llx, lly)
497                urx, ury = self.projection.Inverse(urx, ury)
498    
499            return llx, lly, urx, ury
500    
501      def GetImageFilename(self):      def GetImageFilename(self):
502          return self.filename          return self.filename
503    

Legend:
Removed from v.929  
changed lines
  Added in v.1273

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26