# How to determine if one polygon is contained in another?

Using the Net Topology Suite, what is the easiest way to determine if one polygon is entirely contained in another?

Given two independent polygons, A and B, they may overlap, or they may not. If they do, polygon A may be entirely contained in polygon B or polygon B may be entirely contained in polygon A.

I'm looking for an answer to the simplest case, is polygon A entirely contained within Polygon B.

Is the simplest test simply to see if each point of A lies within B?

• I don't know about JTS or NTS, but no, checking that each vertex is within the other polygon is not enough to ensure the polygon is completely contained, strictly geometrically speaking - very close borders can still possibly fluctuate in and out of each other, even in the space of one vertex to the next. – Paulo Raposo Nov 22 '17 at 20:39
• If you took each point pair in sequence and worked out the equation of the line formed between the points (y=mx+b). Then you would test if any coordinates defined by that equation and bounded by the point pair used to define the line, are outside the polygon. Thats just my thought, but searching google scholar would turn up different implementations – Liam G Nov 22 '17 at 22:09

"Entirely contained" is an expression that could be ambiguous in spatial terms. Take a look at this post from Martin Davis. In NTS/JTS exists some spatial predicates that could fit your needs:

• Contains. Geometry A contains Geometry B iff no points of B lie in the exterior of A, and at least one point of the interior of B lies in the interior of A.
• Covers. Geometry A covers Geometry B iff no points of B lie in the exterior of A.
• ContainsProperly. Geometry A containsProperly Geometry B iff all points of B lie in the interior of A.

In NTS you could test this predicates like this:

``````    IGeometry pol = ...;
IGeometry pol2 = ...;

if (pol.Contains(pol2))
{
// pol contains pol2
}
if (pol.Covers(pol2))
{
// pol covers pol2
}

IPreparedGeometry prepPol = PreparedGeometryFactory.Prepare(pol);
if (prepPol.ContainsProperly(pol2))
{
// pol properly contains pol2
}
``````

In other cases, you could need a more advanced definition to test what you need. Take a look at the DE-9IM.

And here is hoy to test a custom predicate in NTS:

``````    if (pol.Relate(pol2, "T*****FF*"))
{
// pol containspol2
}
``````
• In my case, the degenerate cases aren't likely to be a problem. My source data is an ESRI Shapefile that's being parsed by some broken code that doesn't distinguish, when parsing a MultiPolygon between the rings that make up the exteriors of Polygons and rings that make up holes. My recommendation was that we scrap the parser and use OGR/GDAL, but if we can simply determine which rings are contained in which rings, we'd have a faster fix. – Jeff Dege Nov 24 '17 at 16:43
• In that case, ContainsProperly is the best choice, but anyone could work. If you have a large dataset, use a STRtree to speedup the processing time. – César Argul García Nov 24 '17 at 17:45