|
4 | 4 | import sys |
5 | 5 | import argparse |
6 | 6 |
|
7 | | -def get_osm_gps_traces(min_lon, min_lat, max_lon, max_lat): |
| 7 | +def get_osm_gps_traces(bboxes): |
8 | 8 | url = 'https://api.openstreetmap.org/api/0.6/trackpoints' |
9 | 9 | traces = [] |
10 | 10 |
|
11 | 11 | lon_step = 0.25 |
12 | 12 | lat_step = 0.25 |
13 | | - |
14 | | - current_min_lon = min_lon |
15 | 13 |
|
16 | | - while current_min_lon < max_lon: |
17 | | - current_max_lon = min(current_min_lon + lon_step, max_lon) |
| 14 | + for bbox in bboxes: |
| 15 | + min_lon, min_lat, max_lon, max_lat = map(float, bbox.split(',')) |
18 | 16 |
|
19 | | - current_min_lat = min_lat |
20 | | - while current_min_lat < max_lat: |
21 | | - current_max_lat = min(current_min_lat + lat_step, max_lat) |
22 | | - |
23 | | - bbox = f'{current_min_lon},{current_min_lat},{current_max_lon},{current_max_lat}' |
24 | | - print(f"Requesting bbox: {bbox}", file=sys.stderr) |
25 | | - |
26 | | - params = { |
27 | | - 'bbox': bbox, |
28 | | - 'page': 0 |
29 | | - } |
30 | | - headers = { |
31 | | - 'Accept': 'application/xml' |
32 | | - } |
33 | | - |
34 | | - response = requests.get(url, params=params, headers=headers) |
35 | | - if response.status_code == 200: |
36 | | - traces.append(response.content) |
37 | | - else: |
38 | | - print(f"Error fetching data for bbox {bbox}: {response.status_code} {response.text}", file=sys.stderr) |
| 17 | + current_min_lon = min_lon |
| 18 | + while current_min_lon < max_lon: |
| 19 | + current_max_lon = min(current_min_lon + lon_step, max_lon) |
39 | 20 |
|
40 | | - current_min_lat += lat_step |
41 | | - current_min_lon += lon_step |
| 21 | + current_min_lat = min_lat |
| 22 | + while current_min_lat < max_lat: |
| 23 | + current_max_lat = min(current_min_lat + lat_step, max_lat) |
| 24 | + |
| 25 | + bbox_str = f'{current_min_lon},{current_min_lat},{current_max_lon},{current_max_lat}' |
| 26 | + print(f"Requesting bbox: {bbox_str}", file=sys.stderr) |
| 27 | + |
| 28 | + params = { |
| 29 | + 'bbox': bbox_str, |
| 30 | + 'page': 0 |
| 31 | + } |
| 32 | + headers = { |
| 33 | + 'Accept': 'application/xml' |
| 34 | + } |
| 35 | + |
| 36 | + response = requests.get(url, params=params, headers=headers) |
| 37 | + if response.status_code == 200: |
| 38 | + traces.append(response.content) |
| 39 | + else: |
| 40 | + print(f"Error fetching data for bbox {bbox_str}: {response.status_code} {response.text}", file=sys.stderr) |
| 41 | + |
| 42 | + current_min_lat += lat_step |
| 43 | + current_min_lon += lon_step |
42 | 44 |
|
43 | 45 | return traces |
44 | 46 |
|
@@ -68,15 +70,12 @@ def save_to_csv(data, file): |
68 | 70 | writer.writerows(data) |
69 | 71 |
|
70 | 72 | if __name__ == '__main__': |
71 | | - parser = argparse.ArgumentParser(description='Fetch and output OSM GPS traces for a given bounding box.') |
72 | | - parser.add_argument('min_lon', type=float, help='Minimum longitude of the bounding box') |
73 | | - parser.add_argument('min_lat', type=float, help='Minimum latitude of the bounding box') |
74 | | - parser.add_argument('max_lon', type=float, help='Maximum longitude of the bounding box') |
75 | | - parser.add_argument('max_lat', type=float, help='Maximum latitude of the bounding box') |
| 73 | + parser = argparse.ArgumentParser(description='Fetch and output OSM GPS traces for given bounding boxes.') |
| 74 | + parser.add_argument('bboxes', nargs='+', help='Bounding boxes in the format min_lon,min_lat,max_lon,max_lat') |
76 | 75 |
|
77 | 76 | args = parser.parse_args() |
78 | 77 |
|
79 | | - gpx_data_traces = get_osm_gps_traces(args.min_lon, args.min_lat, args.max_lon, args.max_lat) |
| 78 | + gpx_data_traces = get_osm_gps_traces(args.bboxes) |
80 | 79 | print(f"Collected {len(gpx_data_traces)} trace segments", file=sys.stderr) |
81 | 80 |
|
82 | 81 | all_data = [] |
|
0 commit comments