Skip to content

Instantly share code, notes, and snippets.

@eeevans
Created April 13, 2022 20:28
Show Gist options
  • Save eeevans/f6000b732821fb4d93581554a8d6cf20 to your computer and use it in GitHub Desktop.
Save eeevans/f6000b732821fb4d93581554a8d6cf20 to your computer and use it in GitHub Desktop.
Simple KML file reading
Simple placemark
37.42228990140251, -122.0822035425683, 0
Floating placemark
37.4220033612141, -122.084075, 50
Extruded placemark
37.42156927867553, -122.0857667006183, 50
Roll over this icon
37.42243077405461, -122.0856545755255, 0
Descriptive HTML
No Points
Tessellated
No Points
Untessellated
No Points
Absolute
No Points
Absolute Extruded
No Points
Relative
No Points
Relative Extruded
No Points
Building 40
No Points
Building 41
No Points
Building 42
No Points
Building 43
No Points
The Pentagon
No Points
Absolute
No Points
Absolute Extruded
No Points
Relative
No Points
Relative Extruded
No Points
using SharpKml.Dom;
using SharpKml.Engine;
using var stream = new FileStream("./kml_samples.kml", FileMode.Open, FileAccess.Read);
var kmlFile = KmlFile.Load(stream);
var kml = kmlFile?.Root as Kml;
foreach(var item in kml.Flatten().OfType<Placemark>())
{
var point = item.Flatten().OfType<Point>().FirstOrDefault();
Console.WriteLine(item.Name);
Console.WriteLine(point is null ?
" No Points" :
$" {point?.Coordinate.Latitude.ToString()}, {point?.Coordinate.Longitude.ToString()}, {point?.Coordinate.Altitude.ToString()}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment