Migrate Place Long Lat to Place Records.fh_lua

--[[
@Title: Migrate Place Long Lat to Place Records
@Author: Jane Taubman
@Version: 1.1
@LastUpdated: 18 Feb 2021 
@Description: Copies Long/Lat values from Place fields in Events and overwrites the values in the matching place record
]]

function main()
    local ptrParent = fhNewItemPtr()
    for ptr in allItems('INDI','FAM') do
        sTag = fhGetTag(ptr)
        if sTag == 'MAP' then
            local ptrLong = fhGetItemPtr(ptr,'~.LONG')
            local ptrLati = fhGetItemPtr(ptr,'~.LATI')
            ptrParent:MoveToParentItem(ptr)
            local ptrPlace = fhGetValueAsLink(ptrParent)
            local ptrPlaceMap = fhGetItemPtr(ptrPlace,'~.LATLONG')
            if ptrPlaceMap:IsNull() then
                ptrPlaceMap = fhCreateItem('LATLONG',ptrPlace)
            end
            local sLati = fhGetValueAsText(ptrLati)
            local sLong = fhGetValueAsText(ptrLong)
            print(fhGetDisplayText(ptrPlace),sLati,sLong)
			  local sValue = sLati..' '..sLong
            bOk = fhSetValueAsText(ptrPlaceMap,sValue)
            if bOK == false then
               error('not set')
            end
        end
    end
end

function allItems(...)
    local iTypeCount = nil
    local iPos = 1
    local p1 = fhNewItemPtr()
    local p2 = fhNewItemPtr()
    local tblRecTypes = {}
    
    arg = {...}
    arg['n'] = #arg

    if arg['n'] == 0 then
        -- No parameter do all Record Types
        iTypeCount = fhGetRecordTypeCount() -- Get Count of Record types
        for i = 1,iTypeCount do
            tblRecTypes[i] = fhGetRecordTypeTag(i)
        end
    else
        -- Got Parameters Process them instead
        tblRecTypes = arg
        iTypeCount = arg['n']
    end
    print(tblRecTypes[iPos],iPos)
    p1:MoveToFirstRecord(tblRecTypes[iPos])
    return function()
        repeat
        while p1:IsNotNull() do
            p2:MoveTo(p1)
            p1:MoveNextSpecial()
            if p2:IsNotNull() then
                return p2
            end
        end
        -- Loop through Record Types
        iPos = iPos + 1
        if iPos <= iTypeCount then
            p1:MoveToFirstRecord(tblRecTypes[iPos])
        end
        until iPos > iTypeCount
    end
end
main()

Source:Migrate-Place-Long-Lat-to-Place-Records.fh_lua