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?

Tim
  • 20,184
  • 24
  • 117
  • 214
  • 2
    The quickest way is to simply generete the classes for it using xsd.exe from the xsd at http://www.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 :) – Eddy Sep 14 '11 at 12:07
  • 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... – Tim Sep 14 '11 at 13:44
  • Here is a simple java code that is easy to port http://code.google.com/p/osmtracker-android/source/browse/trunk/src/me/guillaumin/android/osmtracker/gpx/GPXFileWriter.java?r=45 – Blau Sep 15 '11 at 21:13

4 Answers4

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)."

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
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.

Staf
  • 17
  • 6
0

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

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

filimonic
  • 3,988
  • 2
  • 19
  • 26
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);

    }
}
Marcio J
  • 673
  • 7
  • 17