/[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 1219 by bh, Mon Jun 16 17:42:54 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 168  class Layer(BaseLayer): Line 166  class Layer(BaseLayer):
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()  
         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    
# Line 196  class Layer(BaseLayer): Line 211  class Layer(BaseLayer):
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
# Line 267  class Layer(BaseLayer): Line 290  class Layer(BaseLayer):
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):
# Line 283  class Layer(BaseLayer): Line 307  class Layer(BaseLayer):
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()
# Line 349  class Layer(BaseLayer): Line 373  class Layer(BaseLayer):
373      def TreeInfo(self):      def TreeInfo(self):
374          items = []          items = []
375    
376            if hasattr(self, 'filename'):
377                items.append(_("Filename: %s") % self.filename)
378    
379          if self.Visible():          if self.Visible():
380              items.append(_("Shown"))              items.append(_("Shown"))
381          else:          else:
# Line 371  class Layer(BaseLayer): Line 398  class Layer(BaseLayer):
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):
# Line 384  class RasterLayer(BaseLayer): Line 415  class RasterLayer(BaseLayer):
415                        the source image is in.                        the source image is in.
416    
417          visible -- True is the layer should initially be visible.          visible -- True is the layer should initially be visible.
418    
419            Throws IOError if the filename is invalid or points to a file that
420            is not in a format GDAL can use.
421          """          """
422    
423          BaseLayer.__init__(self, title, visible = visible)          BaseLayer.__init__(self, title, visible = visible)
# Line 393  class RasterLayer(BaseLayer): Line 427  class RasterLayer(BaseLayer):
427    
428          self.bbox = -1          self.bbox = -1
429    
430          self.UnsetModified()          if resource.has_gdal_support():
431                #
432                # temporarily open the file so that GDAL can test if it's valid.
433                #
434                dataset = gdal.Open(self.filename, GA_ReadOnly)
435    
436                if dataset is None:
437                    raise IOError()
438    
439            self.UnsetModified()
440    
441      def BoundingBox(self):      def BoundingBox(self):
442          """Return the layer's bounding box in the intrinsic coordinate system.          """Return the layer's bounding box in the intrinsic coordinate system.
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:
452                  self.bbox = None                  self.bbox = None
453              else:              else:
454                  left, bottom = self.__CalcCorner(dataset,                  geotransform = dataset.GetGeoTransform()
455                                                   0, dataset.RasterYSize)                  if geotransform is None:
456                  right, top   = self.__CalcCorner(dataset,                      return None
457                                                   dataset.RasterXSize, 0)  
458                    x = 0
459                    y = dataset.RasterYSize
460                    left = geotransform[0] +        \
461                           geotransform[1] * x +    \
462                           geotransform[2] * y
463    
464                    bottom = geotransform[3] +      \
465                             geotransform[4] * x +  \
466                             geotransform[5] * y
467    
468                    x = dataset.RasterXSize
469                    y = 0
470                    right = geotransform[0] +       \
471                            geotransform[1] * x +   \
472                            geotransform[2] * y
473    
474                    top = geotransform[3] +         \
475                          geotransform[4] * x +     \
476                          geotransform[5] * y
477    
478                  self.bbox = (left, bottom, right, top)                  self.bbox = (left, bottom, right, top)
479    
480          return self.bbox          return self.bbox
481    
     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  
   
482      def LatLongBoundingBox(self):      def LatLongBoundingBox(self):
483          bbox = self.BoundingBox()          bbox = self.BoundingBox()
484          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:  
485              return None              return None
486    
487            llx, lly, urx, ury = bbox
488            if self.projection is not None:
489                llx, lly = self.projection.Inverse(llx, lly)
490                urx, ury = self.projection.Inverse(urx, ury)
491    
492            return llx, lly, urx, ury
493    
494      def GetImageFilename(self):      def GetImageFilename(self):
495          return self.filename          return self.filename
496    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26