1
votes

I am trying to use $geointersects in mongodb to find the polygon in which the point is located.

For that in php i created the test data:

$collection = $db->selectCollection('countries');
$result = $collection->insertMany([
    [
        'c' => 'rus',
        'n' => 'Russia',
        'loc' => [
            "type" => "Polygon",
            "coordinates" => [
                [-109.95117, 39.77477],
                [-100.23926, 46.70974],
                [-94.41608,  37.78902],
                [-104.67541, 32.70375],
                [-109.95117, 39.77477]
            ]
       ]
    ],[
        'c' => 'can',
        'n' => 'Canada',
        'loc' => [
            "type" => "Polygon",
            "coordinates" => [
                [-100.15137, 46.83013],
                [-84.31555,  47.89965],
                [-82.39746,  37.78808],
                [-94.28467,  37.8228 ],
                [-100.15137, 46.83013]
            ]
       ]
    ]
]);

Result is:

enter image description here

Then in Robomongo I am making a select:

db.getCollection('countries').find({"loc": { $geoIntersects: {$geometry: {"type": "Point", "coordinates": [ -88.0123388, 41.8333926 ]}}}})

enter image description here

And no rows are returned! What might be an issue here?

Same in shell:

enter image description here

Here are the polygons I am testing against:

enter image description here

Here is the point (41.8333926, -88.0123388) location:

enter image description here

Using the latest version of mongodb: 3.2.9.

1

1 Answers

0
votes

the answer is that structure of coordinates is incorrect. I wanted to create an index but it failed.. That is how this has been found out.

Correct array:

"coordinates" => [[
                [-109.95117, 39.77477],
                [-100.23926, 46.70974],
                [-94.41608,  37.78902],
                [-104.67541, 32.70375],
                [-109.95117, 39.77477]
            ]]

Therefore in my original question I put points into what supposed to be polys.