Deploying your own Slippy Map Gentoo

From OpenStreetMap Wiki
Jump to: navigation, search

On gentoo things work a little different than on the other distro's so I decided to dedicate this page to Deploying your own Slippy Map on Gentoo. I hacked some ebuilds (nasty hacks) and compiled some programs so many times that I can't be totally sure if it is correct. However that is the beauty of a wiki that YOU can correct my silly mistakes.

I will use Mapnik, TileCache and Openlayers to create the slippy map.

This page is based on Deploying your own Slippy Map.

Setting up your system

You will see version numbers in each emerge. This is because I have not tested it with other versions.

Required packages

The following packages are needed too.

  • dev-util/subversion

Overlay

Until Mapnik 7.1 gets into the main portage tree you will need to manually update the ebuild. You can use The one proposed in the Gentoo bugzilla version bump request along with the supplied patch file: http://bugs.gentoo.org/show_bug.cgi?id=311129

If that one doesn't work you can also try this one:

Download the ebuild to your local overlay. (See here for details on using overlays) Then create the digest with this command:

ebuild /usr/local/portage/sci-geosciences/mapnik/mapnik-*.ebuild digest

Use Flags

Set the following useflags:

  • geos
  • gdal
  • proj
  • postgres
  • python
  • icu (for boost, see Mapnik wiki)

Postgres

Run:

emerge ">=dev-db/postgresql-base-9.0" ">=dev-db/postgresql-server-9.0"

As seen in the command, you want version 9.0 or higher. If you are running on something other than x86 or amd64 you will need to add a line to your /etc/portage/package.keywords file to tell it to use the "unstable" version for your platform. For ppc:

dev-db/postgresql-base ~ppc

Postgis

We need version 1.5.2 or higher.

Install with:

emerge ">=dev-db/postgis-1.5.2"

Mapnik

Installation

Mark as unstable in /etc/portage/package.keywords as needed:

sci-geosciences/mapnik ~amd64
sci-libs/geos ~amd64
sci-libs/gdal ~amd64

Install:

emerge mapnik

Testing

There are three kinds of tests: 1) "visual test" where PNG images that comprise part of the delivered testing suite are compared to images generated by the new mapnik, 2) "C++" tests, and 3) "Python" tests. The first and third may be called individually with Python, the second test "C++" is shell script driven. The first and third tests appears to be based on Python 2.7 and despite the documentations assertion that mapnik will work with Python 3.2, the test suite appears to be based upon Python 2.7. Therefore, if you compile mapnik with Python 3.2, be ready to run the tests with Python 2.7. There seem to be some python conversion scripts which may allow 3.2 to run 2.7 code, but that has not been tested.

If you want to use the project's tests, then install preserving, or "keeping", the build directory:

FEATURES="keepwork" emerge sci-geosciences/mapnik

You will find the build directory under /var/tmp/portage/sci-geosciences/mapnik-xxx

Manually Running Tests

Running the mapnik test suite is accomplished by going into the temporary build directory and invoke the suite either by "make" or using Python for the "visual" and "Python" tests and a shell script for the "C++" test. The shell script run_tests contains the test suite.

make
 cd /var/tmp/portage/sci-geosciences/mapnik-2.2.0-r1/work/mapnik-v2.2.0
 make test
Python

The first test, "visual" may be called with:

   [In the temporary build directory]
   python tests/visual_tests/test.py -q

or for more detail without the "quiet" flag

   [In the temporary build directory]
   python tests/visual_tests/test.py 

Note: the "visual" test suite generates a helpful HTML page under /tmp showing two columns of PNGs, expected vs. actual results, if any of the visual tests fail. Otherwise, if the visual test suite PASSES, the comparison page is not generated.

The third and last test is called with the "-q " flag [quiet] in the accompanying Makefile; if you invoke the test without the -q, then you get reasonable status messages for each test, whereas quiet mode only gives a hint if something goes awry. You can also get more detail by using the -v [verbose] flag which really outputs a tremendous amount of detail. Test 3 "Python" may be invoked without the -q flag:

  [In the temporary build directory]
  python tests/run_tests.py

Get files

Get all the files you need.

Get the planet file

Get a planet file or get an extract (saves time to download)

Get the world boundries

Setting up the database

As the database is pretty large you might not want to store it in the default location (own partition maybe).

First su into the postgres user:

su - postgres

Create the directory where you want the database

mkdir /path/to/new/dir

Now initialise the database:

initdb --pgdata /path/to/new/dir

Now create a new user and a new database:

createuser <username>
createdb -E UTF8 -T template0 -O <username> <db>
createlang plpgsql <db>

The user needs to (temporarily) be a superuser to be able to add postgis in the next step.

Now run:

psql -U <username> -d <db> -f /usr/share/postgresql-9.0/contrib/postgis-1.5/postgis.sql
psql -U <username> -d <db> -f /usr/share/postgresql-9.0/contrib/_int.sql
psql -U <username> -d <db> -f /usr/share/postgresql-9.0/contrib/postgis-1.5/spatial_ref_sys.sql
echo "ALTER TABLE geometry_columns OWNER TO <username>;" | psql -d <db>
echo "ALTER TABLE spatial_ref_sys OWNER TO <username>;"  | psql -d <db>

Permissions

Make sure you add the users that are going to use the database (user running osm2pgsql, the webserver) to the postgres group

gpasswd -a <username> postgres

Fill the database

Now that the database is all set lets fill it

osm2pgsql

There is no osm2pgsql package for gentoo (yet) so get it from svn and compile it:

svn co http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/
cd osm2pgsql
./autogen.sh
./configure
make

Importing the database

./osm2pgsql -U <user> -m -d <db> <planet-file>

If you only want a section of the world do:

./osm2pgsql -U <user> --bbox minlon,minlat,maxlon,maxlat -m -d <db> <planet-file>

Mapnik

Checkout the osm mapnik stuff:

svn checkout http://svn.openstreetmap.org/applications/rendering/mapnik
cd mapnik

Create a local copy of set-mapnik-env and edit the variables inside (they are documented there).

Test Mapnik

You can edit generate_tiles if you want to specify a bounding box.

See the main Mapnik article on setting up mapnik for your environment.

TileCache

Download TileCache (tilecache.org) and setup your webserver correctly (see documentation of tilecache for that).

Edit tilecache.cfg for it to suit your needs

You can now browse tilecache on: http://<yourdomain>/tilecache.cgi/1.0.0/osm/z/x/y.png

Make sure this is contained in the index.html file (included in tilecache)

function init(){
            map = new OpenLayers.Map('map',
                            { maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
                              numZoomLevels: 19,
                              maxResolution: 156543,
                              units: 'm',
                              controls: [new OpenLayers.Control.MouseDefaults(),
                                new OpenLayers.Control.LayerSwitcher(),
                                new OpenLayers.Control.MousePosition({formatOutput: myFormat}),
                                new OpenLayers.Control.PanZoomBar()],
                              displayProjection: new OpenLayers.Projection("EPSG:4326"),
                              projection: new OpenLayers.Projection("EPSG:900913") });

            var layerMapnik = new OpenLayers.Layer.TMS(
                        "Mapnik",
                        "tilecache.cgi/1.0.0/osm/",
                                        {type:'png', getURL: get_osm_url,
                        border: 1} );

            map.addControl(new OpenLayers.Control.LayerSwitcher());

            map.addLayers([layerMapnik]);
            // Must add permalink after adding layers, otherwise it causes
            // layers to be loaded too early.
            var subpagelookup = {
              mapnik: [layerMapnik]
            };
            if( subpagelookup[subpage] != null )
            {
              map.setBaseLayer(subpagelookup[subpage][0]);
              map.setLayerIndex(subpagelookup[subpage][0], 0);
            }

            map.addControl(new OpenLayers.Control.Permalink() );

            map.addControl(new OpenLayers.Control.MousePosition(
               {prefix: "RD Coordinates (", separator: ",", suffix: ")",
                displayProjection: new OpenLayers.Projection("EPSG:28992"), numdigits:0, displayClass: "olTilePosition" }));

            if(!map.getCenter()) {
              var pos;
              if( subpagelookup[subpage] != null && subpagelookup[subpage].length >= 3 )
                pos = new OpenLayers.LonLat(subpagelookup[subpage][1],subpagelookup[subpage][2]);
              else
                pos = new OpenLayers.LonLat(5,52.25);
              map.setCenter(pos.transform(map.displayProjection,map.projection), 10);
            }

            var info = document.getElementById("info_"+subpage);
            if( info != null )
            {
              info.style.display = "block";
              document.getElementById("info").style.display = "none";
            }
            var info = document.getElementById("forum_"+subpage);
            if( info != null )
            {
              info.style.display = "block";
            }
        }

        function get_osm_url (bounds) {
            var res = this.map.getResolution();
            var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
            var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
            var z = this.map.getZoom();
            var path = z + "/" + x + "/" + y + "." + this.type;
            var url = this.url;
            if (url instanceof Array) {
                url = this.selectUrl(path, url);
            }
            return url + path;
        }