Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos: Spatial types, functions and spatial indexes #17317

Open
Tracked by #22951 ...
AndriySvyryd opened this issue Aug 20, 2019 · 16 comments
Open
Tracked by #22951 ...

Cosmos: Spatial types, functions and spatial indexes #17317

AndriySvyryd opened this issue Aug 20, 2019 · 16 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 20, 2019

Use GeoJSON which supports point, polygon and linestring

@smitpatel smitpatel mentioned this issue Aug 20, 2019
82 tasks
@ajcvickers ajcvickers added this to the Backlog milestone Aug 26, 2019
@xtellurian
Copy link

I was super stoked to use EF Core with dotnet 3 - but I've run into an issue with GeoJSON and the Cosmos DB database.

The GeoJSON spec says that coordinates must be stored as follows:

{
"type": "Point",
"coordinates": [125.6, 10.1]
}

So I created a C# POCO class like so:

public class GeoLocation
    {
        [JsonProperty("type")]
        public string Type { get; set; }
        [JsonProperty("coordinates")]
        public double[] Coordinates { get; set; }
    }

However using this with EF Core + Cosmos throws the following:

Exception has occurred: CLR/System.InvalidOperationException
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll: 'The property 'GeoLocation.Coordinates' could not be mapped, because it is of type 'double[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Cosmos.Internal.CosmosModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

The biggest pain point for us is that Azure Search requires that coordinates be serialised as GEOJson, which is just not possible when using EF Core.

In conclusion, it is not possible to use Cosmos, EF Core, and Azure Search with Geo Spacial data :(

@bricelam
Copy link
Contributor

@AndriySvyryd Any easy workarounds using NetTopologySuite.IO.GeoJSON?

var geoJsonString = new GeoJsonWriter().Write(point);
var point = new GeoJsonReader().Read<Point>(geoJsonString);

@AndriySvyryd
Copy link
Member Author

AndriySvyryd commented Sep 26, 2019

Yes, setting up value converters should be enough to get basic functionality.

@bricelam
Copy link
Contributor

@xtellurian Does it have to be embedded JSON or will it work as a string containing JSON?

@bricelam
Copy link
Contributor

bricelam commented Sep 26, 2019

@AndriySvyryd Do we automatically embed JObject properties?

var jObject = new JObject();
new GeoJsonWriter().Write(point, new JTokenWriter(jObject));

var point = new GeoJsonReader().Read<Point>(new JTokenReader(jObject));

@AndriySvyryd
Copy link
Member Author

We don't have any special handling for JObject, but string should work, I'll try it out later

@xtellurian
Copy link

Thanks for jumping on this to help :)

@bricelam
I tried manually serializing the GeoJSON in CosmosDB and indexing with Azure Search (as below), and the search indexer seems like it works!

{
"GeoLocation": "{ \"type\": \"Point\", \"coordinates\": [100.2093,-15.868]}"
}

@AndriySvyryd
It sounds like I can use value conversions to serialize/deserialize the GEOJson with the EF CosmosDB provider.

I don't see a need to use NetTopologySuite.IO.GeoJSON ...

@bricelam
Copy link
Contributor

lol, using NTS just to serialize points is definitely overkill. It’ll come in handy for more complex spatial scenarios though

@cwoolum
Copy link

cwoolum commented Apr 22, 2020

The problem with the solution above is that the whole GeoLocation object is actually saved as a string instead of a JSON literal. This means that Cosmos Indexing won't work. Is there a way to use a value converter but the force EF Core to use the converted value verbatim?

It should be saved as

{
    "GeoLocation": {
        "type": "Point", 
        "coordinates": [100.2093,-15.868]
    }
}

@AndriySvyryd
Copy link
Member Author

@cwoolum You'd be able to do it when #20584 is implemented.
For now you can modify the underlying JObject directly

@cwoolum
Copy link

cwoolum commented Apr 23, 2020

In the short term, I think that by utilizing that with a raw sql command, you could feasibly use ST_DISTANCE querying using EF Core and Cosmos.

@johnn82
Copy link

johnn82 commented Sep 15, 2021

"GeoLocation": "{ \"type\": \"Point\", \"coordinates\": [100.2093,-15.868]}"

@xtellurian this is literally not working! I just tried it with the data explorer. I added the GeoLocation property to a document and then filtered using the query: "WHERE ST_DISTANCE(c.GeoLocation, {"type": "Point", "coordinates":[100.209,-15.86]}) < 40000". No results were provided.

As said by @cwoolum, it cannot work if the GeoJSON object is saved as a serialized string.

Does anybody know how to make it works?

@xtellurian
Copy link

@johnn82 It worked with Azure Search but I never tried using purely Cosmos

@WillTheProgrammer
Copy link

erialize the GEOJson with the EF CosmosDB provider.

@xtellurian - please, please, please provide a quick writeup and code sample of how to do this.

@georgechond94
Copy link

Hello! Any news on this?

@ajcvickers
Copy link
Member

This issue is in the Backlog milestone. We have not yet fully planned the next release but we will re-assess the backlog as part of this. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants