This project has moved and is read-only. For the latest updates, please go here.

Retrieve Coordinates from Placemark

Jan 8, 2015 at 7:51 PM
Hello,

I'm attempting to utilize VB.NET to pull coordinates out of the Placemarks.

Example File:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357
          -112.262073428656,36.08626019085147,2357
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357 
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>
My code so far - I'm having a problem getting to the coordinates innertext:
Dim KMLFileStreamIn As New System.IO.FileStream("Test.kml", IO.FileMode.Open)
        Dim KMLFile As KmlFile = KMLFile.Load(KMLFileStreamIn)
        Dim KMLFileStreamOut As New System.IO.FileStream("TestOut.kml", IO.FileMode.Create)
        Dim Placemark As New Placemark


        Dim kml As Kml = KMLFile.Root
        If Not kml Is Nothing Then

            For Each LineString In kml.Flatten().OfType(Of LineString)()
                TextBox1.Text = LineString.Coordinates.ToString & " " & LineString.Id.ToString & LineString.TargetId.ToString '& Placemark.Id & vbNewLine

            Next


            For Each Placemark In kml.Flatten().OfType(Of Placemark)()
                TextBox1.Text = Placemark.Id.ToString & " " & Placemark.TargetId.ToString & vbNewLine  '& Placemark.Id & vbNewLine
               
            Next
Any help is greatly appreciated. Note: I have no Linq experience so if I need to use that can you please direct me towards some easy links - I'm starting to learn and have books but am very much a beginner.

Thank you.
Jan 8, 2015 at 9:21 PM
I think you're nearly there. To access the coordinates of the KML you've posted, you'll need to cast the Geometry property of the placemark to a LineString, something like the following (note I'm not a VB .NET guy so this is only my best guess at how it could look!)
For Each placemark In kml.Flatten().OfType(Of Placemark)()
    Dim lineString As LineString = TryCast(placemark, LineString)
    If lineString Is Not Nothing Then
        Dim coordinates As CoordinateCollection = lineString.Coordinates
        ' Do something with coordinates, such as iterate over it
    End If
Next
Hope it helps,
Sam