/[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 722 by bh, Thu Apr 17 16:18:48 2003 UTC revision 723 by bh, Thu Apr 24 15:31:53 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    
13  from Thuban import _  from Thuban import _
# Line 22  from color import Color Line 21  from color import Color
21    
22  import classification  import classification
23    
 from table import Table  
   
24  from base import TitledObject, Modifiable  from base import TitledObject, Modifiable
25    
26    
27  class Shape:  class Shape:
28    
29      """Represent one shape"""      """Represent one shape"""
# Line 104  class Layer(BaseLayer): Line 102  class Layer(BaseLayer):
102          LAYER_PROJECTION_CHANGED -- the projection has changed.          LAYER_PROJECTION_CHANGED -- the projection has changed.
103      """      """
104    
105      def __init__(self, title, filename, projection = None,      def __init__(self, title, data, projection = None,
106                   fill = Color.Transparent,                   fill = Color.Transparent,
107                   stroke = Color.Black,                   stroke = Color.Black,
108                   lineWidth = 1,                   lineWidth = 1,
# Line 112  class Layer(BaseLayer): Line 110  class Layer(BaseLayer):
110          """Initialize the layer.          """Initialize the layer.
111    
112          title -- the title          title -- the title
113          filename -- the name of the shapefile          data -- datastore object for the shape data shown by the layer
114          projection -- the projection object. Its Inverse method is          projection -- the projection object. Its Inverse method is
115                 assumed to map the layer's coordinates to lat/long                 assumed to map the layer's coordinates to lat/long
116                 coordinates                 coordinates
# Line 126  class Layer(BaseLayer): Line 124  class Layer(BaseLayer):
124          """          """
125          BaseLayer.__init__(self, title, visible = visible)          BaseLayer.__init__(self, title, visible = visible)
126    
         # Make the filename absolute. The filename will be  
         # 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)  
   
127          self.projection = projection          self.projection = projection
         self.shapefile = 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  
128    
129          #          #
130          # this is really important so that when the classification class          # this is really important so that when the classification class
131          # tries to set its parent layer the variable will exist          # tries to set its parent layer the variable will exist
132          #          #
133          self.__classification = None          self.__classification = None
134          self.__setClassLock = False          self.__setClassLock = False
135    
136            self.SetShapeStore(data)
137    
138          self.SetClassification(None)          self.SetClassification(None)
139    
# Line 159  class Layer(BaseLayer): Line 144  class Layer(BaseLayer):
144    
145          self.UnsetModified()          self.UnsetModified()
146    
     def open_shapefile(self):  
         if self.shapefile is None:  
             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  
   
             # estimate a good depth for the quad tree. Each depth  
             # multiplies the number of nodes by four, therefore we  
             # basically take the base 4 logarithm of the number of  
             # shapes.  
             if self.numshapes < 4:  
                 maxdepth = 1  
             else:  
                 maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))  
147    
148              self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2,      def SetShapeStore(self, store):
149                                               maxdepth)          self.store = store
150            self.shapefile = self.store.Shapefile()
151            self.shapetable = self.store.Table()
152            self.filename = self.store.filename
153            self.table = self.shapetable
154    
155            numshapes, shapetype, mins, maxs = self.shapefile.info()
156            self.numshapes = numshapes
157            self.shapetype = shapelib_shapetypes[shapetype]
158    
159            # if there are shapes, set the bbox accordingly. Otherwise
160            # set it to None.
161            if self.numshapes:
162                self.bbox = mins[:2] + maxs[:2]
163            else:
164                self.bbox = None
165    
166            # estimate a good depth for the quad tree. Each depth
167            # multiplies the number of nodes by four, therefore we
168            # basically take the base 4 logarithm of the number of
169            # shapes.
170            if self.numshapes < 4:
171                maxdepth = 1
172            else:
173                maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))
174    
175            self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2,
176                                             maxdepth)
177            if self.__classification is not None:
178                fieldname = self.__classification.GetField()
179                if not self.store.Table().field_info_by_name(fieldname):
180                    self.SetClassification(None)
181            self.changed(LAYER_CHANGED, self)
182    
183        def ShapeStore(self):
184            return self.store
185    
186      def Destroy(self):      def Destroy(self):
187          BaseLayer.Destroy(self)          BaseLayer.Destroy(self)
         if self.shapefile is not None:  
             self.shapefile.close()  
             self.shapefile = None  
             self.shapetree = None  
188          self.SetClassification(None)          self.SetClassification(None)
         self.table.Destroy()  
189    
190      def BoundingBox(self):      def BoundingBox(self):
191          """Return the layer's bounding box in the intrinsic coordinate system.          """Return the layer's bounding box in the intrinsic coordinate system.

Legend:
Removed from v.722  
changed lines
  Added in v.723

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26