Open Source/existing C# class to write GPX files? (C#)
Asked Answered
B

4

12

I am looking for a C# library or class to help write GPX files from a collection of waypoints I have (lat/long, etc).

I have found quite a few readers - but not so much for writing.

Bonus points if it works on Compact Framework/WinMobile 6.5 - but that is not a hard requirement. I can make it work on a desktop doing post-processing - the Mobile device does not have to build these files.

Does anyone know of some, or a simple way to write the files?

Bracey answered 13/9, 2011 at 20:4 Comment(3)
The quickest way is to simply generete the classes for it using xsd.exe from the xsd at topografix.com/GPX/1/1/gpx.xsd and then serialize it. I personally don't like those generated classes much and wrote a bunch of (simple) wrapper classes to implement the features I needed (so it's not complete). If you need more input about either path just ask :)Hilariahilario
Thanks. I will look at that - but I really don't need the heavyweight classes - this is just for a set of waypoints and I might just wrap it with something simple...Bracey
Here is a simple java code that is easy to port code.google.com/p/osmtracker-android/source/browse/trunk/src/me/…Vermeil
S
7

Have you taken a look at OGL (Open GPS-LBS)?

From the class docs:

This GPX class provide converts GPS data(waypoints, routes, and tracks) to be compatible with GPX format file.

Regarding the Windows Mobile note, the library supports:

"...applications on PC(Windows) or PocketPC(CE)."

Stereoisomerism answered 20/9, 2011 at 15:3 Comment(0)
G
0

The simplest (as in most lo-fi, not most time-efficient) way is opening any gpx file in a text editor and pasting your coordinates in the correct spot, or writing a new one as below. GPX is a type of XML-file, so if you know some HTML, it's easy to pick up on.

The most basic format that I can open (using GPX Viewer on Android) is

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx>
  <!--Waypoint 1-->
  <wpt lat="50.888090" lon="4.698118">
  </wpt>
  <!--Waypoint 2-->
  <wpt lat="50.788090" lon="4.798118">
  </wpt>
  <!--etc-->
  <wpt lat="50.988090" lon="4.618118">
  </wpt>
</gpx>

Add or remove waypoints to taste.

Similar things can be done for tracks and routes, and you can add a lot more information like elevation and time, but you'll have to google for GPX documentation for that.

Gelatinoid answered 27/4, 2016 at 10:54 Comment(0)
H
0

Take a look at https://nettopologysuite.github.io/

It has a GPXFile class https://nettopologysuite.github.io/NetTopologySuite.IO.GPX/

Humerus answered 26/7, 2020 at 20:16 Comment(0)
M
0

A pretty simple way to generate using pure c#:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {        

        List<int> heartRateList = new List<int>() { 80, 81, 82 };
        List<DateTime> timeList = new List<DateTime>() { DateTime.Now.AddSeconds(1), DateTime.Now.AddSeconds(2), DateTime.Now.AddSeconds(3) };
        string trkpt = "";

        for(int c = 0; c < heartRateList.Count; c++)
        {
            trkpt += "<trkpt>"
                + "<time>" + timeList[c].ToString("s") + "Z</time>"
                + "<extensions>"
                + "<gpxtpx:TrackPointExtension>"
                + "<gpxtpx:hr>" + heartRateList[c] + "</gpxtpx:hr>"
                + "</gpxtpx:TrackPointExtension>"
                + "</extensions>"
                + "</trkpt>";
        }
        
        string gpx = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\" xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:ns1=\"http://www.cluetrust.com/XML/GPXDATA/1/0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" creator=\"Zamfit\" version=\"1.3\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\">"
            + "<metadata><time>2022-01-01T00:00:00Z</time></metadata>"
            + "<trk>"
            + "<name>Activity Name</name>"
            + "<trkseg>"
            + trkpt
            + "</trkseg>"
            + "</trk>"
            + "</gpx>";       
     
        Console.WriteLine(gpx);

    }
}
Mozzetta answered 3/2, 2022 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.