summaryrefslogtreecommitdiff
path: root/gpsviewer.pde
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-07-17 21:32:17 +0100
committerTim Redfern <tim@eclectronics.org>2012-07-17 21:32:17 +0100
commit7f5e1a758de9a4705e73d484eead0a3191dcfbf0 (patch)
treeb3b25a396f27346b9390a759113685bb3e3d80c3 /gpsviewer.pde
initial commit
Diffstat (limited to 'gpsviewer.pde')
-rw-r--r--gpsviewer.pde92
1 files changed, 92 insertions, 0 deletions
diff --git a/gpsviewer.pde b/gpsviewer.pde
new file mode 100644
index 0000000..da614b2
--- /dev/null
+++ b/gpsviewer.pde
@@ -0,0 +1,92 @@
+XMLElement kml;
+ArrayList points = new ArrayList();
+
+class p3d {
+ float x,y,z;
+ p3d(float _x,float _y,float _z) {
+ x=_x;
+ y=_y;
+ z=_z;
+ }
+}
+
+p3d max=new p3d(-1000,-1000,-1000);
+p3d min=new p3d(1000,1000,1000);
+
+float xo,yo,sc;
+
+void addPoint(String[] coord) {
+ p3d p;
+ if (coord.length>2) {
+ p=new p3d(float(coord[0]),float(coord[1]),float(coord[2]));
+ }
+ else p=new p3d(float(coord[0]),float(coord[1]),0.0);
+ points.add(p);
+ if (p.x>max.x) max.x=p.x;
+ if (p.y>max.y) max.y=p.y;
+ if (p.z>max.z) max.z=p.z;
+ if (p.x<min.x) min.x=p.x;
+ if (p.y<min.y) min.y=p.y;
+ if (p.z<min.z) min.z=p.z;
+}
+
+void setup() {
+ size(200, 200);
+ kml = new XMLElement(this, "tim-openpaths.kml").getChild("Document");
+ //kml = new XMLElement(this, "everytrip-demo.kml").getChild("Document");
+ int num = kml.getChildCount();
+ for (int i = 0; i < num; i++) {
+ if (kml.getChild(i).getName().equals("Placemark")) {
+ if (kml.getChild(i).getChildren("Point").length>0) { //element contains a single coordinate
+ addPoint(kml.getChild(i).getChild("Point").getChild("coordinates").getContent().split(","));
+ }
+ if (kml.getChild(i).getChildren("coordinates").length>0) { //element contains a table of coordinates
+ String[] coords=kml.getChild(i).getChild("coordinates").getContent().split("\\n");
+ for (int j=0;j<coords.length;j++) {
+ addPoint(coords[j].split(","));
+ }
+ }
+ }
+ }
+ println ("found "+str(points.size())+" data points");
+
+ //work out offsets
+ if (max.x-min.x>max.y-min.y) {
+ //scale to X
+ xo=0;
+ yo=(height/2)-((height*((max.y-min.y)/(max.x-min.x))*0.5));
+ println("ratio: "+str((max.y-min.y)/(max.x-min.x)));
+ }
+ else {
+ //scale to Y
+ xo=(width/2)-((width*((max.x-min.x)/(max.y-min.y))*0.5));
+ println("ratio: "+str((max.x-min.x)/(max.y-min.y)));
+ yo=0;
+ }
+
+ size(512,512);
+ background(0);
+ stroke(255);
+}
+
+void draw() {
+ println(str(min.x)+","+str(min.y)+" to "+str(max.x)+","+str(max.y));
+ println("offset: "+str(xo)+","+str(yo));
+ sc=max(max.x-min.x,max.y-min.y);
+ if (sc==0.0) println("no movement!");
+ else {
+ float psc; //pixels per degree
+ if (max.x-min.x>max.y-min.y) psc=width/sc; //maximum span of latitude/longitude
+ else psc=height/sc;
+ //println("scale: "+str(sc)+" :"+str(width/sc)+" pixels/degree");
+ for (int i=0;i<points.size()-1;i++) {
+ float x1=((((p3d)points.get(i)).x-min.x)*psc)+xo;
+ float y1=((((p3d)points.get(i)).y-min.y)*psc)+yo;
+ float x2=((((p3d)points.get(i+1)).x-min.x)*psc)+xo;
+ float y2=((((p3d)points.get(i+1)).y-min.y)*psc)+yo;
+ //println(str(x1)+","+str(y1)+" to "+str(x2)+","+str(y2));
+ line(x1,y1,x2,y2);
+ }
+ }
+ noLoop();
+}