-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathshpdump.py
More file actions
executable file
·105 lines (68 loc) · 2.07 KB
/
shpdump.py
File metadata and controls
executable file
·105 lines (68 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
"""
Dump the contents of the passed in Shapefile
Usage:
python shpdump.py polygon.shp
"""
import os
import sys
import mapscript
def plural(x):
"""
Returns an 's' if plural
Useful in print statements to avoid something like 'point(s)'
"""
if x > 1:
return "s"
return ""
def get_shapefile_object(sf_path):
# make sure can access .shp file, create shapefileObj
if os.access(sf_path, os.F_OK):
sf_obj = mapscript.shapefileObj(sf_path, -1)
else:
print("Can't access {}".format(sf_path))
sys.exit(2)
return sf_obj
def main(sf_path):
if not sf_path.lower().endswith(".shp"):
sf_path += ".shp"
sf_obj = get_shapefile_object(sf_path)
# create an empty Shapefile object
s_obj = mapscript.shapeObj()
# loop through each shape in the original Shapefile
for i in range(sf_obj.numshapes):
# get the object at index i
sf_obj.get(i, s_obj)
print("Shape %i has %i part%s" % (i, s_obj.numlines, plural(s_obj.numlines)))
print(
"Bounds (%f, %f) (%f, %f)"
% (
s_obj.bounds.minx,
s_obj.bounds.miny,
s_obj.bounds.maxx,
s_obj.bounds.maxy,
)
)
# loop through parts of each shape
for j in range(s_obj.numlines):
# get the jth part of the ith object
part = s_obj.get(j)
print(
"Part %i has %i point%s" % (j, part.numpoints, plural(part.numpoints))
)
# loop through points in each part
for k in range(part.numpoints):
# get the kth point of the jth part of the ith shape
point = part.get(k)
print("%i: %f, %f" % (k, point.x, point.y))
def usage():
"""
Display usage if program is used incorrectly
"""
print("Syntax: %s <shapefile_path>" % sys.argv[0])
sys.exit(2)
# make sure passing in filename argument
if len(sys.argv) != 2:
usage()
sf_path = sys.argv[1]
main(sf_path)