Integrating OpenStreetMap in Bing Maps (Part 1)

image

Introduction

Bing Maps has a very good coverage with roadmaps and aerial imagery. However, there are regions where our data providers have gaps. Crowed-sourcing of geospatial data might be an option to fill these gaps and one of the most active communities is around OpenStreetMap. The OpenStreetMap data is available under the Create Common Attribution-Share Alike 2.0 License which means that as long as you provide the correct attribution to the source you should be fine to integrate the data with Bing Maps.

While it is theoretically possible to link directly to the OpenStreetMap tile servers, users who have a high load are requested to run their own tile servers. This also allows you to take control over availability and scalability of the tile servers.

Requirements

  1. PostgreSQL / PostGIS
    A simple way to install PostgreSQL and PostGIS is the free Postgres Plus Standard Server 8.x from EnterpriseDB.
  2. OpenStreetMap data as .osm XML-file. You can download the whole world or extracts for certain countries or regions here. You can also export smaller regions directly from OpenStreetMap:
    image
  3. osm2pgsql – an OpenStreetMap data to PostgreSQL converter and loader
  4. Python 2.5 – Mapnik doesn’t run with newer Python version
  5. Mapnik
  6. If we are working with Windows we’ll need a file-compression-tool that can handle tar and tar.bz2 archives. I use 7-Zip.
  7. A SVN client. I use Slik SVN.
  8. A Bing Maps Developer Account
  9. The Bing Maps Silverlight Control
  10. Visual Studio 2008 Service Pack 1 or higher
  11. The Silverlight 3 Tools for Visual Studio 2008 SP1
  12. A bit of your time 🙂

Preparing a Spatial-Enabled Database in PostgreSQL

  1. Installing PostgreSQL / PostGIS is very simple – particularly if you use the installer from EnterpriseDB.
  2. Once we have installed the PostgreSQL / PostGIS we can use the graphical user interface pgAdmin III to create a new login role and database.
    image 
  3. Let’s start with a new login role:
    We provide a role name and a password…
    image

    …and to keep it simple we just grant all privileges.
    image

  4. Next we create a new database – let’s call it “gis” – using the PostGIS-template.
    image 

That’s already all we have to do here.

image

Installing osm2pgsql

  1. After downloading osm2pgsql extract it into a new folder and add it to the path in the environmental variables:
    image
  2. Now we open a command prompt and change into the directory where we have extracted osm2pgsql. In order to load the data we execute the following command:

    osm2pgsql -m -s -d gis -U jkebeck -W c:usersjkebeckdownloadsOpenStreetMapmadagascar.osm.bz2

  3. You can get help on osm2pgsql by typing osm2pgsql –h in the command prompt. The parameters we used are:

    -m | Store data in spherical mercator
    -s | Store temporary data in the database. This greatly reduces the RAM usage but is
    much slower.
    -d | The name of the PostgreSQL database to connect to
    -U | Postgresql user name
    -W | Force password prompt

    madagascar.osm.bz2 is the name of the OpenStreetMap data file that we want to load.

Installing Mapnik

  1. First we download and install Python 2.5. Note: Mapnik doesn’t run with newer versions of Python.
  2. Next we download Mapnik and extract it into the root-directory of our C-drive.
  3. Now we add the lib-path to our environment variables:
    image
  4. Next we create a new environment variable PYTHONPATH and add C:mapnik_0_7_0python2.5site-packages as value.
    image
  5. To test if the installation was successful we open a command prompt and change to the Python-directory, e.g. C:Python25. We type the command

    from mapnik import *

    If we don’t receive an error message this is already a good sign. Let’s make a final test by opening the folder C:mapnik_0_7_0demopython in the Windows Explorer and double-clicking rundemo.py. If this creates a couple of demo.*-files we’re done with this step.

Rendering with Mapnik

  1. Mapnik uses external data for coastlines at lower zoom levels. The files we have to download are
    1. http://tile.openstreetmap.org/world_boundaries-spherical.tgz
    2. http://tile.openstreetmap.org/processed_p.tar.bz2
    3. http://tile.openstreetmap.org/shoreline_300.tar.bz2

Extract all 3 archives into a world_boundaries-folder into the Mapnik-directory:
image

  1. Download and install a SVN-client. I use Slik SVN
  2. Export the Mapnik stuff from the OSM subversion repository. Open a command prompt and execute the command

    svn export http://svn.openstreetmap.org/applications/rendering/mapnik

  3. Now move the downloaded content into your Mapnik-directory.
  4. The osm.xml file is built up out of the core osm.xml file and XML include files in the inc/ directory. The inc/ directory holds 3 .template files, which are used as inputs for the generate_xml.py script. To generate our own XML-file with the connection to PostgreSQL / PostGIS we open a command prompt, navigate to the Mapnik-directory and execute the command

    generate_xml.py osm.xml osm-local.xml –host localhost –user jkebeck –dbname gis –symbols ./symbols/ –world_boundaries ./world_boundaries/ –port 5432 –password *Not4All*

  5. We might run into issues where some columns are not found, e.g. shop:
    image 

    In that case we have to remove all occurrences of this column in the files osm.xml and layer-amenity-points.xml.inc. Once this is sorted run the command again.

  6. We are almost ready to render the tiles. Let’s open the Python-script generate_tiles.py in a text editor and edit the parameters for the bounding box at the bottom of the file:
  7. #————————————————————————-
    #
    # Change the following for different bounding boxes and zoom levels
    #
    # Antanarivo, Madagascar
    #
    minZoom = 6
    maxZoom = 17
    bbox = (47.382, -19.042, 47.638, -18.735)
    render_tiles(bbox, mapfile, tile_dir, minZoom, maxZoom)

  8. In order to render the tiles we can now set a few environment variables and execute the Python-script generate_tiles.py.
  9. set HOME=C:mapnik_0_7_0
    set MAPNIK_MAP_FILE=osm-local.xml
    set MAPNIK_TILE_DIR=C:tmpTileCacheOSM
    generate_tiles.py

Advertisements
This entry was posted in Bing Maps. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s