1

I am trying to call GDAL functionalities from the following script I made in the past.

import os, fnmatch
from subprocess import call
call("dir", shell=True)

# define the variables
inFolder= os.path.join ('D:/', 'UMBE', 'PhD', 'Guglielmin', 'Permafrost', 

'Alta_Valtellina', 'Clip_AltaValtellina', 'clip_sept2000')
outFolder= os.path.join ('D:/', 'UMBE', 'PhD', 'Guglielmin', 'Permafrost', 'Alta_Valtellina', 'permafrost_mapping', 'spectral/')
clip= os.path.join ('D:/', 'UMBE', 'PhD', 'Guglielmin', 'Permafrost', 'Alta_Valtellina', 'permafrost_mapping', 'RG_visible_parts.shp')

# path to a folder where to look for needed files
os.chdir (inFolder)

# define a list of raster within a specific path
def findRasters (path, filter):
    for root, dirs, files in os.walk(path, filter):
        for file in fnmatch.filter(files, filter):
            yield os.path.join (root, file)

# for loop-clip all the raster with a poligonal shp
for raster in findRasters (inFolder, '*_refl.tif'):
    (infilepath, infilename)= os.path.split (raster)
    print infilename
    outRaster= outFolder+ 'activeRG_'+ infilename[5:]
    print outRaster
    warp= 'gdalwarp -dstnodata 0 -q -cutline %s -crop_to_cutline -of GTiff %s %s' % (clip, raster, outRaster)
    call (warp) # this command is used to call external functionalities (in this case GDAL)

I am basically trying to batch clip some raster images with the same feature (a polgonal shapefile) using gdalwarp. I am receiving the following error:

Traceback (most recent call last):
  File "D:\UMBE\PhD\Guglielmin\Permafrost\Alta_Valtellina\permafrost_mapping\spectral\AltaValtellina_batchclip.py", line 26, in <module>
    call (warp) # this command is used to call external functionalities (in this case GDAL)
  File "C:\Python27\ArcGIS10.1\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\ArcGIS10.1\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\ArcGIS10.1\lib\subprocess.py", line 893, in _execute_child
    startupinfo)
WindowsError: [Error 2] Impossibile trovare il file specificato

I think this is due to the lack of GDAL variables in the Windows path. Moreover, I am using the default Python shell provided along with the ArcGIS installation. I tried to workaround this (possible) problem by starting the Pythonwin shell in the QGIS folder ('~\QGIS Brighton\apps\Python27\Lib\site-packages\pythonwin\pythonwin.exe'), but it does not open, saying "The application can not locate win32ui.pyd(or Python)(126). Impossible to find the specified module", although the "win32ui.pyd" lies in the same folder. I also tried to add some variables to the system path (e.g. 'C:\Program Files\QGIS Brighton\apps\Python27\Lib\site-packages\osgeo\'; 'C:\Program Files\QGIS Brighton\'), and creating and adding new variables (like 'GDAL_DATA') as explained here, but nothing...

  • 1
    Rather than trying to use the Python shell of ~\QGIS Brighton\apps\Python27\Lib\site-packages\pythonwin\pythonwin.exe, use directly the Python console of QGIS as a classic shell. You can then use the osgeo Python module and Python GDAL/OGR Cookbook:Clip a GeoTiff with Shapefile. – gene Dec 10 '14 at 21:10
  • 2
    I've had better success with subprocess.popen over call. The question is, does gdalwarp work on the command line if you were to type it in? – Michael Stimson Dec 10 '14 at 21:16
  • I would like to avoid using the internal console of QGIS (or other GIS), but thanks for the hint. If I type any command to the cmd (I am on windows OS), it does not recognize the command (e.g. "gdalinfo --version"), so that's why I suppose it's an issue of adding the right variables to the envirnment. – umbe1987 Dec 11 '14 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.