masking geotiff Landsat data using shapefile

11 次查看(过去 30 天)
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)));

回答(1 个)

Junxue Zhang
Junxue Zhang 2019-8-1
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 个评论
Emanuele Ferrentino
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?

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by