Let us consider a Polygon (P) with N points each points are represented with Latitude and Longitude. Now I would like to check a new point P1 (latitude and longitude) is Inside/ Outside the Polygon??
Asked
Active
Viewed 2,250 times
2
-
possible duplicate of [Point in Polygon aka hit test](http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test) – Carlos Sep 17 '15 at 13:43
1 Answers
2
Point within polygon test is a common algorithm that is implemented in many geospatial libraries in different programming languages.
JTS Topology Suite, for example, is a robust open source API of 2D spatial predicates and functions in Java. It provides the methods to test if a geometry (e.g. Point) is contained within another geometry (e.g. Polygon).
Here is a code snippet:
doube lat = ....; // latitude in decimal degrees
double lon = ...; // longitude in decimal degrees
Coordinate[] points = ... // construct/initialize points for polygon
// now construct the polygon and test the point
GeometryFactory gf = new GeometryFactory();
LinearRing jtsRing = gf.createLinearRing(points);
Polygon poly = gf.createPolygon(jtsRing, null);
Coordinate coord = new Coordinate(lon, lat);
Point pt = gf.createPoint(coord);
if (poly.contains(pt)) {
// point is contained within polygon
} else {
// point is NOT contained within polygon
}
The contains() geometry predicate is defined in the JTS javadoc.
See also JTS project on github

CodeMonkey
- 22,825
- 4
- 35
- 75