how to do union of geopolyshape?

15 views (last 30 days)
Sierra
Sierra on 28 Jun 2022
Answered: Jaynik on 6 Oct 2023
I made several geopolyshapes in geoplot. but I want to merge every geopolyshape to one geopolyshape,
I've used union of plot, but it didn't work for geoplot
This is my code for plotting the first image.
for i = 1:length(dep_xl)-1
shape1 = geopolyshape([dep_lat(i,1),dep_lat(i,2),dep_lat(i,3),dep_lat(i,4)],[dep_lon(i,1),dep_lon(i,2),dep_lon(i,3),dep_lon(i,4)]);
shape2 = geopolyshape([dep_lat(i+1,1), dep_lat(i+1,2), dep_lat(i+1,3) ,dep_lat(i+1,4)],[dep_lon(i+1,1),dep_lon(i+1,2), dep_lon(i+1,3) ,dep_lon(i+1,4)]);
geoplot(shape1)
hold on
geoplot(shape2)
end
and this is what i tried.
for i = 1:length(dep_xl)-1
shape1 = geopolyshape([dep_lat(i,1),dep_lat(i,2),dep_lat(i,3),dep_lat(i,4)],[dep_lon(i,1),dep_lon(i,2),dep_lon(i,3),dep_lon(i,4)]);
shape2 = geopolyshape([dep_lat(i+1,1), dep_lat(i+1,2), dep_lat(i+1,3) ,dep_lat(i+1,4)],[dep_lon(i+1,1),dep_lon(i+1,2), dep_lon(i+1,3) ,dep_lon(i+1,4)]);
polyout = union(shape1,shap2)
geoplot(polyout)
hold on
end
Thanks!

Answers (1)

Jaynik
Jaynik on 6 Oct 2023
Hi Sierra,
According to my understanding you want to find the union of shapes of type "geopolyshape". Currently, we cannot find the union for "geopolyshape" type directly. Instead of using "geopolyshape", you can try using the "geoshape" type with the "polybool" function for union. Following is a sample code to perform the union of two "geoshape" type variables:
geoShape1 = geoshape([dep_lat(i,1),dep_lat(i,2),dep_lat(i,3),dep_lat(i,4)],[dep_lon(i,1),dep_lon(i,2),dep_lon(i,3),dep_lon(i,4)]);
geoShape2 = geoshape([dep_lat(i+1,1), dep_lat(i+1,2), dep_lat(i+1,3) ,dep_lat(i+1,4)],[dep_lon(i+1,1),dep_lon(i+1,2), dep_lon(i+1,3) ,dep_lon(i+1,4)]);
[lat, lon] = polybool('union', geoShape1.Latitude, geoShape1.Longitude, geoShape2.Latitude, geoShape2.Longitude);
% Convert the resulting latitude and longitude arrays back to geopolyshape object
polyout = geopolyshape(lat, lon);
geoplot(polyout);
You can also use "polyshape" instead of "geoshape" to directly use the "union" function like this:
geoShape1 = polyshape([dep_lat(i,1),dep_lat(i,2),dep_lat(i,3),dep_lat(i,4)],[dep_lon(i,1),dep_lon(i,2),dep_lon(i,3),dep_lon(i,4)]);
geoShape2 = polyshape([dep_lat(i+1,1), dep_lat(i+1,2), dep_lat(i+1,3) ,dep_lat(i+1,4)],[dep_lon(i+1,1),dep_lon(i+1,2), dep_lon(i+1,3) ,dep_lon(i+1,4)]);
ans = union(geoShape1, geoShape2);
geopoly = geopolyshape(ans.Vertices(:, 1), ans.Vertices(:, 2));
geoplot(geopoly);
You can read more about these functions here:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!