Ogr2ogr is the swiss-army knife when it comes to conversion of GIS data. It is part of the Geospatial Data Abstraction Library and provides an easy way to convert data between common storage formats: GeoJSONShapefile, PostGIS and others.

Ogr2ogr is available as part of the FW Tools Toolkit: http://fwtools.maptools.org/. It is usually called from the command line using the following syntax:

> ogr2ogr -f "file_format" destination_data source_data

 

Here are a few examples:

Convert GeoJSON to PostGIS:

> ogr2ogr -f "PostgreSQL" PG:"dbname=my_database user=postgres" "source_data.json"

If we want to specify a destination table, we can use the -nln table option. In order to append the new data to the already existing records, we can use the -append flag:

> ogr2ogr -f "PostgreSQL" PG:"dbname=my_database user=postgres" "source_data.json" -nln destination_table -append

 

Convert GeoJSON to ESRI Shapefile:

> ogr2ogr -f "ESRI Shapefile" destination_data.shp "source-data.json"

 

 Convert Shapefile to PostGIS:

> ogr2ogr.exe -f "PostgreSQL" PG:"dbname=my_database user=postgres" "source_data.shp" -skip-failures

 

 

6 thoughts on “Using ogr2ogr to convert data between GeoJSON, PostGIS and Esri Shapefile

  1. Joseph Spenner says:

    When trying to convert GeoJSON to PostGIS or GeoJSON to ESRI Shapefile, I get an error after several seconds:

    Killed

    Is there a timeout somewhere I can set? It’s a large JSON file.

    Thanks!

  2. Morphocode says:

    Hi,

    To convert a PostGIS table to a shapefile, you can use the following command:
    ogr2ogr -f “ESRI Shapefile” buildings_mn.shp “PG:host=localhost dbname=nyc user=postgres password=password” “buildings_mn”

    where “buildings_mn” is the name of your db table

    Regards

  3. Aurélie says:

    Bonjour,
    lorsque j’utilise la conversion GeoJson / Shapefile, je perd la projection que j’avais indiqué pour le geojson (par exemple 3944)
    Le shape sort en WGS84 quel que soit le code EPSG indiqué dans mon geojson.

    Y a t-il des librairies spécifiques à charger par ailleurs?
    Je travaille avec Spyder, python 2.7 et GDal.

    Je suis novice en code, j’ai repris le code d’un collègue que j’essaye de faire fonctionner sur mon poste. Chez lui ça fonctionne très bien et je ne comprend pas pourquoi !!

    Merci pour votre aide.

Leave a Reply

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