-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm2position.py
110 lines (80 loc) · 2.72 KB
/
osm2position.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import os
import pandas as pd
import overpass
import numpy as np
import geojson
import time
def construction_requete(key, value):
M = value.split(",")
query = ""
for i in range(len(M)):
query += "node["+key+"="+M[i]+"](area);"
# print(query)
return query
def make_overpass_request(query, api):
response = None
while response is None:
try:
response = api.get(query, verbosity='geom')
time.sleep(5)
except:
print("error")
pass
return response
def store_geojson_file(index, response, folder):
# Convert to a GEOJSON string
# dump as file, if you want to save it in file
isExist = os.path.exists("datas/osmdata/"+folder)
if not isExist:
# Create a new directory because it does not exist
os.makedirs("datas/osmdata/"+folder)
file = "datas/osmdata/"+folder+"/" + str(index)+".geojson"
with open(file, mode="w") as result1:
geojson.dump(response, result1)
def download_osm_data(fichier):
f = pd.read_csv(fichier, sep=",")
api = overpass.API(timeout=500)
for i in range(len(f)):
print("index ="+str(f['id'][i]))
if (f['tags_osm'][i] is not np.nan):
T = f['tags_osm'][i]
# print(f.loc[i,"tags_osm"])
if (f['tags_osm'][i] == str("end")):
break
tableau = T.split(",")
if (len(tableau) == 1):
# print(tableau)
res = tableau[0].split("=")
query = construction_requete(res[0], res[1])
overpass_query = """
area["ISO3166-1"="CM"][admin_level=2];
("""+query+"""
);
"""
print(query)
print(overpass_query)
response1 = make_overpass_request(overpass_query, api)
store_geojson_file(f['id'][i], response1, "Position")
else:
query = ""
for j in range(len(tableau)):
res = tableau[j].split("=")
# print(construction_requete(res[0],res[1]))
query += (construction_requete(res[0],
res[1]))
overpass_query = """
area["ISO3166-1"="CM"][admin_level=2];
("""+query+"""
);
"""
print(query)
print(overpass_query)
response = make_overpass_request(overpass_query, api)
store_geojson_file(f['id'][i], response, "Position")
# exécution
download_osm_data('datas/sous_categories.csv')