- https://www.mathworks.com/help/map/ref/shaperead.html
- https://www.mathworks.com/help/map/ref/readgeoraster.html
- https://www.mathworks.com/help/matlab/ref/inpolygon.html
Extracting Maximum pixel value from a raster (Canopy Height Model) for multiple polygons in a shapefile
3 views (last 30 days)
Show older comments
I have a shape file with approx 600 polygon representing each tree and i have generated canopy height model using DSM-DTM. Now I have to extract maximum pixel value in each polygon of the shape file.
0 Comments
Answers (1)
Paras Gupta
on 14 Sep 2023
Hello,
I understand that you want to extract the maximum pixel value for each polygon in a shape file using a Canopy Height Model (CHM) raster. Please refer to the code below to achieve the same.
% Load the shapefile (shp file) and the CHM (tif file) into MATLAB
shapefile = shaperead('your_shapefile.shp');
CHM = readgeoraster('your_CHM.tif');
numPolygons = numel(shapefile);
% Iterates over each polygon
for i = 1:numPolygons
polygon = shapefile(i).Geometry;
% Create a mask to identify the pixels within the polygon
mask = inpolygon(CHM.X, CHM.Y, polygon(:,1), polygon(:,2));
% Extract the corresponding pixel values from the CHM
values = CHM.Z(mask);
% Calculate the maximum value using the max function
maxPixelValue = max(values);
disp(['Maximum pixel value for polygon ', num2str(i), ': ', num2str(maxPixelValue)]);
end
The above code assumes that coordinate systems of the shapefile and CHM are the same, and that the CHM is in the GeoTIFF format. Please refer to the following documentations for more information on the functions used in the code:
Hope this helps.
0 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!