DonaldShadow
DonaldShadow

Reputation: 23

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

I've been trying all day to write some code that will read the data from a kml containing a google earth polygon and extract the name and the coordinates and store everything as latitudes and longitudes. I already made a user form that allows the user to browse for the kml and then run the extraction code. Unfortunately the extraction doesn't work. I'm very new to VB but I did take three semesters of C++ in college, it's been close to a year since then though. Here's what I have but I understand that I could also be completely wrong..

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

Basic KML Example:

<?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>

Upvotes: 2

Views: 2522

Answers (1)

Heinzi
Heinzi

Reputation: 172270

As far as I can tell, you have the following subproblems:

  1. Parse the XML and extract the name and the coordinates.

  2. Split the coordinates into some data structure.


For step 1, VB's LINQ to XML is the easiest way. The following working code example should help you get started:

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

Some remarks:

Step 2 is left as an exercise, but String.Split (first on line breaks, then on the comma) and String.Trim (to remove the whitespace) should let you solve this easily.

Upvotes: 2

Related Questions