How do you calculate average value of area within a polygon shapefile?

9 views (last 30 days)
Hi all,
I have a high resolution data grid across the U.S.. I would like to find the average value of all data points within each U.S. county. I was thinking I could possibly derive this from a shapefile (.shp) of all the U.S. counties.
Any thoughts, leads, ideas would be greatly appreciated!
Ryan

Accepted Answer

Chad Greene
Chad Greene on 12 Feb 2016
The title of this question and the text of the question seem to be different. I'll try to answer both.
If you want the area of a polygon, use polyarea. If you have some large dataset and you want to know only the data points inside a polygon, use inpolygon to get the indices of all x,y values inside a polygon, then the mean of all z values in the polygon will be z(ind).
  3 Comments
Chad Greene
Chad Greene on 12 Feb 2016
Edited: Chad Greene on 24 Feb 2016
I think you're right on track. The inpolygon function will do exactly what you need and it will work in projected coordinates or geo coordinates. Just make sure your grid coordinates match the shapefile coordinates.
The shaperead function will return geo coordinates or projected coordinates if your shapefile has a georeferencing matrix, use the UseGeoCoords,true or UseGeoCoords,false option depending on which you prefer. You can also use mfwdtran or minvtran to transform the coordinates of your grid. There are lots of ways to skin this cat, just make sure your grid and shapefile coordinates are both geo coordinates or they're both projected coordinates.
If you use projected coordinates maybe your grid has these coordinates:
[X,Y] = meshgrid(1:1000,500:1000);
and the outline of a state is given by x and y. All the indices of your grid Z inside the outline of the state will be given by
ind = inpolygon(X,Y,x,y);
The mean of all Z values inside x,y is then
mean(Z(ind))
If your grid is referenced to LAT,LON it'll look like this:
[LON,LAT] = meshgrid(-120:-65,25:45);
ind = inpolygon(LON,LAT,lon,lat);
Ryan
Ryan on 24 Feb 2016
I think I see the light now! This makes sense and I will verify over the coming weeks. Your input is much appreciated, Chad. Thanks

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!