Loop through rasters and extract values using shapefile

5 views (last 30 days)
Hi,
I wanted to know if it is possible to loop through 50 raster files (.tif) and extract values from the rasters using a point shapefile and saving in a .csv

Answers (1)

Daksh
Daksh on 20 Dec 2022
It is my understanding that you wish to loop through .tif files in a folder, perform Raster to Point Shapefile conversion and save data in a .CSV or .XLS spreadsheet file. You can follow these steps and documentation links to proceed:
  • Use this link to iterate over files in current directory in path with a specific file type (.TIF) for our case. Refer to the following code instance for your case:
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.tif'); %gets all .TIF files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
num = importdata(fullFileName); %or readtable
% all of your actions go here
end
  • Use this link as reference for Raster to Point Shapefile conversion
  • Read through the documentation of MATLAB command: "writetable()" to write matrix data into a file (.CSV or .XLS for our case), something like:
% assuming M is your data matrix to write into file.csv
writematrix(M,'file.xls')
Hope it helps!

Community Treasure Hunt

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

Start Hunting!