[SERVER-38765] Geo within query requires custom crs to specify the larger of the two areas Created: 23/Dec/18  Updated: 27/Oct/23  Resolved: 24/Dec/18

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ian Hannah Assignee: Kelsey Schubert
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File firstQuery.png     PNG File secondQuery.png    
Issue Links:
Related
is related to CSHARP-2464 There is no way to specify the CRS us... Closed
Operating System: ALL
Participants:

 Description   

In our Mongo database we have a test polygon defined by the following coordinates:

0.026856601145655,53.3536401702337 -0.018187415636542,53.3543968971421 -0.016935805565727,53.381347118852 -0.01651815359378,53.3903304943447 -
0.001490628678547,53.390079813912 -0.001911439808241,53.3810965200027 0.028136611683245,53.3805896527841 0.026856601145655,53.3536401702337

We then use a within query to see if this polygon lies within a particular area. This one returns the above polygon:

{{ "PId" : ObjectId("5c1bccda3930321b784dbade"), "GeoPnt" : { "$within" : { "$geometry" :

{ "type" : "Polygon", "coordinates" : [[[-33.353119999999997, -50.140853], [-33.353119999999997, 76.184997999999993], [146.24300500000001, 76.184997999999993], [146.24300500000001, -50.140853], [-33.353119999999997, -50.140853]]] }

} } }}

and this one does not:

{{ "PId" : ObjectId("5c1bccda3930321b784dbade"), "GeoPnt" : { "$within" : { "$geometry" :

{ "type" : "Polygon", "coordinates" : [[[-34.353119999999997, -50.140853], [-34.353119999999997, 76.184997999999993], [146.24300500000001, 76.184997999999993], [146.24300500000001, -50.140853], [-34.353119999999997, -50.140853]]] }

} } }}

As you can see the test polygon is well within the polygon defined in both cases.

I have been reading up about "big polygons" but both of these areas do not cover more than half a hemisphere so I have no idea why both of these queries do not work.

We are using Mongo 3.6.5 and version 2.4 of the C# driver using the legacy query methods.



 Comments   
Comment by Ian Hannah [ 24/Dec/18 ]

Hi Kelsey,

I am using the Mongo C# driver to try and create this query. So I have this:

            GeoJsonCoordinateReferenceSystem crs = new GeoJsonNamedCoordinateReferenceSystem("urn:x-mongodb:crs:strictwinding:EPSG:4326");

            GeoJsonObjectArgs<GeoJson2DGeographicCoordinates> args = new GeoJsonObjectArgs<GeoJson2DGeographicCoordinates>();

            args.CoordinateReferenceSystem = crs;

and use it in this call:

           IMongoQuery geoQuery = Query.Within<GeoJson2DGeographicCoordinates>(MongoItem.PolygonRegionProperty, new GeoJsonPolygon<GeoJson2DGeographicCoordinates>(args, new GeoJsonPolygonCoordinates<GeoJson2DGeographicCoordinates>(new GeoJsonLinearRingCoordinates<GeoJson2DGeographicCoordinates>(polygon.Select(i => new GeoJson2DGeographicCoordinates(i.Longitude, i.Latitude))))));

but this returns this:

                query    {{ "PId" : ObjectId("5c1bccda3930321b784dbade"), "GeoPnt" : { "$within" : { "$geometry" : { "type" : "Polygon", "crs" : { "type" : "name", "properties" :

{ "name" : "urn:x-mongodb:crs:strictwinding:EPSG:4326" }

}, "coordinates" : [[[-34.353119999999997, -50.140853], [-34.353119999999997, 76.184997999999993], [146.24300500000001, 76.184997999999993], [146.24300500000001, -50.140853], [-34.353119999999997, -50.140853]]] } } } }}

 whereas I need this:

        "$geometry" :{ 

            "type" : "Polygon", "coordinates" : [[[-34.353119999999997, -50.140853], [146.24300500000001, -50.140853], [146.24300500000001, 76.184997999999993], [-34.353119999999997, 76.184997999999993], [-34.353119999999997, -50.140853]]],    

            crs:

{                 type: "name",                 properties: \{ name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }

            } 

        }

i.e. the crs setting is in the wrong place and I cannot work out how to get it in the correct place!

Any help would be appreciated.

Thanks

Ian

Comment by Kelsey Schubert [ 24/Dec/18 ]

HI ihannah@meniscus.co.uk,

Thanks for report. In the second example, we can see that the points are a bit more than 180 degrees apart in longitude. As a result, by default MongoDB selects the smaller half of the area. I've drawn the first and second queries below to help visualize:
Here is the first query with

{ "type" : "Polygon", "coordinates" : [[[-33.353119999999997, -50.140853], [-33.353119999999997, 76.184997999999993], [146.24300500000001, 76.184997999999993], [146.24300500000001, -50.140853], [-33.353119999999997, -50.140853]]] }


Here is the second query with

{ "type" : "Polygon", "coordinates" : [[[-34.353119999999997, -50.140853], [-34.353119999999997, 76.184997999999993], [146.24300500000001, 76.184997999999993], [146.24300500000001, -50.140853], [-34.353119999999997, -50.140853]]] }

As you can see the selected space has inverted as the result of the lines being more than 180 degrees apart in longitude.

To override this behavior, you would want to ensure the winding order is correct and specify MongoDB's custom crs:

db.foo.find({"GeoPnt":{ 
    "$within" : { 
        "$geometry" :{ 
            "type" : "Polygon", "coordinates" : [[[-34.353119999999997, -50.140853], [146.24300500000001, -50.140853], [146.24300500000001, 76.184997999999993], [-34.353119999999997, 76.184997999999993], [-34.353119999999997, -50.140853]]],    
            crs: {
                type: "name",
                properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
            } 
        }
    }
}
})

For more details, I suggest checking out this blog post.

Please note that the SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Kind regards,
Kelsey

Generated at Thu Feb 08 04:49:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.