local output = osm2pgsql.define_table({ name = 'osm_nature_features', schema = 'nature_next', ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type', create_index = 'always' }, columns = { { column = 'name', type = 'text' }, { column = 'tags', type = 'jsonb', not_null = true }, { column = 'geom', type = 'geometry', projection = 4326, not_null = true }, { column = 'area_m2', create_only = true, sql_type = [[double precision GENERATED ALWAYS AS ( CASE WHEN GeometryType(geom) IN ('POLYGON', 'MULTIPOLYGON') THEN ST_Area(geom::geography) ELSE NULL END ) STORED]] } } }) local named_values = { amenity = { grave_yard = true }, boundary = { forest = true, national_park = true, protected_area = true }, landuse = { cemetery = true, forest = true, recreation_ground = true }, leisure = { garden = true, nature_reserve = true, park = true }, mountain_pass = { yes = true }, place = { archipelago = true, island = true, islet = true }, tourism = { viewpoint = true }, water = { lake = true, lagoon = true, oxbow = true, pond = true, rapids = true, reservoir = true, river = true, stream = true, stream_pool = true }, waterway = { rapids = true, river = true, stream = true, tidal_channel = true, waterfall = true } } local unnamed_natural = { arch = true, arete = true, atoll = true, bay = true, beach = true, blowhole = true, cape = true, cave = true, cave_entrance = true, cliff = true, coastline = true, crevasse = true, dune = true, fumarole = true, geyser = true, gorge = true, hill = true, hot_spring = true, isthmus = true, peak = true, peninsula = true, reef = true, ridge = true, rock = true, saddle = true, shoal = true, sinkhole = true, spring = true, stone = true, strait = true, valley = true, volcano = true, waterfall = true } local unnamed_geological = { fault = true, giants_kettle = true, glacial_erratic = true, geotope = true, inselberg = true, landslide = true, limestone_pavement = true, meteor_crater = true, moraine = true, nunatak = true, outcrop = true, palaeontological_site = true, rock_glacier = true, volcanic_caldera_rim = true, volcanic_lava_field = true, volcanic_lava_flow = true, volcanic_lava_tube = true, volcanic_vent = true } local function has_name(tags) return tags.name ~= nil and tags.name ~= '' end local function matches_values(tags, catalogue) for key, values in pairs(catalogue) do if tags[key] ~= nil and values[tags[key]] then return true end end return false end local function is_interesting(tags) if has_name(tags) then if tags.natural ~= nil or tags.geological ~= nil then return true end return matches_values(tags, named_values) end if tags.natural ~= nil and unnamed_natural[tags.natural] then return true end if tags.geological ~= nil and unnamed_geological[tags.geological] then return true end return matches_values(tags, { mountain_pass = named_values.mountain_pass, tourism = named_values.tourism, water = { rapids = true, stream_pool = true }, waterway = { rapids = true, waterfall = true } }) end local function insert_feature(object, geometry) if geometry == nil or geometry:is_null() then return end output:insert({ name = object.tags.name, tags = object.tags, geom = geometry }) end function osm2pgsql.process_node(object) if is_interesting(object.tags) then insert_feature(object, object:as_point()) end end function osm2pgsql.process_way(object) if not is_interesting(object.tags) then return end if object.is_closed then insert_feature(object, object:as_polygon()) else insert_feature(object, object:as_linestring()) end end function osm2pgsql.process_relation(object) if not is_interesting(object.tags) then return end if object.tags.type == 'multipolygon' or object.tags.type == 'boundary' then insert_feature(object, object:as_multipolygon()) elseif object.tags.type == 'route' then insert_feature(object, object:as_multilinestring()) else insert_feature(object, object:as_geometrycollection()) end end