Tile proxy server, make those GIS webpages work even without connectivity

Logo_MapProxy.svgYou know when you spend all that time building a really slick webpage with a great map feature and then find out it completely doesn’t work when the ship loses connectivity?  Well I do (with OpenVDMv2).  Here’s how I mitigated the situation.

The Problem

About 6 months ago I was really proud of myself when I added a really slick GIS feature to OpenVDMv2 that leveraged Leaflet and the Ocean Basemap recently made available ESRI.  About a week later I got an email from a marine tech on the Endeavor (where I had just installed the feature) saying OpenVDMv2 was running exceptionally slow due to constant requests to an ESRI server that were timing out due to no connectivity.

Well that was disappointing, but it was a great real-world situation that I would need to overcome if I wanted this feature to stay in OpenVDMv2.  The good news is that the earth’s topology and bathymetry change fairly slowly.  If I could point OpenVDMv2 to map tiles cached on a local server then I would be able to get around this problem.

One Solution: MapProxy

As with 99% of the problems I try to solve, I was not the first person to face this issue.  Turns out there are a plethora of people with the same problem, enough that a bunch of folks more software savvy than I had devised some clever solutions.  The one that best fit my needs was MapProxy.

MapProxy is like other web-proxy solutions, except it is tailored specifically to GIS and therefore can do some neat stuff (think: coordinate translations, tile format conversions, etc).  All I needed was a simple caching of the ESRI basemap, which proved to be incredibly easy.

The Install Process

Now I can’t go into the installation process for every Operating System so I’m only going to provide the instructions for Xubuntu 14.04 LTS.  Please adjust for your particular OS as required.  The MapProxy website is a great resource.

Install the dependencies

sudo apt-get install python-pip python-imaging python-yaml libproj0 libgeos-dev python-lxml libgdal-dev python-shapely

Install MapProxy.

sudo pip install MapProxy

Build the initial configuration.  I’m assuming you are in your home directory.

mapproxy-util create -t base-config mapproxy

The last command creates a file called ~/mapproxy/mapproxy.yaml. You will need to replace the contents of that file with:

# -------------------------------
# MapProxy configuration.
# -------------------------------

# Start the following services:
services:
  demo:
  tms:
    use_grid_names: true
    # origin for /tiles service
    origin: 'nw'
  kml:
    #use_grid_names: true
  wmts:
  wms:
    srs: ['EPSG:900913']
    image_formats: ['image/png']
    md:
      title: MapProxy WMS Proxy
      abstract: This is a minimal MapProxy installation.

#Make the following layers available
layers:
  - name: WorldOceanBase
    title: ESRI World Ocean Base
    sources: [esri_worldOceanBase_cache]

  - name: WorldOceanReference
    title: ESRI World Ocean Reference
    sources: [esri_worldOceanReference_cache]

caches:
  esri_worldOceanBase_cache:
    grids: [esri_online]
    sources: [esri_worldOceanBase]

  esri_worldOceanReference_cache:
    grids: [esri_online]
    sources: [esri_worldOceanReference]

sources:
  esri_worldOceanBase:
    type: tile
    url: http://server.arcgisonline.com/arcgis/rest/services/Ocean/World_Ocean_Base/MapServer/tile/%(z)s/%(y)s/%(x)s.png
    grid: esri_online

  esri_worldOceanReference:
    type: tile
    transparent: true
    url: http://server.arcgisonline.com/arcgis/rest/services/Ocean/World_Ocean_Reference/MapServer/tile/%(z)s/%(y)s/%(x)s.png
    grid: esri_online

grids:
  webmercator:
    base: GLOBAL_WEBMERCATOR

  esri_online:
     tile_size: [256, 256]
     srs: EPSG:900913
     origin: 'nw'
     #num_levels: 25

globals:

Now move the installation to it’s final location and set the user/group ownership

sudo cp ~/mapproxy /var/www/
sudo mkdir /var/www/mapproxy/cache_data
sudo chmod 777 /var/www/mapproxy/cache_data
sudo chown -R root:root /var/www/mapproxy

Prepare Apache2 to host the MapProxy installation

sudo apt-get install libapache2-mod-wsgi

Prepare the MapProxy installation for integration with the Apache2 web-server

cd /var/www/mapproxy
sudo mapproxy-util create -t wsgi-app -f mapproxy.yaml config.py

Edit the apache conf

sudo pico /etc/apache2/sites-available/000-default

Add the following just above </VirutalHost> at the end of the file

WSGIScriptAlias /mapproxy /var/www/mapproxy/config.py

<Directory /var/www/mapproxy/>
  Order deny,allow
  Allow from all
</Directory>

Restart Apache2

sudo service apache2 restart

Verify the installation works by going to http://<servername or IP>/mapproxy/demo/

There’s a working demo of this site on the OpenVDMv2 demo site: click here

All done!

Well, almost all done

When there is no connectivity the MapProxy server can only serve tile that are in it’s cache.  Spend some time on your demo site looking at the areas your vessel typically operates at every possible zoom level.  This will help you pre-populate the cache for when there is no connectivity.

I hope this helps,
– Webb

This entry was posted in General and tagged , , on by .

About webbpinner

I'm Webb, the owner/operator of oceandatarat.org. I started this blog to document some knowledge and tricks I've picked up along the way. My goal is to share what I know in hope that it is useful to others. I'm also the owner operator of Capable Solutions, a small company focused on helping oceanographers and vessel operators turn diesel fuel into quality data.

Leave a Reply