skip to Main Content

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:

enter image description here

interception is not allowed :

enter image description here

How can I solve the problem. Can you recommend any mongodb techniques, algorithms, packages or libraries?

3

Answers


  1. Chosen as BEST ANSWER

    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 has intersects() method that exactly suits my case.

    Working example:

    from shapely.geometry import Polygon
    
    neighbor_areas_coordinates = [
        [
            [68.39740673300008,40.18342910900003],
            [68.39740266000007,40.183370238000045],
            [68.39711592400005,40.183379591000055],
            [68.39712181600004,40.183438503000026],
            [68.39740673300008,40.18342910900003]
        ],
        [
            [68.39740122700005,40.18336154400003],
            [68.39739715300004,40.183302673000064],
            [68.39711041700008,40.18331202600005],
            [68.39711630900007,40.18337093900004],
            [68.39740122700005,40.18336154400003]],
        [
            [68.39739633300007,40.18329360800004],
            [68.39739226200004,40.18323473700008],
            [68.39710552400004,40.183244090000066],
            [68.39711141700008,40.18330300300004],
            [68.39739633300007,40.18329360800004]
        ]
    ]
    
    def convert_to_polygon (points):
        return Polygon([tuple([x[0],x[1]]) for x in points])
    
    def check_intersection():
        new_building_coordinates = [
            [68.39739633300007,40.18329360800004],
            [68.39738675500007,40.183167172000026],
            [68.39710001700007,40.18317652500007],
            [68.39710591000005,40.183235438000054],
            [68.39739082800008,40.18322604400004]
        ]
    
        new_building = convert_to_polygon(new_building_coordinates)
    
        for c in neighbor_areas_coordinates:
            area = convert_to_polygon(c)
            print(new_building.intersects(area))
    
    
    check_intersection()
    

  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search