Sunday 22 October 2017

Unable to call GDAL functionalities from Python


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
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...




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...