Monday, 26 December 2016

raster - Clustering 30m pixels into 120m pixels, fishnet using QGIS?


I have a raster file representing tree cover loss made of 30m*30m pixels taking the value 0 or 1. 0 means no loss and 1 means tree cover loss. I want to cluster these pixels into bigger pixels of the size 120m*120m (i.e. 4 pixels by 4 pixels so 16 pixels in each cluster). I plan to do this for computational reasons as by doing so I reduce the number of observations by a factor of 16. My goal is to measure the share of 30m pixels with value 1 within each 120m cluster of pixels.


I am not sure how to do this.


Here is a solution I thought of:




  • On QGIS I thought of creating a fishnet of 4 by 4 pixels and then doing zonal statistics within each cell. BUT, I could not find how to create this 4 by 4 pixels grid AND I found no solution yet on how to match perfectly cell boundaries with pixel extent (in order to have exactly 16 pixels per fishnet cell, see red cells below).


enter image description here



Answer



So I found a second best solution to the question asked above.


Using R one can aggregate raster pixels by a factor, in my case a factor of 4, and then export the newly created raster. This raster can subsequently be used in QGIS.


Please note that this can take a very long time. My raster is a 80000*40000 pixels so aggregation takes about one hour per raster file.


library(raster)
DEM <- raster("/Users/.../input.tif")

dem_low <- aggregate(DEM, fact = 4, fun = mean)
writeRaster(dem_low,'/Users/output.tif',options=c('TFW=YES'))
rm(DEM,dem_low)

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