Skip to content

Flex example configs #2328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions flex-config/attributes.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-- This config example file is released into the Public Domain.

-- This config shows how to access the attributes of OSM objects: the version,
-- changeset id, timestamp, user id and user name. For this to work the
-- command line option --extra-attributes/-x must be set, otherwise those
-- fields will be empty. Also note that some OSM files do not contain all
-- of those attributes, so check your input data if you get empty fields.
-- changeset id, timestamp, user id and user name. Note that some OSM files do
-- not contain all of those attributes, so check your input data if you get
-- empty fields.

-- Set this to the projection you want to use
local srid = 4326
Expand Down
23 changes: 0 additions & 23 deletions flex-config/bbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ tables.boundaries = osm2pgsql.define_relation_table('boundaries', {
{ column = 'geom', type = 'multilinestring' },
})

-- Helper function to remove some of the tags we usually are not interested in.
-- Returns true if there are no tags left.
local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil

return next(tags) == nil
end

-- Helper function that looks at the tags and decides if this is possibly
-- an area.
local function has_area_tags(tags)
Expand Down Expand Up @@ -91,10 +80,6 @@ local function format_bbox(object)
end

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

tables.pois:insert({
tags = object.tags,
bbox = format_bbox(object),
Expand All @@ -103,10 +88,6 @@ function osm2pgsql.process_node(object)
end

function osm2pgsql.process_way(object)
if clean_tags(object.tags) then
return
end

-- A closed way that also has the right tags for an area is a polygon.
if object.is_closed and has_area_tags(object.tags) then
tables.polygons:insert({
Expand All @@ -124,10 +105,6 @@ function osm2pgsql.process_way(object)
end

function osm2pgsql.process_relation(object)
if clean_tags(object.tags) then
return
end

local relation_type = object:grab_tag('type')

-- Store boundary relations as multilinestrings
Expand Down
20 changes: 11 additions & 9 deletions flex-config/data-types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@
-- at this file. This file demonstrates some column data type options.

local highways = osm2pgsql.define_way_table('highways', {
{ column = 'name', type = 'text' },
{ column = 'name', type = 'text' },
-- We always need a highway type, so we can declare the column as NOT NULL
{ column = 'type', type = 'text', not_null = true },
{ column = 'type', type = 'text', not_null = true },

-- Add a SERIAL column and tell osm2pgsql not to fill it (PostgreSQL will
-- do that for us)
{ column = 'id', sql_type = 'serial', create_only = true },
{ column = 'id', sql_type = 'serial', create_only = true },

-- type "direction" is special, see below
{ column = 'oneway', type = 'direction' },
{ column = 'oneway', type = 'direction' },
{ column = 'maxspeed', type = 'int' },

-- type "bool" is special, see below
{ column = 'lit', type = 'bool' },
{ column = 'tags', type = 'jsonb' }, -- also available: 'json', 'hstore'
{ column = 'lit', type = 'bool' },
{ column = 'tags', type = 'jsonb' }, -- also available: 'json', 'hstore'

-- osm2pgsql doesn't know about PostgreSQL arrays, so we define the SQL
-- type of this column and then have to convert our array data into a
-- valid text representation for that type, see below.
{ column = 'nodes', sql_type = 'int8[]' },
{ column = 'geom', type = 'linestring' },
{ column = 'nodes', sql_type = 'int8[]' },
{ column = 'geom', type = 'linestring' },
})

-- Helper function to remove some of the tags we usually are not interested in.
-- Returns true if there are no tags left.
-- Something like this can be useful if you are writing all tags to the
-- database in a JSON(B) column and don't want that cluttered with lots of tags
-- nobody cares about. Returns true if there are no tags left.
local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
Expand Down
4 changes: 3 additions & 1 deletion flex-config/expire.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ tables.pois = osm2pgsql.define_node_table('pois', {
-- Zero, one or more expire outputs are referenced in an `expire` field in
-- the definition of any geometry column using the Web Mercator (3857)
-- projection.
{ column = 'geom', type = 'point', not_null = true, expire = { { output = expire_outputs.pois } } },
{ column = 'geom', type = 'point', not_null = true, expire = {
{ output = expire_outputs.pois }
}},
})

tables.lines = osm2pgsql.define_way_table('lines', {
Expand Down
23 changes: 0 additions & 23 deletions flex-config/geometries.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ tables.pubs = osm2pgsql.define_node_table('pubs', {
{ column = 'name', type = 'text' }
})

-- Helper function to remove some of the tags we usually are not interested in.
-- Returns true if there are no tags left.
local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil

return next(tags) == nil
end

-- Helper function that looks at the tags and decides if this is possibly
-- an area.
local function has_area_tags(tags)
Expand Down Expand Up @@ -105,10 +94,6 @@ local function has_area_tags(tags)
end

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

local geom = object:as_point()

tables.pois:insert({
Expand All @@ -124,10 +109,6 @@ function osm2pgsql.process_node(object)
end

function osm2pgsql.process_way(object)
if clean_tags(object.tags) then
return
end

-- A closed way that also has the right tags for an area is a polygon.
if object.is_closed and has_area_tags(object.tags) then
-- Creating the polygon geometry takes time, so we do it once here
Expand Down Expand Up @@ -166,10 +147,6 @@ function osm2pgsql.process_way(object)
end

function osm2pgsql.process_relation(object)
if clean_tags(object.tags) then
return
end

local relation_type = object:grab_tag('type')

-- Store boundary relations as multilinestrings
Expand Down
31 changes: 10 additions & 21 deletions flex-config/public-transport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@
local tables = {}

tables.stops = osm2pgsql.define_node_table('stops', {
{ column = 'tags', type = 'jsonb' },
{ column = 'tags', type = 'jsonb' },
{ column = 'rel_refs', type = 'text' }, -- for the refs from the relations
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'point', not_null = true },
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'point', not_null = true },
})

tables.lines = osm2pgsql.define_way_table('lines', {
{ column = 'tags', type = 'jsonb' },
{ column = 'tags', type = 'jsonb' },
{ column = 'rel_refs', type = 'text' }, -- for the refs from the relations
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'linestring', not_null = true },
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'linestring', not_null = true },
})

-- Tables don't have to have a geometry column
tables.routes = osm2pgsql.define_relation_table('routes', {
{ column = 'ref', type = 'text' },
{ column = 'ref', type = 'text' },
{ column = 'type', type = 'text' },
{ column = 'from', type = 'text' },
{ column = 'to', type = 'text' },
{ column = 'to', type = 'text' },
{ column = 'tags', type = 'jsonb' },
})

-- Stop areas contain everything belonging to a specific public transport
-- stop. We model them here by adding a center point as geometry plus the
-- radius of a circle that contains everything in that stop.
tables.stop_areas = osm2pgsql.define_relation_table('stop_areas', {
{ column = 'tags', type = 'jsonb' },
{ column = 'tags', type = 'jsonb' },
{ column = 'radius', type = 'real', not_null = true },
{ column = 'geom', type = 'point', not_null = true },
{ column = 'geom', type = 'point', not_null = true },
})

-- This will be used to store information about relations queryable by member
Expand All @@ -61,15 +61,6 @@ tables.stop_areas = osm2pgsql.define_relation_table('stop_areas', {
local n2r = {}
local w2r = {}

local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil

return next(tags) == nil
end

local function unique_array(array)
local result = {}

Expand Down Expand Up @@ -128,8 +119,6 @@ function osm2pgsql.process_way(object)
return
end

clean_tags(object.tags)

-- Data we will store in the 'lines' table always has the tags from
-- the way
local row = {
Expand Down
17 changes: 3 additions & 14 deletions flex-config/route-relations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
local tables = {}

tables.highways = osm2pgsql.define_way_table('highways', {
{ column = 'tags', type = 'jsonb' },
{ column = 'tags', type = 'jsonb' },
{ column = 'rel_refs', type = 'text' }, -- for the refs from the relations
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'linestring', not_null = true },
{ column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
{ column = 'geom', type = 'linestring', not_null = true },
})

-- Tables don't have to have a geometry column
Expand All @@ -31,23 +31,12 @@ tables.routes = osm2pgsql.define_relation_table('routes', {
-- it can be called any number of times and will lead to the same result.
local w2r = {}

local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil

return next(tags) == nil
end

function osm2pgsql.process_way(object)
-- We are only interested in highways
if not object.tags.highway then
return
end

clean_tags(object.tags)

-- Data we will store in the "highways" table always has the tags from
-- the way
local row = {
Expand Down
34 changes: 7 additions & 27 deletions flex-config/simple.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ local tables = {}
-- ids.
tables.pois = osm2pgsql.define_node_table('pois', {
{ column = 'tags', type = 'jsonb' },
{ column = 'geom', type = 'point', not_null = true }, -- will be something like `GEOMETRY(Point, 4326)` in SQL
-- In most cases we'll need a column for the geometry. The default
-- projection is Web Mercator (3857), so this will result in an SQL
-- type `geometry(Point, 3857)`.
{ column = 'geom', type = 'point', not_null = true },
})

-- A special table for restaurants to demonstrate that we can have any tables
-- with any columns we want.
tables.restaurants = osm2pgsql.define_node_table('restaurants', {
{ column = 'name', type = 'text' },
{ column = 'name', type = 'text' },
{ column = 'cuisine', type = 'text' },
-- We declare all geometry columns as "NOT NULL". If osm2pgsql encounters
-- an invalid geometry (for whatever reason) it will generate a null
-- geometry which will not be written to the database if "not_null" is
-- set. The result is that broken geometries will just be silently
-- ignored.
{ column = 'geom', type = 'point', not_null = true },
{ column = 'geom', type = 'point', not_null = true },
})

-- This is a "way table", it can only contain data derived from ways and will
Expand Down Expand Up @@ -65,29 +68,14 @@ for name, dtable in pairs(tables) do
print(" name='" .. dtable:name() .. "'")
end

-- Helper function to remove some of the tags we usually are not interested in.
-- Returns true if there are no tags left.
local function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil

return next(tags) == nil
end

-- Called for every node in the input. The `object` argument contains all the
-- attributes of the node like `id`, `version`, etc. as well as all tags as a
-- Lua table (`object.tags`).
function osm2pgsql.process_node(object)
if clean_tags(object.tags) then
return
end

if object.tags.amenity == 'restaurant' then
-- Add a row to the SQL table. The keys in the parameter table
-- correspond to the table columns, if one is missing the column will
-- be NULL. Id and geometry columns will be filled automatically.
-- be NULL. The id column will be filled automatically.
tables.restaurants:insert({
name = object.tags.name,
cuisine = object.tags.cuisine,
Expand All @@ -107,10 +95,6 @@ end
-- information as with nodes and additionally a boolean `is_closed` flag and
-- the list of node IDs referenced by the way (`object.nodes`).
function osm2pgsql.process_way(object)
if clean_tags(object.tags) then
return
end

-- Very simple check to decide whether a way is a polygon or not, in a
-- real stylesheet we'd have to also look at the tags...
if object.is_closed then
Expand All @@ -131,10 +115,6 @@ end
-- same information as with nodes and additionally an array of members
-- (`object.members`).
function osm2pgsql.process_relation(object)
if clean_tags(object.tags) then
return
end

-- Store multipolygons and boundaries as polygons
if object.tags.type == 'multipolygon' or
object.tags.type == 'boundary' then
Expand Down
1 change: 0 additions & 1 deletion flex-config/unitable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
-- OSM nodes are converted to Points, ways to LineStrings and relations
-- to GeometryCollections. If an object would create an invalid geometry
-- it is still added to the table with a NULL geometry.
-- XXX expire will currently not work on these tables.
local dtable = osm2pgsql.define_table{
name = "data",
-- This will generate a column "osm_id INT8" for the id, and a column
Expand Down
1 change: 0 additions & 1 deletion flex-config/untagged.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ end
osm2pgsql.process_way = do_way
osm2pgsql.process_untagged_way = do_way