Skip to content

Add support for spatial data? #37

@mguinness

Description

@mguinness

Many EF Core providers support spatial data with NetTopologySuite library. After adding NetTopologySuite package you add the Point type to an entity storing longitude and latitude of a location which can then be queried as follows:

var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
var location = geometryFactory.CreatePoint(new Coordinate(lng, lat));
var result = await ctx.Places.Where(x => x.Location.Distance(location) < 5).ToListAsync();

I did some preliminary research and it seems possible with some minor changes. Firstly you need to add GeoJSON4STJ library to the FileBaseContext project. Then you need to change the JsonRowDataSerializer class as follows:

internal static JsonSerializerOptions CreateJsonOptions(IEnumerable<JsonColumnInfo> columns)
{
    return new JsonSerializerOptions()
    {
        AllowTrailingCommas = true,
        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
        WriteIndented = true,
        Converters =
        {
            new JsonRowDataConverter(columns),
+           new GeoJsonConverterFactory()
        },
+       NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
+       ReferenceHandler = ReferenceHandler.Preserve,
    };
}

This results in locations being serialized as follows:

"Location": {
  "type": "Point",
  "coordinates": [
    -77.036560,
    38.897957
  ]
}

There probably is more to be considered, but I thought it would be worth floating the idea to see if there is any interest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions