datasette-leaflet-freedraw

112 downloads this week        Star

Screenshot of simonw/datasette-leaflet-freedraw

README

datasette-leaflet-freedraw

PyPI Changelog Tests License

Draw polygons on maps in Datasette

Project background: Drawing shapes on a map to query a SpatiaLite database.

Installation

Install this plugin in the same environment as Datasette.

$ datasette install datasette-leaflet-freedraw

Usage

This plugin looks for input fields on a page with names ending in _freedraw - it replaces them with a Leaflet map interface that includes the FreeDraw Leaflet plugin.

Configure canned queries with inputs called freedraw or ending in _freedraw to trigger the map interface.

Demo

You can try out this plugin to run searches against the GreenInfo Network California Protected Areas Database. Here's an example query showing mini parks in San Francisco:

select
  AsGeoJSON(geometry), *
from
  CPAD_2020a_SuperUnits
where
  PARK_NAME like '%mini%' and
  Intersects(GeomFromGeoJSON(:freedraw), geometry) = 1
  and CPAD_2020a_SuperUnits.rowid in (
    select
      rowid
    from
      SpatialIndex
    where
      f_table_name = 'CPAD_2020a_SuperUnits'
      and search_frame = GeomFromGeoJSON(:freedraw)
  )

Screenshot of the plugin in action

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-leaflet-freedraw
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and tests:

pip install -e '.[test]'

To run the tests:

pytest