-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection-links.py
98 lines (69 loc) · 2.52 KB
/
section-links.py
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
# Meg Drouhard
# 8/12/16
# generate links for osm files using specified pattern and add to section file as property
import sys
import geojson
import argparse
# from pprint import pprint
###############################################################################
def addLinks(sectionFileName, pattern, key, outputpath):
# Load section data
with open(sectionFileName, "r") as sectionFile:
sectionData = geojson.load(sectionFile)
# Add link pattern as property in each section
for section in sectionData.features:
if key in section.properties:
linkString = pattern % str(section.properties[key])
section.properties["link"] = linkString
else:
sys.exit("Error: invalid key for geojson file.")
try:
with open(outputpath, "w") as outFile:
geojson.dump(sectionData, outFile)
print ("Links added in %s" % outputpath)
except ValueError:
print ("Error exporting " + fileName)
print (sectionData)
return
###################################################
# Parse arguments
parser = argparse.ArgumentParser(description="Generate links for sections in geojson file")
parser.add_argument("-s",
"--section-file",
dest="sectionFile",
required=True,
help="bounding box file (bounding boxes as list of features)")
parser.add_argument("-p",
"--pattern",
dest="pattern",
required=True,
help="pattern for file links")
parser.add_argument("-k",
"--key",
dest="key",
required=True,
help="geojson key for file identification")
parser.add_argument("-o",
"--output-path",
dest="outputpath",
required=False,
help="path for file output")
args = parser.parse_args()
# setup the arguments
if args.sectionFile:
sectionFile = args.sectionFile
else:
sys.exit("Error: missing section file.")
if args.pattern:
pattern = args.pattern
else:
sys.exit("Error: missing file link pattern.")
if args.key:
key = args.key
else:
sys.exit("Error: missing geojson key.")
if args.outputpath:
outputpath = args.outputpath
else:
outputpath = "links-out.geojson"
addLinks(sectionFile, pattern, key, outputpath)