【问题标题】:Saving geography data type in sql在sql中保存地理数据类型
【发布时间】:2014-09-15 14:11:40
【问题描述】:

我正在尝试使用 c# asp 将数据添加到 sql 中的地理字段。净。

添加 POINt 或 LINESTRING 时没有错误,但添加 POLYGON 时出现以下错误;

  Message: [DataConnection.HandleError]: Query: Proc_CSA_AssetUpdateLocation: caused exception: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 
System.ArgumentException: 24200: The specified input does not represent a valid geography instance.
System.ArgumentException: 
at Microsoft.SqlServer.Types.SqlGeography.ConstructGeographyFromUserInput(GeoData g, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeography.Parse(SqlString s)

我传递的数据是(例如);

POLYGON((54.40854093377361 -6.197662353515625, 54.422126065167866 -6.212425231933594, 54.43011521616425 -6.164703369140625, 54.41093863702367 -6.128997802734375, 54.40094728183984 -6.150970458984375, 54.40854093377361 -6.197662353515625))

存储过程:

@Name nvarchar(20),
    @ModifiedWhen DateTime,
    @itemGUID UniqueIdentifier,
    @GeoLocs nvarchar(max),
    @Type int

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE dbo.csa_AssetGeoData 
    Set ItemModifiedWhen = @ModifiedWhen,GeoCord = @GeoLocs,GeographyTypeItemID = @Type, Name = @Name
    WHERE ItemGUID = @itemGUID

不幸的是,无论我在哪里看,我都找不到数据错误的原因。

我的问题是什么可能导致此错误?

如有必要,我可以提供更多信息,抱歉,如果这太含糊(没有提供信息)。

【问题讨论】:

    标签: c# sql asp.net .net geography


    【解决方案1】:

    你可以尝试传递字符串表示并在脚本中解析,类似这样:

    SET @g = geometry::Parse('POLYGON((1 3, 1 3, 1 3, 1 3))');
    

    我从MSDN Polygon 页面中提取了这一行。

    【讨论】:

    • 我试过了,但那是几何而不是地理,因此会在 sql 中引发错误;操作数类型冲突:sys.geometry 与 sys.geography 不兼容
    • 实际上,通过将nvarchar(max) 值分配给geography 类型的列,您正在导致解析失败并抛出异常。这就是异常堆栈所说的。
    • 我认为太多,但两个:POINT(-6.261348724365234 53.351268586147086)和:LINE(53.74221377343109 -8.3221435546875,53.02139221293762 -8.0804443359375,53.77468884583564 -7.6190185546875,53.40298249424814 -6.8719482421875,54.0335863352108 -7.2015380859375)做工精细!跨度>
    【解决方案2】:

    好的,我想通了。它不喜欢你的多边形线相互交叉。不知道为什么,但它正在保存未交叉的而不是交叉的。

    【讨论】:

      【解决方案3】:

      根据这个博客Ring Orientation SQL Spatial MSSQL 是左撇子。有很多方法可以检查功能的有效性。这是一个例子

              if (polygon.MakeValidIfInvalid().EnvelopeAngle() > 90)
              {
      
                  region.Shape = polygon.ReorientObject().Serialize().Value;
              }
              else
              {
                  region.Shape = polygon.Serialize().Value;
              }
      

      Serialize in SQLGeography 用于通过网络发送空间数据。因此它将实例转换为可以保存在数据库中的 bytes[]。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2022-01-17
        • 2021-12-13
        相关资源
        最近更新 更多