/[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

revision 2741 by bramz, Tue Mar 13 23:30:41 2007 UTC 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    
 print "--- testing shapelib ---"  
   
7  def test_shpobject(obj):  def test_shpobject(obj):
8      # The vertices method returns the shape as a list of lists of tuples.      # The vertices method returns the shape as a list of lists of tuples.
9      print "vertices:", obj.vertices()      print "vertices:", obj.vertices()
# Line 112  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    
# Line 127  def make_dbf(file): Line 182  def make_dbf(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 143  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.2741  
changed lines
  Added in v.2745

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26