Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

Many thanks

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

4 Answers 4

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.

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

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

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

 
discard

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.