/[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 21 by bh, Tue Sep 4 16:45:28 2001 UTC revision 258 by bh, Thu Aug 15 12:48:03 2002 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 7  Line 7 
7    
8  __version__ = "$Revision$"  __version__ = "$Revision$"
9    
10  import shapelib  from math import log, ceil
11    
12    import shapelib, shptree
13    
14  from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \  from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
15       LAYER_VISIBILITY_CHANGED       LAYER_VISIBILITY_CHANGED
# Line 102  class Layer(BaseLayer): Line 104  class Layer(BaseLayer):
104      """      """
105    
106      def __init__(self, title, filename, projection = None,      def __init__(self, title, filename, projection = None,
107                   fill = None, stroke = _black, visible = 1):                   fill = None, stroke = _black, stroke_width = 1, visible = 1):
108          """Initialize the layer.          """Initialize the layer.
109    
110          title -- the title          title -- the title
# Line 121  class Layer(BaseLayer): Line 123  class Layer(BaseLayer):
123          self.projection = projection          self.projection = projection
124          self.fill = fill          self.fill = fill
125          self.stroke = stroke          self.stroke = stroke
126            self.stroke_width = stroke_width
127          self.shapefile = None          self.shapefile = None
128            self.shapetree = None
129          self.open_shapefile()          self.open_shapefile()
130          # shapetable is the table associated with the shapefile, while          # shapetable is the table associated with the shapefile, while
131          # table is the default table used to look up attributes for          # table is the default table used to look up attributes for
# Line 135  class Layer(BaseLayer): Line 139  class Layer(BaseLayer):
139              numshapes, shapetype, mins, maxs = self.shapefile.info()              numshapes, shapetype, mins, maxs = self.shapefile.info()
140              self.numshapes = numshapes              self.numshapes = numshapes
141              self.shapetype = shapelib_shapetypes[shapetype]              self.shapetype = shapelib_shapetypes[shapetype]
142              self.bbox = mins[:2] + maxs[:2]  
143                # if there are shapes, set the bbox accordinly. Otherwise
144                # set it to None.
145                if self.numshapes:
146                    self.bbox = mins[:2] + maxs[:2]
147                else:
148                    self.bbox = None
149    
150                # estimate a good depth for the quad tree. Each depth
151                # multiplies the number of nodes by four, therefore we
152                # basically take the base 4 logarithm of the number of
153                # shapes.
154                if self.numshapes < 4:
155                    maxdepth = 1
156                else:
157                    maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))
158    
159                self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2,
160                                                 maxdepth)
161    
162        def Destroy(self):
163            BaseLayer.Destroy()
164            if self.shapefile is not None:
165                self.shapefile.close()
166                self.shapefile = None
167                self.shapetree = None
168            self.table.Destroy()
169    
170      def BoundingBox(self):      def BoundingBox(self):
171          """Return the bounding box of the layer's shapes in their default          """Return the layer's bounding box in the intrinsic coordinate system.
172          coordinate system"""  
173            If the layer has no shapes, return None.
174            """
175            # The bbox will be set by open_shapefile just as we need it
176            # here.
177          self.open_shapefile()          self.open_shapefile()
178          return self.bbox          return self.bbox
179    
180      def LatLongBoundingBox(self):      def LatLongBoundingBox(self):
181          """Return the layer's bounding box in lat/long coordinates"""          """Return the layer's bounding box in lat/long coordinates.
182          llx, lly, urx, ury = self.BoundingBox()  
183          if self.projection is not None:          Return None, if the layer doesn't contain any shapes.
184              llx, lly = self.projection.Inverse(llx, lly)          """
185              urx, ury = self.projection.Inverse(urx, ury)          bbox = self.BoundingBox()
186          return llx, lly, urx, ury          if bbox is not None:
187                llx, lly, urx, ury = bbox
188                if self.projection is not None:
189                    llx, lly = self.projection.Inverse(llx, lly)
190                    urx, ury = self.projection.Inverse(urx, ury)
191                return llx, lly, urx, ury
192            else:
193                return None
194    
195      def NumShapes(self):      def NumShapes(self):
196          """Return the number of shapes in the layer"""          """Return the number of shapes in the layer"""
# Line 174  class Layer(BaseLayer): Line 215  class Layer(BaseLayer):
215              poly = shape.vertices()[0]              poly = shape.vertices()[0]
216              points = []              points = []
217              for x, y in poly:              for x, y in poly:
218                  points.append(x, y)                  points.append((x, y))
219          return Shape(points)          return Shape(points)
220    
221        def ShapesInRegion(self, box):
222            """Return the ids of the shapes that overlap the box.
223    
224            Box is a tuple (left, bottom, right, top) in the coordinate
225            system used by the layer's shapefile.
226            """
227            left, bottom, right, top = box
228            return self.shapetree.find_shapes((left, bottom), (right, top))
229    
230      def SetProjection(self, projection):      def SetProjection(self, projection):
231          """Set the layer's projection"""          """Set the layer's projection"""
232          self.projection = projection          self.projection = projection
# Line 192  class Layer(BaseLayer): Line 242  class Layer(BaseLayer):
242          stroked."""          stroked."""
243          self.stroke = stroke          self.stroke = stroke
244          self.changed(LAYER_LEGEND_CHANGED, self)          self.changed(LAYER_LEGEND_CHANGED, self)
245    
246        def SetStrokeWidth(self, width):
247            """Set the layer's stroke width."""
248            self.stroke_width = width
249            self.changed(LAYER_LEGEND_CHANGED, self)
250    
251        def TreeInfo(self):
252            items = []
253    
254            if self.Visible():
255                items.append("Shown")
256            else:
257                items.append("Hidden")
258            items.append("Shapes: %d" % self.NumShapes())
259    
260            bbox = self.LatLongBoundingBox()
261            if bbox is not None:
262                items.append("Extent (lat-lon): (%g, %g, %g, %g)" % bbox)
263            else:
264                items.append("Extent (lat-lon):")
265            items.append("Shapetype: %s" % shapetype_names[self.ShapeType()])
266    
267            def color_string(color):
268                if color is None:
269                    return "None"
270                return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)
271            items.append("Fill: " + color_string(self.fill))
272            items.append("Outline: " + color_string(self.stroke))
273    
274            return ("Layer '%s'" % self.Title(), items)

Legend:
Removed from v.21  
changed lines
  Added in v.258

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26