You cannot create a polygon just like that. JTS is a rather heavy framework, so you need to go the full route.
- Everything starts with
GeometryFactory
docs. It is responsible for creating all geometries, it is necessary because it takes into account a few things like PrecisionModel
for example.
- You need to understand the hierarchy of geometries,
Polygon
is described by a line - LineRig
or LineString
(the difference is out of the scope for this question).
- You need to realize that every line consists of points, which can be described either by
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));