# Calculating the percent of binary values in grid of 1 km by 1 km across the entire raster

I have a raster of the entire united states with 30 meter by 30 meter pixel size. Each pixel has a value of either 0 or 1, with 1 defining a certain type of landcover.

I need to find the percent of 1's within 1 km by 1 km areas gridded across the entire raster so that I know for each 1 sq km, what percent of it is the landcover I'm interested in.

Despite a lot of googling, I am unsure about how to proceed. Mostly how to create the grid. It looks like once I have that I could use `v.rast.stats` in GRASS. But maybe I'm way off.

As a result, I don't have a specific error I'm encountering, but rather do not understand the tools well enough to know how to proceed to get my desired output.

• Jan 13, 2021 at 6:52

Since you mentioned GRASS in the question (although not in the tags), i can suggest an easy procedure. Your 30x30 raster is binary, so summing all pixels in a 1kmx1km grid will give you the number of desired landcover pixels in the coarse grid. You can do this by resampling in GRASS. If you then do

``````(sum/(33.3X33.3))*100
``````

divide by the total number of original pixels in each coarse grid pixel and multiply by 100, you'll have the percent.

Here's the code in GRASS: (assume your binary raster is called lc30, and you are using a projected CRS)

``````# First Set computation region to 30 meter landcover raster
g.region -ap rast=lc30
# reset the resolution to the coarse 1km resolution
g.region res=1000
# Resample to the coarse grid, using "sum"
r.resamp.stats input=lc30 output=lc1km method=sum
# and calculate a new map with percentage
r.mapcalc "lc1km_pcent = (lc1km / (33.3*33.3)) * 100.0"
``````
• Thanks. I only have experience with GRASS insofar as what is included in QGIS. It doesn't appear to have a g.region module, however I am looking into how I can add it. Jan 14, 2021 at 20:57
• I warmly recommend working with GRASS independent of QGIS first, to learn the concepts and modules. Then, after understanding what you need to do, you can more easily run the QGIS wrapper procedures. Jan 15, 2021 at 12:39
• Sadly my deadline is just about up, so I'm afraid I won't be able to spend the time to learn GRASS. But thank you for your help! Jan 16, 2021 at 1:30