[SERVER-55027] Geospatial queries over GeoJSONs return wrong results Created: 08/Mar/21  Updated: 15/Aug/22  Resolved: 08/Mar/21

Status: Closed
Project: Core Server
Component/s: Geo, Index Maintenance, Querying
Affects Version/s: 4.2.6, 4.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Pavel Antonov Assignee: Edwin Zhou
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File Screen Shot 2021-03-08 at 3.45.46 PM.png     PNG File Screenshot from 2022-08-15 12-53-30.png     PNG File image-2021-03-08-08-58-51-496.png     PNG File image-2021-03-08-08-59-10-527.png     PNG File image-2021-03-08-09-00-46-731.png    
Operating System: ALL
Steps To Reproduce:

In the following example, I have some stored polygons located over the Baltic sea (between Sweden and Finland). Whenever I draw a polygon over the area where the polygons lie, they are correctly detected, namely all 609 objects are found. (see query and Screenshot_1)

db.geojsons.find({
  location: {
    $geoIntersects: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          [ [17.314453, 61.648162], [17.314453, 64.434892], [26.542969, 64.434892], [26.542969, 61.648162], [17.314453, 61.648162], ],
        ],
      },
    },
  },
});

However, the strange behavior occurs when submitting a wider query polygon a little bit below (see Screenshot 2 and 3 with the corresponding queries).

db.geojsons.find({
  location: {
    $geoIntersects: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          [ [-4.482422, 61.93895], [46.318359, 61.93895], [46.318359, 58.585436], [-4.482422, 58.585436], [-4.482422, 61.93895], ],
        ],
      },
    },
  },
});

As well as:

db.geojsons.find({
  location: {
    $geoIntersects: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          [ [-4.482422, 61.93895], [36.318359, 61.93895], [36.318359, 58.585436], [-4.482422, 58.585436], [-4.482422, 61.93895], ],
        ],
      },
    },
  },
});

 

Hereby, I also attach three example GeoJSONs that I use:

{
  type: "Feature",
  location: {
    type: "Polygon",
    coordinates: [
      [
        [20.9995991794984, 63.4348461784678],
        [20.9995991794984, 63.4240745387888],
        [21.0236395339339, 63.4240745387888],
        [21.0236395339339, 63.4348461784678],
        [20.9995991794984, 63.4348461784678],
      ],
    ],
  },
  properties: { acquisitionDate: ISODate("2017-01-13T09:10:31Z") },
  _class: "com.models.database.GeoJson",
};
 
{
  type: "Feature",
  location: {
    type: "Polygon",
    coordinates: [
      [
        [20.9995993299129, 63.424076492091],
        [20.9995993299129, 63.413304836971],
        [21.0236306669723, 63.413304836971],
        [21.0236306669723, 63.424076492091],
        [20.9995993299129, 63.424076492091],
      ],
    ],
  },
  properties: { acquisitionDate: ISODate("2017-01-13T09:10:31Z") },
  _class: "com.models.database.GeoJson",
};
 
{
  type: "Feature",
  location: {
    type: "Polygon",
    coordinates: [
      [
        [20.9995994802004, 63.4133067893598],
        [20.9995994802004, 63.4025351187936],
        [21.0236218074932, 63.4025351187936],
        [21.0236218074932, 63.4133067893598],
        [20.9995994802004, 63.4133067893598],
      ],
    ],
  },
  properties: { acquisitionDate: ISODate("2017-01-13T09:10:31Z") },
  _class: "com.models.database.GeoJson",
};

 

Thank you in advance!

Participants:

 Description   

Hello everyone,

I am currently working on a project, where I store polygons using the native GeoJSON format and then perform geospatial queries to locate these based on both $geoIntersects and $geoWithin operator. Indeed, everything seemed to be working fine.

However, yesterday I discovered a strange behavior, which I am going to reproduce with an example.



 Comments   
Comment by sudhanshu sirohia [ 15/Aug/22 ]

Hi Edwin,

 

We are also facing issues, and tried using the map you shared and the point shows well inside the polygon, however when using intersect query in mongo, the result does not the return  below polygon which contains this point

 

loadGeoJson(

{ type: "Polygon", coordinates: [ [ *[ 80.14114379882812, 13.011582508520874 ], [ 80.1471519470215, 13.033826278483838 ], [ 80.13702392578126, 13.038341687052323 ], [ 80.12088775634767, 13.043525943415695 ], [ 80.10149002075197, 13.039010629462274 ], [ 80.09204864501955, 13.033157322059589 ], [ 80.08758544921876, 13.0257986820875 ], [ 80.0907611846924, 13.013171415454433 ], [ 80.08878707885744, 13.008822803857846 ], [ 80.08947372436525, 13.006564840836658 ], [ 80.08921623229982, 13.003888709899078 ], [ 80.09033203125001, 13.002383373560995 ], [ 80.09170532226564, 13.000208982727571 ], [ 80.09290695190431, 12.998619992762274 ], [ 80.09325027465822, 12.997072808548213 ], [ 80.09779930114748, 12.992514831438987 ], [ 80.10251998901369, 12.989169473900679 ], [ 80.10543823242189, 12.988751301039581 ], [ 80.10732650756836, 12.98724587291298 ], [ 80.10878562927248, 12.98732950804826 ], [ 80.11058807373048, 12.987496778234293 ], [ 80.11110305786134, 12.98732950804826 ], [ 80.11230468750001, 12.987664048307733 ], [ 80.11376380920412, 12.987664048307733 ], [ 80.11247634887697, 12.987496778234293 ], [ 80.11556625366212, 12.98791495320668 ], [ 80.11350631713869, 12.98791495320668 ], [ 80.11616706848146, 12.98774768330223 ], [ 80.1185703277588, 12.98732950804826 ], [ 80.1208019256592, 12.987413143155283 ], [ 80.12354850769044, 12.98808222299849 ], [ 80.13135910034181, 12.992013030680738 ], [ 80.13925552368165, 12.997700046553673 ], [ 80.14114379882812, 13.011582508520874 ]* ] ] }

)

loadGeoJson({ type: "Point", coordinates: [ [ [ 13.0127093, 80.1087259]] ] })

Comment by Pavel Antonov [ 09/Mar/21 ]

Hi Edwin,

Thanks a lot for your detailed answer!

Regards,

Pavel

Comment by Edwin Zhou [ 08/Mar/21 ]

Hi paveldushev9@gmail.com,

This behavior is caused by GeoJSON visualizers attempting to project orthogonal cartesian coordinates on a spherical shape. Many GeoJSON visualizers on the web plot their coordinates in this way, which can be misleading when trying to interpret how MongoDB queries GeoJSON. To view an accurate representation of how MongoDB queries GeoJSON, I recommend using https://geodndmap.mongodb.com/. You can plot polygons by opening up your browser's developer tools, and running the following command:

loadGeoJson({
    type: "Polygon",
    coordinates: [
      [
        [20.9995994802004, 63.4133067893598],
        [20.9995994802004, 63.4025351187936],
        [21.0236218074932, 63.4025351187936],
        [21.0236218074932, 63.4133067893598],
        [20.9995994802004, 63.4133067893598],
      ],
    ],
  })

where you can replace the object with a polygon of your choice.

The polygons that you've provided in your reproduction will show up as follows:

so we should expect the geospatial query result to include a set of the polygons that you otherwise wouldn't expect when querying using flat coordinates.

Best,
Edwin

Generated at Thu Feb 08 05:35:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.