Sunday, April 12, 2009

Offline OS Maps with MG Maps

I worked out how to get offline OS Maps in the UK on Mobile GMaps, and posted on the forum here.

My post:
I have worked out how to change MapTileCacher.perl (v1.35) so that you can download OS Maps from multimap.co.uk and use them offline in MGMaps. Very handy for hiking, as these are arguably the best maps in the world. I've only done this on OS X - perhaps someone can apply this technique to other OS's and other places (like within Mobile GMaps itself). 

Both 25,000:1 and 50,000:1 scales are downloaded, corresponding to the cache tool's zoom levels 15 and 14 respectively. The 50,000:1 maps are also provided at zoom levels 13 and 12, but below that you end up with pretty standard Live maps, which are handy for locating yourself, but no better than other map providers'. 

Thanks to Christian Streng's post at http://forum.mgmaps.com/viewtopic.php?t=1116&postdays=0&postorder=asc&&start=250, which set me off in the right direction. 

Firstly, you need to set the User Agent for the wget or curl fetch. If you don't, I think Multimap will 95% of the time give you a 403 forbidden response, at least it did for me. For OS X, at line 75 add a User Agent to the curl call (I don't know what you need to do with wget, I'm on OS X): 
Code:
  $wget = "curl -A Safari/3.2 -# -o TempTile.$pid "; 
  $systemRefererParam = ""; 

Secondly, put the following in place of the Osmarender map type (or your own choice of map style that you don't use). This code works out what the correct URL should be, which is a binary tile system like Live maps only broken into six digit segments. I spent a while finding the right 'miXXX' string in the middle - these combinations (where the string depends on the zoom level) give you the 50,000 or 25,000:1 maps as described above, at different zoom levels. You can zoom in further, but there is no point because you just get a blocky, scaled, version of the 25:000:1 map. (There are many other miXXX strings that give you maps for only parts of the country and/or only certain zoom levels - these ones gave me maps for all the places I tested, which wasn't all that many, but spread right across the country.) 
Code:

elsif(index($MapType,"Osmarender") != -1)     # ************ multimap for UK OS Maps ******************** 
  { 
    $digit = ((($y & 1) << 1) + ($x & 1)); # 'randomise' server access mc0, mc1, mc2 or mc3 
    $url .= "http://mc" . $digit . ".multimap.com/ptiles/map/"; 
    # the following selects the map type to use - only certain combinations of zoom and map type work, and some 
    # map numbers different from these work in some areas, but not UK-wide. Hopefully these particular ones 
    # do work UK-wide. 
    $url .= ($zoomLevel+1) >= 16 ? "mi932" : ($zoomLevel+1) >= 13 ? "mi904" : ($zoomLevel+1) >= 6 ? "mi916" : "mi915";  # select map type 
    $url .= "/" . ($zoomLevel + 1) . "/"; 
    
    # Multimap uses quadrant tiles like Microsoft Live, but splits them up into six-digit long segments. 
    # e.g. http://mc1.multimap.com/ptiles/map/mi902/16/031313/131331/311.png 
      
   $mmchars = 0; 
    for($i=$zoomLevel-1; $i >= 0; $i--) 
    { 
        # put a / every six characters 
        if ( ($mmchars == 6) || ($mmchars == 12) ) 
        { 
          $url .= "/"; 
        } 
        $url .= ((((($y >> $i) & 1) <<>> $i) & 1))); 
        $mmchars++; 
    } 
    $url .= ".png"; 
  } 


The $zoomlevel+1 business is because zoom level 15 in MapTileCacher.perl corresponds to zoom level 16 in Multimap, and so forth. 

With this you can use the cache tool, selecting the Osmarender map type, and run MapTileFE.perl as normal, but instead of Osmarender, MapTileCacher.perl will download lovely OS Maps from multimap. 

No comments: