Output the track points into the gpx file
authorDr. Johann Pfefferl <pfefferl@nghape.jpnet>
Tue, 11 May 2010 13:44:13 +0000 (15:44 +0200)
committerDr. Johann Pfefferl <pfefferl@nghape.jpnet>
Tue, 11 May 2010 13:44:13 +0000 (15:44 +0200)
gps-tracker.c

index 680de10..711264b 100644 (file)
@@ -48,10 +48,10 @@ static gchar * interface_file_chooser (AppData * appdata, GtkFileChooserAction a
 static void write_gpx_header(FILE *fp)
 {
   g_return_if_fail(fp);
-  fprintf(fp,
+  g_fprintf(fp,
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
       "<gpx version=\"1.0\"\n"
-      "creator=\"convert2gpx.pl http://www.tangogps.org\"\n"
+      "creator=\"GPS tracker for Maemo\"\n"
       "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
       "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
       "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"
@@ -62,7 +62,7 @@ static void write_gpx_header(FILE *fp)
 static void write_gpx_footer(FILE *fp)
 {
   g_return_if_fail(fp);
-  fprintf(fp, "</trkseg>\n</trk>\n</gpx>\n");
+  g_fprintf(fp, "</trkseg>\n</trk>\n</gpx>\n");
 }
 
 static void cb_start_stop (GtkWidget * w, AppData * data)
@@ -111,55 +111,79 @@ on_gps_device_changed (LocationGPSDevice *device, gpointer data)
   AppData *app_data = data;
   GtkLabel *info = (GtkLabel*)app_data->status_label;
   GString *msg;
+  FILE *fp = app_data->outf_p;
+  gchar sbuf1[16], sbuf2[16];
        if (!device)
                return;
 
   msg = g_string_sized_new (512);
-       if (device->fix) {
-               if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
-                       g_print ("time = %f\n", device->fix->time);
-      g_string_append_printf(msg, "time = %f\n", device->fix->time);
-    }
+  if (device->fix && device->status) {
+    if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
+      g_print ("lat = %f, long = %f\n",
+          device->fix->latitude,
+          device->fix->longitude);
+      g_string_append_printf (msg, "lat = %f, long = %f\n",
+          device->fix->latitude,
+          device->fix->longitude);
+      if(fp)
+        g_fprintf(fp, "\n<trkpt lat=\"%s\" lon=\"%s\">\n",
+          g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->latitude),
+          g_ascii_formatd(sbuf2, sizeof(sbuf2), "%f", device->fix->longitude));
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
+        g_print ("time = %f\n", device->fix->time);
+        g_string_append_printf(msg, "time = %F\n", device->fix->time);
+        if(fp) {
+          gchar st[64];
+          struct tm *tmp;
+          time_t t = device->fix->time;
+          tmp = localtime(&t);
+          strftime(st, sizeof(st), "%FT%T%Z", tmp);
+          /* FIXME Is this really correct */
+          g_fprintf(fp, "<time>%s</time>\n", st);
+        }
+      }
 
-               if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
-                       g_print ("lat = %f, long = %f\n",
-                                       device->fix->latitude,
-                                       device->fix->longitude);
-                       g_string_append_printf (msg, "lat = %f, long = %f\n",
-                                       device->fix->latitude,
-                                       device->fix->longitude);
-    }
+      if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
+        g_print ("alt = %f\n", device->fix->altitude);
+        g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
+        if(fp)
+          g_fprintf(fp, "<ele>%s</ele>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->altitude));
 
-               if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
-                       g_print ("alt = %f\n", device->fix->altitude);
-                       g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
-    }
+      }
 
-               if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
-                       g_print ("speed = %f\n", device->fix->speed);
-                       g_string_append_printf (msg, "speed = %f, ", device->fix->speed);
-    }
+      if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
+        g_print ("speed = %f\n", device->fix->speed);
+        g_string_append_printf (msg, "speed = %f, ", device->fix->speed);
+        if(fp)
+          g_fprintf(fp, "<speed>%s</speed>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->speed));
+      }
 
-               if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
-                       g_print ("track = %f\n", device->fix->track);
-                       g_string_append_printf (msg, "track = %f, ", device->fix->track);
-    }
+      if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
+        g_print ("track = %f\n", device->fix->track);
+        g_string_append_printf (msg, "track = %f, ", device->fix->track);
+        if(fp)
+          g_fprintf(fp, "<course>%s</course>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->track));
+      }
 
-               if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
-                       g_print ("climb = %f\n", device->fix->climb);
-                       g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
-    }
+      if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
+        g_print ("climb = %f\n", device->fix->climb);
+        g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
+      }
 
-               g_print ("Accuracy values:\n");
-               g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
-                               "eps = %e, epc = %e\n",
-                               device->fix->ept,
-                               device->fix->eph,
-                               device->fix->epv,
-                               device->fix->epd,
-                               device->fix->eps,
-                               device->fix->epc);
-       }
+      g_print ("Accuracy values:\n");
+      g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
+          "eps = %e, epc = %e\n",
+          device->fix->ept,
+          device->fix->eph,
+          device->fix->epv,
+          device->fix->epd,
+          device->fix->eps,
+          device->fix->epc);
+      if(fp)
+        g_fprintf(fp, "</trkpt>\n");
+    }
+  }
        
        g_print ("Satellites in view: %d\n", device->satellites_in_view);
        g_print ("Satellites in use: %d\n", device->satellites_in_use);
@@ -279,11 +303,11 @@ int main (int argc, char **argv)
   if(data->outf_p)
     fclose(data->outf_p);
   g_unlink(data->intermediate_gpx_data_filename);
-       g_object_unref (device);
-       g_object_unref (control);
   g_free(data->working_dir);
   g_free(data->intermediate_gpx_data_filename);
   g_free(data);
+       g_object_unref (device);
+       g_object_unref (control);
 
        return 0;
 }