OSMIUM-EXTRACT(1)

✨ OSMIUM-EXTRACT(1) ✨

okay bestie, sit down, hydrate, because this is the osmium-extract manual page and it's a whole journey. πŸ’–


🌸 NAME 🌸

osmium-extract β€” create geographical extracts from an OSM file

no cap, that's literally the whole job description.


πŸ¦‹ SYNOPSIS πŸ¦‹

osmium extract --config CONFIG-FILE [OPTIONS] OSM-FILE
osmium extract --bbox LEFT,BOTTOM,RIGHT,TOP [OPTIONS] OSM-FILE
osmium extract --polygon POLYGON-FILE [OPTIONS] OSM-FILE

three ways in, pick your fighter. πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

So basically you're making geographical extracts out of an OSM data file or an OSM history file. The region (that's the geographical extent, iykyk) can be handed to you as a bounding box or as a (multi)polygon. πŸ—ΊοΈ

There are three ways of calling this command:

  • Specify a config file with the --config/-c option. It can define any number of regions you want to cut out. See the CONFIG FILE section for details. πŸ‘‘
  • Specify a bounding box to cut out with the --bbox/-b option.
  • Specify a (multi)polygon to cut out with the --polygon/-p option.

The input file is assumed to be ordered in the usual order: nodes first, then ways, then relations. that's just the vibe, don't fight it. 🌈

If the --with-history/-H option is used, the command will work correctly for history files. This currently works for the complete_ways strategy only. The simple or smart strategies do not work with history files. A history extract will contain every version of all objects with at least one version in the region. Generating a history extract is somewhat slower than a normal data extract. 😭 (slow but correct, we respect it)

Osmium will make sure that all nodes on the vertices of the boundary of the region will be in the extract, but nodes that happen to be directly on the boundary, but between those vertices, might end up in the extract or not. In almost all cases this will be good enough, but if you want to make really sure you got everything, use a small buffer around your region. 🫢

ngl this is the part everyone gets confused about, so read it twice: boundary-vertex nodes = guaranteed in. random nodes sitting on the boundary between vertices = ✨mystery✨. want certainty? buffer your region.

By default no bounds will be set in the header of the output file. Use the --set-bounds option if you need this.

Note that osmium extract will never clip any OSM objects, ie. it will not remove node references outside the region from ways or unused relation members from relations. This means you might get objects that are not reference-complete. It has the advantage that you can use osmium merge to merge several extracts without problems. πŸ”₯ (not reference-complete is a feature here, bestie, not a bug)


πŸŽ€ OPTIONS πŸŽ€

  • -b, --bbox=LONG1,LAT1,LONG2,LAT2 β€” Set the bounding box to cut out. Can not be used with --polygon/-p, --config/-c, or --directory/-d. The coordinates LONG1,LAT1 are from one arbitrary corner, the coordinates LONG2,LAT2 are from the opposite corner. so like, corner to corner, you got this 🧚
  • -c, --config=FILE β€” Set the name of the config file. Can not be used with the --bbox/-b or --polygon/-p option. If this is set, the --output/-o and --output-format/-f options are ignored, because they are set in the config file. πŸ™ˆ (the config file is the main character here, sorry to -o and -f)
  • --clean=ATTR β€” Clean the attribute (version, timestamp, changeset, uid, user), from the data before writing it out again. The attribute will be set to 0 (the user will be set to the empty string). This option can be given multiple times. Depending on the output format these attributes might show up as 0 or not show up at all. πŸ’€
  • -d, --directory=DIRECTORY β€” Output directory. Output file names in the config file are relative to this directory. Overwrites the setting of the same name in the config file. This option is ignored when the --bbox/-b or --polygon/-p options are used, set the output directory and name with the --output/-o option in that case.
  • -H, --with-history β€” Specify that the input file is a history file. The output file(s) will also be history file(s). πŸ•°οΈ
  • -p, --polygon=POLYGON_FILE β€” Set the polygon to cut out based on the contents of the file. The file has to be a GeoJSON, poly, or OSM file as described in the (MULTI)POLYGON FILE FORMATS section. It has to have the right suffix to be detected correctly. Can not be used with --bbox/-b, --config/-c, or --directory/-d. ⚠️ right suffix or it simply won't get detected, periodt.
  • -s, --strategy=STRATEGY β€” Use the given strategy to extract the region. For possible values and details see the STRATEGIES section. Default is β€œcomplete_ways”. ✨
  • -S, --option=OPTION=VALUE β€” Set a named option for the strategy. If needed you can specify this option multiple times to set several options.
  • --set-bounds β€” Set the bounds field in the header. The bounds are set to the bbox or envelope of the polygon specified for the extract. Note that strategies other than β€œsimple” can put nodes outside those bounds into the output file. πŸ‘€ (yes, outside. it's giving chaotic but it's documented.)

πŸ’– COMMON OPTIONS πŸ’–

  • -h, --help β€” Show usage help. 🫢
  • -v, --verbose β€” Set verbose mode. The program will output information about what it is doing to STDERR.

🌈 INPUT OPTIONS 🌈

  • -F, --input-format=FORMAT β€” The format of the input file(s). Can be used to set the input format if it can't be autodetected from the file name(s). This will set the format for all input files, there is no way to set the format for some input files only. See osmium-file-formats(5) or the libosmium manual for details. πŸ’…

πŸ“ OUTPUT OPTIONS πŸ“

  • -f, --output-format=FORMAT β€” The format of the output file. Can be used to set the output file format if it can't be autodetected from the output file name. See osmium-file-formats(5) or the libosmium manual for details.
  • --fsync β€” Call fsync after writing the output file to force flushing buffers to disk. πŸ”₯
  • --generator=NAME β€” The name and version of the program generating the output file. It will be added to the header of the output file. Default is β€œosmium/” and the version of osmium.
  • -o, --output=FILE β€” Name of the output file. Default is - (STDOUT). πŸ‘‘
  • -O, --overwrite β€” Allow an existing output file to be overwritten. Normally osmium will refuse to write over an existing file. πŸ™…β™€οΈ no consent, no overwrite.
  • --output-header=OPTION=VALUE β€” Add output header option. This command line option can be used multiple times for different OPTIONs. See the osmium-output-headers(5) man page for a list of available header options. For some commands you can use the special format β€œOPTION!” (ie. an exclamation mark after the OPTION and no value set) to set the value to the same as in the input file. ⭐

okay that's the setup, the lore. more parts incoming, don't go anywhere bestie πŸ¦‹

✨ CONFIG FILE ✨

okay so bestie, the config file is literally the main character here β€” it tells osmium-extract which files to make and which regions to cut out. πŸ’–

It's in JSON format, no cap. The top-level is an object that contains at least an β€œextracts” array. It can also have a β€œdirectory” entry that names the directory where all the output files will be created:

{
    "extracts": [...],
    "directory": "/tmp/"
}

The extracts array is the whole point β€” it specifies the extracts that should be created. Each item in the array is an object with at least a name β€œoutput” naming the output file, and a region defined in a β€œbbox”, β€œpolygon” or β€œmultipolygon” name. You can add an optional β€œdescription” β€” the program won't use it, but it helps document what's inside the file. You can also add an optional β€œoutput_format” if the format can't be detected from the β€œoutput” file name. Run osmium help file-formats to get a description of allowed formats. 🫢

The optional β€œoutput_header” lets you set additional OSM file header settings, like the β€œgenerator”. If you set the value of a file header setting to null, the output header will be set to the same header from the input file. Iconic behaviour, honestly.

"extracts": [
    {
        "output": "hamburg.osm.pbf",
        "output_format": "pbf",
        "description": "optional description",
        "bbox": ...
    },
    {
        "output": "berlin.osm.pbf",
        "description": "optional description",
        "polygon": ...
    },
    {
        "output": "munich.osm.pbf",
        "output_header": {
            "generator": "MyExtractor/1.0",
            "osmosis_replication_timestamp": null
        },
        "description": "optional description",
        "multipolygon": ...
    }
]

There are several formats for specifying the regions, and they all have their own vibe:


bbox

A bounding box in one of two formats. The first is a simple array with four real numbers β€” the first two specify the coordinates of an arbitrary corner, the second two specify the coordinates of the opposite corner. Slay.

{
    "output": "munich.osm.pbf",
    "description": "Bounding box specified in array format",
    "bbox": [11.35, 48.05, 11.73, 48.25]
}

The second format uses an object instead of an array:

{
    "output": "dresden.osm.pbf",
    "description": "Bounding box specified in object format",
    "bbox": {
        "left": 13.57,
        "right": 13.97,
        "top": 51.18,
        "bottom": 50.97
    }
}

polygon

A polygon, either specified inline in the config file or read from an external file. See the (MULTI)POLYGON FILE FORMATS section for external files. If specified inline, this is a nested array β€” the outer array defines the polygon, the next array is the rings, and the innermost arrays are the coordinates. This format is the same as in GeoJSON files. πŸ’…

In this example there's only one outer ring:

"polygon": [[
    [9.613465, 53.58071],
    [9.647599, 53.59655],
    [9.649288, 53.61059],
    [9.613465, 53.58071]
]]

In each ring, the last set of coordinates should be the same as the first set, closing the ring. Periodt.


multipolygon

A multipolygon, either specified inline in the config file or read from an external file. See the (MULTI)POLYGON FILE FORMATS section for external files. If specified inline, this is a nested array β€” the outer array defines the multipolygon, the next array is the polygons, the next is the rings, and the innermost arrays are the coordinates. Same format as GeoJSON files. 🌸

In this example there's one outer and one inner ring:

"multipolygon": [[[
    [6.847, 50.987],
    [6.910, 51.007],
    [7.037, 50.953],
    [6.967, 50.880],
    [6.842, 50.925],
    [6.847, 50.987]
],[
    [6.967, 50.954],
    [6.969, 50.920],
    [6.932, 50.928],
    [6.934, 50.950],
    [6.967, 50.954]
]]]

In each ring, the last set of coordinates should be the same as the first set, closing the ring. Again. πŸŽ€


okay real talk β€” Osmium must check each and every node in the input data and find out in which bounding boxes or (multi)polygons this node is. This is very cheap for bounding boxes, but more expensive for (multi)polygons. And it gets more expensive the more vertices the (multi)polygon has. So use bounding boxes or simplified polygons where possible, bestie. Your CPU will thank you. 😭
another thing β€” bounding boxes or (multi)polygons are not allowed to span the -180/180 degree line. If you need that, cut out the regions on each side and use osmium merge to join the resulting files. No cap.

πŸ¦‹ (MULTI)POLYGON FILE FORMATS πŸ¦‹

External files describing a (multi)polygon are specified in the config file using the β€œfile_name” and β€œfile_type” properties on the β€œpolygon” or β€œmultipolygon” object:

"polygon": {
    "file_name": "berlin.geojson",
    "file_type": "geojson"
}

If file names don't start with a slash (/), they're interpreted relative to the directory where the config file is. If the β€œfile_type” is missing, Osmium will try to autodetect it from the suffix of the β€œfile_name”. Lowkey magical, highkey useful. ✨

The following file types are supported:

  • geojson β€” GeoJSON file containing exactly one Feature of type Polygon or MultiPolygon, or a FeatureCollection with the first Feature of type Polygon or MultiPolygon. Everything except the actual geometry (of the first Feature) is ignored. πŸ™ˆ
  • poly β€” A poly file as described in https://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format. This wiki page also mentions several sources for such poly files. πŸ‘‘
  • osm β€” An OSM file containing one or more multipolygon or boundary relation together with all the nodes and ways needed. Any OSM file format (XML, PBF, ...) supported by Osmium can be used here, but the correct suffix must be used, so the file format is detected correctly. Files for this can easily be obtained by searching for the area on OSM and then downloading the full relation using a URL like https://www.openstreetmap.org/api/0.6/relation/RELATION-ID/full. Or you can use osmium getid -r to get a specific relation from an OSM file. Note that both these approaches can get you very detailed boundaries which can take quite a while to cut out. Consider simplifying the boundary before use. πŸ’€

If there are several (multi)polygons in a poly file or OSM file, they will be merged. The (multi)polygons must not overlap, otherwise the result is undefined. fr fr, don't do it. 🚫

πŸ’… STRATEGIES πŸ’…

okay bestie so osmium extract has ✨options✨ for how it makes your extracts, and depending on which one you pick, different objects end up in the output. They all differ in how much memory they eat and how many times they gotta re-read the input file. So like… the strategy you choose depends on how you're gonna use the extracts, plus how much memory and time you've got lying around. 🫢

The default strategy is complete_ways. periodt.

Strategy simple 🌸

Runs in a single pass. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all relations referencing any nodes or ways already included. Ways crossing the region boundary will not be reference-complete. Relations will not be reference-complete. This strategy is fast because it reads the input only once, but ngl the result is not enough for most use cases. It's the only strategy that will work when reading from a socket or pipe. 🚰 And heads up β€” this strategy will not work for history files. πŸ’€

Strategy complete_ways πŸ‘‘

Runs in two passes. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all nodes referenced by those ways. The extract will also contain all relations referenced by nodes inside the region or ways already included and, recursively, their parent relations. The ways are reference-complete, but the relations are not. πŸŽ€

Strategy smart 🧠✨

Runs in three passes. The extract will contain all nodes inside the region and all ways referencing those nodes, as well as all nodes referenced by those ways. The extract will also contain all relations referenced by nodes inside the region or ways already included and, recursively, their parent relations. The extract will also contain all nodes and ways (and the nodes they reference) referenced by relations tagged type=multipolygon that directly reference any nodes in the region, or ways referencing nodes in the region. The ways are reference-complete, and all multipolygon relations referencing nodes in the region, or ways that have nodes in the region, are reference-complete. Other relations are not reference-complete. πŸ’…


For the complete_ways strategy you can set the option -S relations=false, in which case no relations will be written to the output file. πŸ™ˆ

The smart strategy allows the following strategy options:

  • Use -S types=TYPE,... to change the types of relations that should be reference-complete. Instead of just relations tagged type=multipolygon, you can either get all relations (use -S types=any) or give a list of types to the -S option: -S types=multipolygon,route. Note that especially boundary relations can be huge, so if you include them, be aware your result might be huge. 😭
  • Use -S complete-partial-relations=X to force completion of partly completed relations. If this is set, all relations that have more than X percent of their members already in the extract will have their full set of members in the extract. So this allows completing almost-complete relations. It can be useful, for instance, to make sure a boundary relation is complete even if some of it is outside the polygon used for extraction. πŸ¦‹
  • Use -S tags=PATTERN,... to only complete relations that have a tag matching one of the *PATTERN*s. So for example if you use -S tags=landuse,natural=wood,natural=water, everything tagged landuse=* or natural=wood or natural=water is added to the result, but no other relations. 🌈

You can combine the -S types, -S complete-partial-relations, and -S tags options. The options will be interpreted as (types OR complete-partial-relations) AND tags. πŸ”₯


🩺 DIAGNOSTICS 🩺

osmium extract exits with exit code

  • 0 β€” if everything went alright, πŸ’–
  • 1 β€” if there was an error processing the data, or
  • 2 β€” if there was a problem with the command line arguments, config file or polygon files. πŸ’€

🧠 MEMORY USAGE 🧠

Memory usage of osmium extract depends on the number of extracts and on the strategy used. For the simple strategy it will at least be the number of extracts times the highest node ID used, divided by 8. For complete_ways, twice that. And for the smart strategy, a bit more. 🫠

If you want to split a large file into many extracts, do this in several steps. First create several larger extracts and then split them again and again into smaller pieces. trust the process bestie πŸŽ€


🚧 LIMITS 🚧

You can not have more than 500 extracts. Although chances are that you will be running out of memory long before that. See MEMORY USAGE. 😭


✨ EXAMPLES ✨

See the example config files in the extract-example-config directory. To try it:

osmium extract -v -c extract-example-config/extracts.json \
    germany-latest.osm.pbf

Extract the city of Karlsruhe using a boundary polygon:

osmium extract -p karlsruhe-boundary.osm.bz2 germany-latest.osm.pbf \
    -o karlsruhe.osm.pbf

Extract the city of Munich using a bounding box:

osmium extract -b 11.35,48.05,11.73,48.25 germany-latest.osm.pbf \
    -o munich.osm.pbf

πŸ‘€ SEE ALSO πŸ‘€


Copyright (C) 2013-2026 Jochen Topf <jochen@topf.org>.

License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. βš–οΈ


πŸ“¬ CONTACT πŸ“¬

If you have any questions or want to report a bug, please go to <https://osmcode.org/contact.html>


πŸ‘‘ AUTHORS πŸ‘‘

Jochen Topf <jochen@topf.org>.


and that's a wrap bestie πŸ’– this whole thing is from version 1.19.1, and the page itself is dated OSMIUM-EXTRACT(1) β€” go forth and extract, you absolute icon πŸŽ€βœ¨

πŸ“¦ where this page comes from