1

I developing an ASP.NET Core application using Entity Core 2.2. This application saves location boundaries.

I am using google.maps.drawing.DrawingManager to allow users to create a polygon for saving boundaries to a SQL server database using geography data type. I need to in future check if specific geo points are inside or outside this polygon.

Below is the code that calls a .NET Web API

function onPolygonComplete(e) {
    if (!cancelDrawingShape) {
        var Path= e.getPath();


        var thisData = {
            sPolygon: JSON.stringify(path.getArray())            };

        $.ajax({
            url: rootPath + 'Location/AddPolygon',
            data: thisData,
            type: 'POST',
            success: function (res) {
                e.setMap(null);
                removeAllFeatures();
                clientGeoFences = res.ClientGeoFences;
                refreshData(false);
            },
            failure: function (res) {

            }
        });

Called API

public async Task<IActionResult> AddPolygon([FromForm] string sPolygon)
{
}

Since Geography datatype is not supported in Entity Core I Installed NetTopologySuite (NTS) as suggested

https://learn.microsoft.com/en-us/ef/core/modeling/spatial

After scaffolding my model class

has the below property for Geography data type

public IGeometry StoreFence { get; set; }

I am struggling to find examples of how to now use NetTopologySuite classes to set StoreFence property to save the Polygon to the database.

Any examples of saving google.maps.drawing.DrawingManager polygons to SQL Server Geography type column in the database using Entity Core/NetTopologySuite will be great.

1 Answer 1

3

I got the solution after installing NetTopologySuite and scaffolding the DB with Geography column.


My model has:

public IGeometry BoundsFence { get; set; }

As the property for Geography type column.


The code below will set this property to WKT (Well-Known Text) polygon:

IPolygon storeFence = (IPolygon)new WKTReader().Read(wktPolygon);

//Check if the orientation is not counter clock wise and reverse.
if (!storeFence.Shell.IsCCW)
    storeFence = (IPolygon)storeFence.Reverse();

storeFence.SRID = 4326;
store.StoreFence = storeFence;

await _dbContext.SaveChangesAsync();
Sign up to request clarification or add additional context in comments.

3 Comments

I'm in the processing of working on something similar. However, when I add the library and the "using" it stills tells me IGeometry isn't a valid interface. Am I missing something?
@Sean: nope. Forget the interfaces and just use Polygon and Geometry types.
@ZPat thanks for providing your solution with all the code. I am in the process of converting an EF6 project to EF Core and have been trying to find how you determine if the poly is CCW or not and your example just solved it for me so thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.