How do you calculate average value of area within a polygon shapefile?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Chad Greene
2016-2-12
The title of this question and the text of the question seem to be different. I'll try to answer both.
3 个评论
Chad Greene
2016-2-12
编辑:Chad Greene
2016-2-24
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);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!