Before asking this question I read these answers, but could not find the solution:
Check if coordinate is within area
Geolocation – Check if a location belongs to an area
Check if coordinate in selected area
How to calculate intercepting area of polygons in python
I have to check if a given area, for example:
[
[
[61.21079236, 41.892769716],
[61.2107934860001, 41.892589705],
[61.2104923360001, 41.8925886540001],
[61.2104912090001, 41.8927686640001],
[61.21079236, 41.892769716]
]
]
intercepts with any of other provided areas. Chosen area can be in any shape, square, polygon, triangle etc.
[
[ // area 1
[
[61.21079236, 41.892769716],
[61.2107934860001, 41.892589705],
[61.2104923360001, 41.8925886540001],
[61.2104912090001, 41.8927686640001],
]
],
[ // area 2
[
[62.21079236, 41.892769716],
[61.2107934860001, 43.892589705],
[63.2104923360001, 41.8925886540001],
[61.2104912090001, 41.8927686640001],
[63.21079236, 43.892769716],
[61.21079236957, 41.892769716],
[63.210792368746, 44.892769716]
]
],
[ // area 3
[
[61.21079236, 41.892769716],
[61.2107934860001, 44.892589705],
[61.2104923360001, 41.8925886540001],
[61.2104912090001, 42.8927686640001],
[64.21079236, 41.892769716]
]
]
]
The data is stored in MONGODB. So I tried to solve the problem with MONGODB’s Geospatial Queries with no luck.
I also tried to solve the problem with
Sutherland-Hodgman polygon clipping algorithm
but the algorithm did not work correctly(maybe implementation was wrong).
To visualize the problem:
interception is not allowed :
How can I solve the problem. Can you recommend any mongodb techniques, algorithms, packages or libraries?
3
Answers
I was able to solve this problem with this nice python library: Shapely.
This library offers implementation of various geometric objects with their specific methods. Among them is
Ploygon
which hasintersects()
method that exactly suits my case.Working example:
One way to do it, although not a without disadvantages, is using 3 different queries in mongodb:
$geoIntersects
,$geoWithin
and$geoIntersects
again. If the input polygon* does not intersect a current polygon, nor includes one or included in an existing polygon , you should be OK.The first
$geoIntersects
query will tell you if the input polygon is intersecting one or more of the current polygons.The second
$geoWithin
query will tell you if the input polygon is one or more of the current polygons.On the 3rd
$geoIntersects
query you should use only one point of your input polygon to find if it is included inside a current polygon.If all three queries return empty results, you can safely insert the new polygon.
The main disadvantage here, is that this is not an atomic operation, meaning you should limit the insertion operation to one validated polygon at a time.
The main advantage is that you can use the power of mongodb on great amount of polygons already on your database, without any need to pull them all and analyze them from your RAM.
*This answers refers the input as a polygon for clarity, the solution holds in mongodb for all geometries that are boundary of an area.
If you are using Python, you can use the package shapely, which offers many geometrical functions, objects (including
Polygon
) out of the box.The method you need is
Polygon.intersects()
.Here are the docs.