How to find if a point exists in which polygon How to find if a point exists in which polygon postgresql postgresql

How to find if a point exists in which polygon


The basic method (if you have a small number of polygons) is to store all polygons in a collection and loop over the elements to check if a point is inside a polygon.

On the other hand, if you have a considerable number of polygons, I would recommend using an R-tree data structure, which is not available in the standard library. You should check this project, if you want to go with R-tree option: http://sourceforge.net/projects/jsi/.

R-tree allows you to index rectangles (bounding boxes of the polygons in this case). So you can find a small number of candidate polygons very fast using R-tree. Then you can loop over the candidate list to get the final result.


You can use the GeneralPath class to help you with deciding if a point intersects a polygon. First, create a GeneralPath with your coordinates added:

    GeneralPath gp = new GeneralPath();    double[] x = ...    double[] y = ...    gp.moveTo(x[0], y[0]);    for (int i =1; i < x.length; i++) {        gp.lineTo(x[i], y[i]);    }    gp.closePath();    if (gp.contains(pointX, pointY)) {        ...    }

For the problem of which polygon a point is nearer to, this depends a little on how accurately you need a solution.

For an accurate solution., this amounts (without optimisation) to:

  • take the shortest distance between the point and each of the lines (segments) connecting the vertices of each of the polygons (Java2D apparently doesn't provide a method for this, but the shortest distance from a point to a line is a fairly simple calculation)
  • which polygon has the line with the shortest distance to the point?

In practice, you can approximate this process for some applications. For example, you could much more efficiently do this:

  • take the centre point of the bounding rectangle of each polygon (GeneralPath.getBounds() will give you this)
  • take the distance between the query point and each of these centre points, and see which is closest.

If you do need an accurate answer, then you can combine these techniques to optimise your search among all the vertices. For example, you could order the polygons by the distance to their "centrepoint" (defined as above). Search from minimum to maximum distance. If the minimum distance to a segment that you have found so far is d, then you can automatically rule out any polygon P where the distance from your query point to its "centrepoint" is d + r, where r is half the length of the diagonal of P's bounding rectangle (in other words, for simplicity, you imagine a bounding circle around that bounding box and check that the distance to that bounding circle is further than the nearest point found so far on other polygons).

I don't quite understand the bit about the database. Your polygons are just defined as a series of points. How you decide to store these in memory/file doesn't essentially make any difference to the algorithm.