[GIS] Python GDAL: ogr.open or driver.open

gdalpython

I’ve been using GDAL with python to manipulate shapefiles. When opening a source file I first set the driver to “ESRI Shapefile” before calling the driver’s "Open" method with the required shapefile.

I have had no issues with this; however I am curious what the difference is between the Driver instance method “Open” and the ogr function “Open”. For example, what is the difference between:

driver = ogr.GetDriverByName("ESRI Shapefile")
source = driver.Open(“O:\myshp.shp”,0)

and

source = ogr.Open(“O:\myshp.shp”,0)

Is there a particular scenario where you would use one over the other?

Best Answer

If you specify a driver, OGR will only try to open your file with the specified driver.

If you don't specify it, OGR will try to open your file with all the drivers. It will loop over all the drivers until it finds a driver with that it can open your file. The order it tries to open them is the same order as listed in ogrinfo --formats.

See also this question: Force OGR to use specific driver for input format

Related Question