Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netconstructor/61201a175044fed61e7c to your computer and use it in GitHub Desktop.
Save netconstructor/61201a175044fed61e7c to your computer and use it in GitHub Desktop.

TileStache install on DigitalOcean Ubuntu 12.04-64

sources

Step 1: the basics

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:cartodb/gis
sudo add-apt-repository ppa:mapnik/nightly-2.2 
sudo aptitude update  
sudo aptitude upgrade
sudo apt-get install git

Step 2: install Apache, python

sudo apt-get install apache2
sudo apt-get install build-essential
sudo apt-get install python-setuptools python-dev python2.7-dev libpq-dev
sudo apt-get install libapache2-mod-python

Step 3: install GDAL, Geos and proj4

sudo apt-get install gdal-bin libgdal1-dev
sudo apt-get install libgeos-c1 libgeos-dev

Step 4: install PostgreSQL

sudo apt-get install postgresql-9.1 postgresql-client-9.1 postgresql-contrib-9.1 postgresql-server-dev-9.1
sudo nano /etc/postgresql/9.1/main/pg_hba.conf

# Scroll down to the bottom of the file and change the 
# words ident and md5 to “trust” (there should be 3). 

Step 5: install and configure PostGIS

cd /usr/local/src
wget http://download.osgeo.org/postgis/source/postgis-2.0.2.tar.gz
tar xzf postgis-2.0.2.tar.gz
cd postgis-2.0.2
./configure --with-raster --with-topology

make
make install
# add bin to path
export PATH=/usr/lib/postgresql/9.1/bin/:$PATH

Step 6: Configure Postgis template

# create and run setup script
touch /var/lib/postgresql/config_db.sh

nano /var/lib/postgresql/config_db.sh

#!/usr/bin/env bash
POSTGIS_SQL_PATH=/usr/share/postgresql/9.1/contrib/postgis-2.0
createdb -E UTF8 template_postgis
createlang -d template_postgis plpgsql
psql -d postgres -c \ "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis'"
psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql
psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql
psql -d template_postgis -f $POSTGIS_SQL_PATH/legacy.sql
psql -d template_postgis -f $POSTGIS_SQL_PATH/rtpostgis.sql
psql -d template_postgis -f $POSTGIS_SQL_PATH/topology.sql
psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"

sudo su - postgres 
bash config_db.sh

# confirm template exists with
psql --command="\l"
exit

Step 7: Install Mapnik v2.1.0 as unable to install 2.2.0 (broken packages)

# install boost (q: with suggests? --install-suggests, a: so far seems ok without)
sudo apt-get install libboost1.49-dev

# then install mapnik
sudo apt-get install libmapnik-dev python-mapnik mapnik-utils

# can test with
python
import mapnik

Step 8: Install PIP

easy_install pip

Step 9: Install some required python modules (modestmaps fails without extensions)

sudo pip install -U werkzeug
sudo pip install -U simplejson
sudo pip install -U modestmaps --allow-external modestmaps --allow-unverified modestmaps

Step 10: Install PIL (fails without quirky symlink fixes below)

sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

now install PIL itself

sudo pip install -U pil

but since this still fails, do this instead (this package should include PIL per this post)

sudo apt-get install python-imaging

Step 11. Download TileStache

git clone https://github.com/migurski/TileStache
cd TileStache

Step 12. Install TileStache globally and add a couple of additional dependencies (see this)

sudo python setup.py install
sudo pip install -U shapely
sudo pip install -U psycopg2

Step 13. Server config using mod python *(conf.d files included by default)

sudo nano /etc/apache2/conf.d/httpd.conf

add this (headers is an addition to allow cross-domain requests, NOTE: * may not be secure):

<Directory /var/www/tiles>
  AddHandler mod_python .py
  PythonHandler TileStache::modpythonHandler
  PythonOption config /etc/TileStache/tilestache.cfg
  Header set Access-Control-Allow-Origin "*"
</Directory>

For more on CORS see this page

now

# Altering headers requires the use of mod_headers. Mod_headers is enabled by default in Apache, however, you may want to ensure it's enabled by running:
a2enmod headers
# then check config for errors, and if ok, restart/reload apache with 
apachectl -t
sudo service apache2 reload

then

mkdir /var/www/tiles
shutdown -r now # reboot changes

Step 14. Test server (second tests Mapnik install, too)

http://192.241.206.80/tiles/tiles.py/osm/preview.html
http://192.241.206.80/tiles/tiles.py/example/preview.html

apache restart if needed:

sudo /etc/init.d/apache2 restart

Tilestache config notes

  • First make sure data are projected in Spherical Mercator
  • Spherical Mercator EPSG:900913 (EPSG:3857) # went with latter 900913 wouldn't display
  • Reproject in QGIS
  • or use ogr2ogr

ogr2ogr reproject

ogr2ogr -t_srs epsg:900913 shapes/parcels_with_history_merc.shp shapes/parcels_with_history.shp 

Add shape to local pg/postgis

createdb parcel_data -U postgres
psql -d parcel_data -U postgres
parcel_data# CREATE EXTENSION postgis;
CREATE EXTENSION
parcel_data# CREATE SCHEMA sf;
CREATE SCHEMA

Convert shape to SQL

shp2pgsql -I -s 900913 parcels_with_history_merc.shp sf.parcels | psql -d parcel_data -U postgres

Full TileStache config file for vector tiles and utf grid

{
  "cache":
  {
    "name": "Test",
    "path": "/tmp/stache",
    "umask": "0000"
  },
  "layers":
  {
    "osm":
    {
        "provider": {"name": "proxy", "provider": "OPENSTREETMAP"},
        "png options": {"palette": "http://tilestache.org/example-palette-openstreetmap-mapnik.act"}
    },
    "example":
    {
        "provider": {"name": "mapnik", "mapfile": "examples/style.xml"},
        "projection": "spherical mercator"
    },
    "parcels_vec": {
        "allowed origin": "*",
        "provider": {
            "class": "TileStache.Goodies.VecTiles:Provider",
            "kwargs": {
                "dbinfo": {
                    "host": "localhost",
                    "user": "postgres",
                    "database": "parcel_data"
                },
                "queries": [
                    "SELECT gid, geom AS __geometry__ FROM sf.parcels"
                ]
            }
        }
    },
    


    "parcels_utf":
    {
        "provider":
        {
            "name": "mapnik grid", 
            "mapfile": "mapnik_styles/parcels_utf.xml",
            "fields": ["MAPBLKLOT"]
        },   
    }

  }
}

mapnik style sheet for utf grids from postgis

<?xml version="1.0"?>

<!-- An ultra simple Mapnik stylesheet -->

<!DOCTYPE Map [
<!ENTITY google_mercator "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over">
]>

<Map srs="&google_mercator;">
    <Style name="style">
        <Rule>
            <PolygonSymbolizer />
        </Rule>
    </Style>
    <Layer name="layer" srs="&google_mercator;">
        <StyleName>style</StyleName>
    <Datasource>
        <Parameter name="table">
            select gid from sf.parcels
        </Parameter>
        <Parameter name="type">postgis</Parameter>
        <Parameter name="port">5432</Parameter>
        <Parameter name="user">postgres</Parameter>
        <Parameter name="dbname">parcel_data</Parameter>
    </Datasource>
    </Layer>
</Map>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment