-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathingestor_config_builder.py
executable file
·136 lines (112 loc) · 3.07 KB
/
ingestor_config_builder.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
import sys, getopt, subprocess, os, datetime, ogr, re, json
def getDescription():
print "Enter Layer Description (ENTER for none):"
description = str(raw_input())
return description
def getName():
print "Enter Layer Name (ENTER for none):"
name = str(raw_input())
return name
def getYear():
print "Enter Layer Year (ENTER for none):"
year = str(raw_input())
return year
def getTileset():
print "Enter Layer Tileset Type (tms or wmts):"
tileset = str(raw_input())
if tileset == "tms" or tileset == "wmts":
return tileset
else:
return getTileset()
def getTileJSON():
print "Enter Layer TileJSON URL:"
tilejson = str(raw_input())
if tilejson != "":
return tilejson
else:
return getTileJSON()
def main(argv):
instructions = 'Usage: %s <input directory>' % sys.argv[0]
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print instructions
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print instructions
sys.exit()
elif opt in ("-i"):
inputfile = arg
if len(argv) == 1:
inputfile = argv[0]
if inputfile == '':
print instructions
sys.exit(2)
inputfile = argv[0]
print ""
print ""
print ""
print ""
print ""
print "NYPL Labs Map Ingest Config Creator v0.2"
print "========================================"
print "By: Mauricio Giraldo Arteaga @mgiraldo / @nypl_labs"
print ""
config_list = []
layer_bbox = [180,180,-180,-180]
description = getDescription()
name = getName()
year = getYear()
tileset = getTileset()
tilejson = getTileJSON()
for ff in os.listdir(inputfile):
if ff.endswith(".tif"):
base_name = ff[:ff.find(".tif")]
# get geotiff data
geoText = subprocess.Popen(["gdalinfo", inputfile + "/" + ff], stdout=subprocess.PIPE).communicate()[0]
pattern = re.compile(r"Upper Left\s*\(\s*([0-9\-\.]*),\s*([0-9\-\.]*).*\n.*\n.*\nLower Right\s*\(\s*([0-9\-\.]*),\s*([0-9\-\.]*).*")
geoMatch = pattern.findall(geoText)
# print pattern
print "\n"
print "Geodata obtained for : ", ff
print "-----------------"
print "W", geoMatch[0][0]
print "N", geoMatch[0][1]
print "E", geoMatch[0][2]
print "S", geoMatch[0][3]
print "\n"
W = float(geoMatch[0][0])
N = float(geoMatch[0][1])
E = float(geoMatch[0][2])
S = float(geoMatch[0][3])
bbox = [W, S, E, N]
this_config = {}
this_config['id'] = base_name
this_config['bbox'] = bbox
config_list.append(this_config)
if W < layer_bbox[0]:
layer_bbox[0] = W
if S < layer_bbox[1]:
layer_bbox[1] = S
if E > layer_bbox[2]:
layer_bbox[2] = E
if N > layer_bbox[3]:
layer_bbox[3] = N
# NOTE: assumes input of folder WITH NO TRAILING SLASHES
config_data = {
"description":description,
"name":name,
"year":year,
"bbox": layer_bbox, # [W,S,E,N]
"tilejson":tilejson,
"tileset_type":tileset,
"sheets":config_list
}
config_file = open("config-ingest-layer" + (inputfile[inputfile.rfind("/")+1:]) + ".json", "w")
config_file.write(json.dumps(config_data, indent=4))
config_file.close()
if __name__ == "__main__":
main(sys.argv[1:])