How to Use the Command 'osmium' (with Examples)
Osmium is a highly versatile command-line tool designed for efficiently handling OpenStreetMap (OSM) data files. With support for various file formats such as XML and PBF, it provides an array of functionalities including displaying file contents, extracting specific geographic regions, and filtering data based on specific criteria. Osmium is essentially a powerhouse for developers, geographers, and hobbyists working with OSM data files, offering numerous capabilities for data manipulation and extraction.
Show file information
Code:
osmium fileinfo path/to/input.osm
Motivation: Understanding the contents and metadata of an OSM file is crucial before embarking on any data manipulation or extraction processes. This command provides a summary of the file’s statistics without exposing full data content, allowing users to gauge file size, OSM metadata such as bounding box, and whether the data is compressed or not. It provides a quick, efficient way to ascertain file details.
Explanation:
osmium
: This is the tool used to execute the command functions related to OSM files.fileinfo
: A sub-command that retrieves and displays metadata and statistics of the specified OSM file.path/to/input.osm
: The path to the input OSM file whose information you wish to display. It can be in various formats such as .osm or .pbf.
Example Output:
File:
Format: osm
Compression: none
Header:
Bounding box: 1.0,2.0,3.0,4.0
Contains history: no
Contains multiple versions: no
Size:
Uncompressed size: 32456 bytes
Statistics:
Nodes: 1200
Ways: 300
Relations: 50
Display contents
Code:
osmium show path/to/input.osm
Motivation: To verify the raw data contained in an OSM file, seeing the actual metadata and geospatial information is necessary. This command displays the file contents, which is crucial for validation, debugging, or simply gaining an understanding of what data you are working with before performing further operations.
Explanation:
osmium
: The command-line tool for manipulating OSM data.show
: This sub-command outputs the full content of the specified OSM file.path/to/input.osm
: The input file whose data you wish to examine. It needs you provide an accurate path to ensure the file is correctly accessed.
Example Output:
OSM Data:
node 1
longitude=12.34 latitude=56.78
timestamp=2020-01-01T00:00:00Z
way 1
nodes=1,2,3...
relation 1
members=way/1, node/1...
Convert file format from PBF into XML
Code:
osmium cat path/to/input.osm.pbf -o path/to/output.osm
Motivation: Converting OSM data from PBF (Protocolbuffer Binary Format) to XML is often required for compatibility with various OSM-data-related applications or for human-readable text processing. PBF is more compact and faster to read/write, but XML is more verbose and thus easier for manual examination and compatibility with older tools.
Explanation:
osmium
: The osmium tool that facilitates complex manipulations on OSM files.cat
: This command merges files or simply converts them in this context.path/to/input.osm.pbf
: Path to the input file in PBF format.-o
: This flag specifies the output file.path/to/output.osm
: The path where the converted XML file will be stored.
Example Output:
The file 'path/to/input.osm.pbf' has been converted to 'path/to/output.osm' in XML format.
Extract a geographic region by the given bounding box
Code:
osmium extract -b min_longitude,min_latitude,max_longitude,max_latitude path/to/input.pbf -o path/to/output.pbf
Motivation: Subsetting OSM data is often necessary for focused geographical studies and localized applications. By specifying a bounding box, users can reduce the data’s complexity, which in turn optimizes the performance for analysis or display purposes within the specified area.
Explanation:
osmium
: The versatile command for OSM data manipulation.extract
: This sub-command extracts specific geospatial data from the file.-b
: Stands for bounding box. It requires four coordinate arguments defining the geographical area.min_longitude,min_latitude,max_longitude,max_latitude
: These are the coordinates that define the rectangle of the area to be extracted.path/to/input.pbf
: The path to the input PBF file.-o
: Outputs the extracted region to a specified file.path/to/output.pbf
: Specifies where the extracted data will be stored.
Example Output:
The region within the bounding box (min_longitude,min_latitude,max_longitude,max_latitude) has been extracted to 'path/to/output.pbf'.
Extract a geographic region by a GeoJSON file
Code:
osmium extract -p path/to/polygon.geojson path/to/input.pbf -o path/to/output.pbf
Motivation: For tasks involving more complex boundaries than can be specified by a simple rectangle, using a GeoJSON file allows precise extraction of a region defined by more detailed and irregular borders. This use case is particularly beneficial in urban planning and environmental modeling when working with non-rectangular geographical boundaries.
Explanation:
osmium
: The tool employed for operating on OSM files.extract
: This sub-command extracts sections of data as per defined boundaries.-p
: Denotes that a polygon (rather than a bounding box) is specified for the extraction.path/to/polygon.geojson
: Path to the GeoJSON file that defines the extraction area.path/to/input.pbf
: The input PBF file containing OSM data to be filtered.-o
: An option that sets the output destination.path/to/output.pbf
: Specifies where the resulting extracted data will be saved.
Example Output:
Data within the polygon defined in 'path/to/polygon.geojson' has been extracted and stored at 'path/to/output.pbf'.
Filter all objects tagged as “restaurant”
Code:
osmium tags-filter path/to/input.pbf amenity=restaurant -o path/to/output.pbf
Motivation: Filtering OSM data to extract all entries tagged under a certain amenity (e.g., restaurants) is crucial for applications in location-based services, tourism apps, or urban development projects. By focusing only on relevant data, users can streamline their dataset and facilitate faster processing and analysis.
Explanation:
osmium
: The osmium tool for OSM files.tags-filter
: This sub-command is used to select data with specific tags.path/to/input.pbf
: The input file in PBF format.amenity=restaurant
: Specifies that only OSM objects with this tag should be extracted.-o
: Directs the filtered output to a new file.path/to/output.pbf
: Designates the path where the filtered output is saved.
Example Output:
Objects tagged with amenity=restaurant have been extracted to 'path/to/output.pbf'.
Filter for “way” objects tagged as “highway”
Code:
osmium tags-filter path/to/input.pbf w/highway -o path/to/output.pbf
Motivation: Highway data is frequently extracted from OSM files for transport modeling, route planning, and infrastructure development. Filtering ‘way’ objects with the ‘highway’ tag provides a specialized dataset focused on road and path information, useful for many geospatial analyses and applications.
Explanation:
osmium
: The command-line utility for OSM file manipulation.tags-filter
: Allows selection based on tags.path/to/input.pbf
: Input file path to the OSM dataset.w/highway
: The filtering criterion, specifying ‘ways’ that have a ‘highway’ tag.-o
: Specifies the file to write the filtered data to.path/to/output.pbf
: Path for the output file containing only highway data.
Example Output:
All 'way' objects tagged as 'highway' have been filtered and saved to 'path/to/output.pbf'.
Filter “way” and “relation” objects tagged as “building”
Code:
osmium tags-filter path/to/input.pbf wr/building -o path/to/output.pbf
Motivation: Filtering building data is critical for urban development, city planning, 3D modeling, etc. Isolating ‘way’ and ‘relation’ objects tagged with building details supports targeted analysis and application where infrastructure information is a primary concern.
Explanation:
osmium
: This tool is utilized for data manipulation tasks regarding OSM files.tags-filter
: Sub-command for filtering OSM objects by tags.path/to/input.pbf
: The path indicating where the input PBF file is located.wr/building
: A dual tag selector specifying both ‘way’ and ‘relation’ objects with the ‘building’ tag.-o
: Designator for an output file.path/to/output.pbf
: Designed destination for storing the filtered building data.
Example Output:
'Way' and 'relation' objects tagged as 'building' have been saved to 'path/to/output.pbf'.
Conclusion
The Osmium tool stands as a comprehensive command-line utility for effectively managing and manipulating OpenStreetMap data, streamlining tasks including content display, file conversion, geographic extraction, and object filtering. Each functionality facilitates specialized operations crucial for various applications ranging from analysis to development, making it indispensable for professionals and enthusiasts working with map data.