-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.lua
137 lines (116 loc) · 3.47 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
-- Dynamically determine directory path and update package.path
function get_directory_path()
-- Get the file path of the currently executing script
local file_path = debug.getinfo(2, "S").source:sub(2)
-- Match up to the last directory separator (either / or \)
local dir_path = file_path:match("(.*[\\/])")
return dir_path
end
local dir_path = get_directory_path()
package.path = package.path .. ";" .. dir_path .. "?.lua"
local helper = require("helper")
local tables = {}
local srid = 4326
tables.nodes = osm2pgsql.define_table({
name = "nodes",
indexes = {},
cluster = "no",
ids = { type = "node", id_column = "id" },
columns = {
{ column = "tags", type = "hstore" },
{ column = "geom", type = "point", not_null = true, projection = srid },
},
})
tables.ways = osm2pgsql.define_table({
name = "ways",
indexes = {},
cluster = "no",
ids = { type = "way", id_column = "id" },
columns = {
{ column = "tags", type = "hstore" },
{ column = "geom", type = "geometry", not_null = true, projection = srid },
{ column = "from", type = "bigint", not_null = true },
{ column = "to", type = "bigint", not_null = true },
{ column = "oneway", type = "boolean" },
},
})
tables.relations = osm2pgsql.define_table({
name = "relations",
indexes = {},
cluster = "no",
ids = { type = "relation", id_column = "id" },
columns = {
{ column = "tags", type = "hstore" },
{ column = "members", type = "jsonb" },
},
})
tables.nodes_ways = osm2pgsql.define_table({
name = "nodes_ways",
indexes = {},
cluster = "no",
ids = { type = "way", id_column = "way_id" },
columns = {
{ column = "node_id", type = "bigint" },
{ column = "position", type = "smallint" },
},
})
-- Functions to process objects:
local function do_nodes(object)
tables.nodes:insert({
tags = object.tags,
geom = object:as_point(),
})
end
local function array_reverse(x)
local n, m = #x, #x / 2
for i = 1, m do
x[i], x[n - i + 1] = x[n - i + 1], x[i]
end
return x
end
local function do_ways(object)
helper.clean_tags(object.tags)
local nodes = object.nodes
local oneway = false
if object.tags.oneway == "-1" then
-- handle reversed oneway street
nodes = array_reverse(nodes)
oneway = true
else
oneway = object.tags.oneway == "yes"
end
-- 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