-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.lua
116 lines (99 loc) · 2.98 KB
/
pipeline.lua
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- Dynamically determine directory path and update package.path
local separator = package.config:sub(1,1)
function get_directory_path(sep)
local file_path = debug.getinfo(2, "S").source:sub(2)
local dir_path = file_path:match("(.*" .. sep .. ")")
return dir_path
end
local dir_path = get_directory_path(separator)
package.path = package.path .. ";" .. dir_path .. "?.lua"
local helper = require("helper")
local tables = {}
local srid = 4326
tables.nodes = osm2pgsql.define_table({
name = "nodes",
ids = { type = "node", id_column = "id" },
columns = {
{ column = "geom", type = "point", not_null = true, projection = srid },
{ column = "contracted", type = "boolean", not_null = true },
{ column = "area", type = "integer", create_only = true },
},
})
tables.ways = osm2pgsql.define_table({
name = "ways",
ids = { type = "way", id_column = "id" },
columns = {
{ column = "tags", type = "hstore" },
{ column = "geom", type = "geometry", not_null = true, projection = srid },
{ column = "area", type = "smallint", create_only = true },
{ column = "from", type = "integer", not_null = true },
{ column = "to", type = "integer", not_null = true },
{ column = "oneway", type = "boolean" },
},
})
tables.relations = osm2pgsql.define_table({
name = "relations",
ids = { type = "relation", id_column = "id" },
columns = {
{ column = "tags", type = "hstore" },
{ column = "members", type = "jsonb" },
},
})
tables.nodes_ways = osm2pgsql.define_table({
name = "nodes_ways",
ids = { type = "way", id_column = "way_id" },
columns = {
{ column = "id", sql_type = "serial", create_only = true },
{ column = "node_id", type = "integer" },
{ column = "position", type = "smallint" },
{ column = "area", type = "smallint", create_only = true },
},
})
-- Functions to process objects:
local function do_nodes(object)
tables.nodes:insert({
geom = object:as_point(),
contracted = false,
})
end
local function do_ways(object)
helper.clean_tags(object.tags)
local nodes = object.nodes
local oneway = object.tags.oneway == "yes"
-- clean additional tags
object.tags.oneway = nil
-- add to ways
tables.ways:insert({
geom = object:as_linestring(),
tags = object.tags,
oneway = oneway,
from = nodes[1],
to = nodes[#nodes],
})
-- add to nodes_ways
for index, value in ipairs(nodes) do
tables.nodes_ways:insert({
node_id = value,
position = index,
})
end
end
local function do_relations(object)
if helper.clean_tags(object.tags) then
return
end
tables.relations:insert({
tags = object.tags,
members = object.members,
})
end
-- Process tagged objects:
osm2pgsql.process_node = do_nodes
osm2pgsql.process_way = do_ways
osm2pgsql.process_relation = do_relations
-- If osm2pgsql is of version `2.0.0` or higher, assign process_untagged_* functions:
if helper.compare_version(osm2pgsql.version, '2.0.0') then
osm2pgsql.process_untagged_node = do_nodes
osm2pgsql.process_untagged_way = do_ways
osm2pgsql.process_untagged_relation = do_relations
end