I know Esri shapefile can be converted into GeoJSON using Python and GDAL/OGR. Following command works on the terminal.

ogr2ogr -f GeoJSON -t_srs crs:84 outfile.geojson inputfile.shp

I want to do the conversion in a python script so that I can convert multiple files inside a loop. And combine it in my pipeline script.

Is there any way to do this conversion in python script? Can this terminal command be used in a python script?


1 Answer 1


This can be used using GDAL. First, install GDAL for your OS. You can convert files from the terminal by using following command:

#ogr2ogr -f GeoJSON -t_srs crs:84 [output_filename].geojson [input_filename].shp

If you want to use python script then by calling subprocess, this can be achieved. Python script example is given below:

#Convert Esri .shp file into .geoJson file

import subprocess

input_shp = 'inputfile.shp'
output_geoJson = 'outputfile.geojson'
cmd = "ogr2ogr -f GeoJSON -t_srs crs:84 "  + output_geoJson +" " + input_shp
subprocess.call(cmd , shell=True)    
  • 4
    No need for subprocess. You can use gdal.VectorTranslate directly.
    – user2856
    Oct 2, 2019 at 22:24
  • 3
    The help for subprocess warns against using call with shell=True, use Popen like pcs=subprocess.Popen(['ogr2ogr','-f','GeoJSON','-t_srs','crs:84',output_geoJson,input_shp]) then pcs.wait() to wait until the process finishes (optional). Beware of spaces in path names, if there is even a slim possibility of a space it's safer to use '"{}"'.format(output_geoJson) as an example to quote the element for the command processor - paths that don't have spaces but are still quoted are not a problem for the operating system. Oct 2, 2019 at 23:47
  • @user2856 Thanks, gdal.VectorTranslate will be cleaner. Oct 3, 2019 at 17:54
  • @MichaelStimson Thanks, I will try Popen. I wasn't aware of it. Thanks for the tips. Oct 3, 2019 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.