Creating custom (OSM-based) maps with R
This project uses:
- R scripts (inside Docker for generating map-based visualisations)
- Python 3 scripts (for post-processing generated images, downloading OSM tiles/some preprocessing)
- Bash scripts (for minor things)
- Julia scripts (for one-of things, (my legacy, sorrynotsorry.))
This was mainly a project for me to experiment with R (and ggplot) while working on a (hobbyist) data visualization for Apache and it got a bit out of hand. While it's certainly usable to create static custom maps, for more potent, dynamic, capabilities there exist better tools such as:
- The web based OpenLayers
- Java driven OpenStreetMap r package
You might want to increase your docker memory & swap file size (in Preferences/Advanced) because R has a crashing tendency when not given enough resources. See run options here or do it in your docker preferences.
After installing docker pull the required container (with R and all required libraries installed.)
./pullDockerContainers
# or well just atm
# docker pull robinlovelace/geocompr
Now you're all set!
This will step you to the bookcase example. Which comes down to this:
- Fetch tiles from an OSM tile server
- Use the Overpass Query API to query some feature (here the "amenity" "bookcase") (see this page for other options https://wiki.openstreetmap.org/wiki/Map_Features)
- Get the bounding box and generate a script for downloading the required map tiles.
#~/git/OSMap.r · (master±)
./runPrintBB.sh "Oostende,Belgium"
#[1] "Oostende,Belgium"
#Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
#[1] "Oostende,Belgium"
#[1] "./osm_tile_downloaderBB.py 51.2455721 2.8386276 51.1846337 3.0035408"
#[1] "Executable file written: /tmp/wmleuv/osmtiling/tileDLBB-Oostende,Belgium.sh"
- run the download script from the folder osmtiling
#~/git/OSMap.r · (master±)
cd osmtiling/
#~/git/OSMap.r/osmtiling · (master±)
./tileDLBB-Oostende,Belgium.sh
#14:8321-8328/5467-5471
#be/14-8321-5471.png
#downloading 'https://tile.openstreetmap.be/osmbe/14/8321/5471.png'
....
# tiles are in osmtiling/be
- Run the book cases example with the same string.
The r script run is dockerMakeBookcases.r with the given command line argument inside the docker container.
cd ..
#~/git/OSMap.r · (master±)
./runBookcases.sh "Oostende,Belgium"
# "Look in the /output folder."
- Possibly trim the white edges
Requires python3 and Pillow installed.
./trimImages.py output/bookcases.png
- You can add text and logos by tweaking the
addLogo.py
script.
These can be run from within the docker container atm best checkout the wmleuv branch for all the required files.
Then run a command prompt in the docker container with:
#~/git/OSMap.r · (wmleuv)
./docker.sh
This will take you inside the docker container ready to run the r scripts.
The repository map is synced under /tmp/wmleuv
by the docker script and if you open that folder inside the container you can run scripts like:
cd /tmp/wmleuv
r dockergeocode.r # required only once for generating necessary Rdata files.
r dockerMakeFrames.r
Alternative example files run similarly
# Smallest and most trivial example plotting book cases in Leuven.
r dockerMakeLeuvenBookcases.r
r dockerMakeSmallFrames.r
# Incorporating a GPX track:
r dockerMakeWandeling.r
# Overlaying other images:
r dockerANPROverlay.r
The output is placed inside output
directory.
Trim the white edges
./trimImages.py imageName
Next with python 3 and pillow installed run:
./addLogo.py
Finally with imagemagick convert installed run:
./makeGif.sh
With the excellent ffmpeg installed you can run.
./makeWebm.sh
Which results in a modern, smaller webm in ./output
instead.
Leuvense ANPR-camera’s filmen je zonder reden
Winkeldief of shopper: Leuven filmt u, 24/7
De Leuvense Privacy-wandeling van Apache
All public book cases listed on OSM around Leuven:
Since the used data is derived from the OpenStreetMap data (using the overpass API) I think the Open Database License applies here. https://en.wikipedia.org/wiki/Open_Database_License