2

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>
  • 3
    Welcome to SO! Unfortunately, as it currently stands, your question is too broad. I doubt that many readers here are familiar with the details of the KML format. Maybe it would help if you could provide a short, minimal example of a KML file that you want to parse (reduced to the relevant parts). In general, I think that [VB LINQ to XML](http://stackoverflow.com/a/1066418/87698) will help you with your problem, but it's hard to tell without details. – Heinzi Jun 11 '14 at 20:36
  • Ok thanks for the advice, I added a very simple kml above. – DonaldShadow Jun 13 '14 at 18:01

1 Answers1

2

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519