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
|
# Copyright (C) 2024 Leonard Kugis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import math
def calculate_bearing(lat1, lon1, lat2, lon2):
d_lon = lon2 - lon1
x = math.cos(math.radians(lat2)) * math.sin(math.radians(d_lon))
y = math.cos(math.radians(lat1)) * math.sin(math.radians(lat2)) - \
math.sin(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.cos(math.radians(d_lon))
bearing = math.atan2(x, y)
bearing = math.degrees(bearing)
bearing = (bearing + 360) % 360
return bearing
def calculate_distance(lat1, lon1, lat2, lon2):
R = 6371000 # Earth's radius in meters
d_lat = math.radians(lat2 - lat1)
d_lon = math.radians(lon2 - lon1)
a = math.sin(d_lat / 2) * math.sin(d_lat / 2) + \
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * \
math.sin(d_lon / 2) * math.sin(d_lon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = R * c
return distance
def calculate_timestamp(data, current_index):
if current_index == 0:
return 0
else:
prev_timestamp = calculate_timestamp(data, current_index - 1)
return prev_timestamp + data[current_index-1]['distance'] / data[current_index]['speed']
# Function process_route
# Parameters:
# input_filename: Input file name
# output_filename: Output file name
# Returns: None
# This function calculates the bearings and timestamps for a route file in JSON-format
# with given latitude, longitude and speed parameters.
def process_route(input_filename, output_filename):
with open(input_filename, 'r') as infile:
data = json.load(infile)
output_data = []
for i in range(len(data)):
if i == len(data) - 1:
data[i]['bearing'] = calculate_bearing(data[i]['latitude'], data[i]['longitude'],
data[i-1]['latitude'], data[i-1]['longitude'])
else:
bearing = calculate_bearing(data[i]['latitude'], data[i]['longitude'],
data[i+1]['latitude'], data[i+1]['longitude'])
data[i]['bearing'] = bearing
data[i]['distance'] = calculate_distance(data[i]['latitude'], data[i]['longitude'],
data[i+1]['latitude'], data[i+1]['longitude'])
data[i]['timestamp'] = calculate_timestamp(data, i)
output_data.append(data[i])
for d in output_data:
d.pop("distance", None)
with open(output_filename, 'w') as outfile:
json.dump(output_data, outfile, indent=2)
|