-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a3178a
commit 718a199
Showing
5 changed files
with
164 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,61 @@ | ||
# RolandK.Formats.Gpx <img src="assets/Logo_128.png" width="32" /> | ||
### Common Information | ||
## Common Information | ||
A .NET Standard library for reading and writing GPX (GPS Exchange Format) files. | ||
This library was build for my [GpxViewer](https://github.com/RolandKoenig/GpxViewer) project. It is based | ||
on the System.Xml.Serialization.XmlSerializer class and therefore available for .NET Framework and .NET Core projects. | ||
|
||
### Build | ||
## Build | ||
[![Continuous integration](https://github.com/RolandKoenig/RolandK.Formats.Gpx/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/RolandKoenig/RolandK.Formats.Gpx/actions/workflows/continuous-integration.yml) | ||
|
||
### Feature overview | ||
## Feature overview | ||
- Full document model for gpx files | ||
- Load gpx files | ||
- Write gpx files | ||
- Add custom metadata to gpx files | ||
- Don't lose other custom metadate after loading and saving gpx files | ||
- Add custom xml extensions to gpx files | ||
- Don't lose other custom xml extensions information after loading and saving gpx files | ||
|
||
## Samples | ||
### Load GPX file | ||
Load file file (here Kösseine.gpx) and get total count of tracks, routes and waypoints in the file | ||
```csharp | ||
var gpxFile = await GpxFile.LoadAsync("Kösseine.gpx"); | ||
|
||
var countTracks = gpxFile.Tracks.Count; | ||
var countRoutes = gpxFile.Routes.Count; | ||
var countWaypoints = gpxFile.Waypoints.Count; | ||
``` | ||
|
||
### Save GPX file | ||
Save a previously loaded / created / modified file | ||
```csharp | ||
await GpxFile.SaveAsync(gpxFile, "MyFile.gpx"); | ||
``` | ||
|
||
### Add custom xml extension | ||
Your have to define your own xml extension types and give them a namespace | ||
```csharp | ||
[XmlType("MyTrackExtension", Namespace = "http://testing.rolandk.net/")] | ||
public class MyTrackExtension | ||
{ | ||
public bool AlreadyDone { get; set; } = false; | ||
} | ||
``` | ||
|
||
Then you have to register these xml extension types and their namespaces to GpxFile. | ||
You should do this somewhere in your startup code of your application. | ||
```csharp | ||
// You have to register your own xml extension types | ||
GpxFile.RegisterExtensionType(typeof(MyTrackExtension)); | ||
GpxFile.RegisterNamespace("rktest", "http://testing.rolandk.net/"); | ||
``` | ||
|
||
Now you can access these xml extensions from c# code | ||
```csharp | ||
var gpxFile = await GpxFile.LoadAsync(inStream); | ||
var gpxTrack = gpxFile.Tracks[0]; | ||
|
||
gpxTrack.Extensions ??= new GpxExtensions(); | ||
var myTrackExtension = gpxTrack.Extensions.GetOrCreateExtension<MyTrackExtension>(); | ||
|
||
myTrackExtension.AlreadyDone = true; // This property comes from our own xml extension | ||
``` |
48 changes: 48 additions & 0 deletions
48
src/RolandK.Formats.Gpx.Tests/FileLoad/GpxFileCustomExtensionsTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Xml.Serialization; | ||
|
||
namespace RolandK.Formats.Gpx.Tests.FileLoad; | ||
|
||
public class GpxFileCustomExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task AddCustomMetadata() | ||
{ | ||
// Arrange | ||
await using var inStream = GpxTestUtilities.ReadFromEmbeddedResource( | ||
typeof(GpxFileLoadTests),"Test_Gpx1_1.gpx"); | ||
|
||
// Act | ||
GpxFile.RegisterExtensionType(typeof(MyTrackExtension)); | ||
GpxFile.RegisterNamespace("rktest", "http://testing.rolandk.net/"); | ||
|
||
var originalGpxFile = await GpxFile.LoadAsync(inStream); | ||
var originalGpxTrack = originalGpxFile.Tracks[0]; | ||
|
||
originalGpxTrack.Extensions ??= new GpxExtensions(); | ||
var myTrackExtension = originalGpxTrack.Extensions.GetOrCreateExtension<MyTrackExtension>(); | ||
|
||
myTrackExtension.AlreadyDone = true; | ||
|
||
using var writingMemoryStream = new MemoryStream(1024 * 100); | ||
await GpxFile.SaveAsync(originalGpxFile, writingMemoryStream); | ||
|
||
using var readingMemoryStream = new MemoryStream(writingMemoryStream.GetBuffer()); | ||
var reloadedFile = await GpxFile.LoadAsync(readingMemoryStream); | ||
|
||
// Assert | ||
Assert.Single(reloadedFile.Tracks); | ||
|
||
var reloadedTrack = reloadedFile.Tracks[0]; | ||
Assert.NotNull(reloadedTrack.Extensions); | ||
|
||
var reloadedExtension = reloadedTrack.Extensions.TryGetSingleExtension<MyTrackExtension>(); | ||
Assert.NotNull(reloadedExtension); | ||
Assert.True(reloadedExtension.AlreadyDone); | ||
} | ||
|
||
[XmlType("MyTrackExtension", Namespace = "http://testing.rolandk.net/")] | ||
public class MyTrackExtension | ||
{ | ||
public bool AlreadyDone { get; set; } | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.