Module:LocationMap: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 9: Line 9:
     local hasGeoJson = frame.args.hasGeoJson or ''
     local hasGeoJson = frame.args.hasGeoJson or ''
      
      
     -- Get all child locations
     -- Get all child locations with page names
     local query = {
     local query = {
         string.format('[[Is Located In::%s]]', pageName),
         string.format('[[Is Located In::%s]]', pageName),
         '?Has Coordinates',
         '?Has Coordinates',
        'mainlabel=-',
         'limit=500'
         'limit=500'
     }
     }
      
      
     local results = smw.ask(query)
     local results = smw.getQueryResult(query)
      
      
     -- Build coordinates list with titles and colors
     -- Build coordinates list
     local coordinates = {}
     local coordinates = {}
      
      
     -- Add current location (red marker)
     -- Add current location
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
         table.insert(coordinates, hasCoordinates .. '~[[' .. pageName .. ']]~red')
         table.insert(coordinates, hasCoordinates)
     end
     end
      
      
     -- Add child locations (blue markers)
     -- Add child locations
     if results then
     if results and results.results then
         for _, row in ipairs(results) do
         for childPageName, data in pairs(results.results) do
             if row['Has Coordinates'] then
             if data.printouts and data.printouts['Has Coordinates'] and data.printouts['Has Coordinates'][1] then
                -- The page title is usually in a field with key starting with '#'
                 local coords = data.printouts['Has Coordinates'][1]
                 local childPage = ''
                 table.insert(coordinates, coords)
                for key, value in pairs(row) do
                    if type(key) == 'string' and (key:sub(1,1) == '#' or key == '') then
                        childPage = value
                        break
                    end
                end
                 if childPage ~= '' then
                    table.insert(coordinates, row['Has Coordinates'] .. '~[[' .. childPage .. ']]~blue')
                end
             end
             end
         end
         end
Line 46: Line 38:
      
      
     -- Build the display_map call
     -- Build the display_map call
     local coordString = table.concat(coordinates, ';')
     local mapParams = {}
     local mapParams = {
   
        'zoom=6',
    if #coordinates > 0 then
        'service=leaflet',
        table.insert(mapParams, table.concat(coordinates, ';'))
        'height=250px'
    end
    }
   
     table.insert(mapParams, 'zoom=6')
    table.insert(mapParams, 'service=leaflet')
    table.insert(mapParams, 'height=250px')
      
      
     if hasCoordinates ~= '' then
     if hasCoordinates ~= '' then
Line 61: Line 56:
     end
     end
      
      
     if coordString ~= '' then
     if #coordinates > 0 or hasGeoJson ~= '' then
         return frame:callParserFunction('#display_map', coordString, table.concat(mapParams, '|'))
         return frame:callParserFunction('#display_map', table.unpack(mapParams))
     else
     else
         return ''
         return ''

Revision as of 12:40, 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]
                table.insert(coordinates, coords)
            end
        end
    end
    
    -- Build the display_map call
    local mapParams = {}
    
    if #coordinates > 0 then
        table.insert(mapParams, table.concat(coordinates, ';'))
    end
    
    table.insert(mapParams, 'zoom=6')
    table.insert(mapParams, 'service=leaflet')
    table.insert(mapParams, 'height=250px')
    
    if hasCoordinates ~= '' then
        table.insert(mapParams, 'center=' .. hasCoordinates)
    end
    
    if hasGeoJson ~= '' then
        table.insert(mapParams, 'geojson=' .. hasGeoJson)
    end
    
    if #coordinates > 0 or hasGeoJson ~= '' then
        return frame:callParserFunction('#display_map', table.unpack(mapParams))
    else
        return ''
    end
end

return p