Search code examples
mongodbmongodb-querygeolocationgeo

How to check if an area intercepts with any one of other given areas


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?


Solution

  • 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()