masking geotiff Landsat data using shapefile

12 ビュー (過去 30 日間)
chant of rain
chant of rain 2018 年 1 月 26 日
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 件のコメント
KSSV
KSSV 2018 年 1 月 26 日
What error you get? We cannot try your code without the inputs..
chant of rain
chant of rain 2018 年 1 月 26 日
the mask array hallways contain zero values.

サインインしてコメントする。

回答 (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
Emanuele Ferrentino 2020 年 5 月 28 日
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