/[thuban]/branches/WIP-pyshapelib-bramz/libraries/pyshapelib/pytest.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/libraries/pyshapelib/pytest.py

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

trunk/thuban/libraries/pyshapelib/pytest.py revision 2637 by bh, Thu Jun 30 14:20:46 2005 UTC branches/WIP-pyshapelib-bramz/libraries/pyshapelib/pytest.py revision 2745 by bramz, Thu Mar 15 22:27:02 2007 UTC
# Line 4  import shapelib, dbflib, shptree Line 4  import shapelib, dbflib, shptree
4  #       The the shapefile module  #       The the shapefile module
5  #  #
6    
7    def test_shpobject(obj):
8        # The vertices method returns the shape as a list of lists of tuples.
9        print "vertices:", obj.vertices()
10        
11        # The part_types method returns a tuple with the types of every part
12        print "part_types:", obj.part_types()
13    
14        # The extents returns a tuple with two 4-element lists with the min.
15        # and max. values of the vertices.
16        print "extents:", obj.extents()
17    
18        # The type attribute is the type code (one of the SHPT* constants
19        # defined in the shapelib module)
20        print "type:", obj.type
21    
22        # The id attribute is the shape id
23        print "id:", obj.id
24        
25        # the __repr__ method returns a string that can be eval()'ed to
26        # recreate the object.  This __repr__ is also used by __str__
27        # and print
28        print "obj:", obj
29        print "reconstruction using __repr__:",
30        obj_repr = repr(obj)
31        obj_copy = eval(obj_repr)
32        if repr(obj_copy) == obj_repr:
33            print "ok"
34        else:
35            print "failed"
36        
37    
38        
39  def make_shapefile(filename):  def make_shapefile(filename):
40        print "\n* Creating a ShapeFile"
41        
42      # Create a shapefile with polygons      # Create a shapefile with polygons
43      outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)      outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)
44    
45      # Create one very simple polygon and write it to the shapefile.  The      # Create one very simple polygon and write it to the shapefile.  The
46      # vertices should be given in clockwise order to comply with the      # vertices should be given in clockwise order to comply with the
47      # shapefile specification.      # shapefile specification.
48        print "\nA very simple polygon"
49      obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,      obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
50                               [[(10, 10), (10, 20), (20, 20), (10, 10)]])                               [[(10, 10), (10, 20), (20, 20), (10, 10)]])
51      print obj.extents()      test_shpobject(obj)
     print obj.vertices()  
52      outfile.write_object(-1, obj)      outfile.write_object(-1, obj)
53    
54      # Create a polygon with a hole.  Note that according to the      # Create a polygon with a hole.  Note that according to the
# Line 26  def make_shapefile(filename): Line 60  def make_shapefile(filename):
60      # list of part types, one for each part of the shape.  For polygons,      # list of part types, one for each part of the shape.  For polygons,
61      # the part type is always shapelib.SHPP_RING, though.  The part      # the part type is always shapelib.SHPP_RING, though.  The part
62      # types are only relevant for SHPT_MULTIPATCH shapefiles.      # types are only relevant for SHPT_MULTIPATCH shapefiles.
63        print "\nPolygon with a hole"
64      obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,      obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
65                               [[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],                               [[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
66                                [(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)],                                [(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)],
67                                ])                                ])
68      print obj.extents()      test_shpobject(obj)
     print obj.vertices()  
69      outfile.write_object(-1, obj)      outfile.write_object(-1, obj)
70    
71      # close the file.      # close the file.
72      outfile.close()      outfile.close()
73    
74  def read_shapefile(filename):  def read_shapefile(filename):
75        print "\n* Reading a ShapeFile"
76        
77      # open the shapefile      # open the shapefile
78      shp = shapelib.ShapeFile(filename)      shp = shapelib.ShapeFile(filename)
79    
# Line 46  def read_shapefile(filename): Line 82  def read_shapefile(filename):
82      # the SHPT* constants defined in the shapelib module) and min and      # the SHPT* constants defined in the shapelib module) and min and
83      # max are 4-element lists with the min. and max. values of the      # max are 4-element lists with the min. and max. values of the
84      # vertices.      # vertices.
85      print shp.info()      print "info:", shp.info()
   
     # read_object reads a shape  
     obj = shp.read_object(0)  
   
     # The vertices method returns the shape as a list of lists of tuples.  
     print obj.vertices()[0][:10]  
   
     # The extents returns a tuple with two 4-element lists with the min.  
     # and max. values of the vertices.  
     print obj.extents()  
   
     # The type attribute is the type code (one of the SHPT* constants  
     # defined in the shapelib module)  
     print obj.type  
   
     # The id attribute is the shape id  
     print obj.id  
86    
87      # the cobject method returns a PyCObject containing the shapelib      # the cobject method returns a PyCObject containing the shapelib
88      # SHPHandle. This is useful for passing shapefile objects to      # SHPHandle. This is useful for passing shapefile objects to
89      # C-Python extensions.      # C-Python extensions.
90      print shp.cobject()      print "cobject:", shp.cobject()
91        
92        n = shp.info()[0]
93        for i in range(n):
94            obj = shp.read_object(i)
95            print "\nread_object(%i):" % i
96            test_shpobject(obj)
97    
98        print "\n* SHPTree:"
99        
100      # build a quad tree from the shapefile. The first argument must be      # build a quad tree from the shapefile. The first argument must be
101      # the return value of the shape file object's cobject method (this      # the return value of the shape file object's cobject method (this
102      # is currently needed to access the shape file at the C-level). The      # is currently needed to access the shape file at the C-level). The
# Line 83  def read_shapefile(filename): Line 110  def read_shapefile(filename):
110      print tree.find_shapes(minima[:2], maxima[:2])      print tree.find_shapes(minima[:2], maxima[:2])
111    
112    
113    print "--- testing shapelib ---"
114    
115  make_shapefile("testfile")  make_shapefile("testfile")
116  read_shapefile("testfile")  read_shapefile("testfile")
117    
118  #  #
119    #               Test MultiPatch shapefiles
120    #
121    
122    def make_multipatch(filename):
123        print "\n* Creating multipatch ShapeFile"
124        
125        # Create a shapefile with multipatches
126        outfile = shapelib.create(filename, shapelib.SHPT_MULTIPATCH)
127    
128        # Create a quad as a triangle strip and as a triangle fan, in ONE object!
129        # Multipatch shapefiles use XYZM vertices, but you can get away with
130        # only specifying X and Y, Z and M are zero by default.
131        print "\nA triangle strip"
132        obj = shapelib.SHPObject(shapelib.SHPT_MULTIPATCH, 0,
133            [[(0, 0), (0, 10), (10, 0), (10, 10)],
134             [(20, 20), (20, 30), (30, 30), (30, 20)]],
135            [shapelib.SHPP_TRISTRIP, shapelib.SHPP_TRIFAN])
136        test_shpobject(obj)
137        outfile.write_object(-1, obj)
138        
139        # A polygon as an Outer ring and inner ring, with XYZ coordinates
140        # and measure values M.  Here we will use the part types to specify
141        # their particular type.
142        #
143        # You can have more than one polygon in a single Object, as long
144        # as you obey the following sequence: each polygon starts with an
145        # outer ring, followed by its holes as inner rings.  
146        #
147        # None is also accepted as M value to specify no-data.  The ESRI
148        # Shapefile specs define any M value smaller than 1e-38 as no-data.
149        # shapelib will store no-data as a zero.
150        #
151        # If you don't need the M value, you can leave it out and use triples
152        # as vertices instead.  For the first half of the inner ring,
153        # we used None to specify no-data.  In the second half, we just
154        # omitted it.
155        #
156        print "\nA polygon as outer ring and inner ring with XYZM coordinates"
157        obj = shapelib.SHPObject(shapelib.SHPT_MULTIPATCH, 1,
158            [[(0, 0, 0, 35.3), (0, 40, 10, 15.4), (40, 40, 20, 9.5), (40, 0, 10, 24.6), (0, 0, 0, 31.8)],
159             [(10, 10, 5, None), (20, 10, 10, None), (20, 20, 15), (10, 20, 10, 20),(10, 10, 5)]],
160            [shapelib.SHPP_OUTERRING, shapelib.SHPP_INNERRING])
161        test_shpobject(obj)
162        outfile.write_object(-1, obj)
163    
164        # close the file.
165        outfile.close()
166        
167    
168    print "--- testing multipatch ---"
169    
170    make_multipatch("multipatch")
171    read_shapefile("multipatch")
172        
173    #
174  #       Test the DBF file module.  #       Test the DBF file module.
175  #  #
176    
177    print "\n\n--- testing dbflib ---"
178    
179  def make_dbf(file):  def make_dbf(file):
180      # create a new dbf file and add three fields.      # create a new dbf file and add three fields.
181      dbf = dbflib.create(file)      dbf = dbflib.create(file)
182      dbf.add_field("NAME", dbflib.FTString, 20, 0)      dbf.add_field("NAME", dbflib.FTString, 20, 0)
183      dbf.add_field("INT", dbflib.FTInteger, 10, 0)      dbf.add_field("INT", dbflib.FTInteger, 10, 0)
184      dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)      dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
185        dbf.add_field("BOOL", dbflib.FTLogical, 1, 0)
186    
187  def add_dbf_records(file):  def add_dbf_records(file):
188      # add some records to file      # add some records to file
189      dbf = dbflib.open(file, "r+b")      dbf = dbflib.open(file, "r+b")
190      # Records can be added as a dictionary...      # Records can be added as a dictionary...
191      dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535})      dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535, "BOOL":True})
192      # ... or as a sequence      # ... or as a sequence
193      dbf.write_record(1, ("Ogg", 2, -1000.1234))      dbf.write_record(1, ("Ogg", 2, -1000.1234, False))
194    
195  def list_dbf(file):  def list_dbf(file):
196      # print the contents of a dbf file to stdout      # print the contents of a dbf file to stdout
# Line 112  def list_dbf(file): Line 199  def list_dbf(file):
199      format = ""      format = ""
200      for i in range(dbf.field_count()):      for i in range(dbf.field_count()):
201          type, name, len, decc = dbf.field_info(i)          type, name, len, decc = dbf.field_info(i)
202          if type == 0:          if type == dbflib.FTString:
203              format = format + " %%(%s)%ds" % (name, len)              format = format + " %%(%s)%ds" % (name, len)
204          elif type == 1:          elif type == dbflib.FTInteger:
205              format = format + " %%(%s)%dd" % (name, len)              format = format + " %%(%s)%dd" % (name, len)
206          elif type == 2:          elif type == dbflib.FTDouble:
207              format = format + " %%(%s)%dg" % (name, len)              format = format + " %%(%s)%dg" % (name, len)
208            elif type == dbflib.FTLogical:
209                format = format + " %%(%s)s" % name
210      print format      print format
211      for i in range(dbf.record_count()):      for i in range(dbf.record_count()):
212          print format % dbf.read_record(i)          print format % dbf.read_record(i)

Legend:
Removed from v.2637  
changed lines
  Added in v.2745

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26