Spatial Analyst - calculate median raster values within polygons

13380
5
Jump to solution
04-06-2010 07:29 PM
LeahHutchins
New Contributor
Hi Everyone,

I have a floating point elevation raster and a polygon shapefile representing vegetation classes. All I need to do is calculate the median elevation within each vegetation polygon. I thought the Zonal Statistics tool in Spatial Analyst would do this, but when i add my two layers, it removes the 'median' option from the 'Statistics type' field.

Is there a way to accurately calculate the median raster value based on polygons? Does my elevation raster have to be an integer for this to work?

I'm sorry if this has been addressed before, I'm still getting used to the new forum layout...

Thanks in advance!

Cheers,
Leah
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor
it removes the 'median' option from the 'Statistics type' field.


Hi Leah,

It is true that some statistical functions are not available with floating data. These are; MEDIAN, MAJORITY, MINORITY and VARIETY. Those functions require a histogram and this is not present with floating data.

To avoid this you could convert your data to Integer. In order to reduce loss of precision you could multiply your raster with for instance 100 (e.g. express it in cm instead of m), perform the Zonal Statistics and divide the result with the same factor (e.g. 100). In Python this could look like:

import arcpy
from arcpy import env
from arcpy.sa import *
YourOutputRaster = Divide(Float(ZonalStatistics("YourZoneRaster","Value",Int(Times("YourFloatingDataRaster",100)),"MEDIAN","DATA")),100)


Kind regards,

View solution in original post

0 Kudos
5 Replies
XanderBakker
Esri Esteemed Contributor
it removes the 'median' option from the 'Statistics type' field.


Hi Leah,

It is true that some statistical functions are not available with floating data. These are; MEDIAN, MAJORITY, MINORITY and VARIETY. Those functions require a histogram and this is not present with floating data.

To avoid this you could convert your data to Integer. In order to reduce loss of precision you could multiply your raster with for instance 100 (e.g. express it in cm instead of m), perform the Zonal Statistics and divide the result with the same factor (e.g. 100). In Python this could look like:

import arcpy
from arcpy import env
from arcpy.sa import *
YourOutputRaster = Divide(Float(ZonalStatistics("YourZoneRaster","Value",Int(Times("YourFloatingDataRaster",100)),"MEDIAN","DATA")),100)


Kind regards,
0 Kudos
LeahHutchins
New Contributor
Xander, thank you so much! That worked perfectly.
0 Kudos
marianaaraes
New Contributor
I'm sorry, but I don't undestrand something.. how do you calculate the mean with one raster and one polygon layer? I want to do something alike but I can't. When I click in "cell statistics", it only allows me to add raster layers...
0 Kudos
TarunJaiswal
Esri Contributor
I'm sorry, but I don't undestrand something.. how do you calculate the mean with one raster and one polygon layer? I want to do something alike but I can't. When I click in "cell statistics", it only allows me to add raster layers...


Hi,

Leah, is using and referring to Zonal Statistics tool under Zonal toolset of Spatial Analyst. Please read usage section of Zonal Statistics tool for more information.

Thank you.
0 Kudos
MarkBoucher
Regular Contributor II

The experts can correct me if I'm wrong, but another approach is to use Zonal Statistics as Table. This creates a table with all the stats in it.

Process:

  1. Run Zonal Statistics as Table choosing a unique field in the polygon shapefile (FID works).
  2. the table with the statistics is added to the mxd.
  3. join the table to the polygon shapefile using the filed used in step 1.
  4. transfer the mean value to a field you created in the polygon shapefile using the field calculator.
  5. remove the join if you want.
0 Kudos