ogr2ogr: The definitive beginner's guide
January 2025 · 8 min read
ogr2ogr is the command-line vector conversion tool that ships with GDAL. If you work with geospatial data regularly, learning this tool will save you hours every week. It converts between over 80 vector formats, reprojects data between coordinate systems, filters features, and clips datasets to a bounding box — all in a single command.
The name stands for "OGR to OGR" where OGR is GDAL's vector layer API. Don't worry about what that means — what matters is what the tool does.
Installing GDAL
The easiest way to install GDAL depends on your system:
macOS (Homebrew)
brew install gdalUbuntu / Debian
sudo apt install gdal-binWindows (conda)
conda install -c conda-forge gdalAfter installing, run ogr2ogr --version to confirm it is working. You should see something like GDAL 3.9.0.
The basic syntax
Every ogr2ogr command follows the same pattern:
ogr2ogr -f [output_format] [output_file] [input_file]Note the order: output comes before input. This trips up almost everyone the first time.
Common conversions
Shapefile → GeoJSON
ogr2ogr -f GeoJSON output.geojson input.shpGeoJSON → Shapefile
ogr2ogr -f "ESRI Shapefile" output_dir/ input.geojsonNote: Shapefile output goes to a directory, not a file. ogr2ogr creates the .shp, .dbf, .shx, and .prj files inside the directory.
KML → Shapefile
ogr2ogr -f "ESRI Shapefile" output_dir/ input.kmlAnything → GeoPackage
ogr2ogr -f GPKG output.gpkg input.shpGPKG is the format string for GeoPackage. It accepts any supported input format.
CSV (with lat/lon) → GeoJSON
ogr2ogr -f GeoJSON output.geojson input.csv \
-oo X_POSSIBLE_NAMES=longitude,lon,lng \
-oo Y_POSSIBLE_NAMES=latitude,latThe -oo flags tell GDAL which columns to use as coordinates. Adjust the column names to match your data.
Reprojecting coordinate systems
Add -t_srs to reproject during conversion:
# Convert Shapefile to GeoJSON AND reproject to WGS 84 (EPSG:4326)
ogr2ogr -f GeoJSON output.geojson input.shp -t_srs EPSG:4326
# Reproject to British National Grid (EPSG:27700)
ogr2ogr -f "ESRI Shapefile" output_dir/ input.geojson -t_srs EPSG:27700Filtering features by attribute
Use -where to filter rows using SQL-style expressions:
# Extract only features where population > 1,000,000
ogr2ogr -f GeoJSON large_cities.geojson cities.shp -where "population > 1000000"
# Extract only a specific country
ogr2ogr -f GeoJSON uk.geojson countries.shp -where "name = 'United Kingdom'"Clipping to a bounding box
# Clip to a bounding box: -clipdst minX minY maxX maxY
ogr2ogr -f GeoJSON europe.geojson world.shp \
-clipdst -25 34 45 72Useful flags
| Flag | What it does |
|---|---|
| -overwrite | Overwrite the output file if it already exists |
| -append | Append features to an existing layer instead of overwriting |
| -skipfailures | Continue on error (skip the failed feature) |
| -limit N | Only convert the first N features (useful for testing) |
| -nlt GEOMETRY | Force a specific geometry type (POINT, LINESTRING, POLYGON) |
| -select col1,col2 | Only include specific attribute columns in the output |
| -nln layer_name | Set the output layer name |
Common errors and fixes
"The GeoJSON driver does not overwrite existing files"
ogr2ogr refuses to overwrite a file that already exists. Add -overwrite to your command:
ogr2ogr -overwrite -f GeoJSON output.geojson input.shp"Attempt to write non-point geometry to point shapefile"
Shapefile requires all features to have the same geometry type. If your source file has mixed types (e.g. both points and polygons), you need to separate them:
# Extract only polygons
ogr2ogr -f "ESRI Shapefile" polygons_only/ mixed.geojson -nlt POLYGON -skipfailures"ERROR 4: Unable to open EPSG support file"
GDAL cannot find its projection database. On macOS with Homebrew, run:
export PROJ_LIB=/usr/local/share/proj
# or on Apple Silicon:
export PROJ_LIB=/opt/homebrew/share/projDon't want to use the command line?
ogr2ogr is powerful but has a steep learning curve. Maparz wraps the same GDAL engine in a simple drag-and-drop interface — no installation, no flags to remember.
Convert files with GDAL — no command line required
Maparz uses ogr2ogr under the hood. Upload your file, pick an output format, download the result.
Open converter