Module:Carte interactive

Une nouvelle de Wikinews, la source d'informations que vous pouvez écrire.

La documentation pour ce module peut être créée à Module:Carte interactive/Documentation

local p = {}
local coord = require( "Module:Coordinates" )

local function normalise(args)
	local lookup_table = {}
	lookup_table['largeur']= 'width'
	lookup_table['hauteur'] = 'height'
	lookup_table['texte'] = 'text'
	lookup_table['legende'] = 'text'
	lookup_table['légende'] = 'text'
	lookup_table['déscription'] = 'description'
	lookup_table['titre'] = 'title'
	lookup_table['sans_cadre'] = 'frameless'
	lookup_table['symbole'] = 'symbol'
	lookup_table['couleur'] = 'color'
	lookup_table['taille'] = 'size'
	lookup_table['epaisseur'] = 'size'
	lookup_table['épaisseur'] = 'size'
	lookup_table['opacite'] = 'opacity'
	lookup_table['opacité'] = 'opacity'
	lookup_table['epaisseur contour'] = 'size'
	lookup_table['épaisseur contour'] = 'size'
	lookup_table['couleur contour'] = 'color'
	lookup_table['opacite contour'] = 'opacity'
	lookup_table['opacité contour'] = 'opacity'
	lookup_table['couleur fond'] = 'fill_color'
	lookup_table['opacite fond'] = 'fill_opacity'
	lookup_table['opacité fond'] = 'fill_opacity'
	lookup_table['geoline'] = 'contour'
	lookup_table['requête'] = 'query'
	lookup_table['requete'] = 'query'
	lookup_table['request'] = 'query'
	lookup_table['langue'] = 'lang'
	
	for k, v in pairs(lookup_table) do
		if args[k] ~= nil then
			args[v] = args[k]
		end
	end
	
	return args
end

local function decodeJsonList(value)
	--[[ Plusieurs modèles, dont {{Carte interactive/Marqueur}}, ajoutent des
	virgules à la fin des représentations JSON des objets. Cela fonctionne avec
	le parsing JSON relaxé de HHVM, mais pas PHP7. Ce bug est détaillé ici:
		https://phabricator.wikimedia.org/T214984
	Cette fonction retire donc les virgules finales et initiales, avant
	d'appeler mw.text.jsonDecode sur la liste.
	]]
	local trimmedValue = mw.text.trim(value, ',')
	return mw.text.jsonDecode('[' .. trimmedValue .. ']')
end

function p.marker(frame)
	local args = normalise(frame:getParent().args)
	
	if args[1] == nil and args.latlng == nil then --One coordinate is mandatory
		return
	end
	
	local properties = {}
	properties['marker-color'] = args.color == nil and '#555555' or args.color
	properties['marker-size'] = args.size == nil and 'medium' or args.size
	properties['marker-symbol'] = args.symbol == nil and '' or args.symbol
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	local latlng = mw.text.split( (args[1] == nil and args.latlng or args[1]), ",", true )
	
	if args.randomise ~= nil then
		latlng[1] = latlng[1] + math.random(-tonumber(args.randomise), tonumber(args.randomise))/100000
		latlng[2] = latlng[2] + math.random(-tonumber(args.randomise), tonumber(args.randomise))/100000
	end
	
	local geojson = {
            type = "Feature",
            properties = properties,
            geometry = {
                type = "Point",
                coordinates = {
                	tonumber(latlng[2]), 
                	tonumber(latlng[1]),
                }
            }
        }
    return mw.text.jsonEncode(geojson) .. ','
end

function p.line(frame)
	local args = normalise(frame:getParent().args)
	
	if args[2] == nil then --At least two coordinates are mandatory
		return
	end
	
	local properties = {}
	properties["stroke"] = args.color == nil and '#555555' or args.color
	properties["stroke-width"] = args.size == nil and 2 or tonumber(args.size)
	properties["stroke-opacity"] = args.opacity == nil and 1 or tonumber(args.opacity)
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	local coordinates = {}
	local i = 1
	while args[i] ~= nil do
		local latlng = mw.text.split( args[i], ",", true )
		coordinates[i] = {
        	tonumber(latlng[2]), 
        	tonumber(latlng[1]),
        }
		i = i+1
	end
	
	local geojson = {
            type = "Feature",
            properties = properties,
            geometry = {
                type = "LineString",
                coordinates = coordinates
            }
        }
    return mw.text.jsonEncode(geojson) .. ','
end

function p.polygon(frame)
	local args = normalise(frame:getParent().args)
	
	if args[3] == nil then --At least three coordinates are mandatory
		return
	end
	
	local properties = {}
	properties["stroke"] = args.color == nil and '#555555' or args.color
	properties["stroke-width"] = args.size == nil and 2 or tonumber(args.size)
	properties["stroke-opacity"] = args.opacity == nil and 1 or tonumber(args.opacity)
	properties["fill"] = args.fill_color == nil and '#555555' or args.fill_color
	properties["fill-opacity"] = args.fill_opacity == nil and 0.3 or tonumber(args.fill_opacity)
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	local coordinates = {}
	local i = 1
	while args[i] ~= nil do
		local latlng = mw.text.split( args[i], ",", true )
		coordinates[i] = {
        	tonumber(latlng[2]), 
        	tonumber(latlng[1]),
        }
		i = i+1
	end
	local latlng = mw.text.split( args[1], ",", true )
	coordinates[i] = {
    	tonumber(latlng[2]), 
    	tonumber(latlng[1]),
    }
	
	local geojson = {
            type = "Feature",
            properties = properties,
            geometry = {
                type = "Polygon",
                coordinates = { coordinates }
            }
        }
    return mw.text.jsonEncode(geojson) .. ','
end

function p.query(frame)
	local args = normalise(frame:getParent().args)
	
	local query
	if args[1] ~= nil then
		query = args[1]
	else
		return
	end
	
	local properties = {}
	properties["stroke"] = args.color == nil and '#555555' or args.color
	properties["stroke-width"] = args.size == nil and 2 or tonumber(args.size)
	properties["stroke-opacity"] = args.opacity == nil and 1 or tonumber(args.opacity)
	properties["fill"] = args.fill_color == nil and '#555555' or args.fill_color
	properties["fill-opacity"] = args.fill_opacity == nil and 0.3 or tonumber(args.fill_opacity)
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	local service = "geoshape"
	if args.contour ~= nil then
		service = "geoline"
	end
	
	local geojson = {
		type = "ExternalData",
		service = service,
		query = query,
		properties = properties,
	}
    return mw.text.jsonEncode(geojson) .. ','
end

function p.osm(frame)
	local args = normalise(frame:getParent().args)
	
	local qid
	if args[1] == nil then
		qid = mw.wikibase.getEntityIdForCurrentPage()
	else
		qid = args[1]
	end
	
	local properties = {}
	properties["stroke"] = args.color == nil and '#555555' or args.color
	properties["stroke-width"] = args.size == nil and 2 or tonumber(args.size)
	properties["stroke-opacity"] = args.opacity == nil and 1 or tonumber(args.opacity)
	properties["fill"] = args.fill_color == nil and '#555555' or args.fill_color
	properties["fill-opacity"] = args.fill_opacity == nil and 0.3 or tonumber(args.fill_opacity)
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	local service = "geoshape"
	if args.contour ~= nil then
		service = "geoline"
	end
	
	local geojson = {
		type = "ExternalData",
		service = service,
		ids = qid,
		properties = properties,
	}
    return mw.text.jsonEncode(geojson) .. ','
end

function p.commons(frame)
	local args = normalise(frame:getParent().args)
	
	if args[1] == nil then
		return
	end
	
	local page_name = args[1]
	if mw.ustring.find(page_name, "Data:", 1, true) == 1 then
		page_name = string.sub(page_name, 6)
	end
	if mw.ustring.find(page_name, ".map", -4, true) == nil then
		page_name = page_name .. '.map'
	end
	
	local geojson = {
		type = "ExternalData",
		service = "page",
		title = page_name
	}
    return mw.text.jsonEncode(geojson) .. ','
end

function p.wikidata(frame)
	local args = normalise(frame:getParent().args)
	local qid
	
	local properties = {}
	properties['marker-color'] = args.color == nil and '#555555' or args.color
	properties['marker-size'] = args.size == nil and 'medium' or args.size
	properties['marker-symbol'] = args.symbol == nil and '' or args.symbol
	properties['title'] = args.title == nil and '' or args.title
	properties['description'] = args.description == nil and '' or args.description
	
	if args[1] == nil then
		qid = mw.wikibase.getEntityIdForCurrentPage()
	else
		qid = args[1]
	end
	
	local entity = mw.wikibase.getEntity( qid )
	local value = entity:formatPropertyValues( 'P625', { mw.wikibase.entity.claimRanks.RANK_NORMAL, mw.wikibase.entity.claimRanks.RANK_PREFERRED, mw.wikibase.entity.claimRanks.RANK_TRUTH } ).value
	if value == nil then
		error('La propriété P625 "coordonnées géographiques" n\'est pas renseigné sur Wikidata')
	end
	latlng = mw.text.split( mw.text.decode(value), ", ", true )
	geojson = {
		type = "Feature",
		geometry = {
			type = "Point",
			coordinates = {
				coord._dms2dec(coord._parsedmsstring(latlng[2])), 
				coord._dms2dec(coord._parsedmsstring(latlng[1]))
			}
		},
		properties = properties
	}
	
	return mw.text.jsonEncode(geojson) .. ','
end

function p.tag(frame)
	local args = normalise(frame:getParent().args)
	
	-- Choose the tagname
	local tagname = 'mapframe'
	if args.lien ~= nil then
		tagname = 'maplink'
	end

	-- Manage the basics tag params
	local tagArgs = {
		zoom = args.zoom == nil and args.commons == nil and 14 or tonumber(args.zoom),
		height = args.height == nil and 420 or tonumber(args.height),
		width = args.width == nil and 420 or tonumber(args.width),
		align = args.align == nil and 'right' or args.align,
		text = args.text,
	}
	
	if args[1] ~= nil and (args.latitude ~= nil or args.longitude ~= nil) then
		error('La ou les valeurs de longitude et/ou de latitude ont été fournis via deux syntaxe différentes. Consultez la documentation du [[Modèle:Carte interactive]] pour obtenir une syntaxe correcte')
	elseif args.latitude ~= nil and args.longitude ~= nil then
		tagArgs.latitude = tonumber(args.latitude)
		tagArgs.longitude = tonumber(args.longitude)
	elseif args[1] ~= nil and args[2] ~= nil then
		tagArgs.latitude = tonumber(args[1])
		tagArgs.longitude = tonumber(args[2])
	elseif args.commons == nil then --The only exception allowed to put latitude and longitude away is when using an external map stored on commons
		error('Les paramètres de latitude et/ou de longitude sont absent')
	end
	
	if args.frameless then
		tagArgs.frameless = ''
	end
	
	if args.lang ~= nil then
		tagArgs.lang = args.lang
	end
	
	-- Manage the basics GeoJSON params
	local geojson = {}
	
	if args.commons ~= nil then
		geojson[#geojson+1] = {
			type = "ExternalData",
			service = "page",
			title = args.commons,
		}
	end
	
	if args.marqueur ~= nil then
		geojson[#geojson+1] = {
			type = "Feature",
			geometry = {
				type = "Point",
				coordinates = {
					tagArgs.longitude, 
					tagArgs.latitude
				}
			},
			properties = {
				title = args.title == nil and '' or args.title,
				description = args.description == nil and '' or args.description,
			}
		}
	end
	
	if args.osm ~= nil then
		local qid
		if args.osm == 'oui' then
			qid = mw.wikibase.getEntityIdForCurrentPage()
		else
			qid = args.osm
		end
		
		local service = "geoshape"
		if args.contour ~= nil then
			service = "geoline"
		end
		
		geojson[#geojson+1] = {
			type = "ExternalData",
			service = service,
			ids = qid,
			properties = {
				title = args.title == nil and '' or args.title,
				description = args.description == nil and '' or args.description,
			}
		}
	end
	
	local qid = args.wikidata
	if qid and not mw.wikibase.isValidEntityId( qid ) then
		qid = mw.wikibase.getEntityIdForCurrentPage()
	end
	if qid and mw.wikibase.isValidEntityId( qid ) then
		local entity = mw.wikibase.getEntity( qid )
		local value = entity:formatPropertyValues( 'P625', { mw.wikibase.entity.claimRanks.RANK_NORMAL, mw.wikibase.entity.claimRanks.RANK_PREFERRED, mw.wikibase.entity.claimRanks.RANK_TRUTH } ).value
		if value == '' then
			error('La propriété P625 "coordonnées géographiques" n\'est pas renseigné sur Wikidata')
		end
		latlng = mw.text.split( mw.text.decode(value), ", ", true )
		geojson[#geojson+1] = {
			type = "Feature",
			geometry = {
				type = "Point",
				coordinates = {
					coord._dms2dec(coord._parsedmsstring(latlng[2])), 
					coord._dms2dec(coord._parsedmsstring(latlng[1]))
				}
			},
			properties = {
				title = args.title == nil and '' or args.title,
				description = args.description == nil and '' or args.description,
			}
		}
	end
	
	if args.query ~= nil then
		local service = "geoshape"
		if args.contour ~= nil then
			service = "geoline"
		end
		
		geojson[#geojson+1] = {
			type = "ExternalData",
			service = service,
			query = args.query,
			properties = {
				title = args.title == nil and '' or args.title,
				description = args.description == nil and '' or args.description,
			}
		}
	end
	
	--Manage external GeoJSON datas included through models
	if args.formes ~= nil then
		geojson[#geojson+1] = {
			type = "FeatureCollection",
			features = decodeJsonList(args.formes)
		}
	end
	
	if args.externes ~= nil then
		local externes = decodeJsonList(args.externes)
		for k, externe in pairs(externes) do
			geojson[#geojson+1] = externe
		end
	end
	
	return frame:extensionTag(tagname, mw.text.jsonEncode(geojson), tagArgs)
end

return p