「モジュール:Coordinates」の版間の差分

ページの作成:「--[[ This module is intended to replace the functionality of {{Coord}} and related templates. It provides several methods, including {{#invoke:Coordinates | coord }} : General function formatting and displaying coordinate values. {{#invoke:Coordinates | dec2dms }} : Simple function for converting decimal degree values to DMS format. {{#invoke:Coordinates | dms2dec }} : Simple function for converting DMS format to decimal degree format. {{#invoke:Coordinates | l…」
Module:Coordinates>WOSlinker
(use require('strict') instead of require('Module:No globals'))
 
(ページの作成:「--[[ This module is intended to replace the functionality of {{Coord}} and related templates. It provides several methods, including {{#invoke:Coordinates | coord }} : General function formatting and displaying coordinate values. {{#invoke:Coordinates | dec2dms }} : Simple function for converting decimal degree values to DMS format. {{#invoke:Coordinates | dms2dec }} : Simple function for converting DMS format to decimal degree format. {{#invoke:Coordinates | l…」)
23行目: 23行目:
local current_page = mw.title.getCurrentTitle()
local current_page = mw.title.getCurrentTitle()
local page_name = mw.uri.encode( current_page.prefixedText, 'WIKI' );
local page_name = mw.uri.encode( current_page.prefixedText, 'WIKI' );
local coord_link = '//geohack.toolforge.org/geohack.php?pagename=' .. page_name .. '&params='
local coord_link = 'https://geohack.toolforge.org/geohack.php?language=ja&pagename=' .. page_name .. '&params='
local templatestyles = 'Module:Coordinates/styles.css'
local templatestyles = 'Module:Coordinates/styles.css'


--[[ Helper function, replacement for {{coord/display/title}} ]]
--[[ Helper function, replacement for {{coord/display/title}} ]]
local function displaytitle(s, notes)
local function displaytitle(s, notes)
local l = "[[Geographic coordinate system|Coordinates]]: " .. s
local l = "[[地理座標系|座標]]: " .. s
local co = '<span id="coordinates">' .. l .. notes .. '</span>';
local co = '<span id="coordinates">' .. l .. notes .. '</span>';
return '<span style="font-size: small;">' .. co .. '</span>';
return '<span style="font-size: small;">' .. co .. '</span>';
69行目: 69行目:
local result = ""
local result = ""
for i,v in ipairs(errors) do
for i,v in ipairs(errors) do
local errorHTML = '<strong class="error">Coordinates: ' .. v[2] .. '</strong>'
local errorHTML = '<strong class="error">座標: ' .. v[2] .. '</strong>'
result = result .. errorHTML .. "<br />"
result = result .. errorHTML .. "<br />"
end
end
104行目: 104行目:
if uriComponents == "" then
if uriComponents == "" then
-- RETURN error, should never be empty or nil
-- RETURN error, should never be empty or nil
return "ERROR param was empty"
return "エラー: paramが空です"
end
end
if args["name"] then
if args["name"] then
110行目: 110行目:
end
end


local geodmshtml = '<span class="geo-dms" title="Maps, aerial photos, and other data for this location">'
local geodmshtml = '<span class="geo-dms" title="この位置の地図や航空写真などにリンクするページを表示します">'
.. '<span class="latitude">' .. coordinateSpec["dms-lat"] .. '</span> '
.. '<span class="latitude">' .. coordinateSpec["dms-lat"] .. '</span> '
.. '<span class="longitude">' ..coordinateSpec["dms-long"] .. '</span>'
.. '<span class="longitude">' ..coordinateSpec["dms-long"] .. '</span>'
119行目: 119行目:
if lat < 0 then
if lat < 0 then
-- FIXME this breaks the pre-existing precision
-- FIXME this breaks the pre-existing precision
geodeclat = tostring(coordinateSpec["dec-lat"]):sub(2) .. "°S"
geodeclat = "南緯" .. tostring(coordinateSpec["dec-lat"]):sub(2) .. ""
else
else
geodeclat = (coordinateSpec["dec-lat"] or 0) .. "°N"
geodeclat = "北緯" .. (coordinateSpec["dec-lat"] or 0) .. ""
end
end


128行目: 128行目:
if long < 0 then
if long < 0 then
-- FIXME does not handle unicode minus
-- FIXME does not handle unicode minus
geodeclong = tostring(coordinateSpec["dec-long"]):sub(2) .. "°W"
geodeclong = "西経" .. tostring(coordinateSpec["dec-long"]):sub(2) .. ""
else
else
geodeclong = (coordinateSpec["dec-long"] or 0) .. "°E"
geodeclong = "東経" .. (coordinateSpec["dec-long"] or 0) .. ""
end
end


local geodechtml = '<span class="geo-dec" title="Maps, aerial photos, and other data for this location">'
local geodechtml = '<span class="geo-dec" title="この位置の地図や航空写真などにリンクするページを表示します">'
.. geodeclat .. ' '
.. geodeclat .. ' '
.. geodeclong
.. geodeclong
174行目: 174行目:
local m = coordinate % 60;
local m = coordinate % 60;
coordinate = math.floor( (coordinate - m) / 60 );
coordinate = math.floor( (coordinate - m) / 60 );
local d = coordinate % 360 .."°"
local d = coordinate % 360 ..""


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


186行目: 186行目:
local m = coordinate % 60
local m = coordinate % 60
coordinate = math.floor( (coordinate - m) / 60 );
coordinate = math.floor( (coordinate - m) / 60 );
local d = coordinate % 360 .."°"
local d = coordinate % 360 ..""


return d .. string.format( "%02d′", m ) .. string.format( "%02d″", s )
return d .. string.format( "%02d分", m ) .. string.format( "%02d秒", s )
end
end


195行目: 195行目:
degrees, minutes, and seconds format based on the specified precision.
degrees, minutes, and seconds format based on the specified precision.
]]
]]
local function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
local function convert_dec2dms(coordinate, firstPrefix, secondPrefix, precision)
local coord = tonumber(coordinate)
local coord = tonumber(coordinate)
local postfix
local prefix
if coord >= 0 then
if coord >= 0 then
postfix = firstPostfix
prefix = firstPrefix
else
else
postfix = secondPostfix
prefix = secondPrefix
end
end


precision = precision:lower();
precision = precision:lower();
if precision == "dms" then
if precision == "dms" then
return convert_dec2dms_dms( math.abs( coord ) ) .. postfix;
return prefix .. convert_dec2dms_dms( math.abs( coord ) );
elseif precision == "dm" then
elseif precision == "dm" then
return convert_dec2dms_dm( math.abs( coord ) ) .. postfix;
return prefix .. convert_dec2dms_dm( math.abs( coord ) );
elseif precision == "d" then
elseif precision == "d" then
return convert_dec2dms_d( math.abs( coord ) ) .. postfix;
return prefix .. convert_dec2dms_d( math.abs( coord ) );
end
end
end
end
223行目: 223行目:


local factor = 1
local factor = 1
direction = direction:gsub('^ *(.-) *$', '%1');
if direction == "S" or direction == "W" then
if direction == "S" or direction == "W" then
factor = -1
factor = -1
254行目: 255行目:
if strong then
if strong then
if lat_d < 0 then
if lat_d < 0 then
table.insert(errors, {source, "latitude degrees < 0 with hemisphere flag"})
table.insert(errors, {source, "緯度が0度を下回っていますが南北が指定されています"})
end
end
if long_d < 0 then
if long_d < 0 then
table.insert(errors, {source, "longitude degrees < 0 with hemisphere flag"})
table.insert(errors, {source, "経度が0度を下回っていますが東西が指定されています"})
end
end
--[[
--[[
266行目: 267行目:


if long_d > 180 then
if long_d > 180 then
table.insert(errors, {source, "longitude degrees > 180 with hemisphere flag"})
table.insert(errors, {source, "経度が180度を上回っていますが東西が指定されています"})
end
end
]]
]]
272行目: 273行目:


if lat_d > 90 then
if lat_d > 90 then
table.insert(errors, {source, "latitude degrees > 90"})
table.insert(errors, {source, "緯度の度が90を上回っています"})
end
end
if lat_d < -90 then
if lat_d < -90 then
table.insert(errors, {source, "latitude degrees < -90"})
table.insert(errors, {source, "緯度の度が-90を下回っています"})
end
end
if lat_m >= 60 then
if lat_m >= 60 then
table.insert(errors, {source, "latitude minutes >= 60"})
table.insert(errors, {source, "緯度の分が60以上です"})
end
end
if lat_m < 0 then
if lat_m < 0 then
table.insert(errors, {source, "latitude minutes < 0"})
table.insert(errors, {source, "緯度の分が0を下回っています"})
end
end
if lat_s >= 60 then
if lat_s >= 60 then
table.insert(errors, {source, "latitude seconds >= 60"})
table.insert(errors, {source, "緯度の秒が60以上です"})
end
end
if lat_s < 0 then
if lat_s < 0 then
table.insert(errors, {source, "latitude seconds < 0"})
table.insert(errors, {source, "緯度の秒が0を下回っています"})
end
end
if long_d >= 360 then
if long_d >= 360 then
table.insert(errors, {source, "longitude degrees >= 360"})
table.insert(errors, {source, "経度の度が360以上です"})
end
end
if long_d <= -360 then
if long_d <= -360 then
table.insert(errors, {source, "longitude degrees <= -360"})
table.insert(errors, {source, "経度の度が-360以下です"})
end
end
if long_m >= 60 then
if long_m >= 60 then
table.insert(errors, {source, "longitude minutes >= 60"})
table.insert(errors, {source, "経度の分が60以上です"})
end
end
if long_m < 0 then
if long_m < 0 then
table.insert(errors, {source, "longitude minutes < 0"})
table.insert(errors, {source, "経度の分が0を下回っています"})
end
end
if long_s >= 60 then
if long_s >= 60 then
table.insert(errors, {source, "longitude seconds >= 60"})
table.insert(errors, {source, "経度の秒が60以上です"})
end
end
if long_s < 0 then
if long_s < 0 then
table.insert(errors, {source, "longitude seconds < 0"})
table.insert(errors, {source, "経度の秒が0を下回っています"})
end
end


322行目: 323行目:


if not long then
if not long then
return nil, {{"parseDec", "Missing longitude"}}
return nil, {{"parseDec", "経度が指定されていません"}}
elseif not tonumber(long) then
elseif not tonumber(long) then
return nil, {{"parseDec", "Longitude could not be parsed as a number: " .. long}}
return nil, {{"parseDec", "経度が数値として認識できません: " .. long}}
end
end


332行目: 333行目:


local mode = coordinates.determineMode( lat, long );
local mode = coordinates.determineMode( lat, long );
coordinateSpec["dms-lat"]  = convert_dec2dms( lat, "N", "S", mode)  -- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-lat"]  = convert_dec2dms( lat, "北緯", "南緯", mode)  -- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-long"] = convert_dec2dms( long, "E", "W", mode)  -- {{coord/dec2dms|{{{2}}}|E|W|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-long"] = convert_dec2dms( long, "東経", "西経", mode)  -- {{coord/dec2dms|{{{2}}}|E|W|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}


if format then
if format then
348行目: 349行目:


Transforms degrees, minutes, seconds format latitude and longitude
Transforms degrees, minutes, seconds format latitude and longitude
into the a structure to be used in displaying coordinates
into the structure to be used in displaying coordinates
]]
]]
local function parseDMS( lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format )
local function parseDMS( lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format )
363行目: 364行目:
errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', true );
errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', true );
if not long_d then
if not long_d then
return nil, {{"parseDMS", "Missing longitude" }}
return nil, {{"parseDMS", "経度が指定されていません" }}
elseif not tonumber(long_d) then
elseif not tonumber(long_d) then
return nil, {{"parseDMS", "Longitude could not be parsed as a number:" .. long_d }}
return nil, {{"parseDMS", "経度が数値として認識できません:" .. long_d }}
end
end


381行目: 382行目:
end
end


coordinateSpec["dms-lat"]  = lat_d.."°"..optionalArg(lat_m,"") .. optionalArg(lat_s,"") .. lat_f
coordinateSpec["dms-lat"]  = (lat_f=="N" and "北緯" or "南緯") .. lat_d..""..optionalArg(lat_m,"") .. optionalArg(lat_s,"")
coordinateSpec["dms-long"] = long_d.."°"..optionalArg(long_m,"") .. optionalArg(long_s,"") .. long_f
coordinateSpec["dms-long"] = (long_f=="E" and "東経" or "西経") .. long_d..""..optionalArg(long_m,"") .. optionalArg(long_s,"")
coordinateSpec["dec-lat"]  = convert_dms2dec(lat_f, lat_d, lat_m, lat_s) -- {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}}
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}}}}}
coordinateSpec["dec-long"] = convert_dms2dec(long_f, long_d, long_m, long_s) -- {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}}
413行目: 414行目:
if not args[1] then
if not args[1] then
-- no lat logic
-- no lat logic
return errorPrinter( {{"formatTest", "Missing latitude"}} )
return errorPrinter( {{"formatTest", "緯度が指定されていません"}} )
elseif not tonumber(args[1]) then
elseif not tonumber(args[1]) then
-- bad lat logic
-- bad lat logic
return errorPrinter( {{"formatTest", "Unable to parse latitude as a number:" .. args[1]}} )
return errorPrinter( {{"formatTest", "緯度が数値として認識できません:" .. args[1]}} )
elseif not args[4] and not args[5] and not args[6] then
elseif not args[4] and not args[5] and not args[6] then
-- dec logic
-- dec logic
437行目: 438行目:
args[5], args[6], args[7], args[8], args.format)
args[5], args[6], args[7], args[8], args.format)
if args[10] then
if args[10] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
table.insert(errors, {'formatTest', '座標オプションが認識できません'})
end
end
if not result then
if not result then
448行目: 449行目:
args[4], args[5], nil, args[6], args['format'])
args[4], args[5], nil, args[6], args['format'])
if args[8] then
if args[8] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
table.insert(errors, {'formatTest', '座標オプションが認識できません'})
end
end
if not result then
if not result then
459行目: 460行目:
args[3], nil, nil, args[4], args.format)
args[3], nil, nil, args[4], args.format)
if args[6] then
if args[6] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
table.insert(errors, {'formatTest', '座標オプションが認識できません'})
end
end
if not result then
if not result then
467行目: 468行目:
else
else
-- Error
-- Error
return errorPrinter({{"formatTest", "Unknown argument format"}}) .. '[[Category:Pages with malformed coordinate tags]]'
return errorPrinter({{"formatTest", "引数の形式が認識できません"}}) .. '[[Category:座標タグに誤りがあるページ]]'
end
end
result.name = args.name
result.name = args.name
474行目: 475行目:
for _, v in ipairs(extra_param) do
for _, v in ipairs(extra_param) do
if args[v] then
if args[v] then
table.insert(errors, {'formatTest', 'Parameter: "' .. v .. '=" should be "' .. v .. ':"' })
table.insert(errors, {'formatTest', '引数: "' .. v .. '=" "' .. v .. ':" という形式でなければいけません'})
end
end
end
end
480行目: 481行目:
local ret = specPrinter(args, result)
local ret = specPrinter(args, result)
if #errors > 0 then
if #errors > 0 then
ret = ret .. ' ' .. errorPrinter(errors) .. '[[Category:Pages with malformed coordinate tags]]'
ret = ret .. ' ' .. errorPrinter(errors) .. '[[Category:座標タグに誤りがあるページ]]'
end
end
return ret, backward
return ret, backward
490行目: 491行目:
local function makeWikidataCategories(qid)
local function makeWikidataCategories(qid)
local ret
local ret
local qid = qid or mw.wikibase.getEntityIdForCurrentPage()
if mw.wikibase and current_page.namespace == 0 then
if mw.wikibase and current_page.namespace == 0 then
if qid and mw.wikibase.entityExists(qid) and mw.wikibase.getBestStatements(qid, "P625") and mw.wikibase.getBestStatements(qid, "P625")[1] then
local entity = qid and mw.wikibase.getEntityObject(qid) or mw.wikibase.getEntityObject()
local snaktype = mw.wikibase.getBestStatements(qid, "P625")[1].mainsnak.snaktype
if entity and entity.claims and entity.claims.P625 and entity.claims.P625[1] then
local snaktype = entity.claims.P625[1].mainsnak.snaktype
if snaktype == 'value' then
if snaktype == 'value' then
-- coordinates exist both here and on Wikidata, and can be compared.
-- coordinates exist both here and on Wikidata, and can be compared.
ret = 'Coordinates on Wikidata'
ret = 'ウィキデータにある座標'
elseif snaktype == 'somevalue' then
elseif snaktype == 'somevalue' then
ret = 'Coordinates on Wikidata set to unknown value'
ret = 'ウィキデータで不明な値となっている座標'
elseif snaktype == 'novalue' then
elseif snaktype == 'novalue' then
ret = 'Coordinates on Wikidata set to no value'
ret = 'ウィキデータで値なしとなっている座標'
end
end
else
else
-- We have to either import the coordinates to Wikidata or remove them here.
-- We have to either import the coordinates to Wikidata or remove them here.
ret = 'Coordinates not on Wikidata'
ret = 'ウィキデータにない座標'
end
end
end
end
692行目: 693行目:
if frame.args[2] == 'lat' or frame.args[2] == 'long' then
if frame.args[2] == 'lat' or frame.args[2] == 'long' then
local result, negative = mw.text.split((mw.ustring.match(frame.args[1],'[%.%d]+°[NS] [%.%d]+°[EW]') or ''), ' ')
local result, negative = mw.text.split((mw.ustring.match(frame.args[1],'[%.%d]+°[NS] [%.%d]+°[EW]') or ''), ' ')
if frame.args[2] == 'lat' then
if table.maxn(result) ~= 1 then -- input format: '57.30611°N 4.45889°E'
result, negative = result[1], 'S'
if frame.args[2] == 'lat' then
else
result, negative = result[1], 'S'
result, negative = result[2], 'W'
else
result, negative = result[2], 'W'
end
result = mw.text.split(result, '°')
if result[2] == negative then result[1] = '-'..result[1] end
return result[1]
else -- input format: '北緯57.30611度 東経4.45889度'
result = mw.text.split(mw.ustring.match(frame.args[1],'[南北]緯%-?[%.%d]+度 [東西]経%-?[%.%d]+度') or '', ' ')
if frame.args[2] == 'lat' then
result, negative = result[1], '南'
else
result, negative = result[2], '西'
end
result = mw.text.split(result, '[経緯度]')
if result[1] == negative then result[2] = '-'..result[2] end
return result[2]
end
end
result = mw.text.split(result, '°')
if result[2] == negative then result[1] = '-'..result[1] end
return result[1]
else
else
return mw.ustring.match(frame.args[1], 'params=.-_'..frame.args[2]..':(.-)[ _]')
return mw.ustring.match(frame.args[1], 'params=.-_'..frame.args[2]..':(.-)[ _]')