0

I want to change the decimal digits of the raster layer extent in QGIS 3.4.3 with Python, so it ends with 0.5.

My code is as follows:

input = vlayer
output = "Directory"
extent = vlayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()
coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax)
out_raster = processing.run("gdal:rasterize", {'INPUT': input,'FIELD':'ID','BURN':0,'UNITS':1,'WIDTH':1,'HEIGHT':1,'EXTENT':coords,'NODATA':0,'OPTIONS':'TFW=yes','DATA_TYPE':5,'INIT':0,'INVERT':False,'OUTPUT':output+"vlayer.tif"})

When I ask for the coordinates of the extent with extent I get '-7081.840000,124283.820000,144044.740000,217761.860000' but I want them to end with 0.5. So in the end the extent is a little bit larger. The values of the extent may then be -7100.5,125000.5,145000.5,220000.5 Round does not work.

Is there a way in Python to change these values?

1

if you want something larger your must round down the xmin and ymin, and round up the xmax and ymax

first,

import math

to round up :

newxmax = math.ceil(xmax/100)*100 + 0.5

to round down

newxmin = math.floor(xmin/100)*100 + 0.5

As a remark, I don't understand why you add 0.5

  • Thanks for your help! It worked perfect! The reason why we add 0.5 is, because we have a lot of raster layers and when we want to calculate something, we need the same start variable. It is much more easier to handle, if all raster layers start with 0.5 and not one with 0.143 and the other with 0.567. – Helga F. Feb 4 at 8:00
  • I was asking because it seems simpler to use 220000 than 220000.5, especially if your rounding is done at hundreds, whichis already a large shift. And maybe I should force -0.5 for the mins, not + 0.5 – radouxju Feb 4 at 8:19
  • You are right, but my colleague, who do further processing with the files want it this way. Thanks for your advice, I appreciate it very much. – Helga F. Feb 4 at 8:47

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.