Spaces:
Running
Running
File size: 1,477 Bytes
7e103cf |
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 |
import json
import requests
def get_area_lat_lon(area_name: str) -> tuple[float, float]:
"""Get the latitude and longitude of an area from Nominatim.
Uses the [Nominatim API](https://nominatim.org/release-docs/develop/api/Search/).
Args:
area_name: The name of the area.
Returns:
The area found.
"""
response = requests.get(
f"https://nominatim.openstreetmap.org/search?q={area_name}&format=jsonv2",
headers={"User-Agent": "Mozilla/5.0"},
)
response.raise_for_status()
area = json.loads(response.content.decode())
return area[0]["lat"], area[0]["lon"]
def driving_hours_to_meters(driving_hours: int) -> int:
"""Convert driving hours to meters assuming a 70 km/h average speed.
Args:
driving_hours: The driving hours.
Returns:
The distance in meters.
"""
return driving_hours * 70 * 1000
def get_lat_lon_center(bounds: dict) -> tuple[float, float]:
"""Get the latitude and longitude of the center of a bounding box.
Args:
bounds: The bounding box.
```json
{
"minlat": float,
"minlon": float,
"maxlat": float,
"maxlon": float,
}
```
Returns:
The latitude and longitude of the center.
"""
return (
(bounds["minlat"] + bounds["maxlat"]) / 2,
(bounds["minlon"] + bounds["maxlon"]) / 2,
)
|