-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_elas_from_shp
executable file
·42 lines (28 loc) · 1.14 KB
/
extract_elas_from_shp
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
#!/usr/bin/env python
import shapefile
from argparse import ArgumentParser
def extract_elas(filename):
sf = shapefile.Reader(filename)
records = sf.records()
fields = sf.fields
names = dict([[x[0], num-1] for (num, x) in enumerate(fields)])
snowlines = dict ([ [x[names['RGIID']], x[names['MEAN']] ] for x in records ] )
areas = dict ([ [x[names['RGIID']], x[names['AREA']] ] for x in records ] )
outfilename = "elas_%s.dat"%(filename[:-4])
open(outfilename, "w").writelines(["%s\t%s\t%s\n"%(x,snowlines[x], areas[x]) for x in snowlines.keys()])
def parse_args():
parser = ArgumentParser()
parser.description = "Extract ELA information from shapefile and store it in two column (tab delimited) csv"
parser.add_argument("FILES", nargs='*', help="Shapefiles")
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
options = parser.parse_args()
return options
def main():
options = parse_args()
if options.verbose:
print (dir(options))
for infile in options.FILES:
extract_elas(infile)
if __name__ == "__main__":
main()