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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2745 - (show annotations)
Thu Mar 15 22:27:02 2007 UTC (17 years, 11 months ago) by bramz
File MIME type: text/x-python
File size: 7697 byte(s)
Added FTLogical support to dbflib.  Accept None as no-data for M values.  Added name and mode keywords to mimic Python's file behaviour where appropriate.  Added unpack_vertex() to lift some load of shpobject_init().
1 import shapelib, dbflib, shptree
2
3 #
4 # 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):
40 print "\n* Creating a ShapeFile"
41
42 # Create a shapefile with polygons
43 outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)
44
45 # Create one very simple polygon and write it to the shapefile. The
46 # vertices should be given in clockwise order to comply with the
47 # shapefile specification.
48 print "\nA very simple polygon"
49 obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
50 [[(10, 10), (10, 20), (20, 20), (10, 10)]])
51 test_shpobject(obj)
52 outfile.write_object(-1, obj)
53
54 # Create a polygon with a hole. Note that according to the
55 # shapefile specification, the vertices of the outer ring have to be
56 # in clockwise order and the inner rings have to be in counter
57 # clockwise order.
58 #
59 # There's an optional fourth parameter which when given must be a
60 # 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
62 # types are only relevant for SHPT_MULTIPATCH shapefiles.
63 print "\nPolygon with a hole"
64 obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
65 [[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
66 [(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)],
67 ])
68 test_shpobject(obj)
69 outfile.write_object(-1, obj)
70
71 # close the file.
72 outfile.close()
73
74 def read_shapefile(filename):
75 print "\n* Reading a ShapeFile"
76
77 # open the shapefile
78 shp = shapelib.ShapeFile(filename)
79
80 # the info method returns a tuple (num_shapes, type, min, max) where
81 # num_shapes is the number of shapes, type is the type code (one of
82 # 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
84 # vertices.
85 print "info:", shp.info()
86
87 # the cobject method returns a PyCObject containing the shapelib
88 # SHPHandle. This is useful for passing shapefile objects to
89 # C-Python extensions.
90 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
101 # 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
103 # second argument is the dimension and the third the maximum depth.
104 # 0 means to guess an appropriate depth
105 tree = shptree.SHPTree(shp.cobject(), 2, 0)
106
107 # Retrieve the ids for a region. Here we just use the extents of the
108 # object previously read from the shapefile
109 minima, maxima = obj.extents()
110 print tree.find_shapes(minima[:2], maxima[:2])
111
112
113 print "--- testing shapelib ---"
114
115 make_shapefile("testfile")
116 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.
175 #
176
177 print "\n\n--- testing dbflib ---"
178
179 def make_dbf(file):
180 # create a new dbf file and add three fields.
181 dbf = dbflib.create(file)
182 dbf.add_field("NAME", dbflib.FTString, 20, 0)
183 dbf.add_field("INT", dbflib.FTInteger, 10, 0)
184 dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
185 dbf.add_field("BOOL", dbflib.FTLogical, 1, 0)
186
187 def add_dbf_records(file):
188 # add some records to file
189 dbf = dbflib.open(file, "r+b")
190 # Records can be added as a dictionary...
191 dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535, "BOOL":True})
192 # ... or as a sequence
193 dbf.write_record(1, ("Ogg", 2, -1000.1234, False))
194
195 def list_dbf(file):
196 # print the contents of a dbf file to stdout
197 dbf = dbflib.DBFFile(file)
198 print "%d records, %d fields" % (dbf.record_count(), dbf.field_count())
199 format = ""
200 for i in range(dbf.field_count()):
201 type, name, len, decc = dbf.field_info(i)
202 if type == dbflib.FTString:
203 format = format + " %%(%s)%ds" % (name, len)
204 elif type == dbflib.FTInteger:
205 format = format + " %%(%s)%dd" % (name, len)
206 elif type == dbflib.FTDouble:
207 format = format + " %%(%s)%dg" % (name, len)
208 elif type == dbflib.FTLogical:
209 format = format + " %%(%s)s" % name
210 print format
211 for i in range(dbf.record_count()):
212 print format % dbf.read_record(i)
213
214
215 make_dbf("testfile")
216 add_dbf_records("testfile")
217 list_dbf("testfile")

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26