Jump to content

Template:OSM Location map/mapxy

From Wikipedia, the free encyclopedia

50

template:mapxy

Converts lat and lon to xy values from top-left corner of a mapframe in which latbase and lonbase are the centre of the map, returning the bare value of x or y as pixel coordinates.

Takes 8 essential parameters: un-named x or y, plus 7 named ones: lat, lon, latbase, lonbase, width, height, zoom

width and height are the size, in pixels, of the map, which will be centerd around lonbase,latbase

converts lon and lon-base to meter-offsets from coord(0,0), subtracts lonbase from lon, uses zoom and latbase to scale the resulting meter-offset to pixels, and adds it to width. and converts lat and lat-base to meter-offsets from coord(0,0), subtracts lat from latbase, scales the resulting meter-offset to pixels, adds it to height.

I have no idea why it needs the correction factor at higher latitudes. It only really affects marks near the outsides of the frame.

generates the "x": and "y" parameters for CSS values.

Original Python code for xy with 0,0 as the centre of the map print('"x"=',width+(((math.radians(lon1) * 6378137)-

     (math.radians(lonbase) * 6378137))/
     (156543.03*math.cos(latbase/180)/(2**zoom))),' y=',height+((
     (math.log(math.tan(math.pi / 4 + math.radians(latbase) / 2)) * 6378137)-
     (math.log(math.tan(math.pi / 4 + math.radians(lat1) / 2)) * 6378137))/
     (156543.03*math.cos(latbase/180)/(2**zoom)))
    ) 

sources: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames and https://wiki.openstreetmap.org/wiki/Mercator