How do I find If one polygon is inside another?
조회 수: 39 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
Image Analyst
2014년 1월 24일
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
Image Analyst
2021년 8월 19일
@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
2021년 8월 19일
편집: John D'Errico
2021년 8월 19일
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.
John D'Errico
2021년 8월 19일
편집: John D'Errico
2021년 8월 19일
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
Steven Lord
2021년 8월 19일
You might want to pad each polyshape by a little bit using polybuffer for reasons discussed in this post on Loren's blog.
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!