读取kml文件以在c#中获取坐标

Reading kml file to get coordinates in c#

好的,所以我一直在努力读取一个 kml 文件,其中包含美国每个 county/city 边界的坐标。但是,我 运行 遇到了一些问题。特别是如何获取NextNode的值,当元素值中间有另一个元素标签时如何处理。

kml 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="gadm36_USA_2" id="gadm36_USA_2">
    <SimpleField name="NAME_0" type="string"></SimpleField>
    <SimpleField name="NAME_1" type="string"></SimpleField>
    <SimpleField name="NAME_2" type="string"></SimpleField>
</Schema>
<Folder><name>gadm36_USA_2</name>
  <Placemark>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#gadm36_USA_2">
        <SimpleData name="NAME_0">United States</SimpleData>
        <SimpleData name="NAME_1">Alabama</SimpleData>
        <SimpleData name="NAME_2">Autauga</SimpleData>
    </SchemaData></ExtendedData>
      <MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-86.8189620971679,32.3402709960939 -86.8108367919922,32.3471298217775 -86.8097915649414,32.3535118103028 -86.8103485107422,32.3585205078126 -86.8158340454101,32.3703498840333 -86.8239974975586,32.3785285949708 -86.8310775756835,32.3839797973634 -86.83544921875,32.3912506103515 -86.8419876098633,32.3980712890626 -86.8452758789062,32.4044418334961 -86.8458633422851,32.4140090942383 -86.8447875976562,32.4167404174805 </coordinates></LinearRing></outerBoundaryIs></Polygon><Polygon><outerBoundaryIs><LinearRing><coordinates>-86.8426208496093,32.4181213378907 -86.8361129760742,32.4204101562501 -86.8296127319336,32.4227104187012 -86.8274383544922,32.4240798950195 -86.8263626098633,32.4259109497071 -86.8280029296875,32.4277305603028 -86.8307189941406,32.4295387268066 </coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>

请注意,这不是这些 运行dom 元素标签出现在坐标中间的实际示例,拥有它们的县通常有大量坐标列表,根据我的经验,如果我通过kml 文件并且只使用第一个元素标签之前的值,它似乎绘制出正确的县边界

List<string> locationList = new List<string>();
var doc = XDocument.Load("gadm36_USA1.kml");
XNamespace ns = "http://www.opengis.net/kml/2.2";
var result = doc.Root.Descendants(ns + "Placemark");
foreach (XElement xmlInfo in result)
{
   var region = xmlInfo.Element(ns + "ExtendedData").Element(ns + "SchemaData").Value;
   //var country = region.Element(ns + "SimpleData").Value;
   //var state = region.Element(ns + "SimpleData");
   //var cityCounty = region.Element(ns + "SimpleData");
   locationList = xmlInfo.Element(ns + "MultiGeometry").Value.Split(' ').ToList();
   CountyCoordinates.Add(region, locationList);
}

所以当我到达变量 "region" 时,它将所有元素值组合在一起。例如,它会说 "United StatesAutaugaAlabama"。

至于坐标,由于坐标值中间有这些运行dom元素标签,当我用“”分割坐标时,当它碰到这些[=26时就搞砸了=]dom元素标签。 (当它到达坐标中间的文本时,拆分将 return '-86, 32 -86',而不仅仅是 '-86, 32')所以,我基本上是在寻求帮助尽管有这些 运行dom 元素标签,如何分别读取国家、州和县以及如何正确读取坐标。

我刚刚通过以下获取结果进行了测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            List<XElement> simpleFields;

            List<XElement> extendedDatas = doc.Descendants(ns + "ExtendedData").ToList(); 

            foreach(XElement extendedData in extendedDatas)
            {
                simpleFields = extendedData.Descendants(ns + "SimpleData").ToList(); 
            } 
        }
    }
}

我想你的意思是:

     List<XElement> extendedDatas = doc.Descendants(ns + "ExtendedData").ToList();
            var a_docment = root.Element(ns + "Document").Value;
            var result = doc.Root.Descendants(ns + "Placemark");
            foreach (XElement xmlInfo in result)
            {
     
                List<string> locationList = new List<string>();
                var gg  = xmlInfo.Descendants(ns + "outerBoundaryIs").ToList();
    
                foreach (XElement extendedData in gg)
                {
                    locationList.AddRange(extendedData.Value.Split(' ').ToList());
                }