Last active
February 14, 2024 12:43
-
-
Save WalrusGumboot/f91b4839fd117386743accd87dc97117 to your computer and use it in GitHub Desktop.
Haal de maximumsnelheid van een Vlaamse weg op
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
from typing import List, Tuple | |
import requests | |
coordinaatX = float(input("geef de oosterlengte als een decimaal: ")) | |
coordinaatY = float(input("geef de noorderbreedte als een decimaal: ")) | |
delta = 0.02 | |
linkergrens = coordinaatX - delta | |
rechtergrens = coordinaatX + delta | |
bovengrens = coordinaatY - delta | |
ondergrens = coordinaatY + delta | |
query = ( | |
"https://services.arcgis.com/gPAeJh41oYEgw9wR/arcgis/rest/services/Snelheid_wegen_Vlaanderen" | |
f"/FeatureServer/0/query?where=1%3D1&outFields=SNELHEID&geometry={linkergrens}%2C{bovengrens}%2C" | |
f"{rechtergrens}%2C{ondergrens}&geometryType=esriGeometryEnvelope&inSR=4326&spatialRel=esriSpatial" | |
"RelContains&outSR=4326&f=json" | |
) | |
response = requests.get(query) | |
response.raise_for_status() | |
gegevens = response.json() | |
assert "features" in gegevens.keys() | |
# beetje lelijk maar het werkt | |
paden: List[Tuple[int, List[Tuple[float, float]]]] = list( | |
map( | |
lambda elem: ( | |
int(elem["attributes"]["SNELHEID"]), | |
list( | |
map( | |
lambda coord: (float(coord[0]), float(coord[1])), | |
elem["geometry"]["paths"][0] | |
) | |
) | |
), | |
gegevens["features"] | |
) | |
) | |
if all([paden[i][0] == paden[0][0] for i in range(len(paden))]): | |
# alle paden hebben dezelfde snelheid | |
print(paden[0][0]) | |
else: | |
min_afstand = float('inf') | |
corresponderende_snelheid = None | |
for pad in paden: | |
punten = pad[1] | |
for punt in punten: | |
afstand = sqrt((coordinaatX - punt[0])**2 + (coordinaatY - punt[1])**2) | |
if afstand < min_afstand: | |
min_afstand = afstand | |
corresponderende_snelheid = pad[0] | |
print(corresponderende_snelheid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment