ogr2ogr is the Swiss Army knife of vector GIS. It ships with GDAL, reads more than 80 formats, and once you internalise the argument order — ogr2ogr [destination] [source] [options] — most data wrangling collapses into a one-liner.
This is the reference I wish I had three years ago. Every command below is real and tested against GDAL 3.8+.
1. Shapefile → GeoJSON
ogr2ogr -f GeoJSON out.geojson in.shpThe default. The output CRS matches the input .prj. If the input has no .prj, GDAL warns but writes the file.
2. GeoJSON → Shapefile
ogr2ogr -f "ESRI Shapefile" out.shp in.geojsonWatch the 10-character field name limit; -lco RESIZE=YES shrinks string columns to fit their content.
3. Reproject to Web Mercator
ogr2ogr -t_srs EPSG:3857 out.geojson in.geojson-t_srs is *target* SRS. Use -s_srs if the source has no embedded CRS and you need to declare one.
4. Reproject UTM → WGS84
ogr2ogr -t_srs EPSG:4326 -s_srs EPSG:32633 out.geojson in.shpUseful when a Shapefile arrives with a missing or wrong .prj.
5. Shapefile → GeoPackage (single layer)
ogr2ogr -f GPKG out.gpkg in.shp -nln parcels-nln (new layer name) names the layer inside the GeoPackage.
6. Multiple Shapefiles → one GeoPackage
for f in *.shp; do
ogr2ogr -f GPKG -update -append out.gpkg "$f" -nln "$(basename "$f" .shp)"
done-update -append opens the existing .gpkg and adds a new layer per Shapefile.
7. SQL filter at read time
ogr2ogr -f GeoJSON out.geojson in.gpkg -sql "SELECT * FROM parcels WHERE area_ha > 5"-sql runs an OGR SQL query against the source. Cheaper than reading everything and filtering after.
8. Spatial clip to a bounding box
ogr2ogr -spat 12.0 47.0 13.5 48.5 out.geojson in.geojsonCoordinates are minx miny maxx maxy in the source CRS unless you add -spat_srs EPSG:4326.
9. Spatial clip to a polygon
ogr2ogr -clipsrc mask.geojson out.geojson in.geojsonClips features to the mask polygon. Use -clipsrcwhere to use only a subset of the mask.
10. Attribute filter
ogr2ogr out.geojson in.shp -where "landuse = 'residential'"Lighter than -sql; uses simple WHERE syntax against the source layer.
11. Drop columns
ogr2ogr out.geojson in.geojson -select "name,population,geom"Keeps only the listed attributes; everything else is dropped.
12. Rename a column
ogr2ogr out.geojson in.gpkg -sql "SELECT name AS feature_name, geom FROM places"OGR SQL supports AS aliases — the destination column takes the alias.
13. PostGIS → GeoJSON
ogr2ogr -f GeoJSON out.geojson PG:"host=localhost dbname=gis user=me" -sql "SELECT * FROM public.roads"The PG: connection string is the source; the SQL is evaluated server-side.
14. GeoJSON → PostGIS
ogr2ogr -f PostgreSQL PG:"host=localhost dbname=gis user=me" in.geojson -nln roads -overwrite-overwrite drops the existing table; omit it to append.
15. KML → GeoJSON, flatten attributes
ogr2ogr -f GeoJSON out.geojson in.kml -dim XY-dim XY discards the Z dimension if you don't need elevations. KML's ExtendedData attributes are preserved.
16. CSV with coordinates → GeoJSON
ogr2ogr -f GeoJSON out.geojson -s_srs EPSG:4326 \
-oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat in.csv-oo (open option) tells the CSV driver which columns to use as coordinates.
17. Simplify geometries
ogr2ogr -simplify 0.0001 out.geojson in.geojsonDouglas-Peucker simplification with tolerance in source CRS units. 0.0001 degrees ≈ 11m at the equator.
18. Reduce coordinate precision
ogr2ogr -f GeoJSON -lco COORDINATE_PRECISION=6 out.geojson in.geojson6 decimal places resolves to ~11 cm — more than enough for web maps and roughly halves the file size.
19. Append features to an existing layer
ogr2ogr -update -append out.gpkg new_features.shp -nln existing_layerSchemas must match (column names and types). Use -skipfailures to keep going on mismatches.
20. Inspect without converting
ogrinfo -so -al in.gpkgTechnically not ogr2ogr, but the companion tool. -so is "summary only," -al is "all layers." Shows feature counts, CRS, attribute schema.
Closing notes
These twenty cover roughly 90% of day-to-day GIS data work. The remaining 10% is dialect-specific: M-values in Shapefile, GeoPackage attribute styles, GMLAS schema mapping. Reach for the GDAL driver docs when you hit them.