Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bash script to batch export Tilemill projects
#!/bin/bash
#This script generates one export for each year of a Tilemill project to mbtiles format
#Instructions:
#Run using 'bash tilemill-export.sh [project-name]'
#For more info: http://flipside.org/notes/scripted-tilemill-export
#Assumptions
# - stylesheet = style.mss
# - selector = year
project=${1%/}
#Make sure the field separator is a ','
IFS=$','
range=2001,2002,2003,2004,2005
#Check if a project has been specified
if [ -z $project ]; then
echo You haven\'t specified a project, please specify one by using: bash tilemill-export.sh \[project\]
exit
fi
for year in $range
do
#Change the selector to the year we want to export
sed -i -r -e 's/ano=2[0-9]{3}/ano='$year'/g' ~/Documents/MapBox/project/$project/style.mss
#Change the project name and description to include indication of year
sed -i -r -e 's/\"name\": \"Occurrences\"/\"name\": \"Occurrences '$year'\"/g' ~/Documents/MapBox/project/$project/project.mml
sed -i -r -e 's/\"description\": \"Occurrences per area\"/\"description\": \"Occurrences per area '$year'\"/g' ~/Documents/MapBox/project/$project/project.mml
#Do the actual export
/usr/share/tilemill/index.js export $project ~/Documents/$project-$year.mbtiles --bbox='-10.2063,36.5361,-5.8557,42.4640' --format=mbtiles --minzoom=4 --maxzoom=13 --metatile=2
#and upload it. On project we have to specify a unique name, otherwise MapBox will just overwrite the project each time you upload
/usr/share/tilemill/index.js export $project-$year ~/Documents/$project-$year.mbtiles --format=upload --syncAccount="" --syncAccessToken=""
#Change the project name and description back to original
sed -i -r -e 's/\"name\": \"Occurrences [0-9]{4}\"/\"name\": \"Occurrences\"/g' ~/Documents/MapBox/project/$project/project.mml
sed -i -r -e 's/\"description\": \"Occurrences per admin area [0-9]{4}\"/\"description\": \"Occurrences per admin area\"/g' ~/Documents/MapBox/project/$project/project.mml
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment