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!