File: track-changes.lua

package info (click to toggle)
osm2pgsql 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,772 kB
  • sloc: cpp: 60,940; python: 1,115; ansic: 763; sh: 25; makefile: 14
file content (80 lines) | stat: -rw-r--r-- 2,693 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- This config example file is released into the Public Domain.

-- This config shows how to track in a table which OSM object have been
-- added, changed and deleted.

-- The main table logging the changes.
local change_table = osm2pgsql.define_table{
    name = 'change_log',
    -- Disable automatic ID tracking by osm2pgsql. No rows should ever
    -- be deleted. osm2pgsql will issue a warning about this. It can
    -- be safely ignored.
    ids = nil,
    columns = {
        { column = 'osm_type', type = 'text' },
        { column = 'osm_id', type = 'bigint' },
        { column = 'version', type = 'int' },
        -- This column describes the kind of change:
        -- 'A' for added/newly created,
        -- 'M' for modified,
        -- 'D' for deleted
        { column = 'action', type = 'text' },
        { column = 'date', sql_type = 'timestamp' }
    },
    indexes = {
        { column = { 'osm_type', 'osm_id' }, method = 'btree' }
    }
}

-- We only want to catch changes coming from the OSM file input.
-- This flag marks when file reading is done and dependent objects are
-- being processed.
local file_reading_in_progress = true

local function format_date(ts)
    return os.date('!%Y-%m-%dT%H:%M:%SZ', ts)
end

local function add_object_change(object)
    -- In this example only changes while updating the database are recorded.
    -- This happens in 'append' mode.
    if osm2pgsql.mode == 'append' and file_reading_in_progress then
        change_table:insert{
            osm_type = object.type,
            osm_id = object.id,
            version = object.version,
            action = (object.version == 1) and 'A' or 'M',
            date = format_date(object.timestamp)
        }
    end
end

osm2pgsql.process_node = add_object_change
osm2pgsql.process_way = add_object_change
osm2pgsql.process_relation = add_object_change

osm2pgsql.process_untagged_node = add_object_change
osm2pgsql.process_untagged_way = add_object_change
osm2pgsql.process_untagged_relation = add_object_change


local function add_deleted_object(object)
    change_table:insert{
        osm_type = object.type,
        osm_id = object.id,
        version = object.version,
        action = 'D',
        date = format_date(object.timestamp)
    }
end

osm2pgsql.process_deleted_node = add_deleted_object
osm2pgsql.process_deleted_way = add_deleted_object
osm2pgsql.process_deleted_relation = add_deleted_object

function osm2pgsql.after_relations()
  -- This callback is called after the last relation has been read from
  -- the input file. As objects are guaranteed to come in order
  -- node/way/relation, file reading is done at that point.
  file_reading_in_progress = false
end