/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/data.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/data.py

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

revision 984 by bh, Thu May 22 16:37:48 2003 UTC revision 1559 by bh, Thu Aug 7 17:32:20 2003 UTC
# Line 12  __version__ = "$Revision$" Line 12  __version__ = "$Revision$"
12  # $Id$  # $Id$
13    
14  import os  import os
 import warnings  
15  import weakref  import weakref
16    from math import ceil, log
17    
18  import shapelib  import shapelib
19    import shptree
20  import table  import table
21  import transientdb  import transientdb
22    
23    from Thuban import _
24    
25    # Shape type constants
26    SHAPETYPE_POLYGON = "polygon"
27    SHAPETYPE_ARC = "arc"
28    SHAPETYPE_POINT = "point"
29    
30    # mapping from shapelib shapetype constants to our constants
31    shapelib_shapetypes = {shapelib.SHPT_POLYGON: SHAPETYPE_POLYGON,
32                           shapelib.SHPT_ARC: SHAPETYPE_ARC,
33                           shapelib.SHPT_POINT: SHAPETYPE_POINT}
34    
35    #
36    # Raw shape data formats
37    #
38    
39    # Raw data is the same as that returned by the points method.
40    RAW_PYTHON = "RAW_PYTHON"
41    
42    # Raw data is a shapefile. The Shape object will use the shapeid as the
43    # raw data.
44    RAW_SHAPEFILE = "RAW_SHAPEFILE"
45    
46    
47    class ShapefileShape:
48    
49        """Represent one shape of a shapefile"""
50    
51        def __init__(self, shapefile, shapeid):
52            self.shapefile = shapefile
53            self.shapeid = shapeid
54    
55        def compute_bbox(self):
56            """
57            Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy)
58            """
59            xs = []
60            ys = []
61            for part in self.Points():
62                for x, y in part:
63                    xs.append(x)
64                    ys.append(y)
65            return (min(xs), min(ys), max(xs), max(ys))
66    
67        def Points(self):
68            """Return the coordinates of the shape as a list of lists of pairs"""
69            shape = self.shapefile.read_object(self.shapeid)
70            points = shape.vertices()
71            if self.shapefile.info()[1] == shapelib.SHPT_POINT:
72                points = [points]
73            return points
74    
75        def RawData(self):
76            """Return the shape id to use with the shapefile"""
77            return self.shapeid
78    
79        def Shapefile(self):
80            """Return the shapefile object"""
81            return self.shapefile
82    
83    
84  class ShapeTable(transientdb.AutoTransientTable):  class ShapeTable(transientdb.AutoTransientTable):
85    
# Line 59  class ShapefileStore: Line 120  class ShapefileStore:
120          self.dbftable = table.DBFTable(filename)          self.dbftable = table.DBFTable(filename)
121          self.table = ShapeTable(self, session.TransientDB(), self.dbftable)          self.table = ShapeTable(self, session.TransientDB(), self.dbftable)
122    
123            self.numshapes, shapetype, mins, maxs = self.shapefile.info()
124            if self.numshapes:
125                self.bbox = mins[:2] + maxs[:2]
126            else:
127                self.bbox = None
128            self.shapetype = shapelib_shapetypes[shapetype]
129    
130            # estimate a good depth for the quad tree. Each depth multiplies
131            # the number of nodes by four, therefore we basically take the
132            # base 4 logarithm of the number of shapes.
133            if self.numshapes < 4:
134                maxdepth = 1
135            else:
136                maxdepth = int(ceil(log(self.numshapes / 4.0) / log(4)))
137    
138            self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2,
139                                             maxdepth)
140    
141      def Table(self):      def Table(self):
142          """Return the table containing the attribute data"""          """Return the table containing the attribute data"""
143          return self.table          return self.table
# Line 75  class ShapefileStore: Line 154  class ShapefileStore:
154          """Return the filetype. This is always the string 'shapefile'"""          """Return the filetype. This is always the string 'shapefile'"""
155          return "shapefile"          return "shapefile"
156    
157        def ShapeType(self):
158            """Return the type of the shapes in the shapestore.
159    
160            This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON.
161            """
162            return self.shapetype
163    
164        def RawShapeFormat(self):
165            """Return the raw data format of the shape data, i.e. RAW_SHAPEFILE"""
166            return RAW_SHAPEFILE
167    
168        def NumShapes(self):
169            """Return the number of shapes in the shape store"""
170            return self.numshapes
171    
172      def Dependencies(self):      def Dependencies(self):
173          """Return the empty tuple.          """Return the empty tuple.
174    
# Line 82  class ShapefileStore: Line 176  class ShapefileStore:
176          """          """
177          return ()          return ()
178    
179        def OrigShapeStore(self):
180            """Return None.
181    
182            The ShapefileStore was not derived from another shapestore.
183            """
184            return None
185    
186        def BoundingBox(self):
187            """Return the bounding box of the shapes in the shapestore.
188    
189            The coordinate system used is whatever was used in the shapefile.
190            If the shapefile is empty, return None.
191            """
192            return self.bbox
193    
194        def ShapesInRegion(self, box):
195            """Return the ids of the shapes that overlap the box.
196    
197            Box is a tuple (left, bottom, right, top) in the coordinate
198            system used used in the shapefile.
199            """
200            left, bottom, right, top = box
201            return self.shapetree.find_shapes((left, bottom), (right, top))
202    
203        def Shape(self, index):
204            """Return the shape with index index"""
205            return ShapefileShape(self.shapefile, index)
206    
207    
208    
209  class DerivedShapeStore:  class DerivedShapeStore:
210    
# Line 92  class DerivedShapeStore: Line 215  class DerivedShapeStore:
215    
216          The arguments are a shapestore for the shapedata and a table for          The arguments are a shapestore for the shapedata and a table for
217          the tabular data.          the tabular data.
218    
219            Raises ValueError if the number of shapes in the shapestore
220            is different from the number of rows in the table.
221          """          """
222    
223            numShapes = shapestore.Shapefile().info()[0]
224            if numShapes != table.NumRows():
225                raise ValueError(_("Table not compatible with shapestore."))
226    
227          self.shapestore = shapestore          self.shapestore = shapestore
228          self.table = table          self.table = table
229    
# Line 101  class DerivedShapeStore: Line 232  class DerivedShapeStore:
232          return self.table          return self.table
233    
234      def Shapefile(self):      def Shapefile(self):
235          """Return the shapefile of he underlying shapestore"""          """Return the shapefile of the underlying shapestore"""
236          return self.shapestore.Shapefile()          return self.shapestore.Shapefile()
237    
238      def Dependencies(self):      def Dependencies(self):
239          """Return a tuple containing the shapestore and the table"""          """Return a tuple containing the shapestore and the table"""
240          return (self.shapestore, self.table)          return (self.shapestore, self.table)
241    
242        def OrigShapeStore(self):
243            """
244            Return the original shapestore the derived store was instantiated with
245            """
246            return self.shapestore
247    
248        def Shape(self, index):
249            """Return the shape with index index"""
250            return self.shapestore.Shape(index)
251    
252  class SimpleStore:      def ShapesInRegion(self, bbox):
253            """Return the ids of the shapes that overlap the box.
254    
255      """Combine a shapefile and a table object"""          This method is simply delegated to the shapestore the
256            DerivedShapeStore was instantiated with.
257            """
258            return self.shapestore.ShapesInRegion(bbox)
259    
260      def __init__(self, shapefile, table):      def ShapeType(self):
261          warnings.warn("The SimpleStore is deprecated."          """Return the type of the shapes in the layer.
                       " Use DerivedShapeStore instead",  
                       DeprecationWarning, stacklevel = 2)  
         self.shapefile = shapefile  
         self.table = table  
         self.filename = None  
262    
263      def Table(self):          This method is simply delegated to the shapestore the
264          return self.table          DerivedShapeStore was instantiated with.
265            """
266            return self.shapestore.ShapeType()
267    
268      def Shapefile(self):      def NumShapes(self):
269          return self.shapefile          """Return the number of shapes in the shapestore."""
270            return self.shapestore.NumShapes()
271    
272        def BoundingBox(self):
273            """Return the bounding box of the shapes in the shapestore.
274    
275            This method is simply delegated to the shapestore the
276            DerivedShapeStore was instantiated with.
277            """
278            return self.shapestore.BoundingBox()

Legend:
Removed from v.984  
changed lines
  Added in v.1559

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26