imposm3 Documentation

repository·master·Indexed 20 days ago

https://github.com/omniscale/imposm3

imposm3 is a high-performance importer for OpenStreetMap (OSM) data into PostgreSQL/PostGIS, optimized for rendering services like WMS or map tile generators. It supports parallel processing, custom database schemas via JSON or YAML mapping files, and efficient node caching. The tool provides a CLI for full imports, differential updates via the `diff` command, and a background service for automatic updates using the `run` command.

Tokens
9.1K
Snippets
37
Records
49
Agent score
69%

What's inside imposm3

  1. What is Imposm?

    master

    Imposm is an importer for OpenStreetMap (OSM) data. It reads PBF files and imports the data into PostgreSQL/PostGIS databases. It is specifically designed to create databases optimized for rendering, tile services, and map services.

    Key capabilities include:

    • Custom database schemas: Creates separate tables for different feature types to improve styling and rendering performance.
    • Parallel processing: Designed from the ground up to distribute parsing and processing across multiple CPU cores.
    • Data unification: Normalizes values (e.g., converting 1, on, true, and yes to TRUE).
    • Tag filtering: Allows importing only the specific data needed for rendering or use.
    • Efficient node caching: Uses a file-based key-value database to cache nodes, reducing memory usage during the construction of ways and relations.
    • Generalized tables: Can automatically create tables with lower spatial resolutions.
    • Geometry limiting: Can limit imported geometries to polygons from GeoJSON.
    • hstore support: Can store all OSM tags in a PostgreSQL hstore column if specific tags are unknown beforehand.
  2. Define data mapping for OSM imports

    master

    Imposm3 uses a YAML or JSON mapping file to determine which OpenStreetMap (OSM) feature types are imported into which database tables. The mapping file specifies the table structure, the OSM tags required for inclusion, and how those tags are converted into columns.

    Key YAML considerations:

    • Use indentation for nesting; tab characters are not allowed.
    • Quotes are optional for simple strings, but must be used for numbers or boolean values (e.g., yes, no, true, false) when you want them treated as strings (e.g., building: ['no']).
  3. Configure table definitions in mapping

    master

    The tables section is the core of the mapping file. Each table is defined by a unique name and contains several configuration blocks:

    • type: The geometry or entity type. Supported values are point, linestring, polygon, geometry, relation, and relation_member.
    • mapping: Defines the OSM key/value pairs required for an element to be imported. Use __any__ to match all values for a key (e.g., amenity: [__any__]). To match elements regardless of tags, use __any__: [__any__] (requires load_all tags).
    • relation_types: (For relation, relation_member, or polygon types) Restricts imports to specific OSM relation types (e.g., [route, master_route]). For polygon types, this controls which relations are built into multi-polygons. Defaults to [multipolygon, boundary, land_area].
    • columns: A list of columns to create for the table.
    • filters: Logic to include or exclude elements based on tags.
    • mappings: (Optional) Allows an element to be inserted multiple times into the same table if it matches different sub-mappings.
  4. How multipolygon relations are handled

    master

    Imposm automatically handles multipolygon relations for all tables configured with type: polygon. This is the primary way to represent complex geometries and holes in polygons.

    Key behaviors:

    • Automatic Geometry Processing: Imposm uses geometry operations to determine if a member of a multipolygon is a hole or a separate polygon, rather than relying on the OSM role (e.g., inner vs outer).
    • Tag-based Triggering: For a polygon table, Imposm will insert closed ways that have a specific tag (e.g., building) or multipolygon relations that have that same tag.
    • Unsupported Formats: Old-style multipolygon relations (where tags are placed on the outer way instead of the relation itself) are not supported.
    tables:
      buildings:
        type: polygon
        mapping:
          building: [__any__]
  5. Importing complex relations using the relation_member table type

    master

    When relations contain mixed geometry types (e.g., a bus route containing nodes for stops and ways for platforms) or would result in invalid geometries if flattened, use the relation_member table type. This type inserts each member of the relation as a separate row.

    Each row in a relation_member table provides access to:

    • The relation's osm_id.
    • The member's member_id.
    • The member's index (useful for maintaining order, e.g., stop sequences).
    • The member's role and type (0 for nodes, 1 for ways, 2 for relations).
    • The member's geometry.
    • Tags from the relation itself.
    • Tags from the member (if from_member: true is used in the mapping).

    To import tags from the member element, use the from_member: true option in your column configuration.

    route_members:
      type: relation_member
      columns:
      - name: osm_id
        type: id
      - name: member
        type: member_id
      - name: index
        type: member_index
      - name: role
        type: member_role
      - name: type
        type: member_type
      - name: geometry
        type: geometry
      - name: relname
        key: name
        type: string
      - name: name
        key: name
        type: string
        from_member: true
      - key: ref
        name: ref
        type: string
      relation_types: [route]
      mapping:
        route: [bus]
  6. Run Imposm background service for automatic updates

    master

    Imposm includes a background service that automatically downloads and imports the latest OSM changes to keep your database up to date.

    Use the following command to start the update service:

    imposm run
  7. Perform a basic OSM data import

    master

    Use the imposm import subcommand to import OpenStreetMap PBF files into a PostgreSQL/PostGIS database.

    Requirements:

    • A PostgreSQL/PostGIS database.
    • A JSON mapping file (see example-mapping.json in the repo for structure) that defines how OSM data maps to database tables.

    Basic Command Structure:

    imposm import -connection postgis://user:password@host/database \
        -mapping mapping.json -read /path/to/osm.pbf -write

    Key Flags:

    • -connection: The PostGIS connection URI.
    • -mapping: Path to the JSON mapping file.
    • -read: Path to the input .pbf file.
    • -write: Enables writing to the database.
    • -deployproduction: By default, Imposm creates tables in an import schema (e.g., import.osm_roads). Use this flag to move tables to the public schema.
    imposm import -connection postgis://user:password@host/database \
        -mapping mapping.json -read /path/to/osm.pbf -write
  8. Limit imported geometries to a specific area

    master

    You can clip line strings and polygons to a specific boundary using the -limitto option.

    • Provide a GeoJSON file in EPSG:4326 format.
    • Use -limittocachebuffer to define a buffer around the limit geometry. This ensures that elements at the boundaries are captured completely in the cache.
    imposm import -mapping mapping.yml -connection postgis://osm:osm@localhost/osm -read europe.osm.pbf -write -limitto germany.geojson
  9. Filter elements using require and reject

    master

    You can limit which elements are inserted into a table using filters.

    • require: Only import elements that have these specific tags.
    • reject: Do not import elements that have these specific tags.
    • require_regexp: Only import elements where the tag value matches a regular expression.
    • reject_regexp: Do not import elements where the tag value matches a regular expression.

    Important:

    • Regular expressions in require_regexp and reject_regexp must be enclosed in single quotes (') to prevent YAML from interpreting backslashes as escape sequences.
    • You can only filter tags that are explicitly referenced in the mapping or columns of any table.
    tables:
      buildings:
        type: polygon
        filters:
          require:
            name: [__any__]
          reject:
            building: ['no', none]
          reject_regexp:
            level: '^\D+.*$'
        mapping:
          building: [__any__]
        columns:
          ...
  10. Read OpenStreetMap data into an intermediary cache

    master

    The first step of the import process is reading OSM PBF data into an intermediary LevelDB data store. This allows Imposm to perform the random access required to build geometries without high memory usage.

    Important Cache Notes:

    • Cache files are stored in /tmp/imposm by default. Use -cachedir to change this.
    • To prevent errors when existing caches are found, use -appendcache to merge new data or -overwritecache to replace existing caches.
    • Estimate cache size to be 2-3 times the size of the PBF file to avoid LevelDB crashes due to disk exhaustion.
    imposm import -mapping mapping.yml -read germany.osm.pbf
  11. Importing relation metadata using the relation table type

    master

    The relation table type is used to import the metadata (tags) of a relation without importing its geometry. This is useful for creating lookup tables for features like administrative areas or transport routes to avoid data duplication. You can then join this table with relation_member tables to retrieve the actual geometries.

    Note: relation tables do not support geometry columns. If you need the geometry of a multipolygon relation, use a polygon table instead.

    routes:
      type: relation
      columns:
      - name: osm_id
        type: id
      - key: ref
        name: ref
        type: string
      - name: network
        key: network
        type: string
      relation_types: [route]
      mapping:
        route: [bus]