How to use the command ogrinfo (with examples)
The ogrinfo
command is a powerful tool in the GDAL (Geospatial Data Abstraction Library) that allows users to list information about an OGR-supported data source. It provides detailed information about different layers, features, and attributes of various geospatial formats. This article will illustrate several use cases of the ogrinfo
command with corresponding examples.
Use case 1: List supported formats
Code:
ogrinfo --formats
Motivation: This use case is useful when you want to determine all the geospatial formats supported by GDAL, which can be helpful in selecting the appropriate format for your data.
Explanation: The ogrinfo --formats
command lists all the formats supported by GDAL. It provides a comprehensive list of various geospatial formats that you can use as input or output data sources.
Example output:
Supported Formats:
-> "ESRI Shapefile" (read/write)
-> "GPKG" (read/write)
-> "GeoJSON" (read/write)
...
Use case 2: List layers of a data source
Code:
ogrinfo path/to/input.gpkg
Motivation: This use case is useful when you want to retrieve a list of layers present in a specific data source. It helps in understanding the structure of the geospatial dataset before further analysis or processing.
Explanation: The ogrinfo path/to/input.gpkg
command displays the layers available in the specified data source. It provides information like layer names, geometry types, and feature counts.
Example output:
INFO: Open of `path/to/input.gpkg'
using driver `GPKG' successful.
1: layer1 (Point)
2: layer2 (Polygon)
3: layer3 (LineString)
Use case 3: Get detailed information about a specific layer of a data source
Code:
ogrinfo path/to/input.gpkg layer_name
Motivation: This use case is useful when you want to retrieve detailed information about a specific layer within a data source. It helps in understanding the attributes, attribute types, and other properties of the layer.
Explanation: The ogrinfo path/to/input.gpkg layer_name
command provides detailed information about a specific layer named layer_name
within the specified data source. It displays the schema, spatial extent, attribute definitions, and feature count of the layer.
Example output:
INFO: Open of `path/to/input.gpkg'
using driver `GPKG' successful.
Layer name: layer_name
Geometry: Unknown (any)
Feature Count: 10
Extent: (100.00000, 200.00000) - (300.00000, 400.00000)
...
Use case 4: Show summary information about a specific layer of a data source
Code:
ogrinfo -so path/to/input.gpkg layer_name
Motivation: This use case is useful when you want to quickly obtain summary information about a specific layer within a data source. It provides a high-level overview without the need for detailed information.
Explanation: The ogrinfo -so path/to/input.gpkg layer_name
command shows summary information about a specific layer named layer_name
within the specified data source. It displays the layer name, feature count, geometry type, and spatial extent.
Example output:
INFO: Open of `path/to/input.gpkg'
using driver `GPKG' successful.
1: layer_name (Unknown (any))
Use case 5: Show summary of all layers of the data source
Code:
ogrinfo -so -al path/to/input.gpkg
Motivation: This use case is useful when you want to retrieve summary information about all the layers within a data source. It allows you to obtain a quick overview of the entire dataset.
Explanation: The ogrinfo -so -al path/to/input.gpkg
command shows a summary of all the layers present in the specified data source. It displays the layer names, feature counts, geometry types, and spatial extents for each layer.
Example output:
INFO: Open of `path/to/input.gpkg'
using driver `GPKG' successful.
1: layer1 (Point)
2: layer2 (Polygon)
3: layer3 (LineString)
...
Summary:
layer1: Point (0 features)
layer2: Polygon (10 features)
layer3: LineString (5 features)
Use case 6: Show detailed information of features matching a condition
Code:
ogrinfo -where 'attribute_name > 42' path/to/input.gpkg layer_name
Motivation: This use case is useful when you want to filter and retrieve detailed information about specific features in a layer based on a condition. It helps in exploring and analyzing specific subsets of data.
Explanation: The ogrinfo -where 'attribute_name > 42' path/to/input.gpkg layer_name
command retrieves detailed information about features from layer_name
within the specified data source path/to/input.gpkg
that satisfy the given condition attribute_name > 42
. It displays the attributes and their values for each matching feature.
Example output:
INFO: Open of `path/to/input.gpkg'
using driver `GPKG' successful.
Layer name: layer_name
Geometry: Unknown (any)
Feature Count: 3
Extent: (100.00000, 200.00000) - (300.00000, 400.00000)
...
OGRFeature(layer_name):1234
attribute_name (Integer) = 50
attribute_type (String) = "example"
...
Use case 7: Update a layer in the data source with SQL
Code:
ogrinfo path/to/input.geojson -dialect SQLite -sql "UPDATE input SET attribute_name = 'foo'"
Motivation: This use case is useful when you want to update attribute values of a layer within a data source using SQL statements. It allows you to perform data manipulation operations directly on the geospatial data.
Explanation: The ogrinfo path/to/input.geojson -dialect SQLite -sql "UPDATE input SET attribute_name = 'foo'"
command updates the attribute values of a layer named input
within the specified data source path/to/input.geojson
. The -dialect SQLite
argument sets the SQL dialect to SQLite. The SQL statement "UPDATE input SET attribute_name = 'foo'"
updates the value of the attribute_name
field to 'foo'
for all features in the layer.
Example output:
INFO: Open of `path/to/input.geojson'
using driver `GeoJSON' successful.
INFO: UPDATE command executed on `path/to/input.geojson',
updated 10 rows.
Conclusion:
The ogrinfo
command provides essential functionalities for geospatial data exploration and analysis. It allows users to retrieve information about supported formats, list layers within a data source, obtain detailed and summary information about specific layers, filter features based on conditions, and perform data manipulation operations using SQL. By understanding and utilizing these different use cases, users can effectively work with various geospatial datasets and leverage the power of ogrinfo
for their specific use-cases.