|
| 1 | +-- This is a very simple Lua config for the Flex output |
| 2 | +-- which only stores the geometries (not even the tags) |
| 3 | +-- for use with Underpass-API to mimics Overpass |
| 4 | + |
| 5 | +local tables = {} |
| 6 | + |
| 7 | +tables.points = osm2pgsql.define_table({ |
| 8 | + name = 'points', |
| 9 | + ids = { type = 'node', id_column = 'id' }, |
| 10 | + columns = { |
| 11 | + -- { column = 'tags', type = 'jsonb' }, |
| 12 | + { column = 'geom', type = 'point', projection = 4326, not_null = true } |
| 13 | +}}) |
| 14 | + |
| 15 | +tables.lines = osm2pgsql.define_table({ |
| 16 | + name = 'lines', |
| 17 | + ids = { type = 'way', id_column = 'id' }, |
| 18 | + columns = { |
| 19 | + -- { column = 'tags', type = 'jsonb' }, |
| 20 | + { column = 'geom', type = 'multilinestring', projection = 4326, not_null = true } |
| 21 | +}}) |
| 22 | + |
| 23 | +tables.areas = osm2pgsql.define_table({ |
| 24 | + name = 'areas', |
| 25 | + ids = { type = 'area', id_column = 'id' }, |
| 26 | + columns = { |
| 27 | + -- { column = 'tags', type = 'jsonb' }, |
| 28 | + { column = 'geom', type = 'geometry', projection = 4326, not_null = true }, |
| 29 | + -- In this column we'll put the true area calculated on the spheroid |
| 30 | + { column = 'area', type = 'real' } |
| 31 | +}}) |
| 32 | + |
| 33 | + |
| 34 | +-- Helper function to remove some of the tags we usually are not interested in. |
| 35 | +-- Returns true if there are no tags left. |
| 36 | + |
| 37 | +-- modifié : retourne vrai si aucun tag |
| 38 | +local function clean_tags(tags) |
| 39 | + -- tags.odbl = nil |
| 40 | + -- tags.created_by = nil |
| 41 | + -- tags.source = nil |
| 42 | + -- tags['source:ref'] = nil |
| 43 | + |
| 44 | + return next(tags) == nil |
| 45 | +end |
| 46 | + |
| 47 | +-- Helper function that looks at the tags and decides if this is possibly |
| 48 | +-- an area. |
| 49 | +local function has_area_tags(tags) |
| 50 | + if tags.area == 'yes' or tags.area == 'true' or tags.area == '1' then |
| 51 | + return true |
| 52 | + end |
| 53 | + if tags.area == 'no' or tags.area == 'false' or tags.area == '0' then |
| 54 | + return false |
| 55 | + end |
| 56 | + |
| 57 | + return tags.aeroway |
| 58 | + or tags.amenity |
| 59 | + or tags.building |
| 60 | + or tags.harbour |
| 61 | + or tags.historic |
| 62 | + or tags.landuse |
| 63 | + or tags.leisure |
| 64 | + or tags.man_made |
| 65 | + or tags.military |
| 66 | + or tags.natural |
| 67 | + or tags.office |
| 68 | + or tags.place |
| 69 | + or tags.power |
| 70 | + or tags.public_transport |
| 71 | + or tags.shop |
| 72 | + or tags.sport |
| 73 | + or tags.tourism |
| 74 | + or tags.water |
| 75 | + or tags.waterway |
| 76 | + or tags.wetland |
| 77 | + or tags['abandoned:aeroway'] |
| 78 | + or tags['abandoned:amenity'] |
| 79 | + or tags['abandoned:building'] |
| 80 | + or tags['abandoned:landuse'] |
| 81 | + or tags['abandoned:power'] |
| 82 | + or tags['area:highway'] |
| 83 | +end |
| 84 | + |
| 85 | +function osm2pgsql.process_node(object) |
| 86 | + if clean_tags(object.tags) then |
| 87 | + return |
| 88 | + end |
| 89 | + |
| 90 | + local geom = object:as_point() |
| 91 | + |
| 92 | + tables.points:insert({ |
| 93 | + -- tags = object.tags, |
| 94 | + geom = geom -- the point will be automatically be projected to 3857 |
| 95 | + }) |
| 96 | + |
| 97 | +end |
| 98 | + |
| 99 | +function osm2pgsql.process_way(object) |
| 100 | + if clean_tags(object.tags) then |
| 101 | + return |
| 102 | + end |
| 103 | + |
| 104 | + -- A closed way that also has the right tags for an area is a polygon. |
| 105 | + if object.is_closed and has_area_tags(object.tags) then |
| 106 | + -- Creating the polygon geometry takes time, so we do it once here |
| 107 | + -- and later store it in the table and use it to calculate the area. |
| 108 | + local geom = object:as_polygon() |
| 109 | + tables.areas:insert({ |
| 110 | + geom = geom, |
| 111 | + area = geom:spherical_area() -- calculate "real" area in spheroid |
| 112 | + }) |
| 113 | + else |
| 114 | + -- modif : on enregistre la géométrie directement en multilinestring |
| 115 | + -- en mergeant les lignes le plus possible |
| 116 | + tables.lines:insert({ |
| 117 | + geom = object:as_multilinestring():line_merge() |
| 118 | + }) |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +function osm2pgsql.process_relation(object) |
| 123 | + if clean_tags(object.tags) then |
| 124 | + return |
| 125 | + end |
| 126 | + |
| 127 | + local relation_type = object:grab_tag('type') |
| 128 | + |
| 129 | + -- Store route relations as multilinestrings |
| 130 | + if relation_type == 'route' or relation_type == 'associatedStreet' or relation_type == 'public_transport' or relation_type == 'waterway' then |
| 131 | + tables.lines:insert({ |
| 132 | + geom = object:as_multilinestring():line_merge() |
| 133 | + }) |
| 134 | + return |
| 135 | + end |
| 136 | + |
| 137 | + -- Store multipolygon and boundary relations as polygons |
| 138 | + if relation_type == 'multipolygon' or relation_type == 'boundary' then |
| 139 | + local geom = object:as_multipolygon() |
| 140 | + tables.areas:insert({ |
| 141 | + geom = geom, |
| 142 | + area = geom:spherical_area() |
| 143 | + }) |
| 144 | + end |
| 145 | +end |
| 146 | + |
0 commit comments