masking geotiff Landsat data using shapefile

11 vues (au cours des 30 derniers jours)
chant of rain
chant of rain le 26 Jan 2018
I have Landsat Image and shape file , I want to masking this data using a shape file but getting error because my (data model type projected not geographic) So the mask hallways contain zeros... I will write my code and Images of references.
File_Name = 'LC08_L1TP_01_T1_B4.TIF';
[I,R1]=geotiffread(File_Name);
R2 = geotiffinfo(File_Name);
[x,y] = pixcenters(R2);
[X,Y] = meshgrid(x,y);
roi = shaperead('shapefile.shp');
rx = roi.X(1:end-1);
ry = roi.Y(1:end-1);
mask = inpolygon(X,Y,rx,ry);
I2 = bsxfun(@times, I, cast(mask,class(I)));
  2 commentaires
KSSV
KSSV le 26 Jan 2018
What error you get? We cannot try your code without the inputs..
chant of rain
chant of rain le 26 Jan 2018
the mask array hallways contain zero values.

Connectez-vous pour commenter.

Réponses (1)

Junxue Zhang
Junxue Zhang le 1 Août 2019
I think your input tif file and shapefile do not share the same map projection, so that their boundary boxes do not match. First you should reproject your shapefile to the new map projection that your raster uses. And then you codes will take effect. Also, you can't ignore the last values in roi.X and roi.Y, because these values make polygon closed.
BTW, if your shapefile contains multiple polygons, I recommend you to do a for loop for the variable roi, like:
mask = zeros(5000,5000,'logical');
for i = 1:length(roi)
rx = roi(i).X;
ry = roi(i).Y;
mask_temp = inpolygon(X,Y,rx,ry);
mask = mask | mask_temp;
end
  1 commentaire
Emanuele Ferrentino
Emanuele Ferrentino le 28 Mai 2020
Hi,
thank you very much Junxue Zhang for your code.
In this way I can generate a binary image from a shapefile with the same projection of my geotiff.
I need to do another step. I'm looking for a way to associate a value (damage_idx) on the binary image previously obtained. I have the following shapefile:
roi =
64×1 struct array with fields:
Geometry
BoundingBox
X
Y
DN
Prov
Com
Localita
Lat1
Lon1
damage_idx
Anyone knows how to do?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by