Module:Coordinates/sandbox2 explained

--coord }} : General function formatting and displayingcoordinate values.

: Simple function for converting decimaldegree values to DMS format.

: Simple function for converting DMS formatto decimal degree format.

: Export the link used to reach the tools

require('strict')

local math_mod = require("Module:Math")local mTemplateInvocation = require('Module:Template invocation')local mTableTools = require('Module:TableTools')local coordinates =

local current_page = mw.title.getCurrentTitlelocal page_name = mw.uri.encode(current_page.prefixedText, 'WIKI')local coord_link = '//geohack.toolforge.org/geohack.php?pagename=' .. page_name .. '&params='local templatestyles = 'Module:Coordinates/styles.css'

--Helper function, replacement for local function displaytitle(s, notes) local l = "Coordinates: " .. s local co = '

' .. l .. notes .. ''; return mw.getCurrentFrame:extensionTag;end

--Helper function, Replacement for local function displayinline(s, notes) return s .. notesend

--Helper function, used in detecting DMS formatting local function dmsTest(first, second) if type(first) ~= 'string' or type(second) ~= 'string' then return nil end local s = (first .. second):upper return s:find('^[NS][EW]$') or s:find('^[EW][NS]$')end

--local function makeInvokeFunc(funcName) return function (frame) local args = require('Module:Arguments').getArgs(frame,) return coordinates[funcName](args, frame) endend

--Helper function, handle optional args. local function optionalArg(arg, supplement) return arg and arg .. supplement or end

--Formats any error messages generated for displaylocal function errorPrinter(errors) local result = "" for i,v in ipairs(errors) do local errorHTML = 'Coordinates: ' .. v[2] .. '' result = result .. errorHTML .. "
" end return resultend

--Determine the required CSS class to display coordinates

Usually geo-nondefault is hidden by CSS, unless a user has overridden this for himselfdefault is the mode as specificied by the user when calling the templatemode is the display mode (dec or dms) that we will need to determine the css class for

local function displayDefault(default, mode) if default

"" then default = "dec" end

if default

mode then return "geo-default" else return "geo-nondefault" endend

--JSON doesn't handle .78 values, only 0.78we do want to keep our precision though, so tonumber is bad.local function jsonSafeNumber(numberString) numberString = numberString:gsub('^(%.)', '0.', 1) return numberString:gsub('^(-%.)', '-0.', 1)end

--local function addHemisphereToDec(latitude, longitude) local lat = tonumber(latitude) or 0 local returnLat if lat < 0 then -- FIXME this breaks the pre-existing precision returnLat = tostring(latitude):sub(2) .. "°S" else returnLat = (latitude or 0) .. "°N" end

local long = tonumber(longitude) or 0 local returnLong if long < 0 then -- FIXME does not handle unicode minus returnLong = tostring(longitude):sub(2) .. "°W" else returnLong = (longitude or 0) .. "°E" end return returnLat, returnLongend

--splitParam

Split the Geohack parameter string and convert it into an object.

local function splitParam(param) local out = for pair in mw.text.gsplit(param, '_', true) do local keyValue = mw.text.split(pair, ':', true) if #keyValue

2 then out[keyValue[1]] = keyValue[2] end end return outend

--geohackTypeToMarkerSymbol

Convert from Geohack's type to a Kartographer marker symbol

local function geohackTypeToMarkerSymbol(type, population) -- https://en.wikipedia.org/wiki/Template:Coord#type:T -- https://www.mediawiki.org/wiki/Extension:GeoData -- https://www.mediawiki.org/wiki/Help:Extension:Kartographer/Icons local maplinkMarkerSymbol = 'circle' local markerSymbols = if markerSymbols[type] then maplinkMarkerSymbol = markerSymbols[type] end -- https://meta.wikimedia.org/wiki/WikiMiniAtlas population = tonumber(population) if type

'city' and population then if population < 100000 then -- town maplinkMarkerSymbol = 'town' elseif population < 10000 then -- village maplinkMarkerSymbol = 'village' end end return maplinkMarkerSymbolend

--geohackTypeToScale

Convert from Geohack's types to Geohack's scale levels

local function geohackTypeToScale(type, population) local typeScale = local scale if typeScale[type] then scale = typeScale[type] end population = tonumber(population) if type

'city' and population and population > 0 then -- assume city is a circle with density of 1000/square kilometer -- compute diameter scale = 356.82 * math.sqrt(population) -- don't zoom in too far if scale < 30000 then scale = 30000 end end return scaleend

local log2 = 0.693147181

--geohackTypeToScale

Convert from Geohack's scale levels to OSM style zoom levels as used by

local function geohackScaleToMapZoom(scale) scale = tonumber(scale) if not scale or scale <= 0 then return end -- Empirically derived from geohack behavior return 29.214-math.log(scale)/log2end

local function geohackDimToMapZoom(dim, units) dim = tonumber(dim) if not dim or dim <= 0 then return end if units and string.lower(units)

'km' then dim = dim*1000 end -- Empirically derived from geohack behavior return 25.892-math.log(dim)/log2end

local function labelForQID(qid) local entity = mw.wikibase and qid and (mw.wikibase.getEntityObject(qid) or mw.wikibase.getEntityObject) if entity then return entity:getLabel end return nilend

--coordLinkRenderer

Render a traditional coord-style geohacklink based on the provided information

local function coordLinkRenderer(args, coordinateSpec) local uriComponents = coordinateSpec["param"] if uriComponents

"" then -- RETURN error, should never be empty or nil return "ERROR param was empty" end if args["name"] then uriComponents = uriComponents .. "&title=" .. mw.uri.encode(coordinateSpec["name"]) end

local geodmshtml = '

' .. '' .. coordinateSpec["dms-lat"] .. ' ' .. '' ..coordinateSpec["dms-long"] .. '' .. ''

local geodeclat, geodeclong = addHemisphereToDec(coordinateSpec['dec-lat'], coordinateSpec['dec-long']) local geodechtml = '

' .. geodeclat .. ' ' .. geodeclong .. ''

local geonumhtml = '

' .. coordinateSpec["dec-lat"] .. '; ' .. coordinateSpec["dec-long"] .. ''

local inner = '

' .. geodmshtml .. '' .. '&#xfeff; / &#xfeff;' .. ''

if not args["name"] then inner = inner .. geodechtml .. '

&#xfeff; / ' .. geonumhtml .. '
' else inner = inner .. '' .. geodechtml .. '&#xfeff; / ' .. geonumhtml .. '' .. '&#xfeff; (' .. args["name"] .. ')