File: addresses.lua

package info (click to toggle)
osm2pgsql 2.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,652 kB
  • sloc: cpp: 59,934; python: 1,039; ansic: 763; sh: 25; makefile: 14
file content (67 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download | duplicates (5)
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
-- This config example file is released into the Public Domain.

-- Get a location for all objects with addresses. If mapped as a polygon or
-- multipolygon, the centroid will be used.

local addrs = osm2pgsql.define_table({
    name = 'addrs',
    ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
    columns = {
        { column = 'name', type = 'text' },
        { column = 'country', type = 'text' },
        { column = 'state', type = 'text' },
        { column = 'postcode', type = 'text' },
        { column = 'city', type = 'text' },
        { column = 'place', type = 'text' },
        { column = 'street', type = 'text' },
        { column = 'housenumber', type = 'text' },
        { column = 'geom', type = 'point', projection = 4326, not_null = true }
    }
})

local function get_address(tags)
    local addr = {}
    local count = 0

    for _, key in ipairs({'housenumber', 'street', 'city', 'postcode', 'country', 'state', 'place'}) do
        local a = tags['addr:' .. key]
        if a then
            addr[key] = a
            count = count + 1
        end
    end

    return count > 1, addr
end

function osm2pgsql.process_node(object)
    local any, addr = get_address(object.tags)
    if any then
        addr.name = object.tags.name
        addr.geom = object:as_point()
        addrs:insert(addr)
    end
end

function osm2pgsql.process_way(object)
    if object.is_closed then
        local any, addr = get_address(object.tags)
        if any then
            addr.name = object.tags.name
            addr.geom = object:as_polygon():centroid()
            addrs:insert(addr)
        end
    end
end

function osm2pgsql.process_relation(object)
    if object.tags.type == 'multipolygon' then
        local any, addr = get_address(object.tags)
        if any then
            addr.name = object.tags.name
            addr.geom = object:as_multipolygon():centroid()
            addrs:insert(addr)
        end
    end
end