How to plot a large number of rectangles from a matrix

9 次查看(过去 30 天)
I have thousands of files of survey data. My ultimate goal is find a way of grouping these together by geographical region and concatenating those that are similar. This is a difficult concept for me but what Ive decided might be the easiest is to first find there geographical limits, graph a rectangle of each using the NE and SW corner, find the area, then determine if the surveys are in the same region using the amount of overlap from each rectangle. For instance, if the area of one survey rectanlge over laps another by 70 percent then they could be considered the same region and the files should be concatenated before being processed. Hopefully that makes sense and if there is an easier way of doing this please let me know.
To start im trying to create these rectangles. The first thing I did was set up where the files are and how they should be read. Next I found the max and min lat and long. I created four points, minlong maxlat, minlong minlat, maxlon minlat, maxlon maxlat. This should be enough to create a rectangle right? The first issue im running into is how to get every file to add these four values to one matrix, Im getting confused on the format of it all, how should the matrix be setup to be read as ordered points and individual rectangles? Right now I have two columns with 12 total rows from 3 files, everything was read correctly but how I do then plot each of these as individual rectangles to find the area? Im pretty lost but the little bit of code Ive created is below. Thanks for any and all help!
clear
P = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222';
Q = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222/Corrected CSV';
S = dir(fullfile(P,'*.csv'));
% S = natsortfiles(S);
N = numel(S);
C = cell(N,2);
for k= 1:numel(S)
F1 = fullfile(S(k).folder,S(k).name);
M = readmatrix(F1);
lat = M(:,1);
lon = M(:,2);
[minLat, maxLat] = bounds(lat);
[minLon, maxLon] = bounds(lon);
C{k,1} = [maxLat; maxLat; minLat; minLat];
C{k,2} = [maxLon; minLon; maxLon; minLon];
c = cell2mat(C);
end

采纳的回答

Matt J
Matt J 2024-3-19
编辑:Matt J 2024-3-19
I don't really see why you need to find bounding rectangles to do what you describe. You could just represent the regions as polyshapes,
N=numel(S);
p(N)=polyshape();
for k= 1:numel(S)
F1 = fullfile(S(k).folder,S(k).name);
p(k) = polyshape(readmatrix(F1));
end
plot(p)
You can also find the overlap area of two of the regions using polyshape methods,
overlap(j,k) = area( intersect( p(j),p(k) ) )
  4 个评论
Bradley
Bradley 2024-3-20
编辑:Bradley 2024-3-20
Thanks again for the help, I had to brute force the polyshape as it wanted a 2 column matrix with at least 3 rows. My updated code is below, what does p(j) do in the overlap function above? Thanks!
for k= 1:numel(S)
F1 = fullfile(S(k).folder,S(k).name);
M = readmatrix(F1);
lat = M(:,1);
lon = M(:,2);
[minLat, maxLat] = bounds(lat);
[minLon, maxLon] = bounds(lon);
C = [maxLon maxLon minLon minLon; maxLat minLat minLat maxLat];
c = C.';
p(k)=polyshape(c);
end
plot(p)
I used these lines to create a matrix of overlap
polyvec = p;
plot(polyvec)
TF = overlaps(polyvec);
Also, is there any logic behind the color schemes? I have ~10 suverys plotted using this method and some have similar or the same colors while others have unique colors. Is there a way to change the colors based on how many surveys overlap each other? Thanks again for the help.
Matt J
Matt J 2024-3-20
The polyshape colors and alphas are under your control through the handles returned by plot(), e.g.,
p1 = polyshape([0 0 1 1],[1 0 0 1]);
p2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
p3 = polyshape([1.25 1.25 1.75 1.75],[0.75 1.25 1.25 0.75]);
polyvec = [p1 p2 p3];
H=plot(polyvec);
H(1).FaceColor='r'; H(1).FaceAlpha=0.8;
H(2).FaceColor='g'; H(2).FaceAlpha=0.2;
H(3).FaceColor='b';

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by