forked from timwaters/building-inspector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingestor_config_builder.py
executable file
·74 lines (60 loc) · 1.81 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
#!/usr/bin/python
import sys, getopt, subprocess, os, datetime, ogr, re, json
def main(argv):
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.1"
print "========================================"
print "By: Mauricio Giraldo Arteaga @mgiraldo / @nypl_labs"
print ""
config_list = []
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:"
print "-----------------"
print "W", geoMatch[0][0]
print "N", geoMatch[0][1]
print "E", geoMatch[0][2]
print "S", geoMatch[0][3]
print "\n"
W = geoMatch[0][0]
N = geoMatch[0][1]
E = geoMatch[0][2]
S = geoMatch[0][3]
this_config = {}
this_config['id'] = base_name
this_config['bbox'] = [float(W), float(S), float(E), float(N)]
config_list.append(this_config)
# NOTE: assumes input of folder WITH NO TRAILING SLASHES
config_file = open("config-ingest-" + (inputfile[inputfile.rfind("/")+1:]) + ".json", "w")
config_file.write(json.dumps(config_list, indent=4))
config_file.close()
if __name__ == "__main__":
main(sys.argv[1:])