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.
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:
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
JsonRowDataSerializerclass 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:
There probably is more to be considered, but I thought it would be worth floating the idea to see if there is any interest.