5 |
# |
# |
6 |
|
|
7 |
def make_shapefile(filename): |
def make_shapefile(filename): |
8 |
|
# Create a shapefile with polygons |
9 |
|
outfile = shapelib.create(filename, shapelib.SHPT_POLYGON) |
10 |
|
|
11 |
|
# Create one very simple polygon and write it to the shapefile. The |
12 |
|
# vertices should be given in clockwise order to comply with the |
13 |
|
# shapefile specification. |
14 |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
15 |
[[(10, 10), (20, 10), (20, 20), (10, 10)]]) |
[[(10, 10), (10, 20), (20, 20), (10, 10)]]) |
16 |
print obj.extents() |
print obj.extents() |
17 |
print obj.vertices() |
print obj.vertices() |
|
outfile = shapelib.create(filename, shapelib.SHPT_POLYGON) |
|
18 |
outfile.write_object(-1, obj) |
outfile.write_object(-1, obj) |
19 |
del outfile |
|
20 |
|
# Create a polygon with a hole. Note that according to the |
21 |
|
# shapefile specification, the vertices of the outer ring have to be |
22 |
|
# in clockwise order and the inner rings have to be in counter |
23 |
|
# clockwise order. |
24 |
|
# |
25 |
|
# There's an optional fourth parameter which when given must be a |
26 |
|
# list of part types, one for each part of the shape. For polygons, |
27 |
|
# the part type is always shapelib.SHPP_RING, though. The part |
28 |
|
# types are only relevant for SHPT_MULTIPATCH shapefiles. |
29 |
|
obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, |
30 |
|
[[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)], |
31 |
|
[(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)], |
32 |
|
]) |
33 |
|
print obj.extents() |
34 |
|
print obj.vertices() |
35 |
|
outfile.write_object(-1, obj) |
36 |
|
|
37 |
|
# close the file. |
38 |
|
outfile.close() |
39 |
|
|
40 |
def read_shapefile(filename): |
def read_shapefile(filename): |
41 |
# open the shapefile |
# open the shapefile |