User:Jeroen Muris/mkgmap script

From OpenStreetMap Wiki
Jump to: navigation, search

Since September 2009 I use mkgmap convert OSM data for use on my Garmin GPS. Is started off as a bash script on OS X, but after I was forced to move to Windows I rewrote it as a batch file. The way it is set up, is that I can run conversions on any machine with Windows, Java and USB.

This script does not install the converted map in MapSource or anything, but all tiles with the corresponding TYP file are in one folder, and a 'gmapsupp.img' file is installed in a (different) folder - and if the script is on your GPS device, that folder can be the one where your device is looking for it.

This description, and the script provided, is only given as an example of how such a conversion can be done. Use at your own risk!

Set up

The set up is a follows:

  • I have a folder 'Mkgmap' on my GPS.
  • In this folder is the main script (below).
  • Also in this main folder is curl.exe, used for downloading a planet file. This executable can also be somewhere in the %PATH%.
  • In subfolders of the main folder are the mkgmap and splitter installations. For the subfolders I use the names as used in the downloaded ZIP files for these programs, e.g. "mkgmap-r2105". These names are referred to in the main script. It is also possible to define a different location for these Java applications (but that must be the same for both).
  • There is a 'styles' folder in the main folder that contains (a) a slightly modified version of the default mkgmap style and (b) numbered 'overlay' styles.
  • Also in the main folder I have a dedicated TYP file to go with my style definitions.

Procedure

The script performs the following tasks:

  1. If no downloaded OSM file exists, one is downloaded. If so, any existing tiles from a previous run are removed.
  2. If no tiles from a previous run are found, the splitter is run. An existing list of areas is used when available.
  3. All tiles are converted once for every style in the 'styles' folder, but only if the corresponding *.img file doesn't exist (on a file by file basis). The output *.img files are saved in a dedicated folder, to facilitate copying or removing them.
  4. All converted tiles are merged together with the dedicated TYP file into one 'gmapsupp.img' file in a chosen folder.

Main script

echo off

rem Command file to convert OSM map to Garmin

rem Needed: curl.exe in active directory or path + splitter and mkgmap in %java_apps%\%mkgmapversion% & %java_apps%\%splitterversion%
rem %java_apps% defaults to script directory, %mkgmapversion% and %splitterversion% need to be set
rem %mkgmapversion% can direct to compiled version from SVN: mkgmapversion=<svn-directory>\dist

rem Java (version 1.6 or later) must be configured with java.exe in the path
rem Java heap size (%javaheap%) mag niet groter zijn dan beschikbaar RAM geheugen

rem A new *.osm file is only downloaded when there's none present
rem Download can be a gzipped planet file or in protobuf format

rem The planet file is split only if there are no tiles present yet
rem The splitter is set up to use an existing areas.list - if present
rem The maximum number of tiles is 99, as there are two positions to loop through the tiles

rem For the conversion a default style definition is used, plus optional extra layers/styles
rem These should be present as numbered subdirectories in %stylefoler%
rem There can be up to nine extra layers/styles, numbered %firststyle% to %laststyle%
rem The optional layers are on top of the default layer, with priority for the first layer set to %basepriority%

rem The script does not install maps into MapSource
rem The tiles of the final map will be written to %tilefolder%, this path should not contain spaces
rem a separate gmapsupp.img will be written to %gmapsuppfolder%, usually \Garmin

rem All folders should be located on the same drive. It is possible it works otherwise, but this is not tested.

rem Caches for BaseCamp/MapSource will be emptied, if they are configured in %mapsourcecache% and %basecampcache%

rem Steps first used on OS X, script written for Windows 7 - not tested in other versions of Windows
rem Jeroen Muris, september 2009 - november 2011


setlocal ENABLEDELAYEDEXPANSION

if not defined java_apps set java_apps=%~dp0

rem Java configuration
set javaheap=1536M

rem software versions
set splitterversion=splitter-r185
set mkgmapversion=mkgmap-r1995

rem extra mkgmap styles
set stylefolder=style
set firststyle=1
set laststyle=1

rem download configuration
set dl_location=http://planet.openstreetmap.nl/
set dl_filename=planet-benelux-latest.osm.pbf

rem mkgmap configuration
set typfile=M0000c45.TYP
set familyid=3141
set splitprefix=31415
set familyname=OSM Jeroen
set basepriority=25
set tilefolder=%~dp0%output
set gmapsuppfolder=\garmin

rem paths for mapsource/basecamp caches
set mapsourcecache=%APPDATA%\Garmin\MapSource\TileCache
set basecampcache=%LOCALAPPDATA%\Garmin\BaseCamp\TileCache


:MAIN

%~d0
cd %~p0

call :DOWNLOAD
call :SPLIT
call :CONVERT
call :FINISH

echo.
pause
exit /b


:DOWNLOAD

if not exist "%dl_filename%" (
	echo downloading %dl_location%%dl_filename%
	curl --show-error --location --globoff --user-agent OSM2Garmin-personal ^
	--output "%dl_filename%" "%dl_location%%dl_filename%"
	if exist %splitprefix%???.osm.pbf (
		echo erasing old %splitprefix%???.osm.pbf
		erase %splitprefix%???.osm.pbf
	)
) else (
	echo existing %dl_filename% found, skipping download
)

echo -----------------------------------------------------------------------
exit /b


:SPLIT

if not exist %splitprefix%???.osm.pbf (
	if exist areas.list set splitoption=--split-file=areas.list
	java -Xmx%javaheap% -ea -jar %java_apps%\%splitterversion%\splitter.jar ^
	--mixed --max-nodes=2400000 --no-trim --cache="%TEMP%" ^
	%splitoption% --mapid=%splitprefix%001 "%dl_filename%"
) else (
	echo existing %splitprefix%???.osm.pbf found, skipping split
)

echo -----------------------------------------------------------------------
exit /b


:CONVERT

echo mkgmap version:
java -jar %java_apps%\%mkgmapversion%\mkgmap.jar --version

rem base layer, default style
if exist %stylefolder%\default set defaultstyleoption=--style-file=%stylefolder%\default
for %%f in (%splitprefix%???.osm.pbf) do (
	set filename=%%f
	set filesuffix=!filename:%splitprefix%0=!
	set filenumber=!filesuffix:.osm.pbf=!
	set mapname=%splitprefix%0!filenumber!
	if not exist %tilefolder%\!mapname!.img (
		echo !filename! --- %tilefolder%\!mapname!.img
		java -Xmx%javaheap% -ea -jar %java_apps%\%mkgmapversion%\mkgmap.jar ^
		--max-jobs --keep-going --output-dir="%tilefolder%" ^
		--generate-sea=extend-sea-sectors,close-gaps=20 ^
		--country-name=Netherlands --country-abbr=NLD --product-id=1 ^
		--lower-case --latin1 --name-tag-list=name:nl,int_name,name:en,name ^
		--reduce-point-density=2.6 --remove-short-arcs=1 --merge-lines ^
		--net --route --make-opposite-cycleways --drive-on-right --adjust-turn-headings ^
		--check-roundabouts --check-roundabout-flares --report-dead-ends ^
		--link-pois-to-ways --add-pois-to-areas ^
		%defaultstyleoption% --draw-priority=%basepriority% ^
		--description="OSM base layer" --mapname=!mapname! --input-file=!filename!
	)
)

rem extra layers (transparent)
for /l %%n in (%firststyle%,1,%laststyle%) do (
	if exist %stylefolder%\%%n (
		for %%f in (%splitprefix%???.osm.pbf) do (
			set filename=%%f
			set filesuffix=!filename:%splitprefix%0=!
			set filenumber=!filesuffix:.osm.pbf=!
			set mapname=%splitprefix%%%n!filenumber!
			set /a priority=%basepriority%+%%n
			if not exist %tilefolder%\!mapname!.img (
				echo !filename! --- %tilefolder%\!mapname!.img
				java -Xmx%javaheap% -ea -jar %java_apps%\%mkgmapversion%\mkgmap.jar ^
				--max-jobs --keep-going --output-dir="%tilefolder%" ^
				--generate-sea=extend-sea-sectors,close-gaps=20 ^
				--country-name=Netherlands --country-abbr=NLD --product-id=1 ^
				--lower-case --latin1 --name-tag-list=name:nl,int_name,name:en,name ^
				--reduce-point-density=2.6 --remove-short-arcs=1 --merge-lines ^
				--style-file=%stylefolder%\%%n --draw-priority=!priority! --transparent ^
				--description="OSM overlay %%n" --mapname=!mapname! --input-file=!filename!
			)
		)
	)
)

echo -----------------------------------------------------------------------
exit /b


:FINISH

echo TYP file: %typfile%
xcopy /d /f /y %typfile% %tilefolder%

pushd %tilefolder%
if exist %splitprefix%???.img (
	echo %tilefolder%\%splitprefix%???.img --- %gmapsuppfolder%\gmapsupp.img
	java -Xmx%javaheap% -ea -jar %java_apps%\%mkgmapversion%\mkgmap.jar ^
	--family-id=%familyid% --family-name="%familyname%" ^
	--output-dir="%gmapsuppfolder%" ^
	--gmapsupp --verbose %splitprefix%???.img %typfile%
)
popd

if defined mapsourcecache (
	echo MapSource Tile Cache
	if exist "%mapsourcecache%\*.tile" erase /q "%mapsourcecache%\*.tile"
)

if defined basecampcache (
	echo BaseCamp Tile Cache
	if exist "%basecampcache%\*.tile" erase /q "%basecampcache%\*.tile"
)

echo -----------------------------------------------------------------------
exit /b