How to find if a point exists in which given set of polygons ? I have coordinates like
polygonA = 1(0,0),2(0,5),3(3,4),4(3,5),5( 2,2)
polygonB = 1(10,10),2(10,15),3(13,14),4(13,15),5(12,12)
I have a point as (6,4) now want to search if this point is in any of this polygon or in both or nearest to which polygon.
How to store such data (polygon) ? is there a system / database / algorithm to do this search ?
Update : Thanks all for such fast response...I think i need to be more specific...
How to search = Yes...got list of algorithms and library for the same.
How to store = based on my research SQL and NoSQL db have their solutions. NoSQL = MongoDb seems closest what i needed. But issue is I can query like "db.places.find({ "loc" : { "$within" : { "$polygon" : polygonB } } })" But cant make query like db.places.find({ "loc" : { "$within" : { } } }) SQL checked postgre and openGIS for some help. But colud not figureout if its possible.
If someone can help me with that...Thanks in advance.
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.
来源:https://stackoverflow.com/questions/10774427/how-to-find-if-a-point-exists-in-which-polygon