4 |
# The the shapefile module |
# 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): |
def make_shapefile(filename): |
42 |
|
print "\n* Creating a ShapeFile" |
43 |
|
|
44 |
# Create a shapefile with polygons |
# Create a shapefile with polygons |
45 |
outfile = shapelib.create(filename, shapelib.SHPT_POLYGON) |
outfile = shapelib.create(filename, shapelib.SHPT_POLYGON) |
46 |
|
|
47 |
# Create one very simple polygon and write it to the shapefile. The |
# Create one very simple polygon and write it to the shapefile. The |
48 |
# vertices should be given in clockwise order to comply with the |
# vertices should be given in clockwise order to comply with the |
49 |
# shapefile specification. |
# shapefile specification. |
50 |
|
print "\nA very simple polygon" |
51 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
52 |
[[(10, 10), (10, 20), (20, 20), (10, 10)]]) |
[[(10, 10), (10, 20), (20, 20), (10, 10)]]) |
53 |
print obj.extents() |
test_shpobject(obj) |
|
print obj.vertices() |
|
54 |
outfile.write_object(-1, obj) |
outfile.write_object(-1, obj) |
55 |
|
|
56 |
# Create a polygon with a hole. Note that according to the |
# Create a polygon with a hole. Note that according to the |
62 |
# 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, |
63 |
# the part type is always shapelib.SHPP_RING, though. The part |
# the part type is always shapelib.SHPP_RING, though. The part |
64 |
# types are only relevant for SHPT_MULTIPATCH shapefiles. |
# types are only relevant for SHPT_MULTIPATCH shapefiles. |
65 |
|
print "\nPolygon with a hole" |
66 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
67 |
[[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)], |
[[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)], |
68 |
[(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)], |
[(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)], |
69 |
]) |
]) |
70 |
print obj.extents() |
test_shpobject(obj) |
|
print obj.vertices() |
|
71 |
outfile.write_object(-1, obj) |
outfile.write_object(-1, obj) |
72 |
|
|
73 |
# close the file. |
# close the file. |
74 |
outfile.close() |
outfile.close() |
75 |
|
|
76 |
def read_shapefile(filename): |
def read_shapefile(filename): |
77 |
|
print "\n* Reading a ShapeFile" |
78 |
|
|
79 |
# open the shapefile |
# open the shapefile |
80 |
shp = shapelib.ShapeFile(filename) |
shp = shapelib.ShapeFile(filename) |
81 |
|
|
84 |
# the SHPT* constants defined in the shapelib module) and min and |
# 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 |
# max are 4-element lists with the min. and max. values of the |
86 |
# vertices. |
# vertices. |
87 |
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 |
|
88 |
|
|
89 |
# the cobject method returns a PyCObject containing the shapelib |
# the cobject method returns a PyCObject containing the shapelib |
90 |
# SHPHandle. This is useful for passing shapefile objects to |
# SHPHandle. This is useful for passing shapefile objects to |
91 |
# C-Python extensions. |
# C-Python extensions. |
92 |
print shp.cobject() |
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 |
# 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 |
# 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 |
# is currently needed to access the shape file at the C-level). The |
119 |
# Test the DBF file module. |
# Test the DBF file module. |
120 |
# |
# |
121 |
|
|
122 |
|
print "\n\n--- testing dbflib ---" |
123 |
|
|
124 |
def make_dbf(file): |
def make_dbf(file): |
125 |
# create a new dbf file and add three fields. |
# create a new dbf file and add three fields. |
126 |
dbf = dbflib.create(file) |
dbf = dbflib.create(file) |