sibartlett / Geo

A geospatial library for .NET
https://www.nuget.org/packages/Geo/
GNU Lesser General Public License v3.0
179 stars 39 forks source link

GPX to GeoJson #20

Open HarelM opened 9 years ago

HarelM commented 9 years ago

I'm not sure I'm not missing something here but Assuming I have a GPX file and I want to convert it to geojson, how should I be doing it? I see a separation between GPSData and IO but I'm not sure how to bridge the gap...

toburger commented 1 year ago

I want to archieve the same but can't figure out how to convert a Gps.GpsData type to an IGeometry.

HarelM commented 1 year ago

As this was never answered I did not choose to has this library. I suggest to use the net topology suite team packages, they have support for reading and writing GPX files along with ton of other features...

toburger commented 1 year ago

Thanks for the hint to the other library @HarelM!

In the meantime I managed to transform the data with the following code but I will definitely have a look at the other library (F# code following):

#r "nuget: Geo, 1.0.0"

open Geo
open Geo.IO.GeoJson
open Geo.Abstractions.Interfaces
open Geo.Gps.Serialization

let readGpx filepath =
    let s = Gpx11Serializer()
    let stream = System.IO.File.OpenRead filepath
    s.DeSerialize(new StreamWrapper(stream))

let convertToGeoJson geometry =
    let writer = GeoJsonWriter()
    writer.Write(geometry: IGeometry)

let extractWaypoints (data: Gps.GpsData) =
    data.Waypoints

let extractCoordinates: seq<Gps.Waypoint> -> _ =
    Seq.map (fun x -> x.Coordinate)

"file.gpx"
|> readGpx
|> extractWaypoints
|> extractCoordinates
|> Geometries.LineString
|> convertToGeoJson