Using ogr2ogr (Python) to convert between GIS format

This guide is working in progress. More example will be added.

ogr2ogr utility that is a part of the GDAL (Geospatial Data Abstraction Library). It is a tool that can convert between different GIS format. It is a command line tool which means you have to write shell commands to use it. However, once you are familiar with the shell syntax. It can be used within python to convert between file type.

Open your terminal and type in the following. The output should look similar to this.

ogr2ogr --version
// GDAL 2.2.4, released 2018/03/19

If the output informs you that ogr2ogr is not available. You will need to install gdal on your computer to continue.

conda install -c conda-forge gdal

Convert simple file to simple file

If the input file is a simple file like Shapefile or GeoJSON, GDAL knows the input file type and the only parameter you need to supply to ogr2ogr is the output format. The following code converts a shapefile into a GeoJSON file but it can be adapted to work with a different file type. Here is a list of drivers that work with GDAL.

The Python code is not actually running any native Python library to do the conversion. It is calling the command line ogr2ogr for the heavy lifting. There are other libraries out there that can do the conversion such as fiona which is a Python wrapper for OGR/GDAL. If the purpose is not to do any data manipulation in Python, this method will be enough.

Command Line

ogr2ogr -f GeoJSON output.geojson input.shp

Python

import subprocess

subprocess.check_output(['ogr2ogr', '-f', 'GeoJSON', 'output.geojson', 'input.shp'])
  • -f specifies the output file format
  • output.geojson is the path/name of the output file
  • input.shp is the path/name of the input shapefile

Convert simple file to PostGIS

The documentation for working with PostgreSQL in GDAL is here. The following command will convert a GeoJSON file into a new table in PostGIS database in the ‘boundary’ schema. The same command will work with converting a shapefile into a PostGIS table. If you want to learn about how to set up a PostGIS database locally, you can follow this guide.

  • PG: is a string for that sets the connection option. You can find other option is the link above
  • lco: is the ‘Layer Creation Option‘ , this option lets you define how the layer is created, more option can be found in the link above.

Command Line

Make sure the schema already exist in the PostGIS database.

ogr2ogr -f PostgreSQL PG:'host=localhost port=5433 dbname=db user=user password=password' -lco SCHEMA=boundary chicago_boundaries.geojson

Python

import subprocess

subprocess.check_output([
    'ogr2ogr',
    '-f',
    'PostgreSQL',
    "PG:host='{}' port={} dbname='{}' user='{} password='{}'".format('localhost', 5433, 'db', 'user', 'password'),
    '-lco',
    "SCHEMA={}".format('boundary'),
    'chicago_boundaries.geojson'
])

Convert PostGIS records into a simple file

This simple example connects to the PostGIS database and run a SQL query. The result is converted to lat_long_racks.geojson file.

  • -sql: allows you to run the SQL commands you desire for the export.

Command Line

ogr2ogr -f GeoJSON lat_long_racks.geojson PG:'host=localhost port=5433 dbname=db user=user password=password' -sql 'SELECT latitude, longitude, ward, geom FROM bicycle.chicago_rack'

Python

subprocess.check_output([
    'ogr2ogr',
    '-f',
    'GeoJSON',
    'lat_long_racks.geojson',
    "PG:host='{}' port={} dbname='{}' user='{}' password='{}'".format('localhost', 5433, 'db', 'user', 'password'),
    '-sql',
    'SELECT latitude, longitude, ward, geom FROM bicycle.chicago_rack'
])

More examples to come. Leave a comment below on what you would like to see added.

Share the knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *