From 1577d90263ce12122919dcaa1fb1e75954209829 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Sat, 17 Jun 2023 11:47:17 +0100 Subject: [PATCH] coordinate distance functions --- model_data/OpenUprnClient.py | 7 +++-- model_data/app.py | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/model_data/OpenUprnClient.py b/model_data/OpenUprnClient.py index 58248ace..6a8763f4 100644 --- a/model_data/OpenUprnClient.py +++ b/model_data/OpenUprnClient.py @@ -5,9 +5,12 @@ logger = setup_logger() class OpenUprnClient: - COLUMN_NAMES = [ + """ + Specs for this dataset can be found here: + https://www.ordnancesurvey.co.uk/documents/product-support/tech-spec/open-uprn-techspec-v1.pdf + """ - ] + # TODO: Document this def __init__(self, path, uprns): self.path = path diff --git a/model_data/app.py b/model_data/app.py index 23fc49fe..8c5fc488 100644 --- a/model_data/app.py +++ b/model_data/app.py @@ -165,3 +165,56 @@ def handler(): # DATE_K_TYP # DATE_ENTER # AGS_LOG_UR + + from pyproj import Proj, transform, Geod + + def distance_between_coords(longitude, latitude, x_bng, y_bng): + # Define the projections + wgs84 = Proj(init='epsg:4326') # WGS84 (longitude, latitude) + bng = Proj(init='epsg:27700') # British National Grid + + # Convert (longitude, latitude) to BNG coordinates + x, y = transform(wgs84, bng, longitude, latitude) + + # Define a geographic measure object + g = Geod(ellps='WGS84') + + # Calculate the distance between the points + # Note: Pyproj's 'Geod.inv' method returns azimuths and distance. + # We're only interested in distance here, so we only keep the third result + _, _, distance = g.inv(x, y, x_bng, y_bng) + + return distance + + def distance_between_bng_coords(x1_bng, y1_bng, x2_bng, y2_bng): + # Define a geographic measure object + g = Geod(ellps='airy') # Airy ellipsoid is used by the British National Grid + + # Calculate the distance between the points + # Note: Pyproj's 'Geod.inv' method returns azimuths and distance. + # We're only interested in distance here, so we only keep the third result + _, _, distance = g.inv(x1_bng, y1_bng, x2_bng, y2_bng) + + return distance + + import math + + import math + + def distance_between_bng_coords(x1_bng, y1_bng, x2_bng, y2_bng): + # Calculate the Euclidean distance between the points + distance_m = math.sqrt((x2_bng - x1_bng) ** 2 + (y2_bng - y1_bng) ** 2) + distance_km = distance_m / 1000 # convert meters to kilometers + return distance_m, distance_km + + property = input_properties[0] + + borehold_compare_to = borehole_data[0] + + dist_m, dist_km = distance_between_bng_coords( + x1_bng=property.coordinates["x_coordinate"], + y1_bng=property.coordinates["y_coordinate"], + x2_bng=borehold_compare_to["X"], + y2_bng=borehold_compare_to["Y"], + ) + # ground source heat pump.