How do I find If one polygon is inside another?

I have geographic plotting data (lat lon or X Y in this case) for counties and for storm surge levels. I need to see if the storm surge probability lines come in contact with a given county. Here is what the data looks like.
This is the very last part of my thesis model so any help would be FANTASTIC! Thanks so much
Kenny

Answers (2)

Pretty simple. Fortunately for you there is an "inpolygon()" function. Have one polygon be your reference polygon. Then loop over every vertex in your other polygon using inpolygon() to see if that vertex lies in the reference polygon. If any second vertex lies inside the first polygon, there is overlap.

6 Comments

I actually tried this method but I could not get it to work. Could you provide an example? I keep getting the too many input arguments.
When I used loops this was my code. I got a 1 x 10430 array with all 0's and I am positive there are some intersections.
for i = 1:length(surge5)
t(i).X = surge5.X ;
t(i).Y = surge5.Y;
end
for i = 1:length(surge5)
for k = 1:length(counties)
surgeinside = inpolygon(t(i).X,t(i).Y,counties(k).Lat,counties(k).Lon);
end
end
Kenneth: See attached demo, below in blue text, which will produce this plot:
Points inside are indicated by the green circle.
[Edit - changed program to get star inside box as well as box inside star.]
Hi image analyst. I am working on a similar problem to the one above and I think your figure would be a solution, however I cannot figure out what code you used. Has the code input since been deleted? In addition, I am trying to do this for multiple polygons within a single polygon. I am trying to initiate a for loop but have so far been unable to figure out how to do so. If you could advise how to then group the results in a structure, that would be great.
Thanks,
Jonathan
@Jonathan Gingrich, Darned if I know. Maybe another moderator deleted it by accident - it's easy to do with the slip of a mouse button. Click on the red x and BAM! it's gone with no way to undo it or get it back.
I suggest you post your own question with your own data and attempt at using inpolygon().
No. Actually, it is not sufficient to just test is every vertex of one polygon is inside or outside of the other. The polygons can intersect and have no vertices inside the other.
But @Jonathan Gingrich, see my answer just posted.

Sign in to comment.

John D'Errico
John D'Errico on 19 Aug 2021
Edited: John D'Errico on 19 Aug 2021
There is a simple solution. It involves two tests, both of which are essentially vectorized. In fact, now that I think of it, there are several solutions that will be adequate, and fairly efficient.
And it depends on exactly what you are looking to do.
Solution 1: (3 steps)
Test if any vertex of polygon 1 lies inside polygon 2. If any vertex does lie inside, then there is SOME intersection between the polygons.
Test if any vertex of polygon 2 lies inside polygon 1. THe same comment applies.
Test if there is any intersection of the edges of the two polygonal regions. You can do this efficiently using Doug Schwarz's intersections code, as found on the file exchange.
I'm not sure what exactly you need to do, IF there is some intersection.
Solution 2: (this uses polyshapes.)
Convert each polygon into a polyshape.
Compute the intersection of the two polyshapes. The result will be a new polyshape. If it is the null polyshape, then there was no intersection.
You can do other things using solution 2. For example, if you wish to test if polygon 1 is entirely inside polygon 2, then compute the area enclosed by polygon1. Next, compute the union of the two polyshapes, and the area of that. If that area is the same as the area of polyshape 1, then you know that polyshape 2 was entirely inside polyshape 1. Conversely, you can as easily test if 1 was inside 2, by computing the area of polyshape 2, and then comparing that to the union.
My guess is the polyshape solution is best, as it is simplest and may help you to do other computations.
Note that polyshapes were introduced in R2017b, so as long as you have a moderately recent release, you will have them.

1 Comment

You might want to pad each polyshape by a little bit using polybuffer for reasons discussed in this post on Loren's blog.

Sign in to comment.

Categories

Products

Asked:

on 24 Jan 2014

Commented:

on 19 Aug 2021

Community Treasure Hunt

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

Start Hunting!