How do I find If one polygon is inside another?

39 vues (au cours des 30 derniers jours)
Kenneth Morley
Kenneth Morley le 24 Jan 2014
Commenté : Steven Lord le 19 Août 2021
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

Réponses (2)

Image Analyst
Image Analyst le 24 Jan 2014
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 commentaires
Image Analyst
Image Analyst le 19 Août 2021
@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().
John D'Errico
John D'Errico le 19 Août 2021
Modifié(e) : John D'Errico le 19 Août 2021
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.

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 19 Août 2021
Modifié(e) : John D'Errico le 19 Août 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 commentaire
Steven Lord
Steven Lord le 19 Août 2021
You might want to pad each polyshape by a little bit using polybuffer for reasons discussed in this post on Loren's blog.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by