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
There is also a script that automates phases 3-4.
- Install libbsb library and tools for making BSB/KAP files
- Install netpbm for scaling TIFF files
- Create KAP template for a map page
- Create BSB/KAP map using the TIFF map file and KAP template
- Add the BSB/KAP map to AFtrack
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:
< int i, j, idx, *index, magic, max_sample, start_of_raster;
> int i, j, idx, *index, magic, max_sample, start_of_raster, scanres;
< 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);
- Make sure that you have libtiff development package installed (apt-get install libtiff4-dev).
- After this the compiling and installation could be carried out as explained in README file.
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
- tifftopnm converts tiff file for portable raster for further processing
- pnmscale makes the scaling
- ppmquant reduces the color depth for 127 or less colors
- pnmtotiff converts the portable raster to tiff without compressing
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.
- TIFF file containing a raster map, 13000 x 13000 pixels
- MapInfo .tab file describing map datum, projection etc. and calibration points
- GeoTIFF .tfw file for alternate calibration method
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
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.
!Copyright National Land Survey of Finland
! Helsinki, 20100114 , for MapInfo 7.5
(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
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.
TA=90.0000000,UN=METERS,SD=MEAN SEA LEVEL,DX=4,DY=4
- BSB/NA: This name tag is displayed in the AFtrack map selection menu.
- BSB/RA: Map size in pixels. Originally the map was 12000x12000 but after scaling the size is 3000x3000.
- KNP: All values are from the sample template in the AFtrack documentation.
- KNP/DX, KNP/DY: In the original map file one pixel equals 1 meter. After the 25% scaling one pixel equals 4 meters.
- CED: All values are from the sample template in the AFtrack documentation.
- OST: From the sample template in the AFtrack documentation.
- REF/x: Here are the four calibration points. First two numbers
are pixel coordinates (x, y) and the last two are WGS84 coordinates
(N-S, E-W). Please note that the pixel coordinates are in x, y format
but location coordinates the order is opposite (y, x).
- DTM: From the sample template in the AFtrack documentation.
- CPH: From the sample template in the AFtrack documentation.
- IFM: From the sample template in the AFtrack documentation.
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
.kap file to your phone. You may create a folder for the map files.
- Define the folder as a AFtrack map folder: Map > List Maps (0)
> Options > Define new Directory > select your directory.
- Turn WMS off (by default you may have WMS: OpenStreetMap).
- Select "Auto Map (0)".
Script that makes conversionThe attached script nls_to_bsbkap.pl makes the conversion for all files in a directory.
Before using you need two CPAN modules.
The script does not take any parameters, you have to define the source, temp and destination directory by editing the script.
- Image::Resize - Ubuntu has this packaged, install by
sudo apt-get install libimage-size-perl
- Geo::Coordinates::ETRSTM35FIN - Unfortunately this module does not have Ubuntu/Debian package. To install the module you have two options:
- If you have root privileges you can install the module system-wide.
- 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.
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
$MAP_SCALE is the scaling factor for the maps.
Updated 19-NOV-2011 / Matti Lattu