test/apps/util/random_coords.py
2025-03-18 14:18:44 +07:00

33 lines
1000 B
Python

import math
import random
MAX_SPEED_KPH = 50
DEGREES_PER_KM = 1 / 111
MAX_SPEED_DEGREES = MAX_SPEED_KPH * DEGREES_PER_KM * (30 / 3600)
def get_realistic_coords(center_coords: tuple, current_coords: tuple = None,
current_speed: float = 0):
center_lat, center_lon = center_coords
if current_coords:
current_lat, current_lon = current_coords
else:
current_lat, current_lon = center_lat, center_lon
direction = random.uniform(0, 2 * 3.14159)
speed_degrees = float(current_speed) * DEGREES_PER_KM * (
30 / 3600)
lat_offset = speed_degrees * random.uniform(0.8, 1.2) * math.cos(
direction)
lon_offset = speed_degrees * random.uniform(0.8, 1.2) * math.sin(
direction)
new_lat = current_lat + lat_offset
new_lon = current_lon + lon_offset
new_lat = max(center_lat - 1, min(center_lat + 1, new_lat))
new_lon = max(center_lon - 1, min(center_lon + 1, new_lon))
return new_lat, new_lon