-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_tm_osm_data.py
147 lines (103 loc) · 3.75 KB
/
get_tm_osm_data.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
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/python
# -*- coding: UTF-8 -*-
# Author: B. Herfort, 2016, GIScience Heidelberg
###########################################
import json
import sys #Require module sys for reading program options
from osgeo import ogr
import urllib2
import time
import os
def get_tm_project_json(task_id):
#create url from hot tasking manager task_id
url = 'http://tasks.hotosm.org/project/'+str(task_id)+'/tasks.json'
referer = 'http://tasks.hotosm.org/project/'+str(task_id)
x_req = 'XMLHttpRequest\r\n'
#send xmlhttp request
req = urllib2.Request(url)
req.add_header('Referer', referer)
req.add_header('X-Requested-With', x_req)
resp = urllib2.urlopen(req)
content = resp.read()
#convert content to json format and count features
tm_project_json = json.loads(content)
#returns the tm project information in json format
return tm_project_json
def get_tm_info(tm_project_json):
#get feature count
cnt_features = len(tm_project_json['features'])
#create new geometry collection
geomcol = ogr.Geometry(ogr.wkbGeometryCollection)
subtask_id_list = []
#iterate over all features
for z in range(0,cnt_features):
#get subtask_id, type, coordinates from tm_project_json
subtask_id = str(tm_project_json['features'][z]['id'])
type = tm_project_json['features'][z]['geometry']['type']
coordinates = tm_project_json['features'][z]['geometry']['coordinates']
geojson = '{"type":"'+str(type)+'","coordinates":'+str(coordinates)+'}'
#create polygon and add to geometry collection
polygon = ogr.CreateGeometryFromJson(geojson)
geomcol.AddGeometry(polygon)
subtask_id_list.append(subtask_id)
return (geomcol,subtask_id_list)
def get_osm_data(min_lon,max_lat,max_lon,min_lat,output_file):
#get osm data from osm api
url = 'http://api.openstreetmap.org/api/0.6/map?bbox='+min_lon+','+max_lat+','+max_lon+','+min_lat
req = urllib2.Request(url)
resp = urllib2.urlopen(req)
content = resp.read()
#save osm data
fileout = file(output_file, "w")
fileout.write(content)
fileout.close()
#close connection
resp.close()
def main(task_id, output_directory):
#Take start time
start_time = time.time()
if not os.path.exists(output_directory):
os.makedirs(output_directory)
tm_project_json = get_tm_project_json(task_id)
geomcol = get_tm_info(tm_project_json)[0]
subtask_id_list = get_tm_info(tm_project_json)[1]
for z in range(0,len(subtask_id_list)):
#get bbox
polygon = geomcol.GetGeometryRef(z)
min_lon = str(polygon.GetEnvelope()[0])
max_lat = str(polygon.GetEnvelope()[2])
max_lon = str(polygon.GetEnvelope()[1])
min_lat = str(polygon.GetEnvelope()[3])
#get subtask_id
subtask_id = subtask_id_list[z]
#create output file name
output_file = output_directory + '/' + str(task_id)+ '_' + str(subtask_id) + '.osm'
# Check if file already exists
if os.path.exists(output_file):
continue
else:
#get current osm data
get_osm_data(min_lon,max_lat,max_lon,min_lat,output_file)
#Take end time and calculate program run time
end_time = time.time()
run_time = end_time - start_time
print '############ END ######################################'
print '##'
print '## input HOT task id: '+task_id
print '##'
print '## output directory: '+output_directory
print '## number of output files: '+str(len(subtask_id_list))
print '##'
print '## runtime: '+str(run_time)+' s'
print '##'
print '## B. Herfort, GIScience Research Group'
print '##'
print '#######################################################'
if __name__ == "__main__":
#
# example run : $ python get_tm_osm_data.py 1088 testdata
#
if len( sys.argv ) != 3:
print "[ ERROR ] you must supply 2 arguments: HOT_Task_ID output_directory"
sys.exit( 1 )
main( sys.argv[1], sys.argv[2] )