关于.net:使用VB.NET从KML提取多边形名称和坐标

Extract Polygon name and coordinates from a KML using VB.NET

我整天都在尝试编写一些代码,这些代码将从包含Google Earth多边形的kml中读取数据,并提取名称和坐标并将所有内容存储为纬度和经度。
我已经制作了一个用户表单,该表单允许用户浏览kml,然后运行提取代码。不幸的是,提取不起作用。
我是VB的新手,但我确实在大学读了三个学期的C,但是从那时起已经快一年了。
这是我所拥有的,但我知道我也可能完全错了。

1
2
3
4
5
6
7
8
9
10
11
12
13
Function X(InputFile As String, Text As String)

    Dim textReader As New Xml.XmlTextReader(InputFile)
    Dim lastElementName As String =""
    While textReader.Read()
        Select Case textReader.NodeType
            Case Xml.XmlNodeType.Element
                lastElementName = textReader.Name
            Case Xml.XmlNodeType.Text
                MsgBox(lastElementName &":" & textReader.Value)
       End Select
       Console.WriteLine()
   End While

基本KML示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>The Pentagon</name>
    <Polygon>
      <tessellate>1</tessellate>
      <outerBoundaryIs>
        <LinearRing>
      <coordinates>
        -77.05668055019126,38.87154239798456
        -77.05542625960818,38.87167890344077
        -77.05485125901024,38.87076535397792
        -77.05577677433152,38.87008686581446
        -77.05691162017543,38.87054446963351
        -77.05668055019126,38.87154239798456
      </coordinates>
    </LinearRing>
   </outerBoundaryIs>
    </Polygon>
  </Placemark>
</kml>


据我所知,您有以下子问题:

  • 解析XML并提取名称和坐标。

  • 将坐标拆分为某些数据结构。

  • 对于步骤1,VB的LINQ to XML是最简单的方法。以下工作代码示例应帮助您入门:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    Imports <xmlns="http://www.opengis.net/kml/2.2">

    Module Module1
        Sub Main()
            Dim xdata = New XDocument( _
                <kml xmlns="http://www.opengis.net/kml/2.2">
                    <Placemark>
                        <name>The Pentagon</name>
                        <Polygon>
                            <tessellate>1</tessellate>
                            <outerBoundaryIs>
                                <LinearRing>
                                    <coordinates>
                                    -77.05668055019126,38.87154239798456
                                    -77.05542625960818,38.87167890344077
                                    -77.05485125901024,38.87076535397792
                                    -77.05577677433152,38.87008686581446
                                    -77.05691162017543,38.87054446963351
                                    -77.05668055019126,38.87154239798456
                                    </coordinates>
                                </LinearRing>
                            </outerBoundaryIs>
                        </Polygon>
                    </Placemark>
                </kml>)

            For Each p In xdata.Root.<Placemark>
                Console.WriteLine("Name:" & p.<name>.Value)

                For Each c In p...<coordinates>
                    Console.WriteLine("Coordinates:" & c.Value)
                Next
            Next

            Console.ReadLine()
        End Sub
    End Module

    一些评论:

    • 如果要从文件读取数据,请使用XDocument.Load而不是New XDocument
    • 文件顶部的Imports语句很重要,它必须与KML文件开头的xmlns声明匹配。

    第2步是练习,但是String.Split(首先在换行符上,然后在逗号上)和String.Trim(除去空格)应该可以轻松解决此问题。