|
| 1 | +-- This config example file is released into the Public Domain. |
| 2 | + |
| 3 | +-- This is a very simple Lua config for the Flex output not intended for |
| 4 | +-- real-world use. Look at and understand "simple.lua" first, before looking |
| 5 | +-- at this file. This file will show some options around geometry processing. |
| 6 | +-- After you have understood this file, go on to "data-types.lua". |
| 7 | +-- |
| 8 | +-- This is the new version of the file "geometries.lua". It uses the new |
| 9 | +-- "insert()" call available from version 1.7.0. |
| 10 | + |
| 11 | +local tables = {} |
| 12 | + |
| 13 | +tables.pois = osm2pgsql.define_node_table('pois', { |
| 14 | + { column = 'tags', type = 'jsonb' }, |
| 15 | + -- Create a geometry column for point geometries. The geometry will be |
| 16 | + -- in web mercator, EPSG 3857. |
| 17 | + -- |
| 18 | + -- Usually we want to declare all geometry columns as "NOT NULL". If |
| 19 | + -- osm2pgsql encounters an invalid geometry (for whatever reason) it will |
| 20 | + -- generate a null geometry which will not be written to the database if |
| 21 | + -- "not_null" is set. The result is that broken geometries will just be |
| 22 | + -- silently ignored. |
| 23 | + { column = 'geom', type = 'point', not_null = true }, |
| 24 | +}) |
| 25 | + |
| 26 | +tables.ways = osm2pgsql.define_way_table('ways', { |
| 27 | + { column = 'tags', type = 'jsonb' }, |
| 28 | + -- Create a geometry column for linestring geometries. The geometry will |
| 29 | + -- be in latlong (WGS84), EPSG 4326. |
| 30 | + { column = 'geom', type = 'linestring', projection = 4326, not_null = true }, |
| 31 | +}) |
| 32 | + |
| 33 | +tables.polygons = osm2pgsql.define_area_table('polygons', { |
| 34 | + { column = 'tags', type = 'jsonb' }, |
| 35 | + -- If we don't set "not_null = true", we'll get NULL columns for invalid |
| 36 | + -- geometries. This can be useful if we want to detect those cases or |
| 37 | + -- if we have multiple geometry columns and some of them can be valid |
| 38 | + -- and others not. |
| 39 | + { column = 'geom', type = 'geometry', projection = 4326 }, |
| 40 | + { column = 'area', type = 'real' }, |
| 41 | +}) |
| 42 | + |
| 43 | +tables.boundaries = osm2pgsql.define_relation_table('boundaries', { |
| 44 | + { column = 'type', type = 'text' }, |
| 45 | + { column = 'tags', type = 'jsonb' }, |
| 46 | + -- Boundaries will be stitched together from relation members into long |
| 47 | + -- linestrings. This is a multilinestring column because sometimes the |
| 48 | + -- boundaries are not contiguous. |
| 49 | + { column = 'geom', type = 'multilinestring', not_null = true }, |
| 50 | +}) |
| 51 | + |
| 52 | +-- Tables don't have to have a geometry column. This one will only collect |
| 53 | +-- all the names of pubs but without any location information. |
| 54 | +tables.pubs = osm2pgsql.define_node_table('pubs', { |
| 55 | + { column = 'name', type = 'text' } |
| 56 | +}) |
| 57 | + |
| 58 | +-- Helper function to remove some of the tags we usually are not interested in. |
| 59 | +-- Returns true if there are no tags left. |
| 60 | +function clean_tags(tags) |
| 61 | + tags.odbl = nil |
| 62 | + tags.created_by = nil |
| 63 | + tags.source = nil |
| 64 | + tags['source:ref'] = nil |
| 65 | + |
| 66 | + return next(tags) == nil |
| 67 | +end |
| 68 | + |
| 69 | +-- Helper function that looks at the tags and decides if this is possibly |
| 70 | +-- an area. |
| 71 | +function has_area_tags(tags) |
| 72 | + if tags.area == 'yes' then |
| 73 | + return true |
| 74 | + end |
| 75 | + if tags.area == 'no' then |
| 76 | + return false |
| 77 | + end |
| 78 | + |
| 79 | + return tags.aeroway |
| 80 | + or tags.amenity |
| 81 | + or tags.building |
| 82 | + or tags.harbour |
| 83 | + or tags.historic |
| 84 | + or tags.landuse |
| 85 | + or tags.leisure |
| 86 | + or tags.man_made |
| 87 | + or tags.military |
| 88 | + or tags.natural |
| 89 | + or tags.office |
| 90 | + or tags.place |
| 91 | + or tags.power |
| 92 | + or tags.public_transport |
| 93 | + or tags.shop |
| 94 | + or tags.sport |
| 95 | + or tags.tourism |
| 96 | + or tags.water |
| 97 | + or tags.waterway |
| 98 | + or tags.wetland |
| 99 | + or tags['abandoned:aeroway'] |
| 100 | + or tags['abandoned:amenity'] |
| 101 | + or tags['abandoned:building'] |
| 102 | + or tags['abandoned:landuse'] |
| 103 | + or tags['abandoned:power'] |
| 104 | + or tags['area:highway'] |
| 105 | +end |
| 106 | + |
| 107 | +function osm2pgsql.process_node(object) |
| 108 | + if clean_tags(object.tags) then |
| 109 | + return |
| 110 | + end |
| 111 | + |
| 112 | + local geom = object:as_point() |
| 113 | + |
| 114 | + tables.pois:insert({ |
| 115 | + tags = object.tags, |
| 116 | + geom = geom -- the point will be automatically be projected to 3857 |
| 117 | + }) |
| 118 | + |
| 119 | + if object.tags.amenity == 'pub' then |
| 120 | + tables.pubs:insert({ |
| 121 | + name = object.tags.name |
| 122 | + }) |
| 123 | + end |
| 124 | +end |
| 125 | + |
| 126 | +function osm2pgsql.process_way(object) |
| 127 | + if clean_tags(object.tags) then |
| 128 | +-- return |
| 129 | + end |
| 130 | + |
| 131 | + -- A closed way that also has the right tags for an area is a polygon. |
| 132 | + if object.is_closed and has_area_tags(object.tags) then |
| 133 | + -- Creating the polygon geometry takes time, so we do it once here |
| 134 | + -- and later store it in the table and use it to calculate the area. |
| 135 | + local geom = object:as_polygon() |
| 136 | + tables.polygons:insert({ |
| 137 | + tags = object.tags, |
| 138 | + geom = geom, |
| 139 | + -- Calculate the area in Mercator projection and store in the |
| 140 | + -- area column |
| 141 | + area = geom:transform(3857):area() |
| 142 | + }) |
| 143 | + else |
| 144 | + -- We want to split long lines into smaller segments. We can use |
| 145 | + -- the "segmentize" function for that. The parameter specifies the |
| 146 | + -- maximum length the pieces should have. This length is in map units, |
| 147 | + -- so it depends on the projection used. |
| 148 | + -- "Traditional" osm2pgsql sets this to 1 for 4326 geometries and |
| 149 | + -- 100000 for 3857 (web mercator) geometries. |
| 150 | + -- |
| 151 | + -- Because the result of the segmentation is a multigeometry, we'll |
| 152 | + -- have to iterate over all the member geometries to be able to insert |
| 153 | + -- the data into a the 'geom' column of the 'ways' table which is of |
| 154 | + -- type 'linestring'. (We could have used a multilinestring geometry |
| 155 | + -- in our table instead.) |
| 156 | + local multi_geom = object:as_multilinestring():segmentize(1) |
| 157 | + for g in multi_geom:geometries() do |
| 158 | + tables.ways:insert({ |
| 159 | + tags = object.tags, |
| 160 | + geom = g |
| 161 | + }) |
| 162 | + end |
| 163 | + end |
| 164 | +end |
| 165 | + |
| 166 | +function osm2pgsql.process_relation(object) |
| 167 | + if clean_tags(object.tags) then |
| 168 | + return |
| 169 | + end |
| 170 | + |
| 171 | + local relation_type = object:grab_tag('type') |
| 172 | + |
| 173 | + -- Store boundary relations as multilinestrings |
| 174 | + if relation_type == 'boundary' then |
| 175 | + tables.boundaries:insert({ |
| 176 | + type = object:grab_tag('boundary'), |
| 177 | + tags = object.tags, |
| 178 | + -- For relations there is no clear definition what their geometry |
| 179 | + -- is, so you have to decide on the geometry transformation you |
| 180 | + -- want. In this case we want the boundary as multilinestring |
| 181 | + -- and we want lines merged as much as possible. |
| 182 | + geom = object:as_multilinestring():line_merge() |
| 183 | + }) |
| 184 | + return |
| 185 | + end |
| 186 | + |
| 187 | + -- Store multipolygon relations as polygons |
| 188 | + if relation_type == 'multipolygon' then |
| 189 | + local geom = object:as_multipolygon() |
| 190 | + tables.polygons:insert({ |
| 191 | + tags = object.tags, |
| 192 | + -- For relations there is no clear definition what their geometry |
| 193 | + -- is, so you have to decide on the geometry transformation. |
| 194 | + -- In this case we know from the type tag its a (multi)polygon. |
| 195 | + geom = geom, |
| 196 | + -- Calculate the area in Mercator projection and store in the |
| 197 | + -- area column |
| 198 | + area = geom:transform(3857):area() |
| 199 | + }) |
| 200 | + end |
| 201 | +end |
| 202 | + |
0 commit comments