1 |
jan |
1611 |
import shapelib, dbflib, shptree |
2 |
|
|
|
3 |
|
|
# |
4 |
|
|
# The the shapefile module |
5 |
|
|
# |
6 |
|
|
|
7 |
bramz |
2741 |
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 |
jan |
1611 |
def make_shapefile(filename): |
40 |
bramz |
2741 |
print "\n* Creating a ShapeFile" |
41 |
|
|
|
42 |
bh |
2637 |
# 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 |
bramz |
2741 |
print "\nA very simple polygon" |
49 |
jan |
1611 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
50 |
bh |
2637 |
[[(10, 10), (10, 20), (20, 20), (10, 10)]]) |
51 |
bramz |
2741 |
test_shpobject(obj) |
52 |
jan |
1611 |
outfile.write_object(-1, obj) |
53 |
|
|
|
54 |
bh |
2637 |
# 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 |
bramz |
2741 |
print "\nPolygon with a hole" |
64 |
bh |
2637 |
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 |
bramz |
2741 |
test_shpobject(obj) |
69 |
bh |
2637 |
outfile.write_object(-1, obj) |
70 |
|
|
|
71 |
|
|
# close the file. |
72 |
|
|
outfile.close() |
73 |
|
|
|
74 |
jan |
1611 |
def read_shapefile(filename): |
75 |
bramz |
2741 |
print "\n* Reading a ShapeFile" |
76 |
|
|
|
77 |
jan |
1611 |
# 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 |
bramz |
2741 |
print "info:", shp.info() |
86 |
jan |
1611 |
|
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 |
bramz |
2741 |
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 |
jan |
1611 |
|
98 |
bramz |
2741 |
print "\n* SHPTree:" |
99 |
|
|
|
100 |
jan |
1611 |
# 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 |
bramz |
2745 |
print "--- testing shapelib ---" |
114 |
|
|
|
115 |
jan |
1611 |
make_shapefile("testfile") |
116 |
|
|
read_shapefile("testfile") |
117 |
|
|
|
118 |
|
|
# |
119 |
bramz |
2745 |
# 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 |
jan |
1611 |
# Test the DBF file module. |
175 |
|
|
# |
176 |
|
|
|
177 |
bramz |
2741 |
print "\n\n--- testing dbflib ---" |
178 |
|
|
|
179 |
jan |
1611 |
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 |
bramz |
2745 |
dbf.add_field("BOOL", dbflib.FTLogical, 1, 0) |
186 |
jan |
1611 |
|
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 |
bramz |
2745 |
dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535, "BOOL":True}) |
192 |
jan |
1611 |
# ... or as a sequence |
193 |
bramz |
2745 |
dbf.write_record(1, ("Ogg", 2, -1000.1234, False)) |
194 |
jan |
1611 |
|
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 |
bramz |
2745 |
if type == dbflib.FTString: |
203 |
jan |
1611 |
format = format + " %%(%s)%ds" % (name, len) |
204 |
bramz |
2745 |
elif type == dbflib.FTInteger: |
205 |
jan |
1611 |
format = format + " %%(%s)%dd" % (name, len) |
206 |
bramz |
2745 |
elif type == dbflib.FTDouble: |
207 |
jan |
1611 |
format = format + " %%(%s)%dg" % (name, len) |
208 |
bramz |
2745 |
elif type == dbflib.FTLogical: |
209 |
|
|
format = format + " %%(%s)s" % name |
210 |
jan |
1611 |
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") |