1

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?
four-eyes
  • 10,740
  • 29
  • 111
  • 220

2 Answers2

2

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

four-eyes
  • 10,740
  • 29
  • 111
  • 220
0

Not sure if osm2pgsql can do it on the fly, however you can use osmosis for filtering the input files. Another option would be osmium.

On the other way, I'm not sure if discarding polygons a'priori is a proper assumption. You can find a lot of tourism, religion etc. tags on buildings (or other polygons). I would suggest to calculate centroids for them and union them together.

Grzegorz Sapijaszko
  • 1,913
  • 1
  • 5
  • 12
  • Thanks for the late reply. How would I import only `Point` and `Polygon` features with the tag `tourism` and `religion`. How would I modify the `style file` – four-eyes Feb 09 '22 at 08:07
  • Modifying style will not help. I would suggest to filter only points and polygons with requested tags by `osmosis`. There is an [example usage](https://wiki.openstreetmap.org/wiki/Osmosis#Example_usage) and [detailed one](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.48#--tag-filter_.28--tf.29). – Grzegorz Sapijaszko Feb 09 '22 at 18:08
  • I'll look into it, thanks! – four-eyes Feb 09 '22 at 22:24