-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsegeocode_extended_components.py
executable file
·75 lines (70 loc) · 2.66 KB
/
parsegeocode_extended_components.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
#!/usr/bin/env python
import sys
import json
import csv
import types
import os.path, time
cols = ["id","pos","geocodetime","lang","lat","lng","location_type","bbox_n","bbox_e","bbox_s","bbox_w","viewport_n","viewport_e","viewport_s","viewport_w","formatted_address","types"]#,"country","postal_code","administrative_area_level_1","administrative_area_level_2","locality","sublocality","route"]
components = ["country","postal_code","postal_code_prefix","administrative_area_level_1","administrative_area_level_2","administrative_area_level_3","locality","sublocality","neighborhood","route"]
for component in components:
cols.append(component + "_long_name")
cols.append(component + "_short_name")
cw = csv.DictWriter(sys.stdout, cols)
if len(sys.argv) <= 1:
#print "Missing file"
cw.writeheader()
sys.exit()
else:
id = sys.argv[1].split("/")[len(sys.argv[1].split("/"))-1].split(".")[0]
f = open(sys.argv[1], "r")
js = json.loads(f.read())
if len(sys.argv) > 2:
lang = sys.argv[2]
else:
lang = "fr"
try:
t = time.gmtime(os.path.getmtime(sys.argv[1]))
t = time.strftime("%Y-%m-%d %H:%M:%S", t)
except:
t = None
if "results" not in js or len(js["results"]) <= 0:
sys.exit()
for pos in range(len(js["results"])):
res = js["results"][pos]
geom = res["geometry"]
loc = geom["location"]
r = dict()
r["id"] = id
r["pos"] = pos
r["lang"] = lang
r["geocodetime"] = t
r["lat"] = loc["lat"]
r["lng"] = loc["lng"]
r["formatted_address"] = res["formatted_address"]
if "location_type" in geom:
r["location_type"] = geom["location_type"]
if "bounds" in geom:
bbox = geom["bounds"]
r["bbox_n"] = bbox["northeast"]["lat"]
r["bbox_e"] = bbox["northeast"]["lng"]
r["bbox_s"] = bbox["southwest"]["lat"]
r["bbox_w"] = bbox["southwest"]["lng"]
if "viewport" in geom:
viewport = geom["viewport"]
r["viewport_n"] = viewport["northeast"]["lat"]
r["viewport_e"] = viewport["northeast"]["lng"]
r["viewport_s"] = viewport["southwest"]["lat"]
r["viewport_w"] = viewport["southwest"]["lng"]
if "types" in res:
r["types"] = "|".join(res["types"])
for ac in res["address_components"]:
if ac["types"][0] in components:
if "long_name" in ac: r[ac["types"][0] + "_long_name"] = ac["long_name"]
if "short_name" in ac: r[ac["types"][0] + "_short_name"] = ac["short_name"]
for a in cols:
if a in r and (type(r[a]) is types.StringType or type(r[a]) is types.UnicodeType):
try:
r[a] = r[a].encode("utf8")
except:
continue
cw.writerow(r)