# Do a union of polygons and add the associated values for overlapping coordinates

6 views (last 30 days)
Mini Me on 14 Mar 2022
Edited: Mini Me on 17 Mar 2022
I have 6 matrices of lat, Lon and power values. 3 of them have the same size for lat, lon and power. The other 3 have the same matrix size for lat, lon and power. But different size from the other coordinates. So they both form a polygon would power values but one polygon is bigger and there some overlap. I wanted to do a union between the coordinates and where ever the lat and lon overlap, add the power values together.Since the data are matrices, I tried to convert them into vectors to do a polyshape of lats and lons and do a union after. But that does not seem to work. Well I don’t know for sure because one of the matrices is too big and took forever to be converted into a vector.
Poly1 =polyshape(lat1,lon1); Poly2 = polyshape(lat2,lon2); polyout =union(Poly1,Poly2);
Since polyshape function does not take a 3rd variable I have not figured how to add the power values together for overlapping coordinates. Is there any other approach I can take to achieve that.
Simon Chan on 15 Mar 2022
You may add up the power values together for same coordinates by creating a table and using function outerjoin.
Then you may extract the combined coordinates at the end to form the overlapped polygon.
However, I am not sure how efficient is the code for large matrix.
lat1 = [2 2 3 4 5 5 6];
lon1 = [2 5 6 7 5 2 1];
power1 = [4 10 6 12 3 5 8];
T1 = table([lat1',lon1'],power1','VariableNames',{'Coord' 'Apower'}) % Table for 1st polygon
T1 = 7×2 table
Coord Apower ______ ______ 2 2 4 2 5 10 3 6 6 4 7 12 5 5 3 5 2 5 6 1 8
lat2 = [2 5 6 8 3];
lon2 = [4 2 1 8 8];
power2 = [2 7 6 5 3];
T2 = table([lat2',lon2'],power2','VariableNames',{'Coord' 'Bpower'}) % Table for 2nd polygon
T2 = 5×2 table
Coord Bpower ______ ______ 2 4 2 5 2 7 6 1 6 8 8 5 3 8 3
[T,ileft,~] = outerjoin(T1,T2);
indexleft = find(ileft);
Coord = zeros(size(T,1),2); % Extract the coordinates
for k = 1:size(T,1)
if ismember(k,indexleft)
Coord(k,:) = T.Coord_T1(k,:); % Coordintes can be found in 1st polygon
else
Coord(k,:) = T.Coord_T2(k,:); % Coordinates only appear in 2nd polygon
end
end
combinepower = sum([T.Apower,T.Bpower],2,'omitnan'); % Combine power
S = table(Coord,combinepower)
S = 10×2 table
Coord combinepower ______ ____________ 2 2 4 2 4 2 2 5 10 3 6 6 3 8 3 4 7 12 5 2 12 5 5 3 6 1 14 8 8 5
warning off;
pgon1 = polyshape(lat1,lon1);
pgon2 = polyshape(lat2,lon2);
subplot(2,2,1)
plot(pgon1);
hold on
plot(pgon2);
title('Separate polygons')
x = Coord(:,1);
y = Coord(:,2);
k = boundary(x,y);
pgon=polyshape(x(k),y(k));
subplot(2,2,2)
plot(pgon);
title('Combined Coordinates')
subplot(2,2,3)
polyout = convhull(pgon);
plot(polyout)
title('Convex hull of polygon')
Mini Me on 17 Mar 2022
Edited: Mini Me on 17 Mar 2022

### Categories

Find more on Elementary Polygons in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!