Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic simplestyle implementation #7

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ build/
dist/
.DS_Store
.vscode
*.db
Pipfile
!demo/Pipfile
*.db
node_modules
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ Options:
- `tile_layer`: Use a URL template that can be passed to a [Leaflet Tilelayer](https://leafletjs.com/reference-1.7.1.html#tilelayer)
- `tile_layer_options`: All options will be passed to the tile layer. See [Leaflet documentation](https://leafletjs.com/reference-1.7.1.html#tilelayer) for more on possible values here.

## Styling map features

Map features can be styled using the [simplestyle-spec](https://github.com/mapbox/simplestyle-spec). This requires setting specific fields on returned rows. Here's an example:

```sql
SELECT Name, geometry, "#ff0000" as fill, "#0000ff" as stroke, 0.2 as stroke-width, from neighborhoods
```

That will render a neighborhood map where each polygon is filled in red, outlined in blue and lines are 0.2 pixels wide.

A more useful approach would use the `CASE` statement to color features based on data:

```sql
SELECT
Name,
geometry,
CASE
Name
WHEN "Roslindale" THEN "#ff0000"
WHEN "Dorchester" THEN "#0000ff"
ELSE "#dddddd"
END fill
FROM
neighborhoods
```

This will fill Roslindale in red, Dorchester in blue and all other neighborhoods in gray.

## Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:
Expand Down
2 changes: 2 additions & 0 deletions datasette_geojson_map/static/leaflet-simplestyle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion datasette_geojson_map/static/map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datasette_geojson_map/static/map.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ install:
neighborhoods.geojson:
wget -O $@ https://opendata.arcgis.com/datasets/3525b0ee6e6b427f9aab5d0a1d0a1a28_0.geojson

boston.db: neighborhoods.geojson
pipenv run geojson-to-sqlite $@ neighborhoods $^
schools.geojson:
wget -O $@ https://bostonopendata-boston.opendata.arcgis.com/datasets/boston::public-schools.geojson

boston.db: neighborhoods.geojson schools.geojson
pipenv run geojson-to-sqlite $@ neighborhoods neighborhoods.geojson
pipenv run geojson-to-sqlite $@ schools schools.geojson

run: boston.db
pipenv run datasette $^ -m metadata.yml

clean:
rm -f boston.db neighborhoods.geojson schools.geojson
2 changes: 1 addition & 1 deletion demo/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ geojson-to-sqlite = "*"
[dev-packages]

[requires]
python_version = "3.9"
python_version = "3.10"
18 changes: 18 additions & 0 deletions demo/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ plugins:
minZoom: 1
maxZoom: 16
ext: jpg

databases:
boston:
queries:
color_by_name:
title: Color by name
sql: |
SELECT
Name,
geometry,
CASE
Name
WHEN "Roslindale" THEN "#ff0000"
WHEN "Dorchester" THEN "#0000ff"
ELSE "#dddddd"
END fill
FROM
neighborhoods
Loading