|
| 1 | +-- This config example file is released into the Public Domain. |
| 2 | + |
| 3 | +-- This is Lua config showing the handling of custom geometries. |
| 4 | +-- |
| 5 | +-- This is "work in progress", changes are possible. |
| 6 | +-- |
| 7 | +-- NOTE that all the geometry types have a leading "c" for "custom", this is |
| 8 | +-- different than the geometry types before. |
| 9 | + |
| 10 | +local tables = {} |
| 11 | + |
| 12 | +-- This table will get all nodes and areas with an "addr:street" tag, for |
| 13 | +-- areas we'll calculate the centroid. |
| 14 | +tables.addr = osm2pgsql.define_table({ |
| 15 | + name = 'addr', |
| 16 | + ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' }, |
| 17 | + columns = { |
| 18 | + { column = 'tags', type = 'jsonb' }, |
| 19 | + { column = 'geom', type = 'cpoint', projection = 4326 }, |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +tables.ways = osm2pgsql.define_way_table('ways', { |
| 24 | + { column = 'tags', type = 'jsonb' }, |
| 25 | + { column = 'geom', type = 'clinestring', projection = 4326 }, |
| 26 | + { column = 'poly', type = 'cpolygon', projection = 4326 }, |
| 27 | + { column = 'geom3857', type = 'clinestring', projection = 3857 }, |
| 28 | +}) |
| 29 | + |
| 30 | +tables.major_roads = osm2pgsql.define_way_table('major_roads', { |
| 31 | + { column = 'tags', type = 'jsonb' }, |
| 32 | + { column = 'geom', type = 'clinestring' }, |
| 33 | + { column = 'sgeom', type = 'clinestring' } |
| 34 | +}) |
| 35 | + |
| 36 | +tables.polygons = osm2pgsql.define_area_table('polygons', { |
| 37 | + { column = 'tags', type = 'jsonb' }, |
| 38 | + { column = 'geom', type = 'cgeometry' }, |
| 39 | + { column = 'area', type = 'real' } |
| 40 | +}) |
| 41 | + |
| 42 | +-- Helper function to remove some of the tags we usually are not interested in. |
| 43 | +-- Returns true if there are no tags left. |
| 44 | +function clean_tags(tags) |
| 45 | + tags.odbl = nil |
| 46 | + tags.created_by = nil |
| 47 | + tags.source = nil |
| 48 | + tags['source:ref'] = nil |
| 49 | + |
| 50 | + return next(tags) == nil |
| 51 | +end |
| 52 | + |
| 53 | +-- Helper function that looks at the tags and decides if this is possibly |
| 54 | +-- an area. |
| 55 | +function has_area_tags(tags) |
| 56 | + if tags.area == 'yes' then |
| 57 | + return true |
| 58 | + end |
| 59 | + if tags.area == 'no' then |
| 60 | + return false |
| 61 | + end |
| 62 | + |
| 63 | + return tags.aeroway |
| 64 | + or tags.amenity |
| 65 | + or tags.building |
| 66 | + or tags.harbour |
| 67 | + or tags.historic |
| 68 | + or tags.landuse |
| 69 | + or tags.leisure |
| 70 | + or tags.man_made |
| 71 | + or tags.military |
| 72 | + or tags.natural |
| 73 | + or tags.office |
| 74 | + or tags.place |
| 75 | + or tags.power |
| 76 | + or tags.public_transport |
| 77 | + or tags.shop |
| 78 | + or tags.sport |
| 79 | + or tags.tourism |
| 80 | + or tags.water |
| 81 | + or tags.waterway |
| 82 | + or tags.wetland |
| 83 | + or tags['abandoned:aeroway'] |
| 84 | + or tags['abandoned:amenity'] |
| 85 | + or tags['abandoned:building'] |
| 86 | + or tags['abandoned:landuse'] |
| 87 | + or tags['abandoned:power'] |
| 88 | + or tags['area:highway'] |
| 89 | +end |
| 90 | + |
| 91 | +function osm2pgsql.process_node(object) |
| 92 | + if clean_tags(object.tags) then |
| 93 | + return |
| 94 | + end |
| 95 | + |
| 96 | + if object.tags['addr:street'] then |
| 97 | + tables.addr:add_row({ |
| 98 | + tags = object.tags, |
| 99 | + geom = object:as_point() |
| 100 | + }) |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +function osm2pgsql.process_way(object) |
| 105 | + if clean_tags(object.tags) then |
| 106 | + return |
| 107 | + end |
| 108 | + |
| 109 | + if object.is_closed and object.tags['addr:street'] then |
| 110 | + tables.addr:add_row({ |
| 111 | + tags = object.tags, |
| 112 | + geom = object:as_polygon():centroid() |
| 113 | + }) |
| 114 | + end |
| 115 | + |
| 116 | + if object.tags.highway == 'motorway' or |
| 117 | + object.tags.highway == 'trunk' or |
| 118 | + object.tags.highway == 'primary' then |
| 119 | + tables.major_roads:add_row({ |
| 120 | + tags = object.tags, |
| 121 | + geom = object:as_linestring(), |
| 122 | + sgeom = object:as_linestring():simplify(100), |
| 123 | + }) |
| 124 | + end |
| 125 | + |
| 126 | + -- A closed way that also has the right tags for an area is a polygon. |
| 127 | + if object.is_closed and has_area_tags(object.tags) then |
| 128 | + local a = object:as_polygon():area() |
| 129 | + if a < 0.0000001 then |
| 130 | + tables.polygons:add_row({ |
| 131 | + tags = object.tags, |
| 132 | + geom = object:as_polygon(), |
| 133 | + area = object:as_polygon():area() |
| 134 | + }) |
| 135 | + end |
| 136 | + else |
| 137 | + tables.ways:add_row({ |
| 138 | + tags = object.tags, |
| 139 | + geom = object:as_linestring(), |
| 140 | + poly = object:as_polygon(), |
| 141 | + geom3857 = object:as_linestring() |
| 142 | + }) |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | +function osm2pgsql.process_relation(object) |
| 147 | + if clean_tags(object.tags) then |
| 148 | + return |
| 149 | + end |
| 150 | + |
| 151 | + local type = object:grab_tag('type') |
| 152 | + |
| 153 | + if type == 'multipolygon' then |
| 154 | + local g = object:as_multipolygon() |
| 155 | + local a = g:area() |
| 156 | + if a < 0.0000001 then |
| 157 | + tables.polygons:add_row({ |
| 158 | + tags = object.tags, |
| 159 | + geom = g, |
| 160 | + area = g:area() |
| 161 | + }) |
| 162 | + end |
| 163 | + end |
| 164 | +end |
| 165 | + |
0 commit comments