Search code examples
javacoordinatesgeotools

Point In polygon using geootools


I'm trying to extract a area of geopoints by drawing a polygon from 3 or more coordinates to set my logical geoFence but the thing is that I think that LinearRing needs all the points on to close the object anyway can anyone give me an insight of what am I doing wrong

Coordinate[] coordinates = vertices;    
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FIXED), 4326);
LinearRing linearRing = geometryFactory.createLinearRing(coordinates);   
Polygon polygon = geometryFactory.createPolygon(linearRing, null);
setGeometry(polygon);

Solution

  • You are right that a LinearRing (and a polygon) must be a closed ring - it needs a minimum of 4 points and the first and last point must be the same. So I would do something like:

    Coordinate[] coordinates = new Coordinate[vertices.length+1];
    for(int i=0;i<vertices.length;i++){
        coordinate[i] = vertices[i];
    }
    coordinate[vertices.length] = coordinate[0];
     .....