Fill inside a polygon with a color

66 ビュー (過去 30 日間)
BN
BN 2020 年 5 月 3 日
コメント済み: Patrick Laux 2021 年 7 月 13 日
Dear all,
I want to fill a polygon that I readed from shapefile with blue in order to visualzing it as lake. I searched alot and tried:
S2 = shaperead('lake.shp');
polygonwater1_y = S2(1).X;
polygonwater1_x = S2(1).Y;
fill (polygonwater1_y,polygonwater1_x,'b')
And:
patch (polygonwater1_y,polygonwater1_x,'b')
But as you can see below, unfortunately, it doesn't fill:
Do you know how can I do to fill it?
Thank you
  2 件のコメント
KSSV
KSSV 2020 年 5 月 3 日
Atatch the data.
BN
BN 2020 年 5 月 3 日
Dear KSSV
I attached data here. There is my shapefile containing 3 lakes. I want to fill all of them by blue.
Thank you.

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

採用された回答

KSSV
KSSV 2020 年 5 月 3 日
編集済み: KSSV 2020 年 5 月 3 日
It happned because, your region have multiple regions inside and they have NaN's.
shfile = "Lakes/lakes.shp" ;
S = shaperead(shfile) ;
N = length(S) ;
x = S(3).X ; y = S(3).Y ; % Picked the 3rd region
idx = find(isnan(x)) ; % find positions of NaNs
idx = [1 idx length(x)] ; % append first and last position
figure
hold on
for i = 1:length(idx)-1
pos = idx(i):idx(i+1) ; % gEt the required position
xi = x(pos) ; yi = y(pos) ; % GEt the corodinates
% Remove NaN's
xi(isnan(xi)) = [] ;
yi(isnan(yi)) = [] ;
patch(xi,yi,'b') ; % color the area
end
  4 件のコメント
KSSV
KSSV 2020 年 8 月 26 日
Because the user wanted the third region.
Patrick Laux
Patrick Laux 2021 年 7 月 13 日
smart procedure

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 8 月 26 日
Another way to plot and work with this data is to create a polyshape for each region.
S = shaperead('lakes.shp');
for k = 1:numel(S)
P(k) = polyshape(S(k).X, S(k).Y); % This will issue a warning each time
end
plot(P(3), 'FaceColor', 'r')
% Or plot all of them
figure
plot(P)
Some of the functions for working with polyshape objects may be of use to you. For example, computing the area of the polyshape agrees pretty well with the Shape_Area from the shape file.
[area(P(3)); S(3).Shape_Area; area(P(3))-S(3).Shape_Area]
  2 件のコメント
Ishaan Chauhan
Ishaan Chauhan 2021 年 2 月 18 日
Do you know of any ways to code to ignore the warnings?
Patrick Laux
Patrick Laux 2021 年 7 月 13 日
warning off

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

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by