Inpolygon for Georeference raster and polygone files.
14 ビュー (過去 30 日間)
古いコメントを表示
Hi there, I had a shapefile that imported in MATLAB by shaperead (the shapefile contains about 500 polygons, it is now a struct file & each row defines a polygone) and I have a raster which is georeferenced for the same area which I imported by geotiffread. I want to know the values (mean, sum, max) of the raster pixels that are located in each polygons. I have been suggested to use inpolygon, but I don't have any idea how can I put a my struct and raster and in the inpolygon function. Any idea is really appreciated.
0 件のコメント
回答 (1 件)
duilio fonseca
2019 年 8 月 19 日
編集済み: duilio fonseca
2019 年 8 月 19 日
Hi there, i have a solution,
% in my case i used lon and lat from NetCDF file, you can use the georefence data from your tiff.
%example
lon=-(77:0.05:65); %longitude in WGS84 for example with 0.05 degrees of resolution
lon=-(55:0.05:50); %longitude in WGS84 for example with 0.05 degrees of resolution
%Now do you have to craete a Mesh
[X,Y]=meshgrid(lon,lat);
points=[X(:),Y(:)]; %point of the grid
%Load your polygons
S = shaperead('yourfile.shp');
%Loading polygons (one by one)
for i=1:length(S)
%you can define yours polygons with some variable... or do it directly from Shape
polygons={S(i).X,S(i).Y}; %S(i).X and S(i).Y define the polygon "i" coordenates
%Index of point inside of each a polygon "i"
ind=inpolygon(points(:,1),points(:,2),polygons{:,2},polygons{:,3});
%here the function depends of you, in this example i used mean
mean_variable_polygon(i,:)=mean(your_raster_file(ind));
clear polygons ind
end
In other case, i recomend you use Zonal statistic extraction tool from Qgis.
2 件のコメント
Dylan Ruth
2022 年 7 月 17 日
I'm not the original poster, but seriously, thank you so much for this code! Worked like a charm for some work I'm doing with surface elevation DEMs.
参考
カテゴリ
Help Center および File Exchange で Map Display についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!