Skip to content

Commit

Permalink
Initial setup of tiles for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveBathnes committed Mar 6, 2024
1 parent f24b546 commit f616d94
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .github/workflows/generate_tiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Generate tiles

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *" # every day at midnight

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: install tippecanoe
run: brew install tippecanoe

- name: run python download script
run: python3 download.py

- name: run the generate shell file
run: ./generate.sh

- name: check for changes
run: git status

- name: commit changed files
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add .
git diff-index --quiet HEAD || git commit -m "Auto regeneration of tiles"
- name: fetch from main
run: git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*

- name: push code to main
run: git push origin HEAD:main
1 change: 1 addition & 0 deletions CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tiles.mobilelibraries.org
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# mobilelibraries-tiles
# Mobile libraries tiles

Vector tiles for the mobile libraries project

## How it works

The [Mobile libraries API](https://github.com/LibrariesHacked/mobilelibraries-api) exposes endpoints to retrieve mobile library stops and trips as GeoJSON. This repository contains scripts that will extract these GeoJSON files on a nightly basis and convert them into static vector tiles.

Those vector tiles are then available as [Mapbox Vector Tiles](https://github.com/mapbox/vector-tile-spec) for use in web maps. There will be two layers embedded into each tile: `stops` and `trips`. The `stops` layer contains the location of each stop, and the `trips` layer contains the route taken by the mobile library between different stops.

The process is as follows:

1. Download the latest GeoJSON from the Mobile libraries API (see `download.py`).
2. Use tippecanoe to convert the GeoJSON to a directory of static tiles within the `\libraries` directory (see `generate.sh`)

This repository is published as a GitHub pages site, which makes the tiles available in high performance web hosting.

## How to use

These tiles are made available via GitHub pages and a custom domain. They are available at `https://tiles.mobilelibraries.org/tiles/{z}/{x}/{y}.mvt`

## Licence

The tiles are from data compiled by the public and by public library services. They are licensed under the [Open Government Licence](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/).

Code in this repository is available under the [MIT Licence](LICENSE).
Empty file added data/.keep
Empty file.
1 change: 1 addition & 0 deletions data/stops.geojson

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/trips.geojson

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
""" Download the geojson files from the Mobile libraries API and save to data directory. """

import json
import os
import requests

GEOJSON_STOPS_URL = 'https://api.mobilelibraries.org/api/stops?limit=20000'
GEOJSON_TRIPS_URL = 'https://api.mobilelibraries.org/api/trips'


def download_geojson(url):
"""Download the geojson file and return the json data."""

headers = {"Accept": "application/geo+json"}
response = requests.get(url, headers=headers, timeout=10)
return response.json()


def save_geojson(data, file_name):
"""Save the geojson data to the data directory."""

if not os.path.exists('data'):
os.makedirs('data')

with open(f'data/{file_name}', 'w', encoding='utf-8') as f:
json.dump(data, f)


stops_geojson = download_geojson(GEOJSON_STOPS_URL)
save_geojson(stops_geojson, 'stops.geojson')

trips_geojson = download_geojson(GEOJSON_TRIPS_URL)
save_geojson(trips_geojson, 'trips.geojson')
4 changes: 4 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rm -rf tiles
mkdir tiles
tippecanoe -r1 --no-tile-compression --drop-densest-as-needed --no-tile-size-limit --output-to-directory data/stops.geojson data/trips.geojson --force
find . -name '*.pbf' -exec sh -c 'mv "$0" "${0%.pbf}.mvt"' {} \;

0 comments on commit f616d94

Please sign in to comment.