Module:Sandbox/autoscale

p = {}

function p.main(frame) if frame.args.item == nil then return "no item given" elseif mw.wikibase.entityExists(frame.args.item) == nil then return "not a valid item" end local coordtable = {} local usecoordy = nil local usecoordx = nil local cminy = nil local cmaxy = nil local closetag = nil local i = 0 -- if far east, west, north, south if mw.wikibase.getBestStatements(frame.args.item, "P1334")[1] and mw.wikibase.getBestStatements(frame.args.item, "P1334")[1].mainsnak.datavalue and mw.wikibase.getBestStatements(frame.args.item, "P1335")[1] and mw.wikibase.getBestStatements(frame.args.item, "P1335")[1].mainsnak.datavalue and mw.wikibase.getBestStatements(frame.args.item, "P1332")[1] and mw.wikibase.getBestStatements(frame.args.item, "P1332")[1].mainsnak.datavalue and mw.wikibase.getBestStatements(frame.args.item, "P1333")[1] and mw.wikibase.getBestStatements(frame.args.item, "P1333")[1].mainsnak.datavalue then -- getting locations of farthest points in each direction in the area local statementa = mw.wikibase.getBestStatements(frame.args.item, "P1334")[1].mainsnak table.insert(coordtable, statementa.datavalue.value.longitude) local statementb = mw.wikibase.getBestStatements(frame.args.item, "P1335")[1].mainsnak table.insert(coordtable, statementb.datavalue.value.longitude) local statementc = mw.wikibase.getBestStatements(frame.args.item, "P1332")[1].mainsnak table.insert(coordtable, statementc.datavalue.value.latitude) local statementd = mw.wikibase.getBestStatements(frame.args.item, "P1333")[1].mainsnak table.insert(coordtable, statementd.datavalue.value.latitude) -- figuring out the center position of the map, but also used for zoom levels local cminx = math.min(tonumber(coordtable[1]), tonumber(coordtable[2])) local sizex = math.max(tonumber(coordtable[1]), tonumber(coordtable[2])) - cminx usecoordx = tonumber(cminx + (sizex / 2)) cminy = math.min(tonumber(coordtable[3]), tonumber(coordtable[4])) cmaxy = math.max(tonumber(coordtable[3]), tonumber(coordtable[4])) local sizey = cmaxy - cminy usecoordy = tonumber(cminy + (sizey / 2)) -- Zoom level: each zoom level is half of the previous zoom level, with the highest being 360 degress (full circle) local x = 360 while math.max(sizex, sizey) < x do			x = x / 2 i = i + 1 end -- if either central coordinate or basic coordinate along with instance of	elseif mw.wikibase.getBestStatements(frame.args.item, "P31")[1] and mw.wikibase.getBestStatements(frame.args.item, "P31")[1].mainsnak.datavalue then if mw.wikibase.getBestStatements(frame.args.item, "P15140")[1] and mw.wikibase.getBestStatements(frame.args.item, "P15140")[1].mainsnak.datavalue then local statementa = mw.wikibase.getBestStatements(frame.args.item, "P15140")[1].mainsnak usecoordx = statementa.datavalue.value.longitude usecoordy = statementa.datavalue.value.latitude cminy = tonumber(usecoordy) cmaxy = tonumber(usecoordy) elseif mw.wikibase.getBestStatements(frame.args.item, "P625")[1] and mw.wikibase.getBestStatements(frame.args.item, "P625")[1].mainsnak.datavalue then local statementa = mw.wikibase.getBestStatements(frame.args.item, "P625")[1].mainsnak usecoordx = statementa.datavalue.value.longitude usecoordy = statementa.datavalue.value.latitude cminy = tonumber(usecoordy) cmaxy = tonumber(usecoordy) end local statementb = mw.wikibase.getBestStatements(frame.args.item, "P31")[1].mainsnak if statementb.datavalue.value.id == "Q6256" then -- Country i = 3 elseif statementb.datavalue.value.id == "Q515" then -- City i = 11 end end -- northern and southern hemisphere have different scaling, due to the earth being round. -- in other cases the calculated zoom is a bit tight, so easing that up. if cmaxy > 60 or cminy < -30 then i = i - 2 else i = i - 1 end if frame.args.closetag then closetag = " >" else closetag = " />" end return frame:preprocess("<mapframe width=250 height=250 zoom=" .. i .. " longitude=" .. usecoordx .. " latitude=" .. usecoordy .. closetag) end

return p