1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (c) 2014, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * See http://geojson.org/ for the specification
28 #include "vikwindow.h"
31 #include <glib/gstdio.h>
32 #include <glib/gi18n.h>
35 * Perform any cleanup actions once program has completed running
37 static void my_watch ( GPid pid,
41 g_spawn_close_pid ( pid );
45 * a_geojson_write_file:
47 * Returns TRUE if successfully written
49 gboolean a_geojson_write_file ( VikTrwLayer *vtl, FILE *ff )
51 gboolean result = FALSE;
53 gchar *tmp_filename = a_gpx_write_tmp_file ( vtl, NULL );
60 // geojson program should be on the $PATH
62 argv = g_new (gchar*, 5);
63 argv[0] = g_strdup (a_geojson_program_export());
64 argv[1] = g_strdup ("-f");
65 argv[2] = g_strdup ("gpx");
66 argv[3] = g_strdup (tmp_filename);
70 // TODO: monitor stderr?
71 if (!g_spawn_async_with_pipes (NULL, argv, NULL, (GSpawnFlags) G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &mystdout, NULL, &error)) {
73 if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(vtl)) ) {
74 gchar* msg = g_strdup_printf ( _("%s command failed: %s"), argv[0], error->message );
75 vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(vtl), msg, VIK_STATUSBAR_INFO );
79 g_warning ("Async command failed: %s", error->message);
84 // Probably should use GIOChannels...
86 FILE *fout = fdopen(mystdout, "r");
87 setvbuf(fout, NULL, _IONBF, 0);
89 while (fgets(line, sizeof(line), fout)) {
90 fprintf ( ff, "%s", line );
95 g_child_watch_add ( pid, (GChildWatchFunc) my_watch, NULL );
101 // Delete the temporary file
102 (void)g_remove (tmp_filename);
103 g_free (tmp_filename);
109 // https://github.com/mapbox/togeojson
111 // https://www.npmjs.org/package/togeojson
113 // Tested with version 0.7.0
114 const gchar* a_geojson_program_export ( void )
120 // https://github.com/tyrasd/togpx
122 // https://www.npmjs.org/package/togpx
124 // Tested with version 0.3.1
125 const gchar* a_geojson_program_import ( void )
131 * a_geojson_import_to_gpx:
133 * @filename: The source GeoJSON file
135 * Returns: The name of newly created temporary GPX file
136 * This file should be removed once used and the string freed.
137 * If NULL then the process failed.
139 gchar* a_geojson_import_to_gpx ( const gchar *filename )
141 gchar *gpx_filename = NULL;
142 GError *error = NULL;
143 // Opening temporary file
144 int fd = g_file_open_tmp("vik_geojson_XXXXXX.gpx", &gpx_filename, &error);
146 g_warning ( _("failed to open temporary file: %s"), error->message );
147 g_clear_error ( &error );
150 g_debug ( "%s: temporary file = %s", __FUNCTION__, gpx_filename );
155 // geojson program should be on the $PATH
157 argv = g_new (gchar*, 3);
158 argv[0] = g_strdup (a_geojson_program_import());
159 argv[1] = g_strdup (filename);
162 FILE *gpxfile = fdopen (fd, "w");
164 // TODO: monitor stderr?
165 if (!g_spawn_async_with_pipes (NULL, argv, NULL, (GSpawnFlags) G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &mystdout, NULL, &error)) {
166 g_warning ("Async command failed: %s", error->message);
170 // Probably should use GIOChannels...
172 FILE *fout = fdopen(mystdout, "r");
173 setvbuf(fout, NULL, _IONBF, 0);
175 while (fgets(line, sizeof(line), fout)) {
176 fprintf ( gpxfile, "%s", line );
180 g_child_watch_add ( pid, (GChildWatchFunc) my_watch, NULL );