You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.5 KiB

2 years ago
from __future__ import annotations
from math import radians, sin, cos, asin, sqrt, atan2, degrees
from sqlalchemy.types import TypeDecorator, String
2 years ago
from typing import List
from geographiclib.geodesic import Geodesic
2 years ago
class Location(TypeDecorator):
impl = String
cache_ok = True
2 years ago
def __init__(self, latitude: float = None, longitude: float = None):
2 years ago
self.latitude = latitude
self.longitude = longitude
2 years ago
def get_tuple(self):
return self.latitude, self.longitude
def process_bind_param(self, value, dialect):
if value:
return str(value)
def process_result_value(self, value, dialect):
if value:
return Location(* map(float, value.split(",")))
def load_dialect_impl(self, dialect):
# Provide the implementation for the specified dialect
return dialect.type_descriptor(String)
2 years ago
def calculate_distance(self, other: Location):
return Geodesic.WGS84.Inverse(*self.get_tuple(), *other.get_tuple())["s12"] / 1000
2 years ago
2 years ago
def generate_intermediate_points(self, other: Location, num_points) -> List[Location]:
# Calculate the distance between the start and end points
_distance = self.calculate_distance(other) * 1000
spacing = _distance / (num_points + 1)
2 years ago
2 years ago
# Calculate the intermediate points along the geodesic line
points = []
for i in range(1, num_points + 1):
# Calculate the position of the i-th intermediate point
d = i * spacing
g = Geodesic.WGS84.InverseLine(*self.get_tuple(), *other.get_tuple()).Position(d)
lat, lon = g['lat2'], g['lon2']
points.append(Location(lat, lon))
2 years ago
2 years ago
return points
2 years ago
def get_cooldown(self, other):
2 years ago
_distance = self.calculate_distance(other)
2 years ago
2 years ago
if _distance <= 2:
2 years ago
return 1
2 years ago
elif _distance <= 5:
2 years ago
return 2
2 years ago
elif _distance <= 7:
2 years ago
return 5
2 years ago
elif _distance <= 10:
2 years ago
return 7
2 years ago
elif _distance <= 12:
2 years ago
return 8
2 years ago
elif _distance <= 18:
2 years ago
return 10
2 years ago
elif _distance <= 26:
2 years ago
return 15
2 years ago
elif _distance <= 42:
2 years ago
return 19
2 years ago
elif _distance <= 65:
2 years ago
return 22
2 years ago
elif _distance <= 81:
2 years ago
return 25
2 years ago
elif _distance <= 100:
2 years ago
return 35
2 years ago
elif _distance <= 220:
2 years ago
return 40
2 years ago
elif _distance <= 250:
2 years ago
return 45
2 years ago
elif _distance <= 350:
2 years ago
return 51
2 years ago
elif _distance <= 375:
2 years ago
return 54
2 years ago
elif _distance <= 460:
2 years ago
return 62
2 years ago
elif _distance <= 500:
2 years ago
return 65
2 years ago
elif _distance <= 565:
2 years ago
return 69
2 years ago
elif _distance <= 700:
2 years ago
return 78
2 years ago
elif _distance <= 800:
2 years ago
return 84
2 years ago
elif _distance <= 900:
2 years ago
return 92
2 years ago
elif _distance <= 1000:
2 years ago
return 99
2 years ago
elif _distance <= 1100:
2 years ago
return 107
2 years ago
elif _distance <= 1200:
2 years ago
return 114
2 years ago
elif _distance <= 1300:
2 years ago
return 117
else:
return 120
def __str__(self):
return str(self.latitude) + "," + str(self.longitude)
def __repr__(self):
return str(self.latitude) + "," + str(self.longitude)
2 years ago
def __eq__(self, other):
if isinstance(other, Location):
return (self.latitude == other.latitude and
self.longitude == other.longitude)
return False
def __hash__(self):
2 years ago
return hash(self.get_tuple())
2 years ago