Preskočiť na obsah

Modul:Súradnice

z Wikipédie, slobodnej encyklopédie


Túto dokumentáciu [upraviť] [história] [obnoviť]
DokumentáciaDokumentácia

Pre použitie pozri:Šablóna:Súradnice

Pre chybné volania pozri:Kategória:Chyby hlásené modulom Súradnice


Ak máte otázku k tomuto modulu, alebo potrebujete jeho rozšírenie a neviete ho správne naformátovať, pýtajte sa vdiskusii k modulu.Pokiaľ je potrebné modul urgentne opraviť, obráťte sa natechnickú podporu.

--[[
Tento modul je určený na nahradenie šablóny {{Coord}} a súvisiacich šablón.

Základné oužitie:
{{#Invoke:Súradnice | coord }}: všeobecné funkcie formátovania a zobrazovania
súradníc.

{{#Invoke:Súradnice | dec2dms }}: jednoduchá funkcia pre prevod desatinných hodnôt do formátu DMS.
degree values to DMS format.

{{#Invoke:Súradnice | dms2dec }}: jednoduchá funkcia pre prevod z formátu DMS do desatinného formátu.
]]

math_mod=require("Module:Math");
globalFrame=nil

coordinates={};

--[[ Pomocná funkcia, náhrada za {{coord/display/title}} ]]
functiondisplaytitle(s,notes)
locall="[[Zemepisné súradnice|Súradnice]]:"..s
localco='<span id= "coordinates" >'..l..notes..'</span>';
return'<span style= "font-size: small;" >'..co..'</span>';
end

--[[ Pomocná funkcia, náhrada za {{coord/display/inline}} ]]
functiondisplayinline(s,notes)
returns..notes
end

--[[ Pomocná funkcia, používa sa pre detekciu DMS formátovania ]]
localdmsTest=function(first,second)
localconcatenated=first:upper()..second:upper();

ifconcatenated=="SV"orconcatenated=="SZ"orconcatenated=="JV"orconcatenated=="JZ"or
concatenated=="VS"orconcatenated=="ZS"orconcatenated=="VJ"orconcatenated=="ZJ"then
returntrue;
end
returnfalse;
end

--[[
parseDec

Transformuje desatinný tvar zemepisnej šírky a dĺžky do
konštrukcie, ktorá má byť použitá pri zobrazovaní súradníc
]]
functionparseDec(lat,long,format)
localcoordinateSpec={}
localerrors={}

iflong==""orlong==nilthen
returnnil,{{"parseDec","Chýba dĺžka"}}
end

errors=validate(lat,nil,nil,long,nil,nil,'parseDec',false);
coordinateSpec["dec-lat"]=lat;
coordinateSpec["dec-long"]=long;

localmode=coordinates.determineMode(lat,long);
coordinateSpec["dms-lat"]=convert_dec2dms(lat,"S","J",mode)-- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-long"]=convert_dec2dms(long,"V","Z",mode)-- {{coord/dec2dms|{{{2}}}|E|W|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}

ifformat~=""then
coordinateSpec.default=format
else
-- coordinateSpec.default = "dec"
coordinateSpec.default="dms"-- 2016-04-26 Teslaton: defaultné zobrazenie jednotne DMS, bez ohľadu na vstupný formát
end

returncoordinateSpec,errors
end

--[[ Pomocná funkcia, spracováva voliteľné parametre (args). ]]
functionoptionalArg(arg,suplement)
ifarg~=nilandarg~=""then
returnarg..suplement
end
return""
end

--[[
parseDMS

Transformuje DMS tvar zemepisnej šírky a dĺžky do
konštrukcie, ktorá má byť použitá pri zobrazovaní súradníc
]]
functionparseDMS(lat_d,lat_m,lat_s,lat_f,long_d,long_m,long_s,long_f,format)
localcoordinateSpec={}
localerrors={}

lat_f=lat_f:upper();
long_f=long_f:upper();

-- Check if specified backward
iflat_f=='V'orlat_f=='Z'then
localt_d,t_m,t_s,t_f;
t_d=lat_d;
t_m=lat_m;
t_s=lat_s;
t_f=lat_f;
lat_d=long_d;
lat_m=long_m;
lat_s=long_s;
lat_f=long_f;
long_d=t_d;
long_m=t_m;
long_s=t_s;
long_f=t_f;
end

errors=validate(lat_d,lat_m,lat_s,long_d,long_m,long_s,'parseDMS',true);
iflong_d==nilorlong_d==""then
table.insert(errors,{"parseDMS","Chýba dĺžka"})
end

iflat_m==nilandlat_s==nilandlong_m==nilandlong_s==niland#errors==0then
ifmath_mod._precision(lat_d)>0ormath_mod._precision(long_d)>0then
iflat_f:upper()=='J'then
lat_d='-'..lat_d;
end
iflong_f:upper()=='Z'then
long_d='-'..long_d;
end

returnparseDec(lat_d,long_d,format);
end
end

coordinateSpec["dms-lat"]=lat_d.."°"..optionalArg(lat_m,"′")..optionalArg(lat_s,"″")..lat_f
coordinateSpec["dms-long"]=long_d.."°"..optionalArg(long_m,"′")..optionalArg(long_s,"″")..long_f
coordinateSpec["dec-lat"]=convert_dms2dec(lat_f,lat_d,lat_m,lat_s)-- {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}}
coordinateSpec["dec-long"]=convert_dms2dec(long_f,long_d,long_m,long_s)-- {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}}

ifformat~=""then
coordinateSpec.default=format
else
coordinateSpec.default="dms"
end

returncoordinateSpec,errors
end

--[[
specPrinter

Output formatter. Takes the structure generated by either parseDec
or parseDMS and formats it for inclusion on Wikipedia.
]]
functionspecPrinter(args,coordinateSpec)
localuriComponents=coordinateSpec["param"]
ifuriComponents==""then
-- RETURN error, should never be empty or nil
return"Chyba: parameter je prázdny"
end
ifargs["name"]~=""andargs["name"]~=nilthen
uriComponents=uriComponents.."&title="..mw.uri.encode(coordinateSpec["name"])
end

localgeodmshtml='<span class= "geo-dms" title= "Mapy, letecké fotografie a iné dáta pre '
..coordinateSpec["dms-lat"]..' '..coordinateSpec["dms-long"]..' ">'
..'<span class= "latitude" >'..coordinateSpec["dms-lat"]..'</span> '
..'<span class= "longitude" >'..coordinateSpec["dms-long"]..'</span>'
..'</span>'

locallat=tonumber(coordinateSpec["dec-lat"])or0
iflat<0then
-- FIXME this breaks the pre-existing precision
geodeclat=coordinateSpec["dec-lat"]:sub(2).."°J"
else
geodeclat=(coordinateSpec["dec-lat"]or0).."°S"
end

locallong=tonumber(coordinateSpec["dec-long"])or0
iflong<0then
-- FIXME does not handle unicode minus
geodeclong=coordinateSpec["dec-long"]:sub(2).."°Z"
else
geodeclong=(coordinateSpec["dec-long"]or0).."°V"
end

localgeodechtml='<span class= "geo-dec" title= "Mapy, letecké fotografie a iné dáta pre '
..dotToComma(coordinateSpec["dec-lat"])..' '..dotToComma(coordinateSpec["dec-long"])..' ">'
..dotToComma(geodeclat)..' '
..dotToComma(geodeclong)
..'</span>'

localgeonumhtml='<span class= "geo" >'
..coordinateSpec["dec-lat"]..'; '
..coordinateSpec["dec-long"]
..'</span>'

--cf http://www.mediawiki.org/wiki/Extension:GeoData
localr,s,t=string.match(coordinateSpec["param"],'(.*)_(E?W?)_(.*)')
localglobe,globend=string.match(coordinateSpec["param"],'_globe:(%w*)(_*)')
ifargs['displayTitle']and(globe==nilorglobe=='earth')then
globalFrame:callParserFunction('#coordinates',{coordinateSpec["dec-lat"],coordinateSpec["dec-long"],'primary',t})
end

localinner;
inner='<span class= "'..displayDefault(coordinateSpec["default"],"dms")..' ">'..geodmshtml..'</span>'
..'<span class= "geo-multi-punct" >&#xfeff; / &#xfeff;</span>'
..'<span class= "'..displayDefault(coordinateSpec["default"],"dec")..' ">';

ifargs["name"]==""orargs["name"]==nilthen
inner=inner..geodechtml..'<span style= "display:none" >&#xfeff; / '..geonumhtml..'</span></span>'
else
inner=inner..'<span class= "vcard" >'..geodechtml
..'<span style= "display:none" >&#xfeff; / '..geonumhtml..'</span>'
..'<span style= "display:none" >&#xfeff; (<span class= "fn org" >'
..args["name"]..'</span>)</span></span></span>'
end

return'<span class= "plainlinks nourlexpansion" >'..globalFrame:preprocess(
'[//tools.wmflabs.org/geohack/geohack.php?pagename={{FULLPAGENAMEE}}&language=sk&params='..
uriComponents..' '..inner..']')..'</span>'
end

--[[
Formats any error messages generated for display
]]
functionerrorPrinter(errors)
localresult=""
fori,vinipairs(errors)do
localerrorHTML='<strong class= "error" >Súradnice: '..v[2]..'</strong>'
result=result..errorHTML.."<br />"
end
returnresult
end

--[[
Determine the required CSS class to display coordinates

Usually geo-nondefault is hidden by CSS, unless a user has overridden this for himself
default is the mode as specificied by the user when calling the {{coord}} template
mode is the display mode (dec or dms) that we will need to determine the css class for
]]
functiondisplayDefault(default,mode)
ifdefault==""then
default="dec"
end

ifdefault==modethen
return"geo-default"
else
return"geo-nondefault"
end
end

--[[
Check the input arguments for coord to determine the kind of data being provided
and then make the necessary processing.
]]
functionformatTest(args)
localresult,errors;
localprimary=false;

ifargs[1]==""then
-- no lat logic
returnerrorPrinter({{"formatTest","Chýba šírka"}})
elseifargs[4]==""andargs[5]==""andargs[6]==""then
-- dec logic
result,errors=parseDec(commaToDot(args[1]),commaToDot(args[2]),args['format'])
ifresult==nilthen
returnerrorPrinter(errors);
end
result.param=table.concat({commaToDot(args[1]),"_N_",commaToDot(args[2]),"_E_",args[3]});
elseifdmsTest(args[4],args[8])then
-- dms logic
result,errors=parseDMS(args[1],args[2],commaToDot(args[3]),args[4],
args[5],args[6],commaToDot(args[7]),args[8],args['format'])
result.param=table.concat({args[1],args[2],commaToDot(args[3]),formatLatN(args[4]),args[5],
args[6],commaToDot(args[7]),formatLongW(args[8]),args[9]},'_');
ifargs[10]~=''then
table.insert(errors,{'formatTest','Neočakávaný parameter'});
end
elseifdmsTest(args[3],args[6])then
-- dm logic
result,errors=parseDMS(args[1],commaToDot(args[2]),nil,args[3],
args[4],commaToDot(args[5]),nil,args[6],args['format'])
result.param=table.concat({args[1],commaToDot(args[2]),formatLatN(args[3]),args[4],commaToDot(args[5]),
formatLongW(args[6]),args[7]},'_');
ifargs[8]~=''then
table.insert(errors,{'formatTest','Neočakávaný parameter'});
end
elseifdmsTest(args[2],args[4])then
-- d logic
result,errors=parseDMS(commaToDot(args[1]),nil,nil,args[2],
commaToDot(args[3]),nil,nil,args[4],args['format'])
result.param=table.concat({commaToDot(args[1]),formatLatN(args[2]),commaToDot(args[3]),formatLongW(args[4]),args[5]},'_');
ifargs[6]~=''then
table.insert(errors,{'formatTest','Neočakávaný parameter'});
end
else
-- Error
returnerrorPrinter({{"formatTest","Neznámy formát parametra"}})
end
result.name=args["name"]

localextra_param={'dim','globe','scale','region','source','type'}
for_,vinipairs(extra_param)do
if(args[v]or'')~=''then
table.insert(errors,{'formatTest','Parameter: "'..v..'= "should be" '..v..': "'});
end
end

if#errors==0then
returnspecPrinter(args,result)
else
returnspecPrinter(args,result)..""..errorPrinter(errors)..'[[Kategória:Stránky s chybne zadanými súradnicami]][[Kategória:Chyby hlásené modulom Súradnice]]';
end
end

--[[ Pomocná funkcia, prevod desatinnej čiarky na bodku ]]
functioncommaToDot(num)
returnmw.ustring.gsub(num,",",".")
end

--[[ Pomocná funkcia, prevod desatinnej bodky na čiarku ]]
functiondotToComma(num)
returnmw.ustring.gsub(num,"%.",",")
end
--[[ Pomocná funkcia, prevod Z na W a V na E ]]
functionformatLongW(dir)
ifdir:upper()=="Z"then
return"W"
end
ifdir:upper()=="V"then
return"E"
end
returndir
end
--[[ Pomocná funkcia, prevod S na N a J na S ]]
functionformatLatN(dir)
ifdir:upper()=="S"then
return"N"
end
ifdir:upper()=="J"then
return"S"
end
returndir
end
--[[
Pomocná funkcia, convert decimal latitude or longitude to
degrees, minutes, and seconds format based on the specified precision.
]]
functionconvert_dec2dms(coordinate,firstPostfix,secondPostfix,precision)
localcoord=tonumber(coordinate)or0
localpostfix
ifcoord>=0then
postfix=firstPostfix
else
postfix=secondPostfix
end

precision=precision:lower();
ifprecision=="dms"then
returnconvert_dec2dms_dms(math.abs(coord))..postfix;
elseifprecision=="dm"then
returnconvert_dec2dms_dm(math.abs(coord))..postfix;
elseifprecision=="d"then
returnconvert_dec2dms_d(math.abs(coord))..postfix;
end
end

--[[ Pomocná funkcia, prevádza desatinné hodnoty na stupne ]]
functionconvert_dec2dms_d(coordinate)
locald=math_mod._round(coordinate,0).."°"
returnd..""
end

--[[ Pomocná funkcia, prevádza desatinné hodnoty na stupne a minúty ]]
functionconvert_dec2dms_dm(coordinate)
coordinate=math_mod._round(coordinate*60,0);
localm=coordinate%60;
coordinate=math.floor((coordinate-m)/60);
locald=coordinate%360.."°"

returnd..string.format("%02d′",m)
end

--[[ Pomocná funkcia, prevádza desatinné hodnoty na stupne, minúty a sekundy ]]
functionconvert_dec2dms_dms(coordinate)
coordinate=math_mod._round(coordinate*60*60,0);
locals=coordinate%60
coordinate=math.floor((coordinate-s)/60);
localm=coordinate%60
coordinate=math.floor((coordinate-m)/60);
locald=coordinate%360.."°"

returnd..string.format("%02d′",m)..string.format("%02d″",s)
end

--[[
Convert DMS format into a N or E decimal coordinate
]]
functionconvert_dms2dec(direction,degrees_str,minutes_str,seconds_str)
localdegrees=tonumber(degrees_str)or0
localminutes=tonumber(minutes_str)or0
localseconds=tonumber(seconds_str)or0

localfactor=1
direction=mw.ustring.gsub(direction,'^[ ]*(.-)[ ]*$','%1');
ifdirection=="J"ordirection=="Z"then
factor=-1
end

localprecision=0
ifseconds_str~=nilandseconds_str~=''then
precision=5+math.max(math_mod._precision(seconds_str),0);
elseifminutes_str~=nilandminutes_str~=''then
precision=3+math.max(math_mod._precision(minutes_str),0);
else
precision=math.max(math_mod._precision(degrees_str),0);
end

localdecimal=factor*(degrees+(minutes+seconds/60)/60)
returnstring.format("%."..precision.."f",decimal)-- not tonumber since this whole thing is string based.
end

--[[
Checks input values to for out of range errors.
]]
functionvalidate(lat_d,lat_m,lat_s,long_d,long_m,long_s,source,strong)
localerrors={};
lat_d=tonumber(lat_d)or0;
lat_m=tonumber(lat_m)or0;
lat_s=tonumber(lat_s)or0;
long_d=tonumber(long_d)or0;
long_m=tonumber(long_m)or0;
long_s=tonumber(long_s)or0;

ifstrongthen
iflat_d<0then
table.insert(errors,{source,"latitude degrees < 0 with hemisphere flag"})
end
iflong_d<0then
table.insert(errors,{source,"longitude degrees < 0 with hemisphere flag"})
end
--[[
#coordinates is inconsistent about whether this is an error. If globe: is
specified, it won't error on this condition, but otherwise it will.

For not simply disable this check.

if long_d > 180 then
table.insert(errors, {source, "longitude degrees > 180 with hemisphere flag" })
end
]]
end

iflat_d>90then
table.insert(errors,{source,"latitude degrees > 90"})
end
iflat_d<-90then
table.insert(errors,{source,"latitude degrees < -90"})
end
iflat_m>=60then
table.insert(errors,{source,"latitude minutes >= 60"})
end
iflat_m<0then
table.insert(errors,{source,"latitude minutes < 0"})
end
iflat_s>=60then
table.insert(errors,{source,"latitude seconds >= 60"})
end
iflat_s<0then
table.insert(errors,{source,"latitude seconds < 0"})
end
iflong_d>=360then
table.insert(errors,{source,"longitude degrees >= 360"})
end
iflong_d<=-360then
table.insert(errors,{source,"longitude degrees <= -360"})
end
iflong_m>=60then
table.insert(errors,{source,"longitude minutes >= 60"})
end
iflong_m<0then
table.insert(errors,{source,"longitude minutes < 0"})
end
iflong_s>=60then
table.insert(errors,{source,"longitude seconds >= 60"})
end
iflong_s<0then
table.insert(errors,{source,"longitude seconds < 0"})
end

returnerrors;
end

--[[
dec2dms

Wrapper to allow templates to call dec2dms directly.

Usage:
{{ Invoke:Coordinates | dec2dms | decimal_coordinate | positive_suffix |
negative_suffix | precision }}

decimal_coordinate is converted to DMS format. If positive, the positive_suffix
is appended (typical N or E), if negative, the negative suffix is appended. The
specified precision is one of 'D', 'DM', or 'DMS' to specify the level of detail
to use.
]]
functioncoordinates.dec2dms(frame)
globalFrame=frame
localcoordinate=frame.args[1]
localfirstPostfix=frame.args[2]
localsecondPostfix=frame.args[3]
localprecision=frame.args[4]

returnconvert_dec2dms(coordinate,firstPostfix,secondPostfix,precision)
end

--[[
Helper function to determine whether to use D, DM, or DMS
format depending on the precision of the decimal input.
]]
functioncoordinates.determineMode(value1,value2)
localprecision=math.max(math_mod._precision(value1),math_mod._precision(value2));
ifprecision<=0then
return'd'
elseifprecision<=2then
return'dm';
else
return'dms';
end
end

--[[
dms2dec

Wrapper to allow templates to call dms2dec directly.

Usage:
{{ Invoke:Coordinates | dms2dec | direction_flag | degrees |
minutes | seconds }}

Converts DMS values specified as degrees, minutes, seconds too decimal format.
direction_flag is one of N, S, E, W, and determines whether the output is
positive (i.e. N and E) or negative (i.e. S and W).
]]
functioncoordinates.dms2dec(frame)
globalFrame=frame

localdirection=frame.args[1]
localdegrees=frame.args[2]
localminutes=frame.args[3]
localseconds=frame.args[4]

ifframe.args[2]==nilthen
localexplode=mw.text.split(frame.args[1]:match('^%s*(.-)%s*$'),'/')
if(#explode>1)then
direction=explode[#explode]
degrees=explode[1]
else
localsign,val=string.match(explode[1],'([\-]*)([%d.]*)')
ifsign=='-'thendirection='S'elsedirection='N'end
degrees=val
end
if(#explode>2)then
minutes=explode[2]
end
if(#explode>3)then
seconds=explode[3]
end
end

returnconvert_dms2dec(direction,degrees,minutes,seconds)
end

--[[
coord

Main entry point for Lua function to replace {{coord}}

Usage:
{{ Invoke:Coordinates | coord }}
{{ Invoke:Coordinates | coord | lat | long }}
{{ Invoke:Coordinates | coord | lat | lat_flag | long | long_flag }}
...

Refer to {{coord}} documentation page for many additional parameters and
configuration options.

Note: This function provides the visual display elements of {{coord}}. In
order to load coordinates into the database, the {{#coordinates:}} parser
function must also be called, this is done automatically in the Lua
version of {{coord}}.
]]
functioncoordinates.coord(frame)
globalFrame=frame
localargs=frame.args
ifargs[1]==nilthen
localpFrame=frame:getParent();
args=pFrame.args;
fork,vinpairs(frame.args)do
args[k]=v;
end
end

fori=1,10do
ifargs[i]==nilthen
args[i]=""
else
args[i]=args[i]:match('^%s*(.-)%s*$');--remove whitespace
end
end
args['format']=args['format']or'';

locallatdms=mw.text.split(args[1],'/')
locallngdms=mw.text.split(args[2],'/')
if#latdms>1or#lngdms>1then
-- je copie
localargsN=mw.clone(args)
locali=1-- point de départ
localia=1-- point de départ
while(latdms[i]~=nil)do
args[ia]=mw.text.trim(latdms[i])
i=i+1
ia=ia+1
end
locali=1-- point de départ
while(lngdms[i]~=nil)do
args[ia]=mw.text.trim(lngdms[i])
i=i+1
ia=ia+1
end
locali=3-- point de départ
while(argsN[i]~=nil)do
args[ia]=argsN[i]
i=i+1
ia=ia+1
end
end
localNotes=args.notesor""
localDisplay=string.lower(args.displayor"inline")
ifDisplay==''then
Display='inline';
end
localDisplayInline=(string.find(Display,'inline')~=nilorDisplay=='i'orDisplay=='it'orDisplay=='ti')
localDisplayTitle=(string.find(Display,'title')~=nilorDisplay=='t'orDisplay=='it'orDisplay=='ti')
args['displayTitle']=DisplayTitle
localcontents=formatTest(args)

localtext=''
ifDisplayInlinethen
text=displayinline(contents,Notes)
end
ifDisplayTitlethen
text=text..displaytitle(contents,Notes)
end
returntext
end

returncoordinates