I am trying to use JTS to tell if one Polygon contains another Polygon.
I have resources here. How do I utilize the syntax? this is giving me errors. The next step is to apply contains. If anyone has experience with JTS, it would be helpful.
https://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/Polygon.html
Polygon item1 = ((0 0, 1000 0, 1000 1000, 0 1000, 0 0))
Polygon item2 = ((500 500, 1000 500, 600 600, 500 600, 500 500))
You cannot create a polygon just like that. JTS is a rather heavy framework, so you need to go the full route.
GeometryFactory
docs. It is responsible for creating all geometries, it is necessary because it takes into account a few things like PrecisionModel
for example.Polygon
is described by a line - LineRig
or LineString
(the difference is out of the scope for this question).Point
or Coordinate
. So if you want to create a Polygon
The code would look like this:
GeometryFactory factory = new GeometryFactory(); //default
Coordinate[] coordinates1 = {
new CoordinateXY(0,0),
new CoordinateXY(1000,0),
new CoordinateXY(1000, 1000),
new CoordinateXY(0, 1000),
new CoordinateXY(0, 0)
};
Coordinate[] coordinates2 = {
new CoordinateXY(500,500),
new CoordinateXY(1000,500),
new CoordinateXY(600, 600),
new CoordinateXY(500, 600),
new CoordinateXY(500, 500)
};
LinearRing linearRing1 = factory.createLinearRing(coordinates1);
LinearRing linearRing2 = factory.createLinearRing(coordinates2);
Polygon polygon1 = factory.createPolygon(linearRing1);
Polygon polygon2 = factory.createPolygon(linearRing2);
assertTrue(polygon1.contains(polygon2));