我编写了一个小型应用程序,它从一系列 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 消息 = 24200:指定的输入不代表有效的地理实例。
这发生在以下行:return geographyBuilder.ConstructedGeography;
我发现了一些对这个异常的引用,但是在我发现他们在 SQL Server 中遇到并处理这个异常的情况下,而不是在 C# 中。