Skip to content

Cosmos: Spatial types, functions and spatial indexes #17317

Description

@AndriySvyryd

Use GeoJSON which supports point, polygon and linestring

Activity

  1. added this to the Backlog milestone on Aug 26, 2019
  2. xtellurian commented on Sep 26, 2019

    @xtellurian

    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 :(

  3. bricelam commented on Sep 26, 2019

    @bricelam
    Contributor

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

    var geoJsonString = new GeoJsonWriter().Write(point);
    var point = new GeoJsonReader().Read<Point>(geoJsonString);
  4. AndriySvyryd commented on Sep 26, 2019

    @AndriySvyryd
    MemberAuthor

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

  5. bricelam commented on Sep 26, 2019

    @bricelam
    Contributor

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

  6. bricelam commented on Sep 26, 2019

    @bricelam
    Contributor

    @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));
  7. AndriySvyryd commented on Sep 26, 2019

    @AndriySvyryd
    MemberAuthor

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

  8. xtellurian commented on Sep 26, 2019

    @xtellurian

    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 ...

  9. bricelam commented on Sep 26, 2019

    @bricelam
    Contributor

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

  10. cwoolum commented on Apr 22, 2020

    @cwoolum

    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]
        }
    }
    
  11. AndriySvyryd commented on Apr 23, 2020

    @AndriySvyryd
    MemberAuthor

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

  12. cwoolum commented on Apr 23, 2020

    @cwoolum

    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.

  13. johnn82 commented on Sep 15, 2021

    @johnn82
    "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?

  14. xtellurian commented on Sep 16, 2021

    @xtellurian

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

  15. WillTheProgrammer commented on Apr 25, 2022

    @WillTheProgrammer

    erialize the GEOJson with the EF CosmosDB provider.

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

  16. georgechond94 commented on Dec 8, 2023

    @georgechond94

    Hello! Any news on this?

  17. ajcvickers commented on Dec 11, 2023

    @ajcvickers
    Contributor

    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.

  18. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions