1 |
import shapelib, dbflib, shptree |
2 |
|
3 |
# |
4 |
# The the shapefile module |
5 |
# |
6 |
|
7 |
print "--- testing shapelib ---" |
8 |
|
9 |
def test_shpobject(obj): |
10 |
# The vertices method returns the shape as a list of lists of tuples. |
11 |
print "vertices:", obj.vertices() |
12 |
|
13 |
# The part_types method returns a tuple with the types of every part |
14 |
print "part_types:", obj.part_types() |
15 |
|
16 |
# The extents returns a tuple with two 4-element lists with the min. |
17 |
# and max. values of the vertices. |
18 |
print "extents:", obj.extents() |
19 |
|
20 |
# The type attribute is the type code (one of the SHPT* constants |
21 |
# defined in the shapelib module) |
22 |
print "type:", obj.type |
23 |
|
24 |
# The id attribute is the shape id |
25 |
print "id:", obj.id |
26 |
|
27 |
# the __repr__ method returns a string that can be eval()'ed to |
28 |
# recreate the object. This __repr__ is also used by __str__ |
29 |
# and print |
30 |
print "obj:", obj |
31 |
print "reconstruction using __repr__:", |
32 |
obj_repr = repr(obj) |
33 |
obj_copy = eval(obj_repr) |
34 |
if repr(obj_copy) == obj_repr: |
35 |
print "ok" |
36 |
else: |
37 |
print "failed" |
38 |
|
39 |
|
40 |
|
41 |
def make_shapefile(filename): |
42 |
print "\n* Creating a ShapeFile" |
43 |
|
44 |
# Create a shapefile with polygons |
45 |
outfile = shapelib.create(filename, shapelib.SHPT_POLYGON) |
46 |
|
47 |
# Create one very simple polygon and write it to the shapefile. The |
48 |
# vertices should be given in clockwise order to comply with the |
49 |
# shapefile specification. |
50 |
print "\nA very simple polygon" |
51 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
52 |
[[(10, 10), (10, 20), (20, 20), (10, 10)]]) |
53 |
test_shpobject(obj) |
54 |
outfile.write_object(-1, obj) |
55 |
|
56 |
# Create a polygon with a hole. Note that according to the |
57 |
# shapefile specification, the vertices of the outer ring have to be |
58 |
# in clockwise order and the inner rings have to be in counter |
59 |
# clockwise order. |
60 |
# |
61 |
# There's an optional fourth parameter which when given must be a |
62 |
# list of part types, one for each part of the shape. For polygons, |
63 |
# the part type is always shapelib.SHPP_RING, though. The part |
64 |
# types are only relevant for SHPT_MULTIPATCH shapefiles. |
65 |
print "\nPolygon with a hole" |
66 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
67 |
[[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)], |
68 |
[(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)], |
69 |
]) |
70 |
test_shpobject(obj) |
71 |
outfile.write_object(-1, obj) |
72 |
|
73 |
# close the file. |
74 |
outfile.close() |
75 |
|
76 |
def read_shapefile(filename): |
77 |
print "\n* Reading a ShapeFile" |
78 |
|
79 |
# open the shapefile |
80 |
shp = shapelib.ShapeFile(filename) |
81 |
|
82 |
# the info method returns a tuple (num_shapes, type, min, max) where |
83 |
# num_shapes is the number of shapes, type is the type code (one of |
84 |
# the SHPT* constants defined in the shapelib module) and min and |
85 |
# max are 4-element lists with the min. and max. values of the |
86 |
# vertices. |
87 |
print "info:", shp.info() |
88 |
|
89 |
# the cobject method returns a PyCObject containing the shapelib |
90 |
# SHPHandle. This is useful for passing shapefile objects to |
91 |
# C-Python extensions. |
92 |
print "cobject:", shp.cobject() |
93 |
|
94 |
n = shp.info()[0] |
95 |
for i in range(n): |
96 |
obj = shp.read_object(i) |
97 |
print "\nread_object(%i):" % i |
98 |
test_shpobject(obj) |
99 |
|
100 |
print "\n* SHPTree:" |
101 |
|
102 |
# build a quad tree from the shapefile. The first argument must be |
103 |
# the return value of the shape file object's cobject method (this |
104 |
# is currently needed to access the shape file at the C-level). The |
105 |
# second argument is the dimension and the third the maximum depth. |
106 |
# 0 means to guess an appropriate depth |
107 |
tree = shptree.SHPTree(shp.cobject(), 2, 0) |
108 |
|
109 |
# Retrieve the ids for a region. Here we just use the extents of the |
110 |
# object previously read from the shapefile |
111 |
minima, maxima = obj.extents() |
112 |
print tree.find_shapes(minima[:2], maxima[:2]) |
113 |
|
114 |
|
115 |
make_shapefile("testfile") |
116 |
read_shapefile("testfile") |
117 |
|
118 |
# |
119 |
# Test the DBF file module. |
120 |
# |
121 |
|
122 |
print "\n\n--- testing dbflib ---" |
123 |
|
124 |
def make_dbf(file): |
125 |
# create a new dbf file and add three fields. |
126 |
dbf = dbflib.create(file) |
127 |
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
128 |
dbf.add_field("INT", dbflib.FTInteger, 10, 0) |
129 |
dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4) |
130 |
|
131 |
def add_dbf_records(file): |
132 |
# add some records to file |
133 |
dbf = dbflib.open(file, "r+b") |
134 |
# Records can be added as a dictionary... |
135 |
dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535}) |
136 |
# ... or as a sequence |
137 |
dbf.write_record(1, ("Ogg", 2, -1000.1234)) |
138 |
|
139 |
def list_dbf(file): |
140 |
# print the contents of a dbf file to stdout |
141 |
dbf = dbflib.DBFFile(file) |
142 |
print "%d records, %d fields" % (dbf.record_count(), dbf.field_count()) |
143 |
format = "" |
144 |
for i in range(dbf.field_count()): |
145 |
type, name, len, decc = dbf.field_info(i) |
146 |
if type == 0: |
147 |
format = format + " %%(%s)%ds" % (name, len) |
148 |
elif type == 1: |
149 |
format = format + " %%(%s)%dd" % (name, len) |
150 |
elif type == 2: |
151 |
format = format + " %%(%s)%dg" % (name, len) |
152 |
print format |
153 |
for i in range(dbf.record_count()): |
154 |
print format % dbf.read_record(i) |
155 |
|
156 |
|
157 |
make_dbf("testfile") |
158 |
add_dbf_records("testfile") |
159 |
list_dbf("testfile") |