Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 6, 2022 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/9d89bd3d5ce5d0447a62b629d0fc2337 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/9d89bd3d5ce5d0447a62b629d0fc2337 to your computer and use it in GitHub Desktop.
Read GPX Files using C#
// This code example demonstrates how to read GPX Nested Attributes from GPX file
// Specify GPX option
GpxOptions options = new GpxOptions()
{
ReadNestedAttributes = true
};
// Load the GPX file and open layer to read features
using (var layer = Drivers.Gpx.OpenLayer(@"D:\Files\GIS\nested_data.gpx", options))
{
foreach (var feature in layer)
{
// Check for MultiLineString geometry
if (feature.Geometry.GeometryType == GeometryType.MultiLineString)
{
// Read segment
var lines = (MultiLineString) feature.Geometry;
for (int i = 0; i < lines.Count; i++)
{
Console.WriteLine($"....segment({i})......");
var segment = (LineString)lines[i];
// Read points in segment
for (int j = 0; j < segment.Count; j++)
{
// Look for attribute
string attributeName = $"name__{i}__{j}";
if (layer.Attributes.Contains(attributeName) && feature.IsValueSet(attributeName))
{
// Print a point and attribute
var value = feature.GetValue<string>(attributeName);
Console.WriteLine($"{segment[j].AsText()} - {attributeName}: {value}, ");
}
else
{
// Print a point only
Console.WriteLine(segment[j].AsText());
}
}
}
Console.WriteLine("..........");
}
}
}
// This code example demonstrates how to read GPS routes from GPX file
// Load the GPX file
var layer = Drivers.Gpx.OpenLayer(@"D:\Files\GIS\schiehallion.gpx");
foreach (var feature in layer)
{
// Check for LineString geometry
if (feature.Geometry.GeometryType == GeometryType.LineString)
{
// Read Routs
LineString ls = (LineString)feature.Geometry;
foreach (var point in ls)
{
Console.WriteLine(" X: " + point.X + " Y: " + point.Y + " Z: " + point.Z);
}
}
}
// This code example demonstrates how to read Tracks from GPX file
// Load the GPX file
var layer = Drivers.Gpx.OpenLayer(@"D:\Files\GIS\nested_data.gpx");
foreach (var feature in layer)
{
// Check for MultiLineString geometry
if (feature.Geometry.GeometryType == GeometryType.MultiLineString)
{
// Read track
var lines = (MultiLineString)feature.Geometry;
foreach(var line in lines)
{
Console.WriteLine(line.AsText());
}
}
}
// This code example demonstrates how to read waypoints from GPX file
// Load the GPX file
var layer = Drivers.Gpx.OpenLayer(@"D:\Files\GIS\St_Louis_Zoo_sample.gpx");
foreach (var feature in layer)
{
// Check for Point geometry
if (feature.Geometry.GeometryType == GeometryType.Point)
{
// Read Points
Point point = (Point)feature.Geometry;
Console.WriteLine(point.AsText() + " X: " + point.X + " Y: " + point.Y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment