[GIS] OGR2OGR not working in gdaltools module

convertgeojsonogr2ogrpythonshapefile

I'm trying to convert a bunch of shapefiles into geoJSON and found what looks like an easy way to do it but I can't get the code working. It doesn't matter if I run the code in OSGeo4W or just via idle I get the same error. (I do have OSGeo4W added to the system path.) My code and error are below. Could I get some guidance on what might be causing this?

import gdaltools

layer = <myFilePath>

ogr = gdaltools.ogr2ogr()
ogr.set_encoding("UTF-8")
ogr.set_input(layer, srs="EPSG:4326")
ogr.set_output('layer.geojson')
ogr.execute()

And the error:

Traceback (most recent call last):
  File "C:\Users\JOC-001\Desktop\Python Scripts\Random\Folium\GeoJSON\Con2GeoJSON.py", line 9, in <module>
    ogr.execute()
  File "C:\Python27\ArcGIS10.2\lib\site-packages\gdaltools\ogr2ogrcmd.py", line 289, in execute
    return self._do_execute(args)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\gdaltools\basetypes.py", line 90, in _do_execute
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1)
  File "C:\Python27\ArcGIS10.2\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\ArcGIS10.2\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Best Answer

Pygdaltools use the subprocess module to execute directly the GDAL commands. The module must therefore know the exact path of the folder where they are (where are gdalinfo, ogrinfo, ...).

The configuration of the module lets do it:

import gdaltools
gdaltools.Wrapper.BASEPATH = "C/Program Files/Gdal/bin"

In this example, the command used by the module with subprocess are "C/Program Files/Gdal/bin/ogrinfo (ogrinfo.exe in Windows ?)", etc...