2

I am clipping a raster by extents from the python console in QGIS by using the following code :

from processing.tools import *  
general.runalg("gdalogr:cliprasterbyextent","E:/Sreeraj/Task/Split/Extract_agri.tif","","-180.0,0.0,0.0,90.0",5,4,75,6,1,False,0,False,"","E:/Sreeraj/Task/Split/output/1.tif")

Here, I am giving extents as minimum_longitude=180.0, minimum_latitude=0.0, maximim_longitude=0.0, and maximum_latitude=90.0.

Now, I want to do a clipping using processing.runalg in QGIS python console, where I will use a for loop for clipping 4 output .tif files as a batch process.

For doing this batch process of clipping, I tried the given below code:

output_location = "E:/Sreeraj/Task/Split/output/"
output file = 1
for long in range(-180,0,5):
  longmax = long + 5
  for lat in range(0,90,5):
    latmax = lat + 5
    extents = "long,lat,longmax,latmax" 
    output = str(output_location) + str(output file) + ".tif" 
    general.runalg("gdalogr:cliprasterbyextent","E:/Sreeraj/Task Global Data CONVERSION/Split/Extract_agri.TIF","",extents,5,4,75,6,1,False,0,False,"",output)
    output file = output file + 1

Unfortunately, I am getting error message as :

"Error: Wrong parameter value: extents" 

Instead of directly giving the extent values like "-180.0,0.0,0.0,90.0" inside general.runalg(), how can I give "extents" (if extents = "-180,0,0,90").

I think that the issue is with :

extents = "long,lat,longmax,latmax"  

When I do like this :

extents = "-180.0,0.0,0.0,90.0"
general.runalg("gdalogr:cliprasterbyextent","E:/Sreeraj/Task Global Data CONVERSION/Split/Extract_agri.TIF","",extents,5,4,75,6,1,False,0,False,"",output) 

then the code is working.

But, if I do like this :

long = -180.0 
lat = 0.0
longmax = 0.0 
latmax = 90.0  
extents = "long,lat,longmax,latmax"
general.runalg("gdalogr:cliprasterbyextent","E:/Sreeraj/Task Global Data CONVERSION/Split/Extract_agri.TIF","",extents,5,4,75,6,1,False,0,False,"",output)  

then I am getting the error.

So, clearly, the issue is with :

extents = "longmin,longmax,latmin,latmax"  

Please help me to solve this error.

0

You put your variables inside single quotation marks which means whatever is inside those quotes will be read as it is written. So if you type extents, it would show up as:

'long,lat,longmax,latmax'

Remove the quotes and you should get something like:

(-180.0, 0.0, 0.0, 90.0)

This would be closer to what you are looking for but I'm not sure if tuples are accepted as a parameter for the extent (tuples are a sequence of objects inside brackets).


Also, the extent parameters are given by (or at least the last time I checked):

xmin, xmax, ymin, ymax

So in your case it would be:

long, longmax, lat, latmax

And if you want to see this as an extent parameter, you could use:

extents = "%f,%f,%f,%f"% (long, longmax, lat, latmax)

So your final code could look like (note that I change long to lon as the former is used by Python; and output file to output_file):

output_location = "E:/Sreeraj/Task/Split/output/"
output_file = 1
for lon in range(-180,0,5):
    longmax = lon + 5
    for lat in range(0,90,5):
        latmax = lat + 5
        extents = "%f,%f,%f,%f"% (lon, longmax, lat, latmax)
        output = str(output_location) + str(output_file) + ".tif" 
        general.runalg("gdalogr:cliprasterbyextent","E:/Sreeraj/Task Global Data CONVERSION/Split/Extract_agri.TIF","",extents,5,4,75,6,1,False,0,False,"",output)
        output_file = output_file + 1
  • 2
    Perfectly working solution to this issue. extents = "%f,%f,%f,%f"% (lon, longmax, lat, latmax) Thank You. – Sreeraj Jan 9 '18 at 11:15
  • 1
    @Sreeraj - Most welcome, glad it helped :) – Joseph Jan 9 '18 at 11:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.