File: places.lua

package info (click to toggle)
osm2pgsql 1.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,124 kB
  • sloc: cpp: 41,466; ansic: 1,366; python: 564; sh: 19; makefile: 15
file content (53 lines) | stat: -rw-r--r-- 1,486 bytes parent folder | download
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
-- This config example file is released into the Public Domain.

-- This example shows how you can use JSON and JSONB columns in your database.

-- You need the 'dkjson' JSON library for Lua. Install 'dkjson' with LuaRocks
-- or install from http://dkolf.de/src/dkjson-lua.fsl/home . Debian/Ubuntu
-- users can install the 'lua-dkjson' package.

-- Use JSON encoder
local json = require('dkjson')

local places = osm2pgsql.define_node_table('places', {
    -- The jsonb column is handled by osm2pgsql just like a normal text
    -- column. It is the job of the Lua script to put correct json there.
    { column = 'tags', type = 'jsonb' },
    { column = 'geom', type = 'point' },
})

local function starts_with(str, start)
   return str:sub(1, #start) == start
end

function osm2pgsql.process_node(object)
    if not object.tags.place then
        return
    end

    -- Put all name:* tags in their own substructure
    local names = {}
    local has_names = false
    for k, v in pairs(object.tags) do
        if k == 'name' then
            names[''] = v
            object.tags.name = nil
            has_names = true
        elseif starts_with(k, "name:") then
            -- extract language
            local lang = k:sub(6, -1)
            names[lang] = v
            object.tags[k] = nil
            has_names = true
        end
    end

    if has_names then
        object.tags.names = names
    end

    places:add_row({
        tags = json.encode(object.tags)
    })
end