Skip to content

Commit 4f8e195

Browse files
committed
add example for delete callback
1 parent f7991ab commit 4f8e195

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

flex-config/track-changes.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- This config example file is released into the Public Domain.
2+
3+
-- This config shows how to track in a table which OSM object have been
4+
-- added, changed and deleted.
5+
6+
-- The main table logging the changes.
7+
local change_table = osm2pgsql.define_table{
8+
name = 'change_log',
9+
-- Disable automatic ID tracking by osm2pgsql. No rows should ever
10+
-- be deleted. osm2pgsql will issue a warning about this. It can
11+
-- be safely ignored.
12+
ids = nil,
13+
columns = {
14+
{ column = 'osm_type', type = 'text' },
15+
{ column = 'osm_id', type = 'bigint' },
16+
{ column = 'version', type = 'int' },
17+
-- This column describes the kind of change:
18+
-- 'A' for added/newly created,
19+
-- 'M' for modified,
20+
-- 'D' for deleted
21+
{ column = 'action', type = 'text' },
22+
{ column = 'date', sql_type = 'timestamp' }
23+
},
24+
indexes = {
25+
{ column = { 'osm_type', 'osm_id' }, method = 'btree' }
26+
}
27+
}
28+
29+
-- We only want to catch changes coming from the OSM file input.
30+
-- This flag marks when file reading is done and dependent objects are
31+
-- being processed.
32+
local file_reading_in_progress = true
33+
34+
local function format_date(ts)
35+
return os.date('!%Y-%m-%dT%H:%M:%SZ', ts)
36+
end
37+
38+
local function add_object_change(object)
39+
-- In this example only changes while updating the database are recorded.
40+
-- This happens in 'append' mode.
41+
if osm2pgsql.mode == 'append' and file_reading_in_progress then
42+
change_table:insert{
43+
osm_type = object.type,
44+
osm_id = object.id,
45+
version = object.version,
46+
action = (object.version == 1) and 'A' or 'M',
47+
date = format_date(object.timestamp)
48+
}
49+
end
50+
end
51+
52+
osm2pgsql.process_node = add_object_change
53+
osm2pgsql.process_way = add_object_change
54+
osm2pgsql.process_relation = add_object_change
55+
56+
osm2pgsql.process_untagged_node = add_object_change
57+
osm2pgsql.process_untagged_way = add_object_change
58+
osm2pgsql.process_untagged_relation = add_object_change
59+
60+
61+
local function add_deleted_object(object)
62+
change_table:insert{
63+
osm_type = object.type,
64+
osm_id = object.id,
65+
version = object.version,
66+
action = 'D',
67+
date = format_date(object.timestamp)
68+
}
69+
end
70+
71+
osm2pgsql.process_deleted_node = add_deleted_object
72+
osm2pgsql.process_deleted_way = add_deleted_object
73+
osm2pgsql.process_deleted_relation = add_deleted_object
74+
75+
function osm2pgsql.after_relations()
76+
-- This callback is called after the last relation has been read from
77+
-- the input file. As objects are guaranteed to come in order
78+
-- node/way/relation, file reading is done at that point.
79+
file_reading_in_progress = false
80+
end

0 commit comments

Comments
 (0)