Skip to main content
14 votes

Using GeoPandas with rasterstats for zonal statistic gives error

For anyone else that stumbles upon this you can do: To get just one stat: gdf['mean'] = pd.DataFrame( zonal_stats( vectors=gdf['geometry'], raster='raster.tif', stats='...
jwilson8767's user avatar
11 votes
Accepted

COUNT vs. SUM field of zonal statistics as table output table

What you are looking for is COUNT, which is the frequency of the cells that you processed through Zonal Statistics. Sum, on the other hand, is the sum of cell values covered by your polygon. Overly ...
fatih_dur's user avatar
  • 4,993
11 votes
Accepted

How does QGIS Zonal Statistics handle partially overlapping pixels?

QGIS first makes an initial pass, checking to see if the center of each raster cell is within the polygon. If fewer than two cell centers are within the polygon, it performs a vector-based ...
dbaston's user avatar
  • 13.1k
9 votes
Accepted

Calculating statistics per area for categorical raster using R?

In R, you can extract the raster data for each polygon and then summarize it. First, lets create some data (FYI, you can read in a shapefile using raster::shapefile or rgdal::readOGR and a raster ...
Jeffrey Evans's user avatar
8 votes
Accepted

"Zonal" Statistics Using Line Features

For calculating stats from raster cells intersected by line, you can use GRASS v.rast.stats directly from QGIS processing toolbox. It can calculate 13 different stats.
Oto Kaláb's user avatar
  • 6,925
8 votes

Using ZonalStats in QGIS 3.0.2

If you wrote zonal in the Processing toolbox search, you will find many: The zonal Statistics is the second one from the top.
ahmadhanb's user avatar
  • 41.1k
7 votes

Robust Zonal stats in R and QGIS

QGIS and R's raster package use different methods to estimate zonal statistics. Briefly: QGIS compares the centroid of each raster cell to the polygon boundary, initially considering cells to be ...
dbaston's user avatar
  • 13.1k
7 votes
Accepted

Getting zonal statistics of polygons not touching center raster cells in PostGIS?

When pixels are large comparing with polygons you better go "vector way". i.e. vectorize the raster tiles and then procede to a vector/vector intersection/computation. If you use the PostGIS Addons, ...
Pierre Racine's user avatar
7 votes
Accepted

R zonal statistics for classes

You can get the frequency of a single class by passing a custom summary function to exact_extract. For example, to get the fraction of pixels that have a value of 1, you could run: exact_extract(rast, ...
dbaston's user avatar
  • 13.1k
7 votes
Accepted

QGIS zonal statistics only non zero values

Convert raster pixels with value 0 to nodata cells using Raster Calculator. Divide the raster's value by itself: a division by 0 will return nodata. For all other pixels, to keep the value as it is, ...
Babel's user avatar
  • 73.1k
6 votes

Extract raster value into polygon

Before using Zonal Statistics make sure that you have same projections for both raster data and vector (shapefile) data. Sometimes different projection produce empty results. In the following example, ...
ahmadhanb's user avatar
  • 41.1k
6 votes

Zonal Statistics of a polygon and assigning mean value to the polygon

There is a zonal statistics example in the Python GDAL/OGR Cookbook 1.0 documentation. This recipe calculates statistics on values of a raster within the zones of a vector dataset. It returns ...
Aaron's user avatar
  • 51.7k
6 votes
Accepted

Alternatives to Zonal Statistics which include partial pixels (QGIS)

I think you hit on your best option, which is to convert the raster to a vector and then intersect the result with your polygon layer. As a way of explanation regarding the frustration you're ...
rumski20's user avatar
  • 598
6 votes
Accepted

Zonal Histogram - Python

rasterstats zonal_stats with categorical=True. For example with pandas so you can manipulate the data as you like: import geopandas as gpd import pandas as pd from rasterstats import zonal_stats lots ...
Bera's user avatar
  • 74.7k
6 votes
Accepted

How to use zonal statistics in R with shapefile

As recommended raster::extract will accomplish what you are after. However, from a computational speed and modernization standpoint, I would recommend using the terra package, which is a replacement ...
Jeffrey Evans's user avatar
5 votes
Accepted

Robust Zonal stats in R and QGIS

A Robust Zonal stats in QGIS can be implemented with PyQGIS. Following code was run with your layers by using a filter to select only ID_sub.pat between 19 and 27 (showed in your image). import ...
xunilk's user avatar
  • 30k
5 votes

Some zones disappear when running Zonal Statistics as Table in ArcGIS

The Zonal Statistics as Table documentation states the following: If the areas of single features are similar to or smaller than the area of single cells in the value raster, in the feature-to-...
Aaron's user avatar
  • 51.7k
5 votes
Accepted

Zonal Statistics is returning null values in ArcGIS Desktop?

You could try resampling the raster to a finer resolution. Not sure why this works but accourding to the help section for version 10.3 (not present in 10.6): If the zone input is a feature dataset ...
Bera's user avatar
  • 74.7k
5 votes
Accepted

Zonal stats percentile values using R?

Here are two ways to do this. The raster extract function is the common approach. However, the zonal.stats function uses velox under the hood, which speeds things up considerably. You will not see ...
Jeffrey Evans's user avatar
5 votes
Accepted

QGIS zonal stats for vectors

You can use the 'Join attributes by location (summary)' tool in the processing toolbox. I would recommend using a projected CRS for both layers. You will need a field in your blue polygon layer ...
Ben W's user avatar
  • 21.8k
5 votes

Counting percentage of raster cell inside polygon using QGIS

To tool to use for this is Zonal histogram (see documentation). Set the raster layer and the polygon (red hached in the screenshot) as input and it calculates the no. of pixels inside the polygon for ...
Babel's user avatar
  • 73.1k
5 votes
Accepted

Reading values from file Geodatabase table and assigning those values to variable

Use da.SearchCursor(): import arcpy zonetable = r'C:\folder\data.gdb\zonstats' #Change fieldnames = ['field90', 'field75','field50'] #Change PER_90, PER_75, PER_50 = [row for row in arcpy.da....
Bera's user avatar
  • 74.7k
5 votes
Accepted

Unable to perform zonal stats in python because of Attribute Error

You probably have a null geometry, try filtering them out. import geopandas as gpd import pandas as pd from rasterstats import zonal_stats shp = 'vector.shp' ras = 'raster.tif' gdf = gpd.read_file(...
user2856's user avatar
  • 67.1k
5 votes

Zonal Statistics not executing in arcpy

You still have logical errors in your code. You list the feature classes and do nothing with the data. shapefiles = arcpy.ListFeatureClasses() You initialize an empty list and then try to iterate. ...
Mark Bryant's user avatar
  • 1,435
4 votes

Zonal statistics of polygon overlays in PostGIS

That could be the case of when you imported the Landsat raster to your database, you hopefully set a parameter -t 300x300, which splits the image in tiles of 300x300 pixels. So, imagine that you have ...
Deninhos's user avatar
  • 207
4 votes

Is there an open-source equivalent of ArcGIS "Zonal Statistics as Table" tool?

You can do this in R with the help of raster and rgdal library library(raster) library(rgdal) Load the shapefile and raster shp <- readOGR("shape file directory", "shapegile_name") in.ras <- ...
S. Thiyaku's user avatar
4 votes
Accepted

QGIS Python script for zonal statistics not running

I performed a quick test and it seems that you need to turn on the edit mode for obtaining the desired result. If you want to use Python for this, try to use the following code: polygonLayer....
mgri's user avatar
  • 16.3k
4 votes
Accepted

Extract the values from a raster based on polygons and add those values directly to the polygon shapefile table in ArcGIS?

If you look in the toolbox, exactly 1 tool below Zonal Statistics is a tool called Zonal Statistics as Table! This creates a table which you can then join back to your original polygon dataset.
Hornbydd's user avatar
  • 43.8k
4 votes

How does Zonal Statistics work exactly?

Update as April 2018 and QGIS 2.18 The current version of Zonal Statistics Plugin can provide: Count: to count the number of pixels Sum: to sum the pixel values Mean: to get the mean of pixel values ...
whyzar's user avatar
  • 12.1k
4 votes

Some zones disappear when running Zonal Statistics as Table in ArcGIS

I had the same problem. spent a lot of time discussing it with ESRI support and the end resolution was that the code behind zonal stats looks for the center of the pixel to be contained by the ...
Zipper1365's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible