/[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 701 by bh, Thu Apr 17 16:18:48 2003 UTC revision 1452 by bh, Fri Jul 18 12:57:59 2003 UTC
# Line 8  Line 8 
8    
9  __version__ = "$Revision$"  __version__ = "$Revision$"
10    
 import os  
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    
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, CLASS_CHANGED
   
 from color import Color  
19    
20  import classification  import classification
21    
22  from table import Table  from color import Transparent, Black
   
23  from base import TitledObject, Modifiable  from base import TitledObject, Modifiable
24    
25    import resource
26    
27    
28  class Shape:  class Shape:
29    
30      """Represent one shape"""      """Represent one shape"""
# Line 33  class Shape: Line 32  class Shape:
32      def __init__(self, points):      def __init__(self, points):
33          self.points = points          self.points = points
34          #self.compute_bbox()          #self.compute_bbox()
35            self.bbox = None
36    
37      def compute_bbox(self):      def compute_bbox(self):
38            if self.bbox is not None:
39                return self.bbox
40    
41          xs = []          xs = []
42          ys = []          ys = []
43          for x, y in self.points:          for x, y in self.points:
# Line 45  class Shape: Line 48  class Shape:
48          self.urx = max(xs)          self.urx = max(xs)
49          self.ury = max(ys)          self.ury = max(ys)
50    
51            self.bbox = (self.llx, self.lly, self.urx, self.ury)
52    
53            return self.bbox
54    
55      def Points(self):      def Points(self):
56          return self.points          return self.points
57    
# Line 68  class BaseLayer(TitledObject, Modifiable Line 75  class BaseLayer(TitledObject, Modifiable
75    
76      """Base class for the layers."""      """Base class for the layers."""
77    
78      def __init__(self, title, visible = 1):      def __init__(self, title, visible = True, projection = None):
79          """Initialize the layer.          """Initialize the layer.
80    
81          title -- the title          title -- the title
# Line 77  class BaseLayer(TitledObject, Modifiable Line 84  class BaseLayer(TitledObject, Modifiable
84          TitledObject.__init__(self, title)          TitledObject.__init__(self, title)
85          Modifiable.__init__(self)          Modifiable.__init__(self)
86          self.visible = visible          self.visible = visible
87            self.projection = projection
88    
89      def Visible(self):      def Visible(self):
90          """Return true if layer is visible"""          """Return true if layer is visible"""
# Line 87  class BaseLayer(TitledObject, Modifiable Line 95  class BaseLayer(TitledObject, Modifiable
95          self.visible = visible          self.visible = visible
96          self.issue(LAYER_VISIBILITY_CHANGED, self)          self.issue(LAYER_VISIBILITY_CHANGED, self)
97    
98        def HasClassification(self):
99            """Determine if this layer support classifications."""
100            return False
101    
102        def HasShapes(self):
103            """Determine if this layer supports shapes."""
104            return False
105    
106        def GetProjection(self):
107            """Return the layer's projection."""
108            return self.projection
109    
110        def SetProjection(self, projection):
111            """Set the layer's projection"""
112            self.projection = projection
113            self.changed(LAYER_PROJECTION_CHANGED, self)
114    
115  class Layer(BaseLayer):  class Layer(BaseLayer):
116    
# Line 95  class Layer(BaseLayer): Line 119  class Layer(BaseLayer):
119      All children of the layer have the same type.      All children of the layer have the same type.
120    
121      A layer has fill and stroke colors. Colors should be instances of      A layer has fill and stroke colors. Colors should be instances of
122      Color. They can also be None, indicating no fill or no stroke.      Color. They can also be Transparent, indicating no fill or no stroke.
123    
124      The layer objects send the following events, all of which have the      The layer objects send the following events, all of which have the
125      layer object as parameter:      layer object as parameter:
# Line 104  class Layer(BaseLayer): Line 128  class Layer(BaseLayer):
128          LAYER_PROJECTION_CHANGED -- the projection has changed.          LAYER_PROJECTION_CHANGED -- the projection has changed.
129      """      """
130    
131      def __init__(self, title, filename, projection = None,      def __init__(self, title, data, projection = None,
132                   fill = Color.Transparent,                   fill = Transparent,
133                   stroke = Color.Black,                   stroke = Black,
134                   lineWidth = 1,                   lineWidth = 1,
135                   visible = 1):                   visible = True):
136          """Initialize the layer.          """Initialize the layer.
137    
138          title -- the title          title -- the title
139          filename -- the name of the shapefile          data -- datastore object for the shape data shown by the layer
140          projection -- the projection object. Its Inverse method is          projection -- the projection object. Its Inverse method is
141                 assumed to map the layer's coordinates to lat/long                 assumed to map the layer's coordinates to lat/long
142                 coordinates                 coordinates
143          fill -- the fill color or Color.Transparent if the shapes are          fill -- the fill color or Transparent if the shapes are
144                  not filled                  not filled
145          stroke -- the stroke color or Color.Transparent if the shapes          stroke -- the stroke color or Transparent if the shapes
146                  are not stroked                  are not stroked
147          visible -- boolean. If true the layer is visible.          visible -- boolean. If true the layer is visible.
148    
149          colors are expected to be instances of Color class          colors are expected to be instances of Color class
150          """          """
151          BaseLayer.__init__(self, title, visible = visible)          BaseLayer.__init__(self, title,
152                                     visible = visible,
153          # Make the filename absolute. The filename will be                                   projection = projection)
         # interpreted relative to that anyway, but when saving a  
         # session we need to compare absolute paths and it's usually  
         # safer to always work with absolute paths.  
         self.filename = os.path.abspath(filename)  
154    
155          self.projection = projection          self.__classification = None
156          self.shapefile = None          self.store = None
         self.shapetree = None  
         self.open_shapefile()  
         # shapetable is the table associated with the shapefile, while  
         # table is the default table used to look up attributes for  
         # display  
         self.shapetable = Table(filename)  
         self.table = self.shapetable  
   
         #  
         # this is really important so that when the classification class  
         # tries to set its parent layer the variable will exist  
         #  
         self.__classification = None  
         self.__setClassLock = False  
157    
158            self.SetShapeStore(data)
159    
160            self.classification_column = None
161            self.SetClassificationColumn(None)
162          self.SetClassification(None)          self.SetClassification(None)
163    
164          self.__classification.SetDefaultLineColor(stroke)          self.__classification.SetDefaultLineColor(stroke)
165          self.__classification.SetDefaultLineWidth(lineWidth)          self.__classification.SetDefaultLineWidth(lineWidth)
166          self.__classification.SetDefaultFill(fill)          self.__classification.SetDefaultFill(fill)
         self.__classification.SetLayer(self)  
167    
168          self.UnsetModified()          self.UnsetModified()
169    
170      def open_shapefile(self):      def __getattr__(self, attr):
171          if self.shapefile is None:          """Access to some attributes for backwards compatibility
             self.shapefile = shapelib.ShapeFile(self.filename)  
             numshapes, shapetype, mins, maxs = self.shapefile.info()  
             self.numshapes = numshapes  
             self.shapetype = shapelib_shapetypes[shapetype]  
   
             # if there are shapes, set the bbox accordinly. Otherwise  
             # set it to None.  
             if self.numshapes:  
                 self.bbox = mins[:2] + maxs[:2]  
             else:  
                 self.bbox = None  
172    
173              # estimate a good depth for the quad tree. Each depth          The attributes implemented here are now held by the shapestore
174              # multiplies the number of nodes by four, therefore we          if at all. For backwards compatibility pretend that they are
175              # basically take the base 4 logarithm of the number of          still there but issue a DeprecationWarning when they are
176              # shapes.          accessed.
177              if self.numshapes < 4:          """
178                  maxdepth = 1          if attr in ("table", "shapetable"):
179              else:              value = self.store.Table()
180                  maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))          elif attr == "shapefile":
181                value = self.store.Shapefile()
182            elif attr == "filename":
183                value = self.store.FileName()
184            else:
185                raise AttributeError, attr
186            warnings.warn("The Layer attribute %r is deprecated."
187                          " It's value can be accessed through the shapestore"
188                          % attr, DeprecationWarning, stacklevel = 2)
189            return value
190    
191        def SetShapeStore(self, store):
192            # Set the classification to None if there is a classification
193            # and the new shapestore doesn't have a table with a suitable
194            # column, i.e one with the same name and type as before
195            # FIXME: Maybe we should keep it the same if the type is
196            # compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT
197            if self.__classification is not None:
198                columnname = self.classification_column
199                columntype = self.GetFieldType(columnname)
200                table = store.Table()
201                if (columnname is not None
202                    and (not table.HasColumn(columnname)
203                         or table.Column(columnname).type != columntype)):
204                    self.SetClassification(None)
205    
206            self.store = store
207            shapefile = self.store.Shapefile()
208    
209            numshapes, shapetype, mins, maxs = shapefile.info()
210            self.numshapes = numshapes
211            self.shapetype = shapelib_shapetypes[shapetype]
212    
213            # if there are shapes, set the bbox accordingly. Otherwise
214            # set it to None.
215            if self.numshapes:
216                self.bbox = mins[:2] + maxs[:2]
217            else:
218                self.bbox = None
219    
220            # estimate a good depth for the quad tree. Each depth
221            # multiplies the number of nodes by four, therefore we
222            # basically take the base 4 logarithm of the number of
223            # shapes.
224            if self.numshapes < 4:
225                maxdepth = 1
226            else:
227                maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))
228    
229              self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2,          self.shapetree = shptree.SHPTree(shapefile.cobject(), 2,
230                                               maxdepth)                                           maxdepth)
231            self.changed(LAYER_SHAPESTORE_REPLACED, self)
232    
233        def ShapeStore(self):
234            return self.store
235    
236      def Destroy(self):      def Destroy(self):
237          BaseLayer.Destroy(self)          BaseLayer.Destroy(self)
238          if self.shapefile is not None:          if self.__classification is not None:
239              self.shapefile.close()              self.__classification.Unsubscribe(CLASS_CHANGED,
240              self.shapefile = None                                                self._classification_changed)
             self.shapetree = None  
         self.SetClassification(None)  
         self.table.Destroy()  
241    
242      def BoundingBox(self):      def BoundingBox(self):
243          """Return the layer's bounding box in the intrinsic coordinate system.          """Return the layer's bounding box in the intrinsic coordinate system.
# Line 216  class Layer(BaseLayer): Line 261  class Layer(BaseLayer):
261          else:          else:
262              return None              return None
263    
264      def GetFieldType(self, fieldName):      def ShapesBoundingBox(self, shapes):
265          info = self.table.field_info_by_name(fieldName)          """Return a bounding box in lat/long coordinates for the given
266          if info is not None:          list of shape ids.
267              return info[0]  
268            If shapes is None or empty, return None.
269            """
270    
271            if shapes is None or len(shapes) == 0: return None
272    
273            llx = []
274            lly = []
275            urx = []
276            ury = []
277    
278            if self.projection is not None:
279                inverse = lambda x, y: self.projection.Inverse(x, y)
280          else:          else:
281              return None              inverse = lambda x, y: (x, y)
282    
283            for id in shapes:
284                left, bottom, right, top = self.Shape(id).compute_bbox()
285    
286                left, bottom = inverse(left, bottom)
287                right, top   = inverse(right, top)
288    
289                llx.append(left)
290                lly.append(bottom)
291                urx.append(right)
292                ury.append(top)
293    
294            return (min(llx), min(lly), max(urx), max(ury))
295    
296        def GetFieldType(self, fieldName):
297            if self.store:
298                table = self.store.Table()
299                if table.HasColumn(fieldName):
300                    return table.Column(fieldName).type
301            return None
302    
303        def HasShapes(self):
304            return True
305    
306      def NumShapes(self):      def NumShapes(self):
307          """Return the number of shapes in the layer"""          """Return the number of shapes in the layer"""
# Line 235  class Layer(BaseLayer): Line 315  class Layer(BaseLayer):
315    
316      def Shape(self, index):      def Shape(self, index):
317          """Return the shape with index index"""          """Return the shape with index index"""
318          shape = self.shapefile.read_object(index)          shape = self.store.Shapefile().read_object(index)
319    
320          if self.shapetype == SHAPETYPE_POINT:          if self.shapetype == SHAPETYPE_POINT:
321              points = shape.vertices()              points = shape.vertices()
# Line 251  class Layer(BaseLayer): Line 331  class Layer(BaseLayer):
331      def ShapesInRegion(self, box):      def ShapesInRegion(self, box):
332          """Return the ids of the shapes that overlap the box.          """Return the ids of the shapes that overlap the box.
333    
334          Box is a tuple (left, bottom, right, top) in the coordinate          Box is a tuple (left, bottom, right, top) in unprojected coordinates.
         system used by the layer's shapefile.  
335          """          """
336          left, bottom, right, top = box          left, bottom, right, top = box
337    
338            if self.projection is not None:
339                left,  bottom = self.projection.Forward(left, bottom)
340                right, top    = self.projection.Forward(right, top)
341    
342          return self.shapetree.find_shapes((left, bottom), (right, top))          return self.shapetree.find_shapes((left, bottom), (right, top))
343    
344      def SetProjection(self, projection):      def GetClassificationColumn(self):
345          """Set the layer's projection"""          return self.classification_column
346          self.projection = projection  
347          self.changed(LAYER_PROJECTION_CHANGED, self)      def SetClassificationColumn(self, column):
348            """Set the column to classifiy on, or None. If column is not None
349            and the column does not exist in the table, raise a ValueError.
350            """
351            if column:
352                columnType = self.GetFieldType(column)
353                if columnType is None:
354                    raise ValueError()
355            changed = self.classification_column != column
356            self.classification_column = column
357            if changed:
358                self.changed(LAYER_CHANGED, self)
359    
360        def HasClassification(self):
361            return True
362    
363      def GetClassification(self):      def GetClassification(self):
364          return self.__classification          return self.__classification
365    
366      def SetClassification(self, clazz):      def SetClassification(self, clazz):
367          """Set the classification to 'clazz'          """Set the classification used by this layer to 'clazz'
368    
369          If 'clazz' is None a default classification is created          If 'clazz' is None a default classification is created.
         """  
370    
371          # prevent infinite recursion when calling SetLayer()          This issues a LAYER_CHANGED event.
372          if self.__setClassLock: return          """
373    
374          self.__setClassLock = True          if self.__classification is not None:
375                self.__classification.Unsubscribe(CLASS_CHANGED,
376                                                  self._classification_changed)
377    
378          if clazz is None:          if clazz is None:
379              if self.__classification is not None:              clazz = classification.Classification()
                 self.__classification.SetLayer(None)  
             self.__classification = classification.Classification()  
         else:  
             self.__classification = clazz  
             try:  
                 self.__classification.SetLayer(self)  
             except ValueError:  
                 self.__setClassLock = False  
                 raise ValueError  
380    
381          self.changed(LAYER_CHANGED, self)          self.__classification = clazz
382            self.__classification.Subscribe(CLASS_CHANGED,
383                                            self._classification_changed)
384    
385          self.__setClassLock = False          self._classification_changed()
386    
387      def ClassChanged(self):      def _classification_changed(self):
388          """Called from the classification object when it has changed."""          """Called from the classification object when it has changed."""
389          self.changed(LAYER_CHANGED, self)          self.changed(LAYER_CHANGED, self)
390    
391      def TreeInfo(self):      def TreeInfo(self):
392          items = []          items = []
393    
394            items.append(_("Filename: %s") % self.ShapeStore().FileName())
395    
396          if self.Visible():          if self.Visible():
397              items.append(_("Shown"))              items.append(_("Shown"))
398          else:          else:
# Line 312  class Layer(BaseLayer): Line 406  class Layer(BaseLayer):
406              items.append(_("Extent (lat-lon):"))              items.append(_("Extent (lat-lon):"))
407          items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()])          items.append(_("Shapetype: %s") % shapetype_names[self.ShapeType()])
408    
409            if self.projection and len(self.projection.params) > 0:
410                items.append((_("Projection"),
411                            [str(param) for param in self.projection.params]))
412    
413          items.append(self.__classification)          items.append(self.__classification)
414    
415          return (_("Layer '%s'") % self.Title(), items)          return (_("Layer '%s'") % self.Title(), items)
416    
417    
418    if resource.has_gdal_support():
419        import gdal
420        from gdalconst import GA_ReadOnly
421    
422    class RasterLayer(BaseLayer):
423    
424        def __init__(self, title, filename, projection = None, visible = True):
425            """Initialize the Raster Layer.
426    
427            title -- title for the layer.
428    
429            filename -- file name of the source image.
430    
431            projection -- Projection object describing the projection which
432                          the source image is in.
433    
434            visible -- True is the layer should initially be visible.
435    
436            Throws IOError if the filename is invalid or points to a file that
437            is not in a format GDAL can use.
438            """
439    
440            BaseLayer.__init__(self, title, visible = visible)
441    
442            self.projection = projection
443            self.filename = filename
444    
445            self.bbox = -1
446    
447            if resource.has_gdal_support():
448                #
449                # temporarily open the file so that GDAL can test if it's valid.
450                #
451                dataset = gdal.Open(self.filename, GA_ReadOnly)
452    
453                if dataset is None:
454                    raise IOError()
455    
456            self.UnsetModified()
457    
458        def BoundingBox(self):
459            """Return the layer's bounding box in the intrinsic coordinate system.
460    
461            If the there is no support for images, or the file cannot
462            be read, or there is no geographics information available, return None.
463            """
464            if not resource.has_gdal_support():
465                return None
466    
467            if self.bbox == -1:
468                dataset = gdal.Open(self.filename, GA_ReadOnly)
469                if dataset is None:
470                    self.bbox = None
471                else:
472                    geotransform = dataset.GetGeoTransform()
473                    if geotransform is None:
474                        return None
475    
476                    x = 0
477                    y = dataset.RasterYSize
478                    left = geotransform[0] +        \
479                           geotransform[1] * x +    \
480                           geotransform[2] * y
481    
482                    bottom = geotransform[3] +      \
483                             geotransform[4] * x +  \
484                             geotransform[5] * y
485    
486                    x = dataset.RasterXSize
487                    y = 0
488                    right = geotransform[0] +       \
489                            geotransform[1] * x +   \
490                            geotransform[2] * y
491    
492                    top = geotransform[3] +         \
493                          geotransform[4] * x +     \
494                          geotransform[5] * y
495    
496                    self.bbox = (left, bottom, right, top)
497    
498            return self.bbox
499    
500        def LatLongBoundingBox(self):
501            bbox = self.BoundingBox()
502            if bbox is None:
503                return None
504    
505            llx, lly, urx, ury = bbox
506            if self.projection is not None:
507                llx, lly = self.projection.Inverse(llx, lly)
508                urx, ury = self.projection.Inverse(urx, ury)
509    
510            return llx, lly, urx, ury
511    
512        def GetImageFilename(self):
513            return self.filename
514    
515        def TreeInfo(self):
516            items = []
517    
518            items.append(_("Filename: %s") % self.GetImageFilename())
519    
520            if self.Visible():
521                items.append(_("Shown"))
522            else:
523                items.append(_("Hidden"))
524    
525            bbox = self.LatLongBoundingBox()
526            if bbox is not None:
527                items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") % bbox)
528            else:
529                items.append(_("Extent (lat-lon):"))
530    
531            if self.projection and len(self.projection.params) > 0:
532                items.append((_("Projection"),
533                            [str(param) for param in self.projection.params]))
534    
535            return (_("Layer '%s'") % self.Title(), items)
536    

Legend:
Removed from v.701  
changed lines
  Added in v.1452

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26