ogr2ogr
What is ogr2ogr?
ogr2ogr is the vector Swiss-army-knife of GDAL. It reads any OGR-supported format and writes any OGR-supported format, optionally reprojecting, clipping, filtering by attribute or geometry, promoting geometry types, running SQL, renaming layers, and setting creation or layer options along the way. If you are doing anything with vector data on the command line — format conversion, CRS change, schema manipulation, PostGIS load — ogr2ogr is almost certainly the tool.
ogr2ogr [options] <dst_datasource> <src_datasource> [<layer>...]Commonly used options:
-f <format>— output driver (GPKG,GeoJSON,ESRI Shapefile,FlatGeobuf,PostgreSQL, …)-t_srs <CRS>/-s_srs <CRS>— target/source CRS-nln <name>— output layer name-nlt <type>— output geometry type (POINT,LINESTRING,MULTIPOLYGON,PROMOTE_TO_MULTI,CONVERT_TO_LINEAR, etc.)-where "<attribute expression>"— attribute filter-sql "<sql>"/-dialect <dialect>— SQL selection-spat <xmin> <ymin> <xmax> <ymax>— spatial filter (source CRS)-clipsrc <vector>/-clipdst <vector>— clip by a cutline-select "<field1>,<field2>"— keep or reorder fields-lco <NAME>=<VALUE>/-dsco <NAME>=<VALUE>— layer / datasource creation options-append,-overwrite,-update— write mode-skipfailures,-progress,-simplify <tolerance>,-segmentize <max_length>
When would you use ogr2ogr?
Use ogr2ogr whenever vectors need to move, transform, or change shape. Typical jobs: converting a Shapefile to GeoPackage (ogr2ogr -f GPKG out.gpkg in.shp), reprojecting a GeoJSON from WGS84 to Web Mercator (ogr2ogr -t_srs EPSG:3857 -f GeoJSON out.geojson in.geojson), loading a Shapefile into PostGIS (ogr2ogr -f PostgreSQL PG:"dbname=gis" roads.shp -nln roads -lco GEOMETRY_NAME=geom), or clipping a national boundary layer to a study area cutline.
-nlt PROMOTE_TO_MULTI is the right flag when converting between formats that distinguish POLYGON and MULTIPOLYGON — Shapefile only has MULTIPOLYGON, so converting Shapefile to GeoPackage often benefits from promotion to keep downstream tools happy. -sql with -dialect SQLite unlocks SpatiaLite functions during conversion, letting you do ad-hoc spatial joins or aggregations in one step without a staging database.
FAQs
Why do I get mixed POLYGON/MULTIPOLYGON errors?
Some formats (Shapefile) store all polygons as MULTIPOLYGON; others (PostGIS, GeoPackage) distinguish POLYGON from MULTIPOLYGON. Use -nlt PROMOTE_TO_MULTI to force everything to the multi variant, which is usually what downstream consumers want. Alternatively -nlt CONVERT_TO_LINEAR converts curved geometries to linear approximations.
How do I append rather than overwrite?
Pass -append to add features to an existing layer, or -update to open the datasource for write without replacing. -overwrite drops and recreates the layer. Combine with -nln if the source layer name does not match the destination.
Should I use -sql or -where?
Use -where for simple attribute filtering (-where "population > 10000"); it is cheap and works with the driver's own filter pushdown where supported. Use -sql for projections that change the schema, joins, aggregations, spatial functions, or SQL dialect features not expressible in a WHERE clause. -dialect SQLite unlocks SpatiaLite functions on any source.
Why is my PostGIS load slow?
Defaults are conservative. For bulk loads, add -lco SPATIAL_INDEX=NO (build index after load), -lco GEOMETRY_NAME=geom, -lco FID=id, and --config PG_USE_COPY YES to use PostgreSQL COPY instead of INSERT. Post-load, run CREATE INDEX and VACUUM ANALYZE by hand.