1,835
回編集
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 .. '¶ms=' | local coord_link = 'https://geohack.toolforge.org/geohack.php?language=ja&pagename=' .. page_name .. '¶ms=' | ||
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 = "[[ | 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"> | 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 " | return "エラー: paramが空です" | ||
end | end | ||
if args["name"] then | if args["name"] then | ||
110行目: | 110行目: | ||
end | end | ||
local geodmshtml = '<span class="geo-dms" title=" | 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) .. " | geodeclat = "南緯" .. tostring(coordinateSpec["dec-lat"]):sub(2) .. "度" | ||
else | else | ||
geodeclat = (coordinateSpec["dec-lat"] or 0) .. " | 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) .. " | geodeclong = "西経" .. tostring(coordinateSpec["dec-long"]):sub(2) .. "度" | ||
else | else | ||
geodeclong = (coordinateSpec["dec-long"] or 0) .. " | geodeclong = "東経" .. (coordinateSpec["dec-long"] or 0) .. "度" | ||
end | end | ||
local geodechtml = '<span class="geo-dec" title=" | 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( "% | 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( "% | 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, | local function convert_dec2dms(coordinate, firstPrefix, secondPrefix, precision) | ||
local coord = tonumber(coordinate) | local coord = tonumber(coordinate) | ||
local | local prefix | ||
if coord >= 0 then | if coord >= 0 then | ||
prefix = firstPrefix | |||
else | else | ||
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 ) ) | return prefix .. convert_dec2dms_dms( math.abs( coord ) ); | ||
elseif precision == "dm" then | elseif precision == "dm" then | ||
return convert_dec2dms_dm( math.abs( coord ) ) | return prefix .. convert_dec2dms_dm( math.abs( coord ) ); | ||
elseif precision == "d" then | elseif precision == "d" then | ||
return convert_dec2dms_d( math.abs( coord ) ) | 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, " | table.insert(errors, {source, "緯度が0度を下回っていますが南北が指定されています"}) | ||
end | end | ||
if long_d < 0 then | if long_d < 0 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度が0度を下回っていますが東西が指定されています"}) | ||
end | end | ||
--[[ | --[[ | ||
266行目: | 267行目: | ||
if long_d > 180 then | if long_d > 180 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度が180度を上回っていますが東西が指定されています"}) | ||
end | end | ||
]] | ]] | ||
272行目: | 273行目: | ||
if lat_d > 90 then | if lat_d > 90 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の度が90を上回っています"}) | ||
end | end | ||
if lat_d < -90 then | if lat_d < -90 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の度が-90を下回っています"}) | ||
end | end | ||
if lat_m >= 60 then | if lat_m >= 60 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の分が60以上です"}) | ||
end | end | ||
if lat_m < 0 then | if lat_m < 0 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の分が0を下回っています"}) | ||
end | end | ||
if lat_s >= 60 then | if lat_s >= 60 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の秒が60以上です"}) | ||
end | end | ||
if lat_s < 0 then | if lat_s < 0 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "緯度の秒が0を下回っています"}) | ||
end | end | ||
if long_d >= 360 then | if long_d >= 360 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の度が360以上です"}) | ||
end | end | ||
if long_d <= -360 then | if long_d <= -360 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の度が-360以下です"}) | ||
end | end | ||
if long_m >= 60 then | if long_m >= 60 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の分が60以上です"}) | ||
end | end | ||
if long_m < 0 then | if long_m < 0 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の分が0を下回っています"}) | ||
end | end | ||
if long_s >= 60 then | if long_s >= 60 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の秒が60以上です"}) | ||
end | end | ||
if long_s < 0 then | if long_s < 0 then | ||
table.insert(errors, {source, " | table.insert(errors, {source, "経度の秒が0を下回っています"}) | ||
end | end | ||
322行目: | 323行目: | ||
if not long then | if not long then | ||
return nil, {{"parseDec", " | return nil, {{"parseDec", "経度が指定されていません"}} | ||
elseif not tonumber(long) then | elseif not tonumber(long) then | ||
return nil, {{"parseDec", " | 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, " | coordinateSpec["dms-lat"] = convert_dec2dms( lat, "北緯", "南緯", mode) -- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}} | ||
coordinateSpec["dms-long"] = convert_dec2dms( long, " | 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 | 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", " | return nil, {{"parseDMS", "経度が指定されていません" }} | ||
elseif not tonumber(long_d) then | elseif not tonumber(long_d) then | ||
return nil, {{"parseDMS", " | return nil, {{"parseDMS", "経度が数値として認識できません:" .. long_d }} | ||
end | end | ||
381行目: | 382行目: | ||
end | end | ||
coordinateSpec["dms-lat"] = lat_d.." | coordinateSpec["dms-lat"] = (lat_f=="N" and "北緯" or "南緯") .. lat_d.."度"..optionalArg(lat_m,"分") .. optionalArg(lat_s,"秒") | ||
coordinateSpec["dms-long"] = long_d.." | 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", " | return errorPrinter( {{"formatTest", "緯度が指定されていません"}} ) | ||
elseif not tonumber(args[1]) then | elseif not tonumber(args[1]) then | ||
-- bad lat logic | -- bad lat logic | ||
return errorPrinter( {{"formatTest", " | 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', ' | 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', ' | 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', ' | table.insert(errors, {'formatTest', '座標オプションが認識できません'}) | ||
end | end | ||
if not result then | if not result then | ||
467行目: | 468行目: | ||
else | else | ||
-- Error | -- Error | ||
return errorPrinter({{"formatTest", " | 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', ' | 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: | 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 | ||
if mw.wikibase and current_page.namespace == 0 then | if mw.wikibase and current_page.namespace == 0 then | ||
local entity = qid and mw.wikibase.getEntityObject(qid) or mw.wikibase.getEntityObject() | |||
local 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 = ' | ret = 'ウィキデータにある座標' | ||
elseif snaktype == 'somevalue' then | elseif snaktype == 'somevalue' then | ||
ret = ' | ret = 'ウィキデータで不明な値となっている座標' | ||
elseif snaktype == 'novalue' then | elseif snaktype == 'novalue' then | ||
ret = ' | 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 = ' | 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' | ||
if frame.args[2] == 'lat' then | |||
else | result, negative = result[1], 'S' | ||
result, negative = result[2], ' | 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 | ||
else | else | ||
return mw.ustring.match(frame.args[1], 'params=.-_'..frame.args[2]..':(.-)[ _]') | return mw.ustring.match(frame.args[1], 'params=.-_'..frame.args[2]..':(.-)[ _]') |