Mkgmap/help/scripts/Multilayer.py

From OpenStreetMap Wiki
< Mkgmap‎ | help‎ | scripts
Jump to navigation Jump to search

Info

multilayer.py is a python script to generate a multilayer map for Garmin devices using mkgmap and gmt. This gives better control about the drawing order (i.e. rivers below roads) and enables the user to switch off unwanted layers in the device.

Status

Working, but work in progress.

Code

#!/usr/bin/env python
"""Make Multilayer Maps

"""
osmdatapath = "/home/tt/osm/data/"
TYPFILE = "M0001727.TYP"

layerstack=[
        "land",
        "water",
        "motorway",
        "roads",
        ]


countries={
        "at":40,
        "it":30,
        "ch":20,
        "bw":10,
        }

# mapid, routing, typfile
submaps={
        "water":(00,0,TYPFILE),
        "land":(01,0,TYPFILE),
        "roads":(10,1,TYPFILE),
        "motorway":(11,0,TYPFILE),
        }

import  os 

def system(cmd):
    print cmd
    os.system(cmd)

def mkgmap(country,submap):
    """
    make one submap of all countries in countrylist
    """
    print "Making map: %s %s"%(country,submap)
    mapid = countries[country]
    submapid,routing,typfile = submaps[submap]
    mapname="%2.2i%2.2i0000"%(mapid,submapid)
    priority = layerstack.index(submap)
    osmfiles= "%s/%s*.osm.gz"%(osmdatapath,mapid)
    style=submap
    if routing:
        routingstr="--route --ignore-osm-bounds --remove-short-arcs=1 "
    else:
        routingstr=""
    system("java -Xms256m -Xmx1024m -ea -jar mkgmap.jar \
           --transparent \
           --latin1 \
           --code-pange=1572 \
           --location-autofill=1 \
           --mapname=%s \
           --draw-priority=%s \
           --style-file=%s \
           %s \
           %s %s \
           "%(
               mapname,
               priority,
               style,
               routingstr,
               typfile,
               osmfiles
               )
           )

def makesubmaps(country):
    for submap in layerstack:
        mkgmap(country,submap)

def mergemaps(countrylist):
    filelist=""
    outname="_".join(countrylist)
    outname+=".img"
    for country in countrylist:
        filelist += " %s*.img"%countries[country]
    for s in submaps.values():
        typfile = s[2]
        filelist += " %s"%typfile
    system("gmt -j -o %s -f 5927 %s"%(outname,filelist))

if __name__=="__main__":
    from sys import argv
    countrylist = argv[1:]
    if countrylist[0].lower()=="all":
        countrylist=layerstack
    for country in countrylist:
        makesubmaps(country) 
    mergemaps(countrylist)