JTS Topology Suite Point in Polygon

by GarciaPL on Monday 3 September 2012


I would like to show you how easily calculate if an object i mean some point is inside in some king of polygon. Below algorithm is very useful for determining points in some area for example in Google Maps. All what you need to possess is set of coordinates of this area and point.

public boolean sprawdzObiektwObszarze(ArrayList obszar, Coordinate abonent) {
        final GeometryFactory gf = new GeometryFactory();
        LinearRing linearring = new LinearRing(new CoordinateArraySequence(obszar.toArray(newCoordinate[obszar.size()])), gf);
        final Polygon polygon = gf.createPolygon(linearring, null);
        final Point point = gf.createPoint(abonent);
        return point.within(polygon);
}

Function return true if point is in polygon or false if it is not.

PS. You must remember that first and last point of ArrayList<Coordinate> should be the same point to close the area.


Reference :
[1] JTS Topology Suite SourceForge.net
[2] Source Pastebin.com