From dd1163aedf184dacc21de00336ee779ba247cc0d Mon Sep 17 00:00:00 2001 From: David Walker Date: Wed, 29 Dec 2021 10:07:53 +0000 Subject: [PATCH] Added geocoding of UK postcodes --- docker/Dockerfile | 10 +++---- requirements.txt | 5 ++++ setup.py | 2 +- .../locations/locations_blueprint.py | 26 +++++++++++++++++++ .../locations/templates/locations/edit.html | 22 +++++++++++++--- src/naturerec_web/static/css/site.css | 5 ++++ .../static/script/location-geocoder.js | 15 +++++++++++ 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 src/naturerec_web/static/script/location-geocoder.js diff --git a/docker/Dockerfile b/docker/Dockerfile index cfd6102..9d06e0a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,15 +1,15 @@ FROM python:3.10-slim-bullseye AS runtime -COPY naturerecorderpy-1.0.11.0 /opt/naturerecorderpy-1.0.11.0 +COPY naturerecorderpy-1.0.12.0 /opt/naturerecorderpy-1.0.12.0 -WORKDIR /opt/naturerecorderpy-1.0.11.0 +WORKDIR /opt/naturerecorderpy-1.0.12.0 RUN apt-get update -y RUN pip install -r requirements.txt -RUN pip install nature_recorder-1.0.11-py3-none-any.whl +RUN pip install nature_recorder-1.0.12-py3-none-any.whl -ENV NATURE_RECORDER_DATA_FOLDER=/var/opt/naturerecorderpy-1.0.11.0 -ENV NATURE_RECORDER_DB=/var/opt/naturerecorderpy-1.0.11.0/naturerecorder.db +ENV NATURE_RECORDER_DATA_FOLDER=/var/opt/naturerecorderpy-1.0.12.0 +ENV NATURE_RECORDER_DB=/var/opt/naturerecorderpy-1.0.12.0/naturerecorder.db ENTRYPOINT [ "python" ] CMD [ "-m", "naturerec_web" ] diff --git a/requirements.txt b/requirements.txt index d56d082..ba454bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,12 +12,17 @@ imagesize==1.3.0 itsdangerous==2.0.1 Jinja2==3.0.3 MarkupSafe==2.0.1 +numpy==1.21.5 packaging==21.3 +pandas==1.3.5 pdfkit==1.0.0 +pgeocode==0.3.0 Pygments==2.10.0 pyparsing==3.0.6 +python-dateutil==2.8.2 pytz==2021.3 requests==2.26.0 +six==1.16.0 snowballstemmer==2.2.0 Sphinx==4.3.0 sphinx-rtd-theme==1.0.0 diff --git a/setup.py b/setup.py index 9e651d6..374e036 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ def find_package_files(directory, remove_root): setuptools.setup( name="nature_recorder", - version="1.0.11", + version="1.0.12", description="Wildlife sightings database", packages=setuptools.find_packages("src"), include_package_data=True, diff --git a/src/naturerec_web/locations/locations_blueprint.py b/src/naturerec_web/locations/locations_blueprint.py index 524b18c..83e17c3 100644 --- a/src/naturerec_web/locations/locations_blueprint.py +++ b/src/naturerec_web/locations/locations_blueprint.py @@ -2,6 +2,8 @@ The locations blueprint supplies view functions and templates for location management """ +import pgeocode +import pandas as pd from flask import Blueprint, render_template, request, redirect from naturerec_model.logic import list_locations, get_location, create_location, update_location @@ -82,3 +84,27 @@ def edit(location_id): return _render_location_editing_page(location_id, e) else: return _render_location_editing_page(location_id, None) + + +@locations_bp.route("/geocode/") +def geocode(postcode): + """ + Query a postcode and return the latitude and longitude + + :param postcode: Postcode to query + :return: Dictionary containing the latitude and longitude for the postcode + """ + result = {"latitude": "", "longitude": ""} + + try: + # Currently only implemented for UK postcodes : Other countries to follow + nomi = pgeocode.Nominatim("gb") + geocode_sr = nomi.query_postal_code(postcode) + if not pd.isnull(geocode_sr.latitude) and not pd.isnull(geocode_sr.longitude): + result["latitude"] = round(geocode_sr.latitude, 6) + result["longitude"] = round(geocode_sr.longitude, 6) + except ValueError: + # Already set up the default dictionary, above, so nothing to do here + pass + + return result diff --git a/src/naturerec_web/locations/templates/locations/edit.html b/src/naturerec_web/locations/templates/locations/edit.html index b8f4734..a43bfe2 100644 --- a/src/naturerec_web/locations/templates/locations/edit.html +++ b/src/naturerec_web/locations/templates/locations/edit.html @@ -50,8 +50,8 @@

{{ title }}

value="{{ county }}" required>
- - Postcode +
@@ -61,12 +61,14 @@

{{ title }}

- +
- +
@@ -77,3 +79,15 @@

{{ title }}

{% endblock %} + +{% block scripts %} + + +{% endblock %} diff --git a/src/naturerec_web/static/css/site.css b/src/naturerec_web/static/css/site.css index dd4620d..c52c439 100644 --- a/src/naturerec_web/static/css/site.css +++ b/src/naturerec_web/static/css/site.css @@ -179,3 +179,8 @@ div.message { content: " *"; color: red; } + +.geocode { + color: #286090; + cursor: pointer; +} \ No newline at end of file diff --git a/src/naturerec_web/static/script/location-geocoder.js b/src/naturerec_web/static/script/location-geocoder.js new file mode 100644 index 0000000..c157d2e --- /dev/null +++ b/src/naturerec_web/static/script/location-geocoder.js @@ -0,0 +1,15 @@ +function geocode_location(postcode) { + $.ajax({ + url: "/locations/geocode/" + postcode, + type: "GET", + cache: false, + dataType: "json", + success: function(data, textStatus, jqXHR) { + $("#latitude").val(data.latitude); + $("#longitude").val(data.longitude); + }, + error: function(jqXHR, textStatus, errorThrown) { + // Nothing to do here, for now + } + }); +}