Go to file
Edward Betts 98d11b62a6 Record errors in the database
Closes: #12
2024-02-25 21:42:56 +00:00
geocode Record errors in the database 2024-02-25 21:42:56 +00:00
static/css Show error page 2023-11-25 14:29:34 +00:00
templates Add missing templates 2024-01-10 16:26:24 +00:00
tests Retry failed Wikidata SPARQL queries 2024-02-25 18:56:46 +00:00
.gitignore Add .hypothesis to gitignore 2024-02-25 18:40:36 +00:00
LICENSE Add README.md and LICENSE 2023-10-10 10:08:15 +01:00
README.md Add README.md and LICENSE 2023-10-10 10:08:15 +01:00
lookup.py Web interface with map where user can place pin for testing 2024-01-10 16:25:22 +00:00
requirements.txt Add requirements.txt 2023-11-17 13:50:21 -03:00


Wikidata reverse geocoder

This project offers a reverse geocoding service that returns Wikidata and OpenStreetMap (OSM) information based on geographical coordinates within the UK and Ireland. It uses Python 3 and Flask for the web server, while SQLAlchemy provides database connectivity. This service can look up administrative divisions such as Scottish parishes, English counties, or city districts based on latitude and longitude.


The main Python script lookup.py performs the following tasks:

  • Initializes a Flask application and database
  • Provides routes to various API endpoints
  • Implements geocoding logic by querying OSM and Wikidata

The geocode package consists of:

  • database.py: Initializes SQLAlchemy session and database engine
  • model.py: SQLAlchemy model for the database schema
  • scotland.py: Functions for handling Scottish parishes


  • Python 3.8+
  • Flask
  • SQLAlchemy
  • psycopg2
  • GeoAlchemy2
  • lxml


  1. Clone the repository to your local machine.
  2. Create a Python virtual environment and install the requirements.
  3. Update the configuration settings in config.default.
  4. Run the Flask application.
  5. Set environment variables for database connectivity, if needed.


Create a file named default.py in the config directory and configure database parameters and other settings.


# config.py
DB_URL = "postgresql://username:password@localhost/geocode_db"


To start the server:

python lookup.py

The web server will start at

API Endpoints

Home /

Renders the homepage where samples are displayed.

Random Location /random

Displays random UK location details based on latitude and longitude.

Wikidata Tag /wikidata_tag

Returns details based on a specific Wikidata tag.

Detail Page /detail

Displays detail based on latitude and longitude coordinates.

Database Schema

See geocode/model.py for the SQLAlchemy database schema definitions.


For now, manual testing is the way to go. Automated tests are planned for future iterations of the project.


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


This project is licensed under the MIT License.


If you have any queries or suggestions, feel free to contact the maintainer, Edward Betts, at edward@4angle.com.