← 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
guru@xxxxxxxxxxx


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:

```
    9/138/222.png
    10/277/444.png
    11/554/888.png
    11/554/889.png
    11/555/888.png
    11/555/889.png
    12/1109/1776.png
    ...
```

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@192.168.2.102:.
    ssh phablet@192.168.2.102

    ls /media/phablet/
    9CC0-D6CE
    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/
    osm
    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
lines:
```
    [Desktop Entry]
    Name=unav-osm
    Type=Application
    Exec=/usr/bin/nohup /home/phablet/unav.sh
    Icon=/userdata/system-data/opt/click.ubuntu.com/navigator.costales/current/icon.png
    Terminal=false
    X-Ubuntu-Touch=true
```
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.

```
    #!/bin/sh
    #
    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:
`/userdata/system-data/opt/click.ubuntu.com/navigator.costales/current/nav/index.html`
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
210c210
< 		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:
```
    Mode
    Online

    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
Ocean.
-- 
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!