Module:LocationMap: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 32: Line 32:
             if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
             if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
                 local coords = data.printouts['Has Coordinates'][1]
                 local coords = data.printouts['Has Coordinates'][1]
                -- Handle if coords is a table or string
                if type(coords) == 'table' then
                    coords = coords.lat .. ',' .. coords.lon
                end
                 table.insert(coordinates, coords)
                 table.insert(coordinates, coords)
             end
             end
Line 38: Line 42:
      
      
     -- Build the display_map call
     -- Build the display_map call
     local mapParams = {}
     local coordString = ''
   
     if #coordinates > 0 then
     if #coordinates > 0 then
         table.insert(mapParams, table.concat(coordinates, ';'))
         coordString = table.concat(coordinates, ';')
     end
     end
      
      
     table.insert(mapParams, 'zoom=6')
     local mapParams = 'zoom=6|service=leaflet|height=250px'
    table.insert(mapParams, 'service=leaflet')
    table.insert(mapParams, 'height=250px')
      
      
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         table.insert(mapParams, 'center=' .. hasCoordinates)
         mapParams = mapParams .. '|center=' .. hasCoordinates
     end
     end
      
      
     if hasGeoJson ~= '' then
     if hasGeoJson ~= '' then
         table.insert(mapParams, 'geojson=' .. hasGeoJson)
         mapParams = mapParams .. '|geojson=' .. hasGeoJson
     end
     end
      
      
     if #coordinates > 0 or hasGeoJson ~= '' then
     if coordString ~= '' then
         return frame:callParserFunction('#display_map', table.unpack(mapParams))
        return frame:callParserFunction('#display_map', coordString, mapParams)
    elseif hasGeoJson ~= '' then
         return frame:callParserFunction('#display_map', 'geojson=' .. hasGeoJson, 'zoom=6|service=leaflet|height=250px')
     else
     else
         return ''
         return ''

Revision as of 12:41, 16 February 2026

Documentation for this module may be created at Module:LocationMap/doc

local p = {}

function p.displayMap(frame)
    local mw = require('mw')
    local smw = mw.smw or mw.ext.smw
    
    local pageName = frame.args.pageName or mw.title.getCurrentTitle().text
    local hasCoordinates = frame.args.hasCoordinates or ''
    local hasGeoJson = frame.args.hasGeoJson or ''
    
    -- Get all child locations with page names
    local query = {
        string.format('[[Is Located In::%s]]', pageName),
        '?Has Coordinates',
        'mainlabel=-',
        'limit=500'
    }
    
    local results = smw.getQueryResult(query)
    
    -- Build coordinates list
    local coordinates = {}
    
    -- Add current location
    if hasCoordinates ~= '' then
        table.insert(coordinates, hasCoordinates)
    end
    
    -- Add child locations
    if results and results.results then
        for childPageName, data in pairs(results.results) do
            if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
                local coords = data.printouts['Has Coordinates'][1]
                -- Handle if coords is a table or string
                if type(coords) == 'table' then
                    coords = coords.lat .. ',' .. coords.lon
                end
                table.insert(coordinates, coords)
            end
        end
    end
    
    -- Build the display_map call
    local coordString = ''
    if #coordinates > 0 then
        coordString = table.concat(coordinates, ';')
    end
    
    local mapParams = 'zoom=6|service=leaflet|height=250px'
    
    if hasCoordinates ~= '' then
        mapParams = mapParams .. '|center=' .. hasCoordinates
    end
    
    if hasGeoJson ~= '' then
        mapParams = mapParams .. '|geojson=' .. hasGeoJson
    end
    
    if coordString ~= '' then
        return frame:callParserFunction('#display_map', coordString, mapParams)
    elseif hasGeoJson ~= '' then
        return frame:callParserFunction('#display_map', 'geojson=' .. hasGeoJson, 'zoom=6|service=leaflet|height=250px')
    else
        return ''
    end
end

return p