How to use the command ogr2ogr (with examples)
The ogr2ogr command is a powerful tool for converting geospatial vector data between different file formats. Whether you need to convert Shapefiles to GeoPackage, filter features in a GeoJSON, change coordinate reference systems, load data into a PostGIS database, or clip layers in a GeoPackage, ogr2ogr can handle these tasks efficiently. This article provides examples of each use case, along with explanations of the command arguments and their motivations.
Use case 1: Convert a Shapefile into a GeoPackage
Code:
ogr2ogr -f GPKG path/to/output.gpkg path/to/input.shp
Motivation: Converting a Shapefile to a GeoPackage can be useful when you need to use a different file format that supports advanced features, such as multiple geometry types, attribute tables, and spatial indexes. GeoPackage is an open standard format that is widely supported by Geographic Information System (GIS) software.
Explanation:
- The
-f GPKG
argument specifies the output file format as GeoPackage. path/to/output.gpkg
is the path where the converted GeoPackage will be saved.path/to/input.shp
is the path to the input Shapefile.
Example Output:
The Shapefile located at path/to/input.shp
will be converted to a GeoPackage file located at path/to/output.gpkg
.
Use case 2: Reduce a GeoJSON to features matching a condition
Code:
ogr2ogr -where 'myProperty > 42' -f GeoJSON path/to/output.geojson path/to/input.geojson
Motivation:
When working with large GeoJSON files, it can be helpful to filter out features that do not meet specific criteria. This can reduce the file size and improve processing performance. The -where
argument allows you to specify the filtering condition based on attribute values.
Explanation:
- The
-where 'myProperty > 42'
argument specifies the condition for feature reduction. In this example, it filters features where the attribute ‘myProperty’ is greater than 42. - The
-f GeoJSON
argument specifies the output file format as GeoJSON. path/to/output.geojson
is the path where the reduced GeoJSON file will be saved.path/to/input.geojson
is the path to the input GeoJSON file.
Example Output:
The GeoJSON file located at path/to/input.geojson
will be reduced to include only features where the attribute ‘myProperty’ is greater than 42. The resulting GeoJSON file will be saved at path/to/output.geojson
.
Use case 3: Change coordinate reference system of a GeoPackage from EPSG:4326 to EPSG:3857
Code:
ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:3857 -f GPKG path/to/output.gpkg path/to/input.gpkg
Motivation: Changing the coordinate reference system (CRS) of a GeoPackage can be necessary when working with data that is in a different CRS, or when the projection needs to be modified for a specific use case. In this example, we convert a Geopackage from EPSG:4326 (WGS84) to EPSG:3857 (Web Mercator).
Explanation:
- The
-s_srs EPSG:4326
argument specifies the source CRS as EPSG:4326. - The
-t_srs EPSG:3857
argument specifies the target CRS as EPSG:3857. - The
-f GPKG
argument specifies the output file format as GeoPackage. path/to/output.gpkg
is the path where the converted GeoPackage will be saved.path/to/input.gpkg
is the path to the input GeoPackage.
Example Output:
The GeoPackage located at path/to/input.gpkg
will be converted, changing its coordinate reference system from EPSG:4326 to EPSG:3857. The resulting GeoPackage will be saved at path/to/output.gpkg
.
Use case 4: Convert a CSV file into a GeoPackage, specifying the names of the coordinate columns and assigning a coordinate reference system
Code:
ogr2ogr -f GPKG path/to/output.gpkg path/to/input.csv -oo X_POSSIBLE_NAMES=longitude -oo Y_POSSIBLE_NAMES=latitude -a_srs EPSG:4326
Motivation: Converting CSV files containing geospatial data to a standardized format like GeoPackage allows for better visualization, analysis, and interoperability with other GIS software. This example demonstrates how to specify the names of the coordinate columns in the CSV file and assign a coordinate reference system (CRS).
Explanation:
-f GPKG
specifies the output file format as GeoPackage.path/to/output.gpkg
is the path where the converted GeoPackage will be saved.path/to/input.csv
is the path to the input CSV file.-oo X_POSSIBLE_NAMES=longitude
and-oo Y_POSSIBLE_NAMES=latitude
specify the column names in the CSV file that contain the X and Y coordinates respectively.-a_srs EPSG:4326
assigns the coordinate reference system (CRS) as EPSG:4326 (WGS84) to the output GeoPackage.
Example Output:
The CSV file located at path/to/input.csv
will be converted to a GeoPackage file located at path/to/output.gpkg
, with the coordinate columns ’longitude’ and ’latitude’. The GeoPackage will have the coordinate reference system (CRS) set to EPSG:4326.
Use case 5: Load a GeoPackage into a PostGIS database
Code:
ogr2ogr -f PostgreSQL PG:dbname="database_name" path/to/input.gpkg
Motivation: Loading a GeoPackage into a PostGIS database allows for advanced spatial querying and analysis capabilities, as well as seamless integration with other spatial data sources. In this example, we load a GeoPackage file into a PostGIS database named “database_name”.
Explanation:
-f PostgreSQL
specifies the output file format as PostgreSQL.PG:dbname="database_name"
is the connection string to the PostGIS database with the specified database name.path/to/input.gpkg
is the path to the GeoPackage file that will be loaded into the PostGIS database.
Example Output:
The GeoPackage file located at path/to/input.gpkg
will be loaded into the PostGIS database named “database_name”.
Use case 6: Clip layers of a GeoPackage file to the given bounding box
Code:
ogr2ogr -spat min_x min_y max_x max_y -f GPKG path/to/output.gpkg path/to/input.gpkg
Motivation: Clipping layers in a GeoPackage file to a specific bounding box can be useful when you need to extract only the features that fall within a certain area of interest. This can be particularly valuable when dealing with large datasets or when you want to focus on a specific region.
Explanation:
-spat min_x min_y max_x max_y
specifies the bounding box coordinates that define the clipping extent.-f GPKG
specifies the output file format as GeoPackage.path/to/output.gpkg
is the path where the clipped GeoPackage file will be saved.path/to/input.gpkg
is the path to the input GeoPackage file.
Example Output:
The layers in the GeoPackage file located at path/to/input.gpkg
will be clipped to the given bounding box coordinates. The resulting GeoPackage file will be saved at path/to/output.gpkg
.
Conclusion:
The ogr2ogr command is a versatile tool for converting, filtering, and manipulating geospatial vector data. With the examples provided in this article, you can now leverage ogr2ogr for various use cases, ranging from format conversion and data reduction to coordinate system transformation and database integration. Experiment with these examples and adapt them to your specific requirements to get the most out of ogr2ogr.