We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
GPX Viewer (Read 2959 times)
GPX Viewer
Feb 17th, 2009, 4:19pm
 
hi guys...

i would like to make a gpx viewer application in java. i found a good library (http://www.processing.org/hacks/hacks:gpx) but unfortunately the draw function is missing. Can somebody help me to write it? an example code? i want to make a very simple 2d display...

thx you so much in advance!
bye
Re: GPX Viever
Reply #1 - Feb 17th, 2009, 5:10pm
 
The draw function isn't missing, it is what you made out of the parsed data. There are so many ways to show this data. The hack you point to gives the link to an example sketch, with visualization of this data.
Re: GPX Viewer
Reply #2 - Feb 17th, 2009, 9:49pm
 
thx, but if i use that code, i get the earth with my 200feet long track on it Cheesy... it's a small dot Cheesy... how can i make a 2D display without the earth? i would like to see my track clearly, so i should have a zoom function or something like that to make my whole track visible! i dont beggar for you to write me the full code, just give me some advice to start. im beginner. thx!!

ps: if you sent the full code, i would kiss you immediately! Cheesy
Re: GPX Viewer
Reply #3 - Feb 18th, 2009, 12:01pm
 
You are lucky: I am always curious to explore new stuff, this one moderately interest me, the associated library is small and doesn't need special hardware I don't own (unlike Arduino/Midi/video questions), although I accept GPS donations... :-D
And it is simple enough to be done quickly.

I found a couple of GPX visu examples, at http://www.skitour.fr/gpx/embed_exemple.html and http://www.routeyou.com/route/view/1818/itinraire-de-voiture-et-de-moto-dolomieten-anw-gpx.fr (French sites), the latter giving me a sample GPX file and a visualization to check if my code is correct.

Limitations of my code: I scale naively data. I should use computed distance between points for an accurate elevation diagram. I map blindly min and max values to min and max viewport, without respecting scale. And I don't do any kind of realistic projection, although on small scale tracks, it shouldn't show much.
I hope it is a decent starting point for better visualizations.
Maybe I will come back and improve the above points someday... Smiley

Code:
// Based on http://www.processing.org/hacks/hacks:gpx
import tomc.gpx.*;

GPX gpx;
// I limit the sketch to one track with one segment...
// Just taking the first ones if there are several
// So I don't use here gpx.getTrackCount() nor track.size()
GPXTrack track;
GPXTrackSeg trackSeg;
String trackName;

double minLat, maxLat;
double minLon, maxLon;
double minEle, maxEle;

final static int SEPARATOR = 200;

void setup()
{
 size(500, 700);
 gpx = new GPX(this);
 // Load GPX file in data folder
 gpx.parse("route-Dolomieten.gpx"); // or a URL
 track = gpx.getTrack(0);
 trackName = track.name;
 trackSeg = track.getTrackSeg(0);

 // Find scope of segement
 minLat = 100; minLon = 200; minEle = 50000;
 for (int i = 0; i < trackSeg.size(); i++)
 {
   GPXPoint pt = trackSeg.getPoint(i);
   if (pt.lat < minLat)
   {
     minLat = pt.lat;
   }
   if (pt.lon < minLon)
   {
     minLon = pt.lon;
   }
   if (pt.ele < minEle)
   {
     minEle = pt.ele;
   }

   if (pt.lat > maxLat)
   {
     maxLat = pt.lat;
   }
   if (pt.lon > maxLon)
   {
     maxLon = pt.lon;
   }
   if (pt.ele > maxEle)
   {
     maxEle = pt.ele;
   }
 }
println("Lat: " + minLat + " to " + maxLat);
println("Lon: " + minLon + " to " + maxLon);
println("Ele: " + minEle + " to " + maxEle);
}

void draw()
{
 background(255);
 stroke(#FF0000);
 line(0, SEPARATOR, width, SEPARATOR);

 double distance = 0;
 GPXPoint prevPt = trackSeg.getPoint(0);
 PVector prevEle = GetElevation(0, prevPt);
 PVector prevPos = GetPosition(prevPt);
 for (int i = 1; i < trackSeg.size(); i++)
 {
   GPXPoint pt = trackSeg.getPoint(i);

   // Show altitude
   // I should compute the real distance in kilometers between two geo points
   // probably using Vincenty's formulae <http://en.wikipedia.org/wiki/Vincenty%27s_formulae>
   // Here I just use mean distance between points
   PVector ele = GetElevation(i, pt);
   stroke(#0000AA);
   line(prevEle.x, prevEle.y, ele.x, ele.y);
   prevEle = ele;

   // Show track
   PVector pos = GetPosition(pt);
   stroke(#008888);
   line(prevPos.x, prevPos.y, pos.x, pos.y);
   prevPos = pos;
 }
}

PVector GetElevation(int n, GPXPoint pt)
{
 return new PVector(
     map(n, 0, trackSeg.size(), 10, width - 10),
     map((float) pt.ele, (float) minEle, (float) maxEle, SEPARATOR - 10, 10)
 );
}

PVector GetPosition(GPXPoint pt)
{
 return new PVector(
     map((float) pt.lon, (float) minLon, (float) maxLon, 10, width - 10),
     map((float) pt.lat, (float) minLat, (float) maxLat, SEPARATOR + 10, height - 10)
 );
}
Page Index Toggle Pages: 1