コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:地図/行政区域

モジュールの解説[作成]
local rootnode=require("Module:地図/data")
local head={}
for k,v in ipairs({string.byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",1,-1)}) do
	head[v]=k-1
end
local tail={}
for k,v in ipairs({string.byte("==9xsy+===4ldYem5=8kUNIOVn/wcMEAFPfzvbLD=BJZt3jTHCGQg0.rXSKRWo*=7qiahp6===^2u1-==",1,-1)}) do
	if v~=string.byte("=") then
		tail[v]={(k-1)%9-4,math.floor((k-1)/9)-4}
	end
end
local function decodeHead(b,index)
	return head[b[index]]+64*(head[b[index+1]]+64*head[b[index+2]])
end
local function decode(s)
	local b={s:byte(1,-1)}
	local p={decodeHead(b,1),decodeHead(b,4)}
	local list={}
	table.insert(list,{p[1]*360/65536,p[2]*360*2/3/65536})
	for k,v in ipairs(b) do
		if k>=7 then
			local d=tail[v]
			p={p[1]+d[1],p[2]+d[2]}
			table.insert(list,{p[1]*360/65536,p[2]*360*2/3/65536})
		end
	end
	return list
end
local function getOuter(result,projection,node,depth,targetdepth)
	local r={node[3]:byte(1,-1)}
	if projection.contains({decodeHead(r,1)*360/65536,decodeHead(r,4)*360/65536,decodeHead(r,7)*360*2/3/65536,decodeHead(r,10)*360*2/3/65536}) then
		if depth==targetdepth then
			local t=""
			for line in string.gmatch(node[4],"[^,]+") do
				local ps=projection.convertMulti(false,decode(line))
				if #ps>=3 then
					local s=""
					for k,v in ipairs(ps) do
						s=s..string.format("%s%.1f %.1f",(k==1 and "M") or "L",v[1],v[2])
					end
					s=s.."Z"
					t=t..s
				end
			end
			if #t>0 then
				table.insert(result,{node[1],node[2],t})
			end
		end
		local subdepth=depth+((depth==1 and node[1]~=01000) and 2 or 1)
		if subdepth<=targetdepth and node[6]~=nil then
			for _,subnode in ipairs(node[6]) do
				getOuter(result,projection,subnode,subdepth,targetdepth)
			end
		end
	end
end
local function getInner(result,projection,node,depth,targetdepth)
	local r={node[3]:byte(1,-1)}
	if projection.contains({decodeHead(r,1)*360/65536,decodeHead(r,4)*360/65536,decodeHead(r,7)*360*2/3/65536,decodeHead(r,10)*360*2/3/65536}) then
		local subdepth=depth+((depth==1 and node[1]~=01000) and 2 or 1)
		if subdepth==targetdepth and node[5]~=nil then
			for line in string.gmatch(node[5],"[^,]+") do
				local ps=projection.convertMulti(true,decode(line))
				if #ps>=2 then
					local s=""
					for k,v in ipairs(ps) do
						s=s..string.format("%s%.1f %.1f",(k==1 and "M") or "L",v[1],v[2])
					end
					result[1]=result[1]..s
				end
			end
		end
		if subdepth<targetdepth and node[6]~=nil then
			for _,subnode in ipairs(node[6]) do
				getInner(result,projection,subnode,subdepth,targetdepth)
			end
		end
	end
end
return function(projection)
	return{
		getOuterPath=function(depth)
			local result={}
			getOuter(result,projection,rootnode,0,depth)
			return result
		end,
		getCoast=function()
			local result={}
			getOuter(result,projection,rootnode,0,0)
			return result[1][3]
		end,
		getInnerPath=function(depth)
			local result={""}
			getInner(result,projection,rootnode,0,depth)
			return result[1]
		end
	}
end