-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding.py
More file actions
executable file
·147 lines (132 loc) · 4.46 KB
/
geocoding.py
File metadata and controls
executable file
·147 lines (132 loc) · 4.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
import urllib,csv
import json, time
#import simplejson as json
#http://www.portailsig.org/content/python-geocodage-geolocalisation
#friendly function of geocode_json
def geocode_csv(addr, api_key=''):
url = "http://maps.google.com/maps/geo?q=%s&output=csv&api_key=%s" % (urllib.quote(addr), urllib.quote(api_key))
coord = urllib.urlopen(url).read().split(',')
#coortext = 'lat: %s,long: %s' % (coord[2],coord[3])
return coord[2:4] # lat / long
#friendly function of geocode_csv
def geocode_json(addr, api_key=''):
url = "http://maps.google.com/maps/geo?q=%s&output=json&api_key=%s" % (urllib.quote(addr), urllib.quote(api_key))
data = urllib.urlopen(url).read()
code = json.loads(data)['Status']["code"]
if code != 200:
print " -> bad error code: %d" % code,
return None
accuracy = json.loads(data)['Placemark'][0]['AddressDetails']['Accuracy']
if accuracy < 6:
print " -> bad accuracy: %d" % accuracy,
return None
coord = json.loads(data)['Placemark'][0]['Point']['coordinates']
coord = [ coord[1], coord[0] ]
return coord # lat / long
def update_csv(filename='export opendata voies actuelles 2012-01-17_utf8.csv'):
reader = csv.reader(open(filename, 'rb'), delimiter=';')
writer = csv.writer(open(filename[:-4] + '_new.csv', 'wb'), delimiter='\t')
titles = reader.next()
#print titles
for row in reader:
#print row
typo = row[3]
hist = row[6]
for i in range(9,29):
if row[i] == 'FAUX':
continue
postal = '750%02d' % (i-8)
address = typo + ' ' + postal + ' Paris'
print "%s" % address,
coord = geocode_json(address)
time.sleep(2)
if not coord:
print " -> skipped"
continue #ignore the line
lat = coord[0]
lon = coord[1]
#print typo, hist, lat, lon
#break
# titre / desc / adresse / lat / long / categorie
writer.writerow([address, hist, address, lat, lon, "rue"])
print " -> done"
def update_csv2(filename='MH-Ile-de-France.txt-fr_utf8.csv'):
reader = csv.reader(open(filename, 'rb'), delimiter='\t')
writer = csv.writer(open(filename[:-4] + '_new.csv', 'wb'), delimiter='\t')
titles = reader.next()
#print titles
for row in reader:
#print row
monument = row[7]
address_ = row[8]
commune = row[5]
if True:
address = monument + " " + address_ + " " + commune
print "%s" % address,
coord = geocode_json(address)
time.sleep(2)
if not coord:
print " -> skipped"
continue #ignore the line
lat = coord[0]
lon = coord[1]
# titre / desc / adresse / lat / long / categorie
writer.writerow([monument, row[11] + '. ' + row[12] + '. ' + row[13], address, lat, lon, "monument"])
print " -> done"
def update_csv3(filename='Arbres_remarquables_utf8.csv'):
reader = csv.reader(open(filename, 'rb'), delimiter=';')
writer = csv.writer(open(filename[:-4] + '_new.csv', 'wb'), delimiter='\t')
titles = reader.next()
#print titles
for row in reader:
#print row
arron = row[0]
address_ = row[7]
if True:
postal = '750%02d' % int(arron)
address = address_ + " " + arron + " Paris"
print "%s" % address,
coord = geocode_json(address)
time.sleep(2)
if not coord:
print " -> skipped"
continue #ignore the line
lat = coord[0]
lon = coord[1]
# titre / desc / adresse / lat / long / categorie
writer.writerow([row[8], "Genre : " + row[1] + '. Espece : ' + row[2] + '. Famille : ' + row[3] + ". Annee : " + row[4] + ". Hauteur : " + row[5] + ". Circonference : " + row[6], address, lat, lon, "arbre"])
print " -> done"
def update_csv4(filename='Coordonnees musees de France_data.gouv_IDF_stripped.csv'):
reader = csv.reader(open(filename, 'rb'), delimiter=';')
writer = csv.writer(open(filename[:-4] + '_new.csv', 'wb'), delimiter='\t')
titles = reader.next()
#print titles
for row in reader:
print row
address_ = row[5]
postal = row[6]
ville = row[7]
ferme_ = row[2]
ferme = ""
if ferme_ == "OUI":
ferme = "Ferme actuellement. "
if True:
address = address_ + " " + postal + " " + ville
print "%s" % address,
coord = geocode_json(address)
time.sleep(2)
if not coord:
print " -> skipped"
continue #ignore the line
lat = coord[0]
lon = coord[1]
# titre / desc / adresse / lat / long / categorie
writer.writerow([row[4], row[10] + ". Fermeture annuelle : " + row[9] + ". " + ferme + row[8], address, lat, lon, "musees"])
print " -> done"
if __name__ == '__main__':
#update_csv()
#update_csv2()
#update_csv3()
update_csv4()
#coordonnees = geocode_json("Place de l'Universite 1, 1348 Louvain-la-Neuve")
#print coordonnees