File: indexes.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 (158 lines) | stat: -rw-r--r-- 5,032 bytes parent folder | download | duplicates (4)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- This config example file is released into the Public Domain.

-- This file shows some options around index creation.

local tables = {}

-- When "indexes" is explicitly set to an empty Lua table, there will be no
-- index on this table. (The index for the id column is still built if
-- osm2pgsql needs that for updates.)
tables.pois = osm2pgsql.define_table({
    name = 'pois',
    ids = { type = 'node', id_column = 'node_id' },
    columns = {
        { column = 'tags', type = 'jsonb' },
        { column = 'geom', type = 'point', not_null = true },
    },
    indexes = {}
})

-- The "indexes" field is not set at all, you get the default, a GIST index on
-- the only (or first) geometry column.
tables.ways = osm2pgsql.define_way_table('ways', {
    { column = 'tags', type = 'jsonb' },
    { column = 'geom', type = 'linestring', not_null = true },
})

-- Setting "indexes" explicitly: Two indexes area created, one on the polygon
-- geometry ("geom"), one on the center point geometry ("center"), both use
-- the GIST method.
tables.polygons = osm2pgsql.define_area_table('polygons', {
    { column = 'tags', type = 'jsonb' },
    { column = 'geom', type = 'geometry', not_null = true },
    { column = 'center', type = 'point', not_null = true },
}, { indexes = {
    { column = 'geom', method = 'gist' },
    { column = 'center', method = 'gist' }
}})

-- You can put an index on any column, not just geometry columns, and use any
-- index method available in your PostgreSQL version. To get a list of methods
-- use: "SELECT amname FROM pg_catalog.pg_am WHERE amtype = 'i';"
tables.pubs = osm2pgsql.define_node_table('pubs', {
    { column = 'name', type = 'text' },
    { column = 'geom', type = 'geometry', not_null = true },
}, { indexes = {
    { column = 'geom', method = 'gist' },
    { column = 'name', method = 'btree' }
}})

-- You can also create indexes using multiple columns by specifying an array
-- as the "column". And you can add a where condition to the index. Note that
-- the content of the where condition is not checked, but given "as is" to
-- the database. You have to make sure it makes sense.
tables.roads = osm2pgsql.define_way_table('roads', {
    { column = 'name', type = 'text' },
    { column = 'type', type = 'text' },
    { column = 'ref', type = 'text' },
    { column = 'geom', type = 'linestring', not_null = true },
}, { indexes = {
    { column = { 'name', 'ref' }, method = 'btree' },
    { column = { 'geom' }, method = 'gist', where = "type='primary'" }
}})

-- Instead of on a column (or columns) you can define an index on an expression.
-- Indexes can be named (the default name is the one that PostgreSQL creates).
tables.postboxes = osm2pgsql.define_node_table('postboxes', {
    { column = 'operator', type = 'text' },
    { column = 'geom', type = 'point', not_null = true },
}, { indexes = {
    { expression = 'lower(operator)',
      method = 'btree',
      name = 'postbox_operator_idx' },
}})

-- Helper function that looks at the tags and decides if this is possibly
-- an area.
local function has_area_tags(tags)
    if tags.area == 'yes' then
        return true
    end
    if tags.area == 'no' then
        return false
    end

    return tags.aeroway
        or tags.amenity
        or tags.building
        or tags.harbour
        or tags.historic
        or tags.landuse
        or tags.leisure
        or tags.man_made
        or tags.military
        or tags.natural
        or tags.office
        or tags.place
        or tags.power
        or tags.public_transport
        or tags.shop
        or tags.sport
        or tags.tourism
        or tags.water
        or tags.waterway
        or tags.wetland
        or tags['abandoned:aeroway']
        or tags['abandoned:amenity']
        or tags['abandoned:building']
        or tags['abandoned:landuse']
        or tags['abandoned:power']
        or tags['area:highway']
end

function osm2pgsql.process_node(object)
    local geom = object:as_point()

    tables.pois:insert({
        tags = object.tags,
        geom = geom
    })

    if object.tags.amenity == 'pub' then
        tables.pubs:insert({
            name = object.tags.name,
            geom = geom
        })
    elseif object.tags.amenity == 'post_box' then
        tables.postboxes:insert({
            operator = object.tags.operator,
            geom = geom
        })
    end
end

function osm2pgsql.process_way(object)
    if object.is_closed and has_area_tags(object.tags) then
        local geom = object:as_polygon()
        tables.polygons:insert({
            tags = object.tags,
            geom = geom,
            center = geom:centroid()
        })
    else
        tables.ways:insert({
            tags = object.tags,
            geom = object:as_linestring()
        })
    end

    if object.tags.highway then
        tables.roads:insert({
            type = object.tags.highway,
            name = object.tags.name,
            ref = object.tags.ref,
            geom = object:as_linestring()
        })
    end
end