GeoHack/Replacement script

From MediaWiki.org
Jump to navigation Jump to search
Wikimedia Community Logo-Toolserver.svg

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

/* Replace external links to GeoHack with direct links to a single mapping provider.
 *
 * It support the following replacement variables found in GeoHack:
 * {latdegdec} {londegdec} {scale} {span} {osmzoom}
 *
 * Maintainer: [[Toolserver:{{subst:ucfirst:User:Dispenser}}|User:Dispenser]]
 * Released into the Public Domain
 */
function replaceGeoHack(){
	var mapprovider = "http://maps.google.com/maps?ll={latdegdec},{londegdec}&spn={span},{span}&q={latdegdec},{londegdec}";
	//var mapprovider = "http://www.openstreetmap.org/index.html?mlat={latdegdec}&mlon={londegdec}&zoom={osmzoom}&layers=B000FTF";
	var geohack_base = "http://toolserver.org/~geohack/geohack.php";
	var coord_filter = /&params=([\d.+-]+)_([\d.+-]*)_?([\d.+-]*)_?([NSZ])_([\d.+-]+)_([\d.+-]*)_?([\d.+-]*)_?([EOW])([^&=]*)/;
	var link, lat, lon, scale;
	var links = document.getElementsByTagName('A');
	for(var i=0; (link=links[i]); i++){
		if(link.href.indexOf(geohack_base)==0 && coord_filter.exec(link.href)) {
			// latitude and longitude
			lat=(1.0*RegExp.$1) + ((RegExp.$2||0)/60.0) + ((RegExp.$3||0)/3600.0);
			if(RegExp.$4!='N') lat*=-1;
			lon=(1.0*RegExp.$5) + ((RegExp.$6||0)/60.0) + ((RegExp.$7||0)/3600.0);
			if(RegExp.$8=='W') lon*=-1;
			
			// Determine scale/zoom
			var params = RegExp.$9;
			if(/_globe:(?!earth|_|$)/i.test(params)) continue;
			if(/_type:(adm3rd|city|mountain|isle|river|waterbody)/.test(params))scale = 100000;
			else if(/_type:(event|forest|glacier)/.test(params))scale = 50000;
			else if(/_type:(airport|edu|pass|landmark|railwaystation)/.test(params))scale = 10000;
			else scale = 300000;
			if(/_dim:([\d.+-]+)(km)?m?/.exec(params))scale = RegExp.$1 * (RegExp.$2?10000:10);
			if(/_scale:(\d+)(_|$)/.exec(params))scale = RegExp.$1;
			var osmzoom = 18 - Math.round(Math.log(scale/1693) / Math.log(2));
			var zoom =  Math.log( 1.5e8/scale) / Math.log(2);
			
			// Replace link
			link.href=mapprovider.replace(/\{latdegdec\}/g, lat).replace(/\{londegdec\}/g, lon).replace(/\{scale\}/g, scale)
			                     .replace(/\{span\}/g, scale/1e6).replace(/\{osmzoom\}/g, osmzoom).replace(/\{zoom\}/g, zoom);
		}
	}
}

if(doneOnloadHook) replaceGeoHack(); //imported dynamically
else addOnloadHook(replaceGeoHack);

Category:Geo tools