Work with GPS Exchange Format GPX files in C#
Contents
[
Hide
]
Working with GPS Exchange File (GPX) Files
Aspose.GIS lets you open and read features from GPS Exchange File (GPX) files.
Reading Features from GPX File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = Drivers.Gpx.OpenLayer(dataDir + "schiehallion.gpx")) | |
{ | |
foreach (var feature in layer) | |
{ | |
switch (feature.Geometry.GeometryType) | |
{ | |
// GPX waypoints are exported as features with point geometry. | |
case GeometryType.Point: | |
Console.WriteLine(feature.Geometry.Dimension); | |
//HandleGpxWaypoint(feature); | |
break; | |
// GPX routes are exported as features with line string geometry. | |
case GeometryType.LineString: | |
//HandleGpxRoute(feature); | |
LineString ls = (LineString)feature.Geometry; | |
foreach (var point in ls) | |
{ | |
Console.WriteLine(point.AsText()); | |
} | |
break; | |
// GPX tracks are exported as features with multi line string geometry. | |
// Every track segment is line string. | |
case GeometryType.MultiLineString: | |
//HandleGpxTrack(feature); | |
Console.WriteLine(feature.Geometry.AsText()); | |
break; | |
default: break; | |
} | |
} | |
} |
Reading Features for each point in segment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
// specify option | |
GpxOptions options = new GpxOptions() | |
{ | |
ReadNestedAttributes = true | |
}; | |
// open GPX layer to read features | |
using (var layer = Drivers.Gpx.OpenLayer(dataDir + "nested_data.gpx", options)) | |
{ | |
foreach (var feature in layer) | |
{ | |
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(".........."); | |
} | |
} | |
} |
Write Polygons As Lines to GPX File
GPX format does not support polygons and multipolygons. As result, sometime conversion is failed from other formats. You should apply the WritePolygonsAsLines option to resolve this issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = Drivers.Gpx.CreateLayer(dataDir + "lines_out.gpx", new GpxOptions() | |
{ | |
WritePolygonsAsLines = true | |
})) | |
{ | |
// The GPX format does not support polygons, | |
// but we use the WritePolygonsAsLines options to resolve this issue. | |
Feature feature = layer.ConstructFeature(); | |
feature.Geometry = Geometry.FromText("POLYGON((1 2, 1 4, 3 4, 3 2))"); | |
layer.Add(feature); | |
} |