Add a needs_commons parameter (default true) to both the API endpoint and the detail page. When needs_commons=false, look up Wikidata items by OSM relation ID (P402) via WDQS to return the most specific matching item even if it has no Wikimedia Commons category. Only activate this path when the matched item has no Commons category, so that locations with a Commons cat always get the same result regardless of the parameter. Remove the nearest-polygon fallback that was returning incorrect results for inland points in broad admin areas (e.g. returning Falmer for a point in Brighton). That fallback found the nearest polygon by boundary distance without requiring containment, so the pin would appear outside the polygon. The geosearch handles these cases correctly. Redesign the detail page: place name as heading, result card, collapsible API response and SPARQL query, improved OSM element cards with left-border highlight on the matched element, and a toggle button between modes. Redesign the index page: two-column layout with numbered steps and API documentation including the needs_commons parameter, Bootstrap form, and examples as a table. Closes #28 (Add support for returning Wikidata item instead of commons category) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> |
||
|---|---|---|
| geocode | ||
| static/css | ||
| templates | ||
| tests | ||
| .gitignore | ||
| LICENSE | ||
| lookup.py | ||
| README.md | ||
| requirements.txt | ||
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.
Overview
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 enginemodel.py: SQLAlchemy model for the database schemascotland.py: Functions for handling Scottish parishes
Dependencies
- Python 3.8+
- Flask
- SQLAlchemy
- psycopg2
- GeoAlchemy2
- lxml
Installation
- Clone the repository to your local machine.
- Create a Python virtual environment and install the requirements.
- Update the configuration settings in
config.default. - Run the Flask application.
- Set environment variables for database connectivity, if needed.
Configuration
Create a file named default.py in the config directory and configure database parameters and other settings.
Example:
# config.py
DB_URL = "postgresql://username:password@localhost/geocode_db"
Usage
To start the server:
python lookup.py
The web server will start at http://0.0.0.0:5000.
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.
Testing
For now, manual testing is the way to go. Automated tests are planned for future iterations of the project.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License.
Contact
If you have any queries or suggestions, feel free to contact the maintainer, Edward Betts, at edward@4angle.com.