LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

ogr2ogr

Convert vector geospatial data between formats

TLDR

Convert a shapefile to GeoJSON
$ ogr2ogr -f "GeoJSON" [output.json] [input.shp]
copy
Convert GeoJSON to GeoPackage
$ ogr2ogr -f "GPKG" [output.gpkg] [input.json]
copy
Reproject data to WGS84
$ ogr2ogr -t_srs EPSG:4326 [output.shp] [input.shp]
copy
Import a shapefile into PostGIS
$ ogr2ogr -f "PostgreSQL" PG:"dbname=[db]" [input.shp]
copy
Filter features with a WHERE clause
$ ogr2ogr -where "[population > 10000]" [output.shp] [input.shp]
copy
Clip features to a bounding box
$ ogr2ogr -spat [xmin] [ymin] [xmax] [ymax] [output.shp] [input.shp]
copy
Append data to an existing PostGIS layer
$ ogr2ogr -append -f "PostgreSQL" PG:"dbname=[db]" [input.shp]
copy
Select specific fields and rename the output layer
$ ogr2ogr -select [name,population] -nln [cities] [output.gpkg] [input.shp]
copy

SYNOPSIS

ogr2ogr [options] dstdatasource srcdatasource [layer]

DESCRIPTION

ogr2ogr converts vector geospatial data between file formats, databases, and web services. It is part of the GDAL/OGR library and supports over 80 vector formats including Shapefile, GeoJSON, GeoPackage, PostGIS, KML, and GML.Beyond simple format conversion, ogr2ogr can reproject coordinates between spatial reference systems, filter features by attribute or spatial extent, clip geometries, select specific fields, and transform geometry types.

PARAMETERS

DSTDATASOURCE_

Destination dataset (file path, database connection string, etc.).
SRCDATASOURCE_
Source dataset.
-f FORMAT
Output format name (e.g., "GeoJSON", "ESRI Shapefile", "PostgreSQL", "GPKG").
-t_srs SRS
Target spatial reference system (e.g., EPSG:4326).
-s_srs SRS
Source spatial reference system (override if not defined in source).
-a_srs SRS
Assign a spatial reference system to the output without reprojecting.
-select FIELDS
Comma-separated list of fields to copy from the source.
-where EXPR
SQL WHERE clause to filter features from the source.
-sql STATEMENT
SQL statement to execute against the source for feature selection.
-spat XMIN YMIN XMAX YMAX
Spatial filter: only select features intersecting this bounding box.
-clipsrc XMIN YMIN XMAX YMAX
Clip geometries to the specified bounding box or WKT geometry.
-overwrite
Delete and recreate the output layer if it already exists.
-append
Append to an existing layer instead of creating a new one.
-update
Open existing output datasource in update mode.
-nln NAME
Assign a new name to the output layer.
-nlt TYPE
Define the geometry type for the output layer (e.g., POINT, POLYGON, MULTILINESTRING).
-lco NAME=VALUE
Layer creation option (format specific).
-dsco NAME=VALUE
Dataset creation option (format specific).
-skipfailures
Continue processing after a failure, skipping the failed feature.
-progress
Display a progress bar on the terminal.
-gt N
Group N features per transaction (default 20000). Increase for better performance with database drivers.

INSTALL

sudo apt install gdal-bin
copy
sudo apk add gdal-tools
copy

CAVEATS

Part of the GDAL suite and must be installed separately. Format support depends on how GDAL was compiled. Coordinate system reprojection requires correct SRS definitions. The -skipfailures option forces transaction grouping to 1, which can severely slow database inserts.

SEE ALSO

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid