From 897cbfdca8304dcd308cebc2b2bff6346c1e2aeb Mon Sep 17 00:00:00 2001 From: Dylan Everingham Date: Tue, 17 Jun 2025 23:24:43 +0200 Subject: [PATCH] added first draft of score calculation script --- scripts/score.py | 33 +++++++++++++++++++++ scripts/score_test.ipynb | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 scripts/score.py create mode 100644 scripts/score_test.ipynb diff --git a/scripts/score.py b/scripts/score.py new file mode 100644 index 0000000..117b853 --- /dev/null +++ b/scripts/score.py @@ -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) \ No newline at end of file diff --git a/scripts/score_test.ipynb b/scripts/score_test.ipynb new file mode 100644 index 0000000..08f9266 --- /dev/null +++ b/scripts/score_test.ipynb @@ -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 +}