Skip to content

Commit

Permalink
Merge pull request #22 from davewalker5/postcode-geocoding
Browse files Browse the repository at this point in the history
Added geocoding of UK postcodes
  • Loading branch information
davewalker5 authored Dec 29, 2021
2 parents 70e99cd + dd1163a commit 6c7a922
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
10 changes: 5 additions & 5 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions src/naturerec_web/locations/locations_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/<postcode>")
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
22 changes: 18 additions & 4 deletions src/naturerec_web/locations/templates/locations/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ <h1>{{ title }}</h1>
value="{{ county }}" required>
</div>
<div class="form-group">
<label class="control-label">City</label>
<input class="form-control" name="postcode" placeholder="Postcode e.g. OX1 3RF"
<label class="control-label">Postcode</label>
<input class="form-control" name="postcode" id="postcode" placeholder="Postcode e.g. OX1 3RF"
value="{{ postcode }}">
</div>
<div class="form-group required">
Expand All @@ -61,12 +61,14 @@ <h1>{{ title }}</h1>
</div>
<div class="form-group">
<label class="control-label">Latitude</label>
<input class="form-control" name="latitude" placeholder="Latitude e.g. 51.75948"
<i class="fa fa-globe geocode" title="Populate latitude and longitude from postcode"></i>
<input class="form-control" name="latitude" id="latitude" placeholder="Latitude e.g. 51.75948"
value="{{ latitude }}">
</div>
<div class="form-group">
<label class="control-label">Longitude</label>
<input class="form-control" name="longitude" placeholder="Longitude e.g. -1.25053"
<i class="fa fa-globe geocode" title="Populate latitude and longitude from postcode"></i>
<input class="form-control" name="longitude" id="longitude" placeholder="Longitude e.g. -1.25053"
value="{{ longitude }}">
</div>
<div class="button-bar">
Expand All @@ -77,3 +79,15 @@ <h1>{{ title }}</h1>
</div>
</form>
{% endblock %}

{% block scripts %}
<script type="text/javascript" src="{{ url_for( 'static', filename='script/location-geocoder.js') }}"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".geocode").click(function() {
var postcode = $("#postcode").val();
geocode_location(postcode);
});
})
</script>
{% endblock %}
5 changes: 5 additions & 0 deletions src/naturerec_web/static/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,8 @@ div.message {
content: " *";
color: red;
}

.geocode {
color: #286090;
cursor: pointer;
}
15 changes: 15 additions & 0 deletions src/naturerec_web/static/script/location-geocoder.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}

0 comments on commit 6c7a922

Please sign in to comment.