postgresqlpostgisopenstreetmaposm2pgsql

Import only point features with osm2pgsql using style file


I am very new osm2pqsgl. I have downloaded a osm.pbf file for all of Europe and I want to add this data to my Postgres database. However, I am only interested in points, no linestrings nor polygon, and within the points I am only interested in these tags and its information (like denomination, or name)

I have edited the style file from osm2pgsql down to this

node,way   historic     text         polygon
node,way   natural      text         polygon 
node,way   religion     text         linear
node,way   tourism      text         polygon
  1. How to import only Point features from a osm.pbf file with osm2pgsql?
  2. How to import only Point features with a specific tag, like tourism from a osm.pbf file with osm2pgsql?

Solution

  • I ended up using the flex output option from osm2pgsql and created a .lua file. Here with the tag religion.

    local tables = {}
        
    -- this creates the table for point and its columns
    tables.religion = osm2pgsql.define_node_table('religion_point', {
        { column = 'osm_type',     type = 'text', not_null = true },
        { column = 'name',     type = 'text', not_null = true},
        { column = 'geom',     type = 'point' },
    }, { schema = 'public' })
    
    -- tags we don't need
    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_node(object)
        -- We are only interested in religion details
        -- replace here with the tag you want to import i.e. natural, historic, ...
        if not object.tags.religion then
            return
        end
    
        clean_tags(object.tags)
    
        -- Using grab_tag() removes from remaining key/value saved to Pg
        local osm_type = object:grab_tag('religion')
        local name = object:grab_tag('name')
        tables.religion:add_row({
            osm_type = osm_type,
            tags = json.encode(object.tags),
            name = name,
            geom = { create = 'point' }
        })
    end
    

    And then run it with

    $ osm2pgsql -c -d <DATABASENAME> -U postgres -H localhost -O flex -S ./<NAME>.lua ./<FILENAME>.osm.pbf
    

    Sources for the Script