135 lines
4.3 KiB
C
135 lines
4.3 KiB
C
![]() |
/*
|
||
|
*
|
||
|
* mappix-fileio
|
||
|
*
|
||
|
* Copyright (C) 2006 Nils Faerber <nils.faerber@kernelconcepts.de>
|
||
|
*
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 2 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <fcntl.h>
|
||
|
|
||
|
#include "mappix.h"
|
||
|
#include "geo-tools.h"
|
||
|
|
||
|
|
||
|
static void
|
||
|
write_trackpoint(gpointer data, gpointer user_data)
|
||
|
{
|
||
|
trackdata *tdata = (trackdata *)data;
|
||
|
FILE *mfile = (FILE *)user_data;
|
||
|
gfloat llat, llong;
|
||
|
gchar dstr[G_ASCII_DTOSTR_BUF_SIZE];
|
||
|
|
||
|
if (tdata != NULL) {
|
||
|
g_fprintf(mfile, "<trkpt ");
|
||
|
unit2latlon(tdata->unitx, tdata->unity, llat, llong);
|
||
|
g_ascii_dtostr(dstr, G_ASCII_DTOSTR_BUF_SIZE, llat);
|
||
|
g_fprintf(mfile, "lat=\"%s\" ", dstr);
|
||
|
g_ascii_dtostr(dstr, G_ASCII_DTOSTR_BUF_SIZE, llong);
|
||
|
g_fprintf(mfile, "long=\"%s\">\n", dstr);
|
||
|
g_fprintf(mfile, "<ele>%.6f</ele>\n", tdata->elevation);
|
||
|
#if 0
|
||
|
// if (tdata->datetime != NULL)
|
||
|
// g_fprintf(mfile, "<time>%s</time>\n", tdata->datetime);
|
||
|
g_fprintf(mfile, "<sat>%d</sat>\n", tdata->nrsat);
|
||
|
g_fprintf(mfile, "<hdop>%f</hdop>\n", tdata->hdop);
|
||
|
g_fprintf(mfile, "<vdop>%f</vdop>\n", tdata->vdop);
|
||
|
g_fprintf(mfile, "<pdop>%f</pdop>\n", tdata->pdop);
|
||
|
#endif
|
||
|
g_fprintf(mfile, "</trkpt>\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
static void
|
||
|
write_track(gpointer data, gpointer user_data)
|
||
|
{
|
||
|
track_head *thead = (track_head *)data;
|
||
|
FILE *mfile = (FILE *)user_data;
|
||
|
gfloat minlat, minlon, maxlat, maxlon;
|
||
|
|
||
|
if (thead != NULL) {
|
||
|
g_fprintf(mfile, "<trk>\n");
|
||
|
g_fprintf(mfile, "<name>%s</name>\n", thead->name);
|
||
|
g_fprintf(mfile, "<type>%d</type>\n", thead->type); /* NOT GPX CONFORMANT */
|
||
|
g_fprintf(mfile, "<show>%s</show>\n", thead->show ? "true" : "false"); /* NOT GPX CONFORMANT */
|
||
|
unit2latlon(thead->xmin, thead->ymin, minlat, minlon);
|
||
|
unit2latlon(thead->xmax, thead->ymax, maxlat, maxlon);
|
||
|
g_fprintf(mfile, "<bounds minlat=\"%f\" minlon=\"%f\" maxlat=\"%f\" maxlon=\"%f\"/>\n", minlat, minlon, maxlat, maxlon);
|
||
|
g_fprintf(mfile, "<trkseg>\n");
|
||
|
if (thead->track != NULL)
|
||
|
g_list_foreach(thead->track, write_trackpoint, mfile);
|
||
|
g_fprintf(mfile, "</trkseg>\n");
|
||
|
g_fprintf(mfile, "</trk>\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
static void
|
||
|
write_waypoint(gpointer data, gpointer user_data)
|
||
|
{
|
||
|
waypoint *tdata = (waypoint *)data;
|
||
|
FILE *mfile = (FILE *)user_data;
|
||
|
gfloat llat, llong;
|
||
|
gchar dstr[G_ASCII_DTOSTR_BUF_SIZE];
|
||
|
|
||
|
if (tdata != NULL) {
|
||
|
g_fprintf(mfile, "<wpt ");
|
||
|
unit2latlon(tdata->unitx, tdata->unity, llat, llong);
|
||
|
g_ascii_dtostr(dstr, G_ASCII_DTOSTR_BUF_SIZE, llat);
|
||
|
g_fprintf(mfile, "lat=\"%s\" ", dstr);
|
||
|
g_ascii_dtostr(dstr, G_ASCII_DTOSTR_BUF_SIZE, llong);
|
||
|
g_fprintf(mfile, "long=\"%s\">\n", dstr);
|
||
|
g_fprintf(mfile, "<name>%s</name>\n", tdata->name);
|
||
|
g_fprintf(mfile, "<ele>%.6f</ele>\n", tdata->elevation);
|
||
|
#if 0 // not yet used
|
||
|
if (tdata->datetime != NULL)
|
||
|
g_fprintf(mfile, "<time>%s</time>\n", tdata->datetime);
|
||
|
g_fprintf(mfile, "<sat>%d</sat>\n", tdata->nrsat);
|
||
|
g_fprintf(mfile, "<hdop>%f</hdop>\n", tdata->hdop);
|
||
|
g_fprintf(mfile, "<vdop>%f</vdop>\n", tdata->vdop);
|
||
|
g_fprintf(mfile, "<pdop>%f</pdop>\n", tdata->pdop);
|
||
|
#endif
|
||
|
g_fprintf(mfile, "</wpt>\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
gint
|
||
|
save_project_to_file(gchar *filename, mapwin *Mapper)
|
||
|
{
|
||
|
FILE *mfile;
|
||
|
|
||
|
mfile = g_fopen(filename, "w");
|
||
|
|
||
|
g_fprintf(mfile, "<?xml version=\"1.0\"?>\n<gpx version=\"1.0\" creator=\"Mappix\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\nxmlns=\"http://www.topografix.com/GPX/1/0\"\nxsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
|
||
|
if (Mapper->tracklist)
|
||
|
g_list_foreach(Mapper->tracklist, write_track, mfile);
|
||
|
if (Mapper->waypoints)
|
||
|
g_list_foreach(Mapper->waypoints, write_waypoint, mfile);
|
||
|
g_fprintf(mfile, "</gpx>\n");
|
||
|
|
||
|
fclose(mfile);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|