30

I am trying to convert a GML to an ESRI shapefile using ogr2ogr utility in a python script.

I have successfully installed the GDAL/OGR package via osgeo but I am now struggling to find/understand any details on the syntax for using ogr2ogr in python.

After importing ogr, all I have found is ogr2ogr -f "ESRI Shapefile" output.shp input.gml

How do I use this in python? Is it as simple as assigning the input and output files?

Everything I try I just get syntax errors. Any pointers in the right direction to get me started would be great.

1
  • what errors are you getting? and what is your GDAL version? pls share for helping to you..
    – urcm
    Commented Nov 1, 2012 at 14:42

6 Answers 6

41

Grab a copy of the ogr2ogr Python port, which is distributed with the GDAL source code download or can be found here: http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/ogr2ogr.py

Once you import that into your code, you can use it like this:

import ogr2ogr

def main():
  #note: main is expecting sys.argv, where the first argument is the script name
  #so, the argument indices in the array need to be offset by 1
  ogr2ogr.main(["","-f", "KML", "out.kml", "data/san_andres_y_providencia_administrative.shp"])
2
  • This is the only working example that I've seen anywhere.
    – bozdoz
    Commented Mar 25, 2014 at 17:15
  • 1
    I don't understand, how should I install ogr or GDAL to use it instead of downloading a single py file?
    – jokoon
    Commented Jun 24, 2020 at 18:40
5

You will probably find much better answers than I can give you at http://www.gis.usu.edu/~chrisg/python/2009/lectures/ospy_slides1.pdf and http://trac.osgeo.org/gdal/wiki/GdalOgrInPython.

Try there or another tutorial linked from the second URL and maybe ask another more specific question if you are stuck.

5

Read up on os.system or os.popen or subprocess. Or just use ogr2ogr and your shell. Even cmd.exe (Windows) lets you loop over input files.

5

Eric Levine's Answer works, however the linked ogr2ogr.py is a bit outdated (file says 2013). For e.g. the flag "-nlt CONVERT_TO_LINEAR" is not implemented.

Instead of using ogr2ogr (command line tool) you can use gdal.VectorTranslate() with the same options of ogr2ogr. It is the same as ogr2ogr only implemented as proper python library (check Even Rouoalt's Blog post about it here).

In your example:

from osgeo import gdal

gdal.VectorTranslate(
    'output.shp',
    'input.gml',
    options='-f "ESRI Shapefile"'
)

Find further explanation & different syntax in this answer

1

A detailed explanation how to substitute the ogr2ogr command line tool with the OGR Python API can be found here.

0

I found it simpler to use OGR CopyLayer which I found here.

ds = ogr.GetDriverByName("GPKG").CreateDataSource(self.output_path)
sf1 = ogr.Open(src_ds)
sf_lyr1 = sf1.GetLayerByIndex(0)
ds.CopyLayer(sf_lyr1, self.output_fc_name, [])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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