added first draft of score calculation script

This commit is contained in:
Dylan Everingham 2025-06-17 23:24:43 +02:00
parent d4c4d3bba7
commit 897cbfdca8
2 changed files with 97 additions and 0 deletions

33
scripts/score.py Normal file
View File

@ -0,0 +1,33 @@
import geopandas as gpd
from scipy.spatial import cKDTree
import numpy as np
import pyproj
from functools import lru_cache
# Constants
EPSG_WGS84 = 4326
EPSG_WebMercator = 3857
# Load the geodata.
gdf = gpd.read_file("berlin-latest-free.shp/gis_osm_pois_free_1.shp")
# Get all restaurant points and represent as meters on web mercator projection.
rest = gdf[gdf['fclass'].isin(['restaurant','cafe','fast_food','biergarten','pub'])].to_crs(epsg=EPSG_WGS84)
# Construct a cKDTree of all restaurant points for easy nearest-neighbor lookup.
coords_rest = np.vstack([rest.geometry.x, rest.geometry.y]).T
tree_rest = cKDTree(coords_rest)
# Construct a projection from lat/long to web mercator.
proj_rest = pyproj.Transformer.from_crs(EPSG_WGS84, EPSG_WebMercator, always_xy=True)
def distance_to_nearest_restaurant(lon: float, lat: float) -> float:
# Project coordinates to mercator.
x, y = proj.transform(lon, lat)
# Get the closest point from the tree.
dist, idx = tree.query([x, y])
# Returned distance is in meters (from mercator projection).
return float(dist)

64
scripts/score_test.ipynb Normal file
View File

@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "fadca06b",
"metadata": {},
"outputs": [],
"source": [
"from score import *"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "060c6bca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"75.16849866412393"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"distance_to_nearest_restaurant(13.4050, 52.5200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "210c57dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}