【问题标题】:Set SkipGeographyChecks to true将 SkipGeographyChecks 设置为 true
【发布时间】:2019-01-30 11:41:57
【问题描述】:

您好,我在保存多个多边形时遇到了这个异常。如何将 SkipGeographyChecks 设置为 true ?

例外:

在编写 SQL Server 地理值时,多边形的外壳必须逆时针方向。要编写没有外壳的多边形,请设置 SkipGeographyChecks

代码示例:

var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
var poly = new Polygon[] {
new Polygon(new LinearRing(new Coordinate[]
{
        new Coordinate(19.2385498607974, -51.50390625,0),
        new Coordinate(24.1367281697474, -37.6171875,0),
        new Coordinate(13.8487471475372, -18.10546875,0),
        new Coordinate(19.2385498607974, -51.50390625,0),
                    })),
new Polygon(new LinearRing(new Coordinate[]
{
        new Coordinate(-10.0445849842118, -53.0859375,0),
        new Coordinate(4.13824308398371, -58.7109375,0),
        new Coordinate(2.20770545570541, -68.73046875,0),
        new Coordinate(-8.83079518432893, -79.1015625,0),
        new Coordinate(-17.3820949478775, -81.2109375,0),
        new Coordinate(-21.0332372344673, -51.328125,0),
        new Coordinate(-10.0445849842118, -53.0859375,0),
})) 
};

var currentLocation = geometryFactory.CreateMultiPolygon(poly) as MultiPolygon;
dbset.Polygons = currentLocation;
_context.Add(dbset);
await _context.SaveChangesAsync();

【问题讨论】:

    标签: geometry entity-framework-core polygon


    【解决方案1】:

    据我所知,SkipGeographyChecks 在对其的引用从该错误中删除之前已作为属性被删除 - Github issue for removing the reference to this property

    我还发现,如果多边形的点按错误的方向排列,可能会出现此错误。惯例是点的方向是多边形的“内部” - 即:如果多边形的点是逆时针的,则该多边形的区域在边界内,但如果点是顺时针的,那么该区域是整个世界除了在该边界内(因为这些点基本上是逆时针围绕边界外的所有东西)。您可以通过检查 IPolygon.Shell.IsCCW 属性来确定这是否是您的问题。如果这是您的问题,您可以使用IPolygon.Shell.Reverse() 方法解决它。有关您可以使用的一些实际代码,请参阅 this GIS StackExchange question(我在其中找到此信息)。

    【讨论】:

      【解决方案2】:

      希望这可以节省一些时间。

      我还需要解决问题中引用的错误(多边形必须逆时针方向)。 我一直在寻找一种干净的方法来解决问题,而不必在任何地方都反转几何形状

      在阅读了几个问题后,我收集了以下信息:

      • 配置 NetTopologySuite.NtsGeometryServices 以简化几何图形的使用
      • GeometryFactoryEx 类被创建用于处理方向
      • 扩展NetTopologySuite.NtsGeometryServices 以在整个应用程序中使用GeometryFactoryEx(请参阅this comment

      这是我最终需要的

      扩展NetTopologySuite.NtsGeometryServices的类

      我刚刚添加了我需要的构造函数。

          public class NtsGeometryServicesEx : NetTopologySuite.NtsGeometryServices
          {
              public NtsGeometryServicesEx() { }
      
              public NtsGeometryServicesEx(PrecisionModel precisionModel, int srid)
                  : base(precisionModel, srid)
              {
              }
              
      
              protected override GeometryFactory CreateGeometryFactoryCore(
                  PrecisionModel precisionModel,
                  int srid,
                  CoordinateSequenceFactory coordinateSequenceFactory)
              {
                  return new GeometryFactoryEx(precisionModel, srid, coordinateSequenceFactory);
              }
          }
      

      在应用程序启动时将我所需的配置分配给默认的NetTopologySuite.NtsGeometryServices 实例

        // set default NTS Geometry Services
        NetTopologySuite.NtsGeometryServices.Instance
          = new Infrastructure.Spatial.NtsGeometryServicesEx(
            PrecisionModel.Floating.Value, GeometryExtensions.Wgs84Srid);
      

      瞧! - 解决了外壳方向错误。

      【讨论】:

      • 我已经完全按照你的方式实现了这个,并且正在运行 NtsGeometryServices.Instance.CreateGeometryFactory(srid) 来创建我的多边形,但我仍然收到错误。
      猜你喜欢
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2011-10-23
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多