Přeskočit na obsah

Modul:GlobeCoordinate

Tuto stránku mohou editovat jen zavedení uživatelé a správci.
Z Wikipedie, otevřené encyklopedie

localGlobeCoordinate={}

--Internal functions
--[[
Check if a value is a number in the given range
@param mixed value
@param number min
@param number max
@return boolean
]]--
localfunctionvalidateNumberInRange(value,min,max)
returntype(value)=='number'andvalue>=minandvalue<=max
end

--[[
Validate a GlobeCoordinate defintion
@param table definition data
@return boolean
]]--
localfunctionvalidate(definition)
--Validate constantes
ifdefinition.globe~=GlobeCoordinate.GLOBE.EARTHthen
returnfalse
end

--Validate precision
ifnotvalidateNumberInRange(definition.precision,0,1)then
returnfalse
end

--Validate latitude and longitude
ifnotvalidateNumberInRange(definition.latitude,-180,360)ornotvalidateNumberInRange(definition.longitude,-180,360)then
returnfalse
end

returntrue
end

--[[
Try to find the relevant precision for a latitude or longitude as float
@param float float
@return number the precision
]]--
localfunctiondetectPrecisionForFloat(float)
localparts=mw.text.split(tostring(float),'.')
ifparts[2]then
returnmath.pow(10,-1*#parts[2])
else
return1
end
end

--[[
Try to find the relevant precision for a GlobeCoordinate definition
@param table GlobeCoordinate definition
@return number the precision
]]--
localfunctionguessPrecision(definition)
returnmath.max(detectPrecisionForFloat(definition.latitude),detectPrecisionForFloat(definition.longitude))
end

--[[
Format a float coordinate as DMS according to the precision
@param float float
@param precision float
@param positive string the tag if the coordinate is positive, like 'N'
@param positive string the tag if the coordinate is negative, like 'S'
@return string the coordinate in DMS format
]]--
localfunctionformatDMS(float,precision,positive,negative)
localisNegative=float<0
float=math.abs(float)
locald=math.floor(float)
localdms=d..'°'

ifprecision<=1/60then
float=(float-d)*60
localm=math.floor(float)
dms=dms..' '..m..'′'

ifprecision<=1/3600then
float=(float-m)*60
locals
iffloat%2~=0.5then
s=math.floor(float+0.5)
else
s=float-0.5
end
dms=dms..' '..s..'″'
--TODO: precision higher than second
end
end

ifisNegativethen
returndms..' '..negative
else
returndms..' '..positive
end
end

--Public interface
--[[
Build a new GlobeCoordinate
@param table definition definition of the coodinate
@return GlobeCoordinate|nil
]]--
functionGlobeCoordinate.new(definition)
--Default values
ifdefinition.precision==nilthen
definition.precision=guessPrecision(definition)
end
ifdefinition.globe==nilthen
definition.globe=GlobeCoordinate.GLOBE.EARTH
end

ifnotvalidate(definition)then
returnnil
end

localcoord={
latitude=definition.latitude,
longitude=definition.longitude,
globe=definition.globeorGlobeCoordinate.GLOBE.EARTH,
precision=definition.precisionor0
}

setmetatable(coord,{
__index=GlobeCoordinate,
__tostring=function(self)returnself:toString()end
})

returncoord
end

--[[
Build a new GlobeCoordinate from a Wikidata GlobeCoordinate value
@param table wikidataValue the coordinate as represented by Wikidata
@return GlobeCoordinate|nil
]]--
functionGlobeCoordinate.newFromWikidataValue(wikidataValue)
ifwikidataValue.globe=='http:// wikidata.org/entity/Q2'then
wikidataValue.globe=GlobeCoordinate.GLOBE.EARTH
else
returnnil
end

returnGlobeCoordinate.new(wikidataValue)
end

--[[
Return a GlobeCoordinate as a string
@param mw.language|string|nil language to use. By default the content language.
@return string
@todo i18n
]]--
functionGlobeCoordinate:toString(language)
returnformatDMS(self.latitude,self.precision,'N','S')..' '..formatDMS(self.longitude,self.precision,'E','W')
end

--[[
Return a GlobeCoordinate in HTMl (with a <GlobeCoordinate> node)
@param mw.language|string|nil language to use. By default the content language.
@param table|nil attributes table of attributes to add to the <GlobeCoordinate> node.
@return string
]]--
functionGlobeCoordinate:toHtml(language,attributes)
returnmw.text.tag(
'span',{
["class"]="geo"
},
mw.text.tag('span',{
["class"]="latitude",
["title"]=self.latitude,
},
formatDMS(self.latitude,self.precision,'N','S')
)..
' '..
mw.text.tag('span',{
["class"]="longitude",
["title"]=self.longitude,
},
formatDMS(self.longitude,self.precision,'E','W')
)
)
end

--[[
Supported globes
]]--
GlobeCoordinate.GLOBE={
EARTH='Earth'
}

returnGlobeCoordinate