Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
Map
Python
QGIS
Uncategorized

ogr2ogr basics Cheat sheet

What is ogr2ogr

ogr2ogr is a powerful command-line tool for converting and processing geospatial data between different formats, and performing a wide range of data processing tasks. Some of the things you can do with ogr2ogr include:

  • Converting between different geospatial file formats, such as Shapefile, GeoJSON, KML, GML, and many others
  • Extracting subsets of data based on spatial and attribute queries
  • Merging multiple datasets into a single file
  • Performing spatial operations, such as buffering, clipping, and intersecting
  • Calculating new attributes based on mathematical expressions or spatial relationships
  • Simplifying or resampling geometry
  • Generating centroids, convex hulls, or other spatial features
  • Working with data from databases such as PostgreSQL, MySQL, and SQLite

However, it does require some familiarity with command-line interfaces and geospatial data formats to use effectively. The GDAL website provides comprehensive documentation for ogr2ogr, including usage examples and tutorials.

Want to stay ahead of the geospatial curve? Listen to our podcast!

Here are some examples of what can be done with ogr2ogr

  1. To convert a file to a different format with ogr2ogr, specify the output file format using the -f option, followed by the name of the output file. For example, to convert a Shapefile to a GeoJSON file, you could use the command ogr2ogr -f GeoJSON output.json input.shp.
  2. To transform the projection of a file with ogr2ogr, use the -t_srs option to specify the target coordinate reference system (CRS). For example, to transform a Shapefile from WGS84 to Web Mercator, you could use the command ogr2ogr -t_srs EPSG:3857 output.shp input.shp.
  3. To clip or subset a file with ogr2ogr, use the -spat option to specify a bounding box or the -clipsrc option to specify a clipping polygon. For example, to clip a Shapefile to a bounding box, you could use the command ogr2ogr -spat xmin ymin xmax ymax output.shp input.shp.
  4. To merge multiple files with ogr2ogr, specify the output file name, followed by the names of the input files. For example, to merge two Shapefiles, you could use the command ogr2ogr output.shp input1.shp input2.shp.
  5. To convert a directory of files with ogr2ogr, you can use a loop that runs the ogr2ogr command for each file in the directory. For example, in a Bash script, you could use a command like for file in *.shp; do ogr2ogr -f GeoJSON “${file%.shp}.json” “$file”; done.
  6. To convert a CSV file to a Shapefile with ogr2ogr, you can use the -oo X_POSSIBLE_NAMES and -oo Y_POSSIBLE_NAMES options to specify the column names for the X and Y coordinates. For example, you could use the command ogr2ogr -f “ESRI Shapefile” output.shp input.csv -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat.
  7. To convert a GeoJSON file to a KML file with ogr2ogr, you can use the -f KML option to specify the output format. For example, you could use the command ogr2ogr -f KML output.kml input.json.
  8. To simplify the geometry of a file with ogr2ogr, you can use the -simplify option to specify a tolerance value. For example, you could use the command ogr2ogr -simplify 0.01 output.shp input.shp to simplify the geometry of a Shapefile.
  9. To extract a subset of columns from a file with ogr2ogr, you can use the -sql option to specify a SQL query that selects the desired columns. For example, you could use the command ogr2ogr -f GeoJSON output.json input.shp -sql “SELECT col1, col2 FROM input” to extract the “col1” and “col2” columns from a Shapefile.
  10. To convert a GeoPackage file to a PostGIS database with ogr2ogr, you can use the -f PostgreSQL option to specify the output format, and provide the connection string to the target database. For example, you could use the command ogr2ogr -f PostgreSQL PG:”dbname=mydb user=myuser password=mypassword host=myhost” input.gpkg to load the GeoPackage data into a PostGIS database.
  11. To convert a MapInfo TAB file to a GeoJSON file with ogr2ogr, you can use the -f GeoJSON option to specify the output format. For example, you could use the command ogr2ogr -f GeoJSON output.json input.tab to convert a MapInfo TAB file to a GeoJSON file.
  12. To filter features based on an attribute value with ogr2ogr, you can use the -where option to specify a SQL query that selects the desired features. For example, you could use the command `ogr2ogr -f GeoJSON output.json (input.shp -where “name=’New York'”)` to select only the features with a “name” attribute equal to “New York” in a Shapefile.
  13. To convert a DXF file to a GeoJSON file with ogr2ogr, you can use the -f GeoJSON option to specify the output format. For example, you could use the command ogr2ogr -f GeoJSON output.json input.dxf to convert a DXF file to a GeoJSON file.
  14. Convert a PostGIS table to a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp PG:”host=myhost user=myuser password=mypassword dbname=mydb” -sql “SELECT * FROM mytable”
  15. Merge multiple GeoJSON files into a single file: ogr2ogr -f GeoJSON output.json input1.json && ogr2ogr -update -append output.json input2.json -nln output && ogr2ogr -update -append output.json input3.json -nln output
  16. Convert a KML file to a GeoJSON file: ogr2ogr -f GeoJSON output.json input.kml
  17. Convert a GeoJSON file to a Shapefile with custom attributes: ogr2ogr -f “ESRI Shapefile” output.shp input.json -sql “SELECT *, CAST(‘MyValue’ AS CHARACTER(10)) as MyAttribute FROM input”
  18. Filter features based on a spatial relationship: ogr2ogr -f GeoJSON output.json input.shp -clipsrc xmin ymin xmax ymax
  19. Convert a GeoPackage file to a CSV file: ogr2ogr -f CSV output.csv input.gpkg
  20. Merge multiple Shapefiles into a single file: ogr2ogr -f “ESRI Shapefile” output.shp input1.shp && ogr2ogr -update -append output.shp input2.shp -nln output && ogr2ogr -update -append output.shp input3.shp -nln output
  21. Convert a DWG file to a GeoJSON file: ogr2ogr -f GeoJSON output.json input.dwg
  22. Convert a TAB file to a KML file: ogr2ogr -f KML output.kml input.tab
  23. Convert a Shapefile to a PostgreSQL table: ogr2ogr -f PostgreSQL PG:”host=myhost user=myuser password=mypassword dbname=mydb” input.shp -nln mytable
  24. Convert a CSV file to a GeoJSON file with custom attribute names: ogr2ogr -f GeoJSON output.json input.csv -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -oo KEEP_GEOM_COLUMNS=NO
  25. Convert a GeoPackage file to a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.gpkg
  26. Simplify the geometry of a GeoJSON file: ogr2ogr -f GeoJSON output.json input.json -simplify 0.01
  27. Convert a PostGIS table to a GeoJSON file: ogr2ogr -f GeoJSON output.json PG:”host=myhost user=myuser password=mypassword dbname=mydb” -sql “SELECT * FROM mytable”
  28. Calculate a new attribute based on a mathematical expression: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -sql “SELECT *, field1 + field2 AS newfield FROM input”
  29. Extract only the first n features from a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -limit n
  30. Convert a CSV file to a PostGIS table: ogr2ogr -f PostgreSQL PG:”host=myhost user=myuser password=mypassword dbname=mydb” input.csv -nln mytable
  31. Merge multiple KML files into a single file: ogr2ogr -f KML output.kml input1.kml && ogr2ogr -update -append output.kml input2.kml -nln output && ogr2ogr -update -append output.kml input3.kml -nln output
  32. Filter features based on their spatial relation to another layer: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -clipsrc input2.shp
  33. Convert a GPX file to a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.gpx
  34. Convert a GeoPackage file to a CSV file: ogr2ogr -f CSV output.csv input.gpkg
  35. Merge multiple GeoJSON files into a single file and remove duplicates: ogr2ogr -f GeoJSON output.json input1.json && ogr2ogr -update -append -skipfailures -dialect sqlite -sql “SELECT DISTINCT * FROM input2” output.json input2.json && ogr2ogr -update -append -skipfailures -dialect sqlite -sql “SELECT DISTINCT * FROM input3” output.json input3.json
  36. Convert a Shapefile to a GeoJSON file and include only specified attributes: ogr2ogr -f GeoJSON output.json input.shp -select field1,field2,field3
  37. Convert a GeoJSON file to a KML file: ogr2ogr -f KML output.kml input.json
  38. Split a Shapefile into multiple smaller Shapefiles: ogr2ogr -f “ESRI Shapefile” -explodecollections -sql “SELECT * FROM input” output_folder input.shp
  39. Generate a centroid point for each polygon in a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -dialect sqlite -sql “SELECT ST_Centroid(geometry), * FROM input”
  40. Convert a GeoPackage file to a GeoJSON file: ogr2ogr -f GeoJSON output.json input.gpkg
  41. Convert a CSV file to a Shapefile and specify the geometry type: ogr2ogr -f “ESRI Shapefile” output.shp input.csv -oo GEOM_TYPE=LineString -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -oo KEEP_GEOM_COLUMNS=NO
  42. Convert a Shapefile to a GeoJSON file and remove null geometries: ogr2ogr -f GeoJSON output.json input.shp -where “ST_IsValid(geometry)”
  43. Convert a KML file to a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.kml
  44. Filter features based on an attribute value: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -sql “SELECT * FROM input WHERE attribute=’value'”
  45. Convert a GML file to a GeoJSON file: ogr2ogr -f GeoJSON output.json input.gml
  46. Add a new attribute to a Shapefile and set its value to a constant: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -sql “ALTER TABLE input ADD COLUMN newfield VARCHAR(50) DEFAULT ‘value'”
  47. Generate a buffer around each feature in a Shapefile: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -dialect sqlite -sql “SELECT ST_Buffer(geometry, 100), * FROM input”
  48. Convert a Shapefile to a PostGIS table: ogr2ogr -f PostgreSQL PG:”host=myhost user=myuser password=mypassword dbname=mydb” input.shp -nln mytable
  49. Extract a subset of a Shapefile based on a spatial query: ogr2ogr -f “ESRI Shapefile” output.shp input.shp -sql “SELECT * FROM input WHERE ST_Contains(geometry, ST_GeomFromText(‘POLYGON((lon1 lat1,lon2 lat2,lon3 lat3,lon4 lat4,lon1 lat1))’))”
  50. Merge multiple Shapefiles into a single file and remove duplicates: ogr2ogr -f “ESRI Shapefile” output.shp input1.shp && ogr2ogr -update -append -skipfailures -dialect sqlite -sql “SELECT DISTINCT * FROM input2” output.shp input2.shp && ogr2ogr -update -append -skipfailures -dialect sqlite -sql “SELECT DISTINCT * FROM input3” output.shp input3.shp
  51. Convert a DXF file to a GeoJSON file: ogr2ogr -f GeoJSON output.json input.dxf
  52. Convert a GeoJSON file to a PostGIS table: ogr2ogr -f PostgreSQL PG:”host=myhost user=myuser password=mypassword dbname=mydb” input.json -nln mytable
About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.