← Back to team overview

ubuntu-phone team mailing list archive

How To Use OSM Tiles Offline with uNav


How To Use OSM Tiles Offline with uNav

The problem

The app for UbuntuTouch "uNav" works only with Internet connection
when presenting maps and locations. Sometimes Internet is not reliable
or over data mobile even expensive and the idea to workaround is having
the maps' tiles prefetched and stored in the Ubuntu phone. Here is the
tool chain so solve this.

1. Prefetching OSM tiles for a given area of interest and 

There is a C-written tool 'osmtiles' to be fetched an compiled
on your Linux or FreeBSD workstation: http://www.millions.ca/~stacy/osmtiles.tgz
Compilation is simple (see its README):

    tar xzf osmtiles.tgz
    cd osmtiles
    cc -o osmtiles osmtiles.c -lm

The tool 'osmtiles' allows to make a list of all OSM tiles of a given GPS
coordinates of an area, for example:

     osmtiles -z 9 -Z 18                \
        23.178555 -82.462692            \
        23.050039 -82.288628            \
        > havana.txt 

The above region 23.178555 -82.462692 x 23.050039 -82.288628 is the capital
of Cuba, Havana. The output of the tool is a list file like this:


Some ~18.000 lines, i.e. tiles, to fetch. This list is stored for fetch (and
later updates) in a file, let's say havana.txt. I have a bunch of such files
for the places I visit.

Based on this list I have a shell script which in principle does for
any of the files in the list:

    while read name ; do
        fetch the MD5 sum from the server for the file http://tile.openstreetmap.org/$name
	if the MD5 is different from what I have
	    fetch the file http://tile.openstreetmap.org/$name
    done < havana.txt

This way, later on updates, only modified tiles will be fetched.
I can share this script, no problem, if you promise not to blame
me, but send patches for it.

Next step is making, again based on the file of the place to visit,
a tar archive:

    cd osm
    tar --files-from=../Havana.lst -czf ../Havana.tgz
    ls -lh ../Havana.tgz
    -rw-r--r--  1 guru  wheel    93M 24 may.  16:58 ../Havana.tgz

i.e. such a file is around 100 MByte in size.

Move these file(s) to the phone with SCP or ADB and unpack them on
the SD card to some directory which will later be served by a small
Python web server.

2. The modifications in the phone

We start with copy over the file Havana.tgz to the phone and unpack it there:
    scp -p Havana.tar.gz phablet@
    ssh phablet@

    ls /media/phablet/
    mkdir /media/phablet/9CC0-D6CE/Maps
    cd /media/phablet/9CC0-D6CE/Maps
    tar xzf ~/Havana.tar.gz
Note: the dir `9CC0-D6CE` may vary depending on the SD type. Change
this to the name in your phone and later in the start script for the
Python web server too. Unpacking the tar archive should give:
    ls /media/phablet/9CC0-D6CE/Maps/
    ls /media/phablet/9CC0-D6CE/Maps/osm
    10  11  12  13  14  15  16  17  18  9

i.e. the tiles in their sub-dirs `9 ... 18`.

To start our special `uNav-osm` app we need a new desktop entry as
`~/.local/share/applications/unav-osm.desktop` which must contain the following
    [Desktop Entry]
    Exec=/usr/bin/nohup /home/phablet/unav.sh
As you see it will not execute the `uNav` app directly, but a small shell script
`/home/phablet/unav.sh`. This script starts upfront the Python web service and
then the `uNav` app itself. It has the folling few lines.

    cd /media/phablet/9CC0-D6CE/Maps
    python3 -m http.server 8888 &
    cd /userdata/system-data/opt/click.ubuntu.com/navigator.costales/current
    qmlscene %u qml/Main.qml > /dev/null 2> /dev/null
Create this script `/home/phablet/unav.sh` with `vi` and make it executable:
    vi /home/phablet/unav.sh
    chmod 0755 /home/phablet/unav.sh
Last action is to make a small modification in `uNav` itself. At the time of
writing it is the version 2.3 and the fix must be done in this file:
What to change is best visible in this diff:
    cd /userdata/system-data/opt/click.ubuntu.com/navigator.costales/current/nav
    diff index.html index.html.orig
< 		source: new ol.source.OSM({url: 'http://localhost:8888/osm/{z}/{x}/{y}.png'}),
> 		source: new ol.source.OSM({url: 'http://{a-c}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png'}),

The file must be modified as root and it's a good idea to make a copy (like I did):
    sudo su
    cd /userdata/system-data/opt/click.ubuntu.com/navigator.costales/current/nav
    cp -p index.html index.html.orig
    vi index.html
Note: the URL `http://{a-c}.basemaps.cartocdn.com/rastertiles/...` appears three times
in the file. Change only the first location for `/voyager/....` because this is also
what you want to pick-up in the `Settings` menu of `uNav` for the value:

    Online style
    Carto Voyager        <---------------
That's it as changes.

One final note: You have only offline tiles for small places and not the world between.
So, you can't slide over the world, for example from Munich to Havana. If you
leave your location of offline tiles you will see only a white map in `uNav`.
The way to move is configure in `Settings` some other `Online style`,
for example `Mapbox`. Or configure locations as `Favourites` to jump over the
Matthias Apitz, ✉ guru@xxxxxxxxxxx, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub
May, 9: Спаси́бо освободители! Thank you very much, Russian liberators!