December 18, 2025
GISGDALOGRShapefile

Working with ESRI Shapefiles using GDAL / OGR

Working with ESRI Shapefiles using GDAL / OGR

This guide covers common OGR/GDAL command-line operations for working with ESRI Shapefiles and PostGIS databases, including inspection, reprojection, format conversion, querying, and database updates.

Identify the Geometry Type of a Shapefile

ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp

This command lists layers and shows the geometry type (Polygon, Point, etc.).

Get Full Details of a Shapefile (Schema + Metadata)

ogrinfo -so Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary"

  • -so → summary only (no feature dump)
  • Shows fields, geometry type, and spatial reference

Get Full Details of a Specific Record (by FID)

ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary WHERE fid = 1"

Reproject an Existing ESRI Shapefile

ogr2ogr ../newShp/Bittner_BellePrairieSec8_N40_Boundary.shp \ -t_srs EPSG:4326 \ Bittner_BellePrairieSec8_N40_Boundary.shp

  • Converts projection to WGS84 (EPSG:4326)

Convert an ESRI Shapefile to GeoJSON

ogr2ogr -f GeoJSON \ ../newShp/Bittner_BellePrairieSec8_N40_Boundary.json \ -t_srs EPSG:4326 \ Bittner_BellePrairieSec8_N40_Boundary.shp

Select Records and Create a New Shapefile

ogr2ogr ../newShp/filtered.shp \ Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary WHERE name = 'Apple'"

Count Records in a Shapefile

ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT COUNT(*) FROM Bittner_BellePrairieSec8_N40_Boundary"

Count DISTINCT Records in a Shapefile

ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT COUNT(DISTINCT name) FROM Bittner_BellePrairieSec8_N40_Boundary"

Clip Spatial Data (Points within Polygon)

ogr2ogr ../newShp/clipped.shp \ ../esridata/points.shp \ -clipsrc Bittner_BellePrairieSec8_N40_Boundary.shp

Note:

  • Input geometry must be POINT
  • Clip geometry must be POLYGON

OGR & PostGIS Queries

Connect to PostGIS and List Tables

ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1"

Extract Data from PostGIS to GeoJSON

ogr2ogr -f GeoJSON \ ../newShp/trees.geojson \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary"

Extract Data from PostGIS to an ESRI Shapefile

ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary"

Extract & Reproject PostGIS Data to EPSG:32643

ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ -t_srs EPSG:32643 \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary"

Extract Specific Fields & Records from PostGIS

ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ -select name \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary WHERE name = 'mango'"

Insert Shapefile Records into PostGIS

ogr2ogr -f PostgreSQL \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ Bittner_BellePrairieSec8_N40_Boundary.shp

Add a New Column to a PostGIS Table

ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "ALTER TABLE bittner_belleprairiesec8_n40_boundary ADD COLUMN surveyor TEXT"

Update PostGIS Table Records Using WHERE Clause

ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -dialect SQLite \ -sql "UPDATE bittner_belleprairiesec8_n40_boundary SET surveyor = 'Akash' WHERE gid = 1"

Conclusion

OGR/GDAL provides a powerful CLI for:

  • Inspecting spatial data
  • Reprojecting layers
  • Converting formats
  • Querying spatial datasets
  • Integrating Shapefiles with PostGIS

These commands are essential for GIS developers, data engineers, and backend developers working with spatial data.