1-- This config example file is released into the Public Domain.
2
3-- This is a very simple Lua config for the Flex output not intended for
4-- real-world use. Look at and understand "simple.lua" first, before looking
5-- at this file. This file demonstrates some column data type options.
6
7local highways = osm2pgsql.define_way_table('highways', {
8    { column = 'name',     type = 'text' },
9    -- We always need a highway type, so we can declare the column as NOT NULL
10    { column = 'type',     type = 'text', not_null = true },
11
12    -- Add a SERIAL column and tell osm2pgsql not to fill it (PostgreSQL will
13    -- do that for us)
14    { column = 'id',       sql_type = 'serial', create_only = true },
15
16    -- type "direction" is special, see below
17    { column = 'oneway',   type = 'direction' },
18    { column = 'maxspeed', type = 'int' },
19
20    -- type "bool" is special, see below
21    { column = 'lit',      type = 'bool' },
22    { column = 'tags',     type = 'jsonb' }, -- also available: 'json', 'hstore'
23
24    -- osm2pgsql doesn't know about PostgreSQL arrays, so we define the SQL
25    -- type of this column and then have to convert our array data into a
26    -- valid text representation for that type, see below.
27    { column = 'nodes',    sql_type = 'int8[]' },
28    { column = 'geom',     type = 'linestring' },
29})
30
31-- Helper function to remove some of the tags we usually are not interested in.
32-- Returns true if there are no tags left.
33function clean_tags(tags)
34    tags.odbl = nil
35    tags.created_by = nil
36    tags.source = nil
37    tags['source:ref'] = nil
38
39    return next(tags) == nil
40end
41
42local highway_types = {
43    'motorway',
44    'motorway_link',
45    'trunk',
46    'trunk_link',
47    'primary',
48    'primary_link',
49    'secondary',
50    'secondary_link',
51    'tertiary',
52    'tertiary_link',
53    'unclassified',
54    'residential',
55    'track',
56    'service',
57}
58
59-- Prepare table "types" for quick checking of highway types
60local types = {}
61for _, k in ipairs(highway_types) do
62    types[k] = 1
63end
64
65-- Parse a maxspeed value like "30" or "55 mph" and return a number in km/h
66function parse_speed(input)
67    if not input then
68        return nil
69    end
70
71    local maxspeed = tonumber(input)
72
73    -- If maxspeed is just a number, it is in km/h, so just return it
74    if maxspeed then
75        return maxspeed
76    end
77
78    -- If there is an 'mph' at the end, convert to km/h and return
79    if input:sub(-3) == 'mph' then
80        local num = tonumber(input:sub(1, -4))
81        if num then
82            return math.floor(num * 1.60934)
83        end
84    end
85
86    return nil
87end
88
89function osm2pgsql.process_way(object)
90    if clean_tags(object.tags) then
91        return
92    end
93
94    -- Get the type of "highway" and remove it from the tags
95    local type = object:grab_tag('highway')
96
97    -- We are only interested in highways of the given types
98    if not types[type] then
99        return
100    end
101
102    -- We want to put the name in its own column
103    local name = object:grab_tag('name')
104
105    highways:add_row({
106        name = name,
107        type = type,
108
109        -- The 'maxspeed' column gets the maxspeed in km/h
110        maxspeed = parse_speed(object.tags.maxspeed),
111
112        -- The 'oneway' column has the special type "direction", which will
113        -- store "yes", "true" and "1" as 1, "-1" as -1, and everything else
114        -- as 0.
115        oneway = object.tags.oneway or 0,
116
117        -- The 'lit' column has the special type "bool", which will store
118        -- "yes" and "true" as true and everything else as false value.
119        lit = object.tags.lit,
120
121        -- The way node ids are put into a format that PostgreSQL understands
122        -- for a column of type "int8[]".
123        nodes = '{' .. table.concat(object.nodes, ',') .. '}',
124
125        tags = object.tags
126    })
127end
128
129