Skip to content

Instantly share code, notes, and snippets.

@jlyon
Last active December 23, 2015 07:09
Show Gist options
  • Save jlyon/903fc8b0526b26b9d69c to your computer and use it in GitHub Desktop.
Save jlyon/903fc8b0526b26b9d69c to your computer and use it in GitHub Desktop.
Getting started creating custom tiles with Mapbox's Tilemill and Open Streep Map.Following along http://www.mapbox.com/tilemill/docs/guides/ubuntu-service/ and http://www.mapbox.com/tilemill/docs/guides/osm-bright-ubuntu-quickstart/.Tested on Ubuntu 12.04 (LTS).

Tilemill, PostGIS, OSM Bright

Useful resource: Setting up PostGIS (postgres-9.3), qGIS.

Setting up tilemill as a service

From http://www.mapbox.com/tilemill/docs/guides/ubuntu-service/.

# Tilemill
sudo add-apt-repository ppa:developmentseed/mapbox
sudo add-apt-repository ppa:mapnik/v2.2.0
sudo apt-get update
sudo apt-get install tilemill libmapnik nodejs
# Preferences can be changed by editing /etc/tilemill/tilemill.config

# Run with /usr/share/tilemill/index.js --server=true
# You should also be able to run with service tilemill start, but 
# I couldn't get that to read the config file properly.

Setting up PostGIS (and Postgres)

PostGIS install instructions from Setting up PostGIS (postgres-9.3), qGIS.

# Dependencies: postGIS
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" >> /etc/apt/sources.list'
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.3-postgis-2.1 pgadmin3 postgresql-contrib
sudo apt-get install phppgadmin

Edit file sudo nano /etc/postgresql/9.3/main/pg_hba.conf Add (change last column to trust):

local   all             postgres                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

To make Postgres accessible from outside localhost (from http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html):

su - postgres
echo 'host all all 50.174.116.72/24 trust' >> /etc/postgresql/9.3/main/pg_hba.conf
#echo 'host all all 0.0.0.0/0 trust' >> /etc/postgresql/9.3/main/pg_hba.conf

The first will give access to a specifc ip. The latter gives access to everyone. See this blog post for details.

Edit /etc/postgresql/9.3/main/postgresql.conf, change listen_addresses='localhost' to listen_addresses='*'.

sudo /etc/init.d/postgresql restart

Now create the osm table and import PostGIS:

export GTFS_DB=osm
psql -U postgres -c "drop database if exists \"$GTFS_DB\";"
psql -U postgres -c "create database \"$GTFS_DB\";"
psql -U postgres -d "$GTFS_DB" -c "CREATE EXTENSION fuzzystrmatch;"
psql -U postgres -d "$GTFS_DB" -c "CREATE EXTENSION postgis;"
psql -U postgres -d "$GTFS_DB" -c "CREATE EXTENSION postgis_topology;"
psql -U postgres -d "$GTFS_DB" -c "CREATE EXTENSION postgis_tiger_geocoder;"

Postgres Tips and tricks

From http://stackoverflow.com/questions/761327/hidden-features-of-postgresql

Show list of all dbs and their size:

select datname, pg_size_pretty(pg_database_size(datname)) as size
  from pg_database;

Connect to db:

\c dbname

Show all tables in a db:

\d

Show size of all tables in db:

select schemaname, relname,
    pg_size_pretty(pg_relation_size(schemaname || '.' || relname)) as size
  from (select schemaname, relname, 'table' as type
          from pg_stat_user_tables
        union all
        select schemaname, relname, 'index' as type
          from pg_stat_user_indexes) x;

Show table schema

\d+ tablename

Setting up OSM Bright

From http://www.mapbox.com/tilemill/docs/guides/osm-bright-ubuntu-quickstart/.

# Dependencies: imposm
sudo aptitude install build-essential python-dev protobuf-compiler \
    libprotobuf-dev libtokyocabinet-dev python-psycopg2 libgeos-c1
sudo apt-get install python-pip
sudo pip install imposm

# Download Mapbox OSM Bright
cd ~/
wget https://github.com/mapbox/osm-bright/zipball/master
unzip master
mv mapbox-osm-bright-14bd07d osm-bright
rm master

Download .pbf osm file from http://metro.teczno.com or http://download.geofabrik.de/osm/. Then import the file into PostGIS with imposm:

imposm -U postgres -d osm -m /root/osm-bright/imposm-mapping.py \
    --read --write --optimize --deploy-production-tables <data.osm.pbf>

Set up OSM Bright in ~/osm-bright

cd ~/osm-bright
cp configure.py.sample configure.py
  • Change config["importer"] = "osm2pgsql" to config["importer"] = "imposm", unless you prefer to use osm2pgsql and have that set up.
  • Optionally change the name of your project from the default, ‘OSM Bright’.
  • Adjust the path to point to your MapBox project folder, /root/Documents/MapBox/project/.
  • change the line that says config["postgis"]["user"] = "" to config["postgis"]["user"] = "postgres"
  • Save & close the file.
# Build a project file
./make.py

Starting Tilemill on the server:

/usr/share/tilemill/index.js --server=true

Connect to the remove server via ssh (we set up a subdomain maps.albatrossdemos.com pointing to 54.214.3.211):

ssh -CA root@54.214.3.79 -L 20009:localhost:20009 -L 20008:localhost:20008
# now you can access http://localhost:20009 in your local browser

Or, make the instance publically accessible (assign an IP address, create and A record, then make Tilemill listen to the url:

sudo sh -c 'echo export TILEMILL_HOST="maps.albatrossdemos.com"  >> /etc/profile'
mkdir /var/log/tilemill/
source /etc/profile
/usr/share/tilemill/index.js --server=true --listenHost=0.0.0.0 --coreUrl=${TILEMILL_HOST}:20009 --tileUrl=${TILEMILL_HOST}:20008

Or run it from a call to forever in crontab. Install forever (npm install forever -g). Run crontab -e and add:

@reboot forever "/usr/share/tilemill/index.js --server=true --listenHost=0.0.0.0 --coreUrl=${TILEMILL_HOST}:20009 --tileUrl=${TILEMILL_HOST}:20008' -l=/var/log/tilemill/forever.log -o=/var/log/tilemill/stdout.log -e=/var/log/tilemill/stderr.log"

Next Steps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment