Hi,
{ coordinates: { longitude, latitude }, type: ‘Point’ } should be an invalid geoJSON object.
Thanks for reporting this! It is an issue, and we have opened https://jira.mongodb.org/browse/SERVER-29021 to track its progress. Please feel free to upvote/watch the ticket.
Best regards,
Kevin
Hi Tim
I have found a similar issue (maybe with the .Net driver) - when serializing a MongoDb GeoJSON Point object using the MongoDb.Net driver it yields the following invalid GeoJSON
I tried this code using the C# driver version 2.4.1:
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GeoJsonObjectModel;
namespace ConsoleApplication
{
public class GeoDoc {
public double Id {get; set;}
public GeoJsonPoint<GeoJson2DGeographicCoordinates> loc {get; set;}
}
public class GeoTest {
public static void Main(string[] args) {
var client = new MongoClient("mongodb://localhost:27017");
var collection = client.GetDatabase("test").GetCollection<GeoDoc>("geo");
var doc = collection.Find(new BsonDocument{{"_id", 1}}).First();
Console.WriteLine(doc.ToJson());
}
}
}
but haven’t been able to see the same result you do:
{ "_id" : 1.0, "loc" : { "type" : "Point", "coordinates" : [1.0, 2.0] } }
which is identical to the content of the collection:
> db.geo.find()
{ "_id": 1, "loc": { "type": "Point", "coordinates": [ 1, 2 ] } }
Could you post a compilable version of the code used, an example document from the collection, along with your MongoDB version and your C# driver version?
Best regards,
Kevin
{"id":1.0,"loc":{"coordinates":{"values":[-121.9,37.5],"longitude":-121.9,"latitude":37.5},"type":7,"boundingBox":null,"coordinateReferenceSystem":null,"extraMembers":null}}
{"id":2.0,"loc":{"coordinates":[-96.770401,32.816774],"type":"Point"}}
Hi Tim
I believe what you’re seeing is due to the mix between objects & the serializer used. As an example, I tried this code:
// GeoJSON.Net Point object
var Loc1 = new GeoJSON.Net.Geometry.Point(new GeoJSON.Net.Geometry.Position(1, 2));
// MongoDB GeoJsonPoint object
var Loc2 = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(1, 2));
// A: Serialize GeoJSON.Net Point object using MongoDB ToJson() method
Console.WriteLine("A: " + Loc1.ToJson());
// B: Serialize GeoJSON.Net Point object using Newtonsoft SerializeObject() method
Console.WriteLine("B: " + Newtonsoft.Json.JsonConvert.SerializeObject(Loc1));
// C: Serialize MongoDB GeoJsonPoint object using MongoDB ToJson() method
Console.WriteLine("C: " + Loc2.ToJson());
// D: Serialize MongoDB GeoJsonPoint object using Newtonsoft SerializeObject() method
Console.WriteLine("D: " + Newtonsoft.Json.JsonConvert.SerializeObject(Loc2));
and the resulting output is:
A: { "BoundingBoxes" : null, "CRS" : { "_t" : "DefaultCRS", "Properties" : { "name" : "urn:ogc:def:crs:OGC::CRS84" }, "Type" : 1 }, "Type" : 0, "Coordinates" : { "_t" : "Position", "Altitude" : null, "Latitude" : 1.0, "Longitude" : 2.0 } }
B: {"coordinates":[2.0,1.0],"type":"Point"}
C: { "type" : "Point", "coordinates" : [1.0, 2.0] }
D: {"Coordinates":{"Values":[1.0,2.0],"Longitude":1.0,"Latitude":2.0},"Type":7,"BoundingBox":null,"CoordinateReferenceSystem":null,"ExtraMembers":null}
Result marked “D” looks quite similar to the serialized output you have. This output is the result of MongoDB C# driver’s GeoJsonPoint
object when serialized using Newtonsoft’s SerializeObject
method (which appears to be GeoJSON.Net’s default serializer).
Note that outputs marked “A” and “D” are not canonical GeoJSON object according to RFC 7946, whereas “B” and “C” are correct GeoJSON objects.
I believe the mismatched object/serializer outputs the internal representation of the objects, instead of the correct GeoJSON object.
Also of note is GeoJSON.Net’s Latitude-Longitude coordinate order during object creation, which contrasts with the C# Driver’s Longitude-Latitude order. This results in the reversed coordinates in the “B” vs. “C” output above.
Best regards,
Kevin