Skip to content

Commit c875ae8

Browse files
committed
add example for delete callback
1 parent 96a1d75 commit c875ae8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

flex-config/track-changes.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
{ column = 'action', type = 'text' },
18+
{ column = 'date', sql_type = 'timestamp' }
19+
},
20+
indexes = {
21+
{ column = { 'osm_type', 'osm_id' }, method = 'btree' }
22+
}
23+
}
24+
25+
-- We only want to catch changes coming from the OSM file input.
26+
-- This flag marks when file reading is done and dependent objects are
27+
-- being processed.
28+
local file_reading_in_progress = true
29+
30+
local function format_date(ts)
31+
return os.date('!%Y-%m-%dT%H:%M:%SZ', ts)
32+
end
33+
34+
local function add_object_change(object)
35+
-- In this example only changes while updating the database are recorded.
36+
-- This happens in 'append' mode.
37+
if osm2pgsql.mode == 'append' and file_reading_in_progress then
38+
change_table:insert{
39+
osm_type = object.type,
40+
osm_id = object.id,
41+
version = object.version,
42+
action = (object.version == 1) and 'A' or 'M',
43+
date = format_date(object.timestamp)
44+
}
45+
end
46+
end
47+
48+
osm2pgsql.process_node = add_object_change
49+
osm2pgsql.process_way = add_object_change
50+
osm2pgsql.process_relation = add_object_change
51+
52+
osm2pgsql.process_untagged_node = add_object_change
53+
osm2pgsql.process_untagged_way = add_object_change
54+
osm2pgsql.process_untagged_relation = add_object_change
55+
56+
57+
local function add_deleted_object(object)
58+
change_table:insert{
59+
osm_type = object.type,
60+
osm_id = object.id,
61+
version = object.version,
62+
action = 'D',
63+
date = format_date(object.timestamp)
64+
}
65+
end
66+
67+
osm2pgsql.process_deleted_node = add_deleted_object
68+
osm2pgsql.process_deleted_way = add_deleted_object
69+
osm2pgsql.process_deleted_relation = add_deleted_object

0 commit comments

Comments
 (0)