How to Convert a .OSM File to a .GeoJSON File using Python
If you’re working with geospatial data, you’ll likely come across OpenStreetMap (.osm) files and may need to convert them to a more widely-used format like GeoJSON. Whether you’re analyzing urban infrastructure, building a map visualization, or working on a geospatial project, GeoJSON is the go-to format for web mapping and data interchange. A few websites do the job for you, but they are either paid or have a conversion limit. In this article, I’ll walk you through the process of converting .osm files to .geojson using Python, completely free of charge.
To get started, you’ll need Python installed on your machine along with two libraries: osmnx
and geopandas
.
Installing Required Libraries
First, install the necessary Python packages by running:
pip install osmnx geopandas
- osmnx is a Python library that simplifies downloading and working with OpenStreetMap data.
- geopandas extends the capabilities of pandas to allow for spatial data manipulation.
Python Code to Extract Data from .OSM and Save as .GeoJSON
Let’s assume you have an .osm
file containing data from OpenStreetMap. You can use the following code to extract specific features like roads, buildings, and land use, and save them as separate .geojson
files.
import osmnx as ox
import geopandas as gpd
# Load the .osm file into a GeoDataFrame
gdf = ox.geometries_from_xml('your_osm_file.osm')
# Extract roads and save to .geojson
roads = gdf[gdf['highway'].notnull()]
roads.to_file('output_directory/roads.geojson', driver='GeoJSON')
# Extract buildings and save to .geojson
buildings = gdf[gdf['building'].notnull()]
buildings.to_file('output_directory/buildings.geojson', driver='GeoJSON')
# Extract land use and save to .geojson
landuse = gdf[gdf['landuse'].notnull()]
landuse.to_file('output_directory/landuse.geojson', driver='GeoJSON')
Breakdown of the Code
- Load the OSM data: The
osmnx.geometries_from_xml
function loads the OpenStreetMap data into a GeoDataFrame. You simply need to provide the path to your.osm
file. - Extract specific features: By filtering the GeoDataFrame, you can extract roads, buildings, and land use. The filtering uses the
.notnull()
function to select only rows that contain non-empty values for specific features (likehighway
for roads orbuilding
for buildings). - Save to .GeoJSON: The
to_file
function saves the filtered data into GeoJSON format, specifying the output directory and setting thedriver
parameter to 'GeoJSON'.
Why This Method?
This method is cost-free and highly efficient for extracting data. Libraries like osmnx
and geopandas
simplify the workflow, allowing you to handle large and complex .osm
files without having to rely on external paid services. You also get full control over what data you extract and how you use it in your projects.
Conclusion
Converting .osm
files to .geojson
is a crucial step in making OpenStreetMap data more usable for visualization and analysis. With just a few lines of Python code and open-source tools, you can easily extract and manipulate geospatial data for your next project.
If you’re working on geospatial analysis or web mapping, this process will help you bridge the gap between complex OSM data and the lightweight, widely-used GeoJSON format. Happy mapping!