gpt4 book ai didi

sql-server-2008 - 使用 SqlGeographyBuilder 时“指定的输入不代表有效的地理实例”异常

转载 作者:行者123 更新时间:2023-12-04 06:56:12 25 4
gpt4 key购买 nike

我编写了一个小应用程序,它从一系列 KML 文件中读取数据,然后使用以下代码将它们转换为 Microsoft.SqlServer.Types.SqlGeography 类型:

    private SqlGeography CreateGeographyFromKML( string kml, bool debug )
{
// use SqlGeographyBuilder to help create the SqlGeography type
var geographyBuilder = new SqlGeographyBuilder();

// Get co-ordinates
var xml = XDocument.Parse(kml);
var df = xml.Root.Name.Namespace;
XElement coordinates = xml.Descendants(df + "coordinates").Single();

// set the Spatial Reference Identifiers that will used to create the point
geographyBuilder.SetSrid(_srid);
geographyBuilder.BeginGeography(OpenGisGeographyType.Polygon);

var longLat = coordinates.Value.Split(' ').Select(c => new { Lat = Convert.ToDouble(c.Split(',')[1]), Long = Convert.ToDouble(c.Split(',')[0]) });

Console.Write("Found {0} ", longLat.Count());

foreach (var coord in longLat.Select((x, i) => new { Index = i, Value = x }))
{
if (coord.Index == 0)
{ // First point
if ( debug ) Console.WriteLine("First point: {0},{1}", coord.Value.Lat, coord.Value.Long);
geographyBuilder.BeginFigure(coord.Value.Lat, coord.Value.Long);
}
else
{ // Intermediate points
if (debug) Console.WriteLine("Intermediate point: {0},{1}", coord.Value.Lat, coord.Value.Long);
geographyBuilder.AddLine(coord.Value.Lat, coord.Value.Long);
}

if (coord.Index == longLat.Count() - 1 )
{ // Last point (Close polygon)
if (debug) Console.Write("Last point: ");

// Check not already added
if (longLat.Last().Lat == longLat.First().Lat && longLat.Last().Long == longLat.First().Long)
{
if (debug) Console.Write("Already exists - not adding...");
}
else
{
if (debug) Console.Write("{0},{1}", longLat.Last().Lat, longLat.Last().Long);
geographyBuilder.AddLine(longLat.Last().Lat, longLat.Last().Long);
}

geographyBuilder.EndFigure(); // End figure
}
}

if (debug) Console.WriteLine();

// close the figure and geography class
geographyBuilder.EndGeography();

// get the geography builder to return the sqlgeography type
return geographyBuilder.ConstructedGeography;
}

基本上,这段代码从 KML 文件中检索纬度/经度列表,然后遍历它们以创建一个多边形。

但是,我导入的某些 KML 文件失败,但出现以下异常:

System.ArgumentException was caught Message=24200: The specified input does not represent a valid geography instance.

这发生在以下行:return geographyBuilder.ConstructedGeography;

我找到了一些 reference to this exception ,但是在我发现他们在 SQL Server 中而不是在 C# 中遇到并处理此异常的情况下。

最佳答案

我遇到了同样的错误,但结果证明是多边形环方向问题。一个简单的翻转坐标数组顺序的问题就解决了。

为了说明,这失败并出现上述错误:

 select geography::STGeomFromText ('Polygon  ( (10 10, 10 20, 20 20, 20 10, 10 10))',4326)

这是可行的:

 select geography::STGeomFromText ('Polygon  ( (10 10, 20 10, 20 20, 10 20, 10 10))',4326)

请注意,我不是在一个点内翻转 x,y 对,而是翻转整个点数组的顺序(例如 {pt1, pt2, pt3, pt4, pt5} 变为 {pt5, pt4, pt3, pt2, pt1}

关于sql-server-2008 - 使用 SqlGeographyBuilder 时“指定的输入不代表有效的地理实例”异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8271349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com