-1

I have written a PyQGIS script that uses gdals warp. The piece of code doing this for me is as follows:

warp = 'gdalwarp -ot Byte -q -of GTiff -tr 2.81932541777e-05 -2.81932541777e-05 -tap -cutline %s -crop_to_cutline -co COMPRESS=DEFLATE -co PREDICTOR=1 -co ZLEVEL=6 -wo OPTIMIZE_SIZE=TRUE %s %s' % (instrv, ('"' + pathsplitedit + '"'), outputpath2)                                                                          
call (warp)   

So I have this in a loop and all is good. However each time it executes a new command window is opened, which isn't ideal as it loops through up 100 features in a shapefile. Is there a way I can not have the command window open at all? Any help is really appreciated!!

  • `call (warp)` does not look like python. – Stop harming Monica Mar 06 '17 at 09:01
  • it originates from `from subprocess import call`. Its an easier way of calling the gdal function. As far as I know it is python http://chris35wills.github.io/subprocess_gdal/. So I'd appreciate if you could remove the down vote on my question – Michael Alan Cohen Mar 06 '17 at 09:53
  • The question is unclear without that detail and votes can't be changed unless the question is edited. If it's about `subprocess.call` then it looks like this question has been asked and answered [before](http://stackoverflow.com/q/1813872/2142055) (`call` just wraps `Popen`). – Stop harming Monica Mar 06 '17 at 12:49

1 Answers1

2

Since GDAL 2.1 (which recent versions of QGIS use) you can access the command line utilities from the bindings itself, which has a lot of benefits.

Your call becomes something like this, notice that i didn't copy all your creation options, its just to give you an idea on how to use it.

warpopts = gdal.WarpOptions(outputType=gdal.GDT_Byte, 
                            format='GTiff', 
                            xRes=2.81932541777e-05,
                            yRes=-2.81932541777e-05, 
                            cutlineDSName='cutline_vec.shp',
                            cropToCutline=True, 
                            targetAlignedPixels=True, 
                            options=['COMPRESS=DEFLATE'])

ds = gdal.Warp('raster_out.tif', 'raster_in.tif', options=warpopts)
ds = None

One of the benefits is that the input files don't have to be on-disk but can also be opened gdal/ogr Datasets. The gdal.Warp cmd also returns the output file as an opened Dataset, which you can then pass on to other commands.

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97