.NET核心实体框架保存空间地理数据类型SQL Server

时间:2019-05-10 07:29:19

标签: .net asp.net-core .net-core

我使用Entity Core 2.2开发ASP.NET Core应用程序。此应用程序可以保存位置边界。

我正在使用google.maps.drawing.DrawingManager允许用户创建一个多边形,以使用地理数据类型将边界保存到SQL Server数据库。以后,我需要检查特定的地理位置是否在此多边形内部或外部。

下面是调用.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) {

            }
        });

称为API

公共异步任务AddPolygon([FromForm]字符串sPolygon) {

}

由于实体核心不支持Geography数据类型,因此我建议安装NetTopologySuite(NTS)

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

搭建我的模型课后

具有“地理位置”数据类型的以下属性

public IGeometry StoreFence {get;组; }

我正在努力寻找有关如何现在使用NetTopologySuite类设置StoreFence属性以将Polygon保存到数据库的示例。

任何使用Entity Core / NetTopologySuite将google.maps.drawing.DrawingManager多边形保存到数据库中的SQL Server Geography类型列的示例都是很棒的。

1 个答案:

答案 0 :(得分:0)

解决方案

安装NetTopologySuite并使用Geogrpahy列将数据库折叠起来

我的模特有

public IGeometry BoundsFence {get;组; }

作为“地理位置类型”列的属性

下面的代码会将此属性设置为WKT(众所周知的tex)多边形

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

                        //check if orientation is not counter clock wise and reverse 
                        if (!storeFence.Shell.IsCCW)
                        {
                            storeFence = (IPolygon)storeFence.Reverse();
                        }
                        storeFence.SRID = 4326;
                        store.StoreFence = storeFence;

等待_dbContext.SaveChangesAsync();

相关问题