1

I have a collection of polygons in a geojson format and I'm trying to find for one polygon, its neighbors.

I tried to use the $geoIntersects operator like so

coords = [...] # some coordinates of a polygon of my collection
db.coll.find({
  "loc": {
    "$geoIntersects": {
      "$geometry": {
        "type": "Polygon" ,
        "coordinates": coords
      }
    }
  }
})

It didn't give me any results.

For clarification here two polygons that are next to one another and I'm trying to find one when I'm calling my function with the other. two polygons next to each other They do not need to share exactly one full side, just a portion of it.

Is that something that's doable with mongo? Or should I aim for another strategy?

2
  • do your polygons actually intersect? maybe try it with some test data to be sure
    – Ian Turton
    Commented Apr 26, 2021 at 10:02
  • Yes the polygon I'm using should have at least one neighbor. I've added an example of two polygons that "touch" each other and thus, when using one polygon with the function I'm trying to create, it should return the other.
    – bebenzer
    Commented Apr 26, 2021 at 10:19

1 Answer 1

1

By looking at the MongoDB documentation about $geoIntersects it seems the case of finding a polygon that shares the same edge isn't guaranteed by this operator:

$geoIntersects does not guarantee that it will consider a polygon to intersect with its own edges; its own vertices; or another polygon sharing vertices or edges but no interior space.

Source: https://docs.mongodb.com/manual/reference/operator/query/geoIntersects/#degenerate-geometry

EDIT: here I will provide my solution for the problem using $nearSphere query (https://docs.mongodb.com/manual/reference/operator/query/nearSphere/):

from shapely import geometry

# get minimum bounding box around polygon
x, y = polygon.minimum_rotated_rectangle.exterior.coords.xy

# get upper bound on the outer diameter of the polygon
outer_diameter = max([polygon.centroid.distance(geometry.Point(x_i, y_i)) for x_i, y_i in zip(x, y)])

# prepare query
query = {
    "geometry": {
        "$nearSphere": {
            "$geometry": geometry.mapping(selected_polygon.centroid),
            "$minDistance": 0,
            "$maxDistance": outer_diameter
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.