Preparing National Land Survey of Finland (Maanmittauslaitos) Maps to AFtrack

This note describes how I prepared NLS (peruskartta) maps in TIFF-format to AFtrack (http://www.afischer-online.de/sos/AFTrack/) in BSB/KAP-format, a Symbian 60 GPS utility. The maps are copyrighted material and not delivered for free.

The workstation is Ubuntu Linux 10.04 LTS and the mobile phone is Nokia E63 with AFtrack 1.09 installed.

Overview of the process

  1. Install libbsb library and tools for making BSB/KAP files
  2. Install netpbm for scaling TIFF files
  3. Create KAP template for a map page
  4. Create BSB/KAP map using the TIFF map file and KAP template
  5. Add the BSB/KAP map to AFtrack
There is also a script that automates phases 3-4.

Detailed description

1) Install libbsb

The first step is to install libbsb library and utilities. This tool makes BSB/KAP maps from TIFF files.

- Get the libbsb source, http://libbsb.sourceforge.net/ (version 0.0.7 was used).
- I had to make a minor change to ppm2bsb.c required by the gcc 4.4.3 in Ubuntu 10.04, see diff output:

71c71
<     int            i, j, idx, *index, magic, max_sample, start_of_raster;
---
>     int            i, j, idx, *index, magic, max_sample, start_of_raster, scanres;
90,92c90,92
<     fscanf(ppm, "P%d\n", &magic);
<     fscanf(ppm, "%d %d\n", &image.width, &image.height);
<     fscanf(ppm, "%d\n", &max_sample);
---
>     scanres = fscanf(ppm, "P%d\n", &magic);
>     scanres = fscanf(ppm, "%d %d\n", &image.width, &image.height);
>     scanres = fscanf(ppm, "%d\n", &max_sample);

2) Install netpbm

Install netpbm tools for the TIFF command-line manipulation (apt-get install netpbm).

Now we have the environment ready and we can begin to work with the maps.

3) TIFF scaling and color reduction

AFtrack needs files uncompressed and with less than 128 colors. According to the AFtracj documentation the images can be larger than the phone memory if they are stored in TIFF/BSB or KAP format. However, large files mean more work for the processor.

NLS map files are 13000 x 13000 pixels where each pixel equals 1 meter. The file size is far too large for my E63. After making some testing I decided to reduce the size to 25%. Your mileage may vary.

Following stream makes the scaling and reducing color depth:
tifftopnm UL4134L_RK1_1.tif | pnmscale 0.25 | ppmquant 127 | pnmtotiff -none >UL4134L_RK1_1_scaled.tif

4) KAP template for a map page

The source maps were delivered with three files for each map page:
In my solution I used TIFF and .tab files for making the BSB/KAP map.

4a) Conversion from ETRS-TM35FIN to WGS84

The supplied maps (.tab files) had coordinates in ETRS-TM35FIN while the KAP template required WGS84. Here is one sample .tab file
!table
!version 300
!charset WindowsLatin1
!Copyright National Land Survey of Finland
!          Helsinki, 20100114 , for MapInfo 7.5
Definition Table
File "UL4134L_RK1_1.tif"
Type "Raster"
(380000.000,6690000.000) (0,0) Label "Pt 1",
(391999.000,6690000.000) (11999,0) Label "Pt 2",
(391999.000,6678001.000) (11999,11999) Label "Pt 3",
(380000.000,6678001.000) (0,11999) Label "Pt 4"
CoordSys Earth Projection 8,115,"m",27,0,0.9996,500000,0
Units "m"
Focus on the rows containing the four calibration points (Label "Pt 1" etc.). They define location of the four edges in ETRS-TM35FIN coordinates. These coordinates must be converted to WGS84 for AFtrack.

It would be nice to make the conversion in the workstation but at this point (15-SEP-2010) such a utility is not available. The conversion was made manually at Map Site. With a command-line conversion tool from ETRS-TM35FIN to WGS84 the whole process could be automatized.

4b) Writing KAP template

After making the coordinate conversion in 3b you're almost ready for writing the new values to KAP template.

!Handmade testi_025.kaptmp
VER/2.0
BSB/NA=Testi25pros
    RA=3000,3000
KNP/GD=WGS84,PR=MERCATOR,PP=55.00,PI=0.000,SP=,SK=0.0000000
    TA=90.0000000,UN=METERS,SD=MEAN SEA LEVEL,DX=4,DY=4
CED/SE=2000,RE=03,ED=01/01/2000
OST/1
REF/1,0,0,60.32873184,24.82674222
REF/2,2999,0,60.33210447,25.04389348
REF/3,2999,2999,60.22442946,25.05031394
REF/4,0,2999,60.22107145,24.83387414
DTM/0,0
CPH/0
IFM/5

4) Run tif2bsb

Finally you have your scaled TIFF file and KAP template. Now it's time to execute the libbsb tool prepared in phase 1:
tif2bsb testi.kapmtp UL4134L_RK1_1_127colors.tif UL4134L_RK1_1.kap

5) Install the KAP file to the phone

  1. Upload the .kap file to your phone. You may create a folder for the map files.
  2. Define the folder as a AFtrack map folder: Map > List Maps (0) > Options > Define new Directory > select your directory.
  3. Turn WMS off (by default you may have WMS: OpenStreetMap).
  4. Select "Auto Map (0)".

Script that makes conversion

The attached script nls_to_bsbkap.pl makes the conversion for all files in a directory.

Before using you need two CPAN modules.
  1. Image::Resize - Ubuntu has this packaged, install by sudo apt-get install libimage-size-perl
  2. Geo::Coordinates::ETRSTM35FIN - Unfortunately this module does not have Ubuntu/Debian package. To install the module you have two options:
    1. If you have root privileges you can install the module system-wide.
    2. It is possible to install the module under your home directory. However, a more straightforward method is to grab the source package (Geo-Coordinates-ETRSTM35FIN-x.y.tar.gz) and place the containing folder Geo-Coordinates-ETRSTM35FIN-0.01/lib/Geo and its contents to a directory where you later execute nls_to_bsbkap.pl.
The script does not take any parameters, you have to define the source, temp and destination directory by editing the script.
my $PATH_SOURCE = '../mml/peruskartta/2010/RK1_1/L4';
my $PATH_TEMP = '/tmp';
my $PATH_RESULT = '/tmp';

my $MAP_SCALE = 0.25;
$PATH_SOURCE is the directory where the maps are. $PATH_TEMP is for the temporary files and $PATH_RESULT for the final xxx.kap files. $MAP_SCALE is the scaling factor for the maps.

References

Updated 19-NOV-2011 / Matti Lattu