Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
what errors are you getting? and what is your GDAL version? pls share for helping to you.. – Aragon Nov 1 '12 at 14:42

Grab a copy of the ogr2ogr Python port, which is distributed with the GDAL source code download or can be found here:

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"])
share|improve this answer
This is the only working example that I've seen anywhere. – bozdoz Mar 25 '14 at 17:15

You will probably find much better answers than I can give you at and

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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