[go: up one dir, main page]

Menu

[1f9cef]: / src / geojson.c  Maximize  Restore  History

Download this file

278 lines (240 with data), 7.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* viking -- GPS Data and Topo Analyzer, Explorer, and Manager
*
* Copyright (c) 2014, Rob Norris <rw_norris@hotmail.com>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* See http://geojson.org/ for the specification
*
*/
#include "geojson.h"
#include "gpx.h"
#include "globals.h"
#include "vikwindow.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>
/**
* Perform any cleanup actions once program has completed running
*/
static void my_watch ( GPid pid,
gint status,
gpointer user_data )
{
g_spawn_close_pid ( pid );
}
/**
* a_geojson_write_file:
*
* Returns TRUE if successfully written
*/
gboolean a_geojson_write_file ( VikTrwLayer *vtl, FILE *ff )
{
gboolean result = FALSE;
gchar *tmp_filename = a_gpx_write_tmp_file ( vtl, NULL );
if ( !tmp_filename )
return result;
GPid pid;
gint mystdout;
// geojson program should be on the $PATH
gchar **argv;
argv = g_new (gchar*, 5);
argv[0] = g_strdup (a_geojson_program_export());
argv[1] = g_strdup ("-f");
argv[2] = g_strdup ("gpx");
argv[3] = g_strdup (tmp_filename);
argv[4] = NULL;
GError *error = NULL;
// TODO: monitor stderr?
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)) {
if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(vtl)) ) {
gchar* msg = g_strdup_printf ( _("%s command failed: %s"), argv[0], error->message );
vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(vtl), msg, VIK_STATUSBAR_INFO );
g_free (msg);
}
else
g_warning ("Async command failed: %s", error->message);
g_error_free(error);
}
else {
// Probably should use GIOChannels...
gchar line[512];
FILE *fout = fdopen(mystdout, "r");
setvbuf(fout, NULL, _IONBF, 0);
while (fgets(line, sizeof(line), fout)) {
fprintf ( ff, "%s", line );
}
fclose(fout);
g_child_watch_add ( pid, (GChildWatchFunc) my_watch, NULL );
result = TRUE;
}
g_strfreev (argv);
// Delete the temporary file
(void)g_remove (tmp_filename);
g_free (tmp_filename);
return result;
}
//
// https://github.com/mapbox/togeojson
//
// https://www.npmjs.org/package/togeojson
//
// Tested with version 0.7.0
const gchar* a_geojson_program_export ( void )
{
return "togeojson";
}
//
// https://github.com/tyrasd/togpx
//
// https://www.npmjs.org/package/togpx
//
// Tested with version 0.3.1
const gchar* a_geojson_program_import ( void )
{
return "togpx";
}
/**
* a_geojson_import_to_gpx:
*
* @filename: The source GeoJSON file
*
* Returns: The name of newly created temporary GPX file
* This file should be removed once used and the string freed.
* If NULL then the process failed.
*/
gchar* a_geojson_import_to_gpx ( const gchar *filename )
{
gchar *gpx_filename = NULL;
GError *error = NULL;
// Opening temporary file
int fd = g_file_open_tmp("vik_geojson_XXXXXX.gpx", &gpx_filename, &error);
if (fd < 0) {
g_warning ( _("failed to open temporary file: %s"), error->message );
g_clear_error ( &error );
return NULL;
}
g_debug ( "%s: temporary file = %s", __FUNCTION__, gpx_filename );
GPid pid;
gint mystdout;
// geojson program should be on the $PATH
gchar **argv;
argv = g_new (gchar*, 3);
argv[0] = g_strdup (a_geojson_program_import());
argv[1] = g_strdup (filename);
argv[2] = NULL;
FILE *gpxfile = fdopen (fd, "w");
// TODO: monitor stderr?
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)) {
g_warning ("Async command failed: %s", error->message);
g_error_free(error);
}
else {
// Probably should use GIOChannels...
gchar line[512];
FILE *fout = fdopen(mystdout, "r");
setvbuf(fout, NULL, _IONBF, 0);
while (fgets(line, sizeof(line), fout)) {
fprintf ( gpxfile, "%s", line );
}
fclose(fout);
g_child_watch_add ( pid, (GChildWatchFunc) my_watch, NULL );
}
fclose (gpxfile);
g_strfreev (argv);
return gpx_filename;
}
/**
* Handle response from
* http://project-osrm.org/docs/v5.23.0/api/#route-service
* ATM only try to get the geometry out of the response
* i.e. no handling of 'code' response or 'message'
*/
gboolean a_geojson_read_file_OSRM ( VikTrwLayer *vtl, const gchar *filename )
{
JsonParser *jp = json_parser_new();
GError *error = NULL;
gboolean ans = json_parser_load_from_file ( jp, filename, &error );
if ( error ) {
g_warning ( "%s: parse load failed: %s", __FUNCTION__, error->message );
g_error_free ( error );
return FALSE;
}
JsonNode *result;
JsonPath *path = json_path_new ();
json_path_compile ( path, "$.routes..geometry..coordinates", NULL );
result = json_path_match ( path, json_parser_get_root(jp) );
JsonGenerator *generator = json_generator_new ();
json_generator_set_root ( generator, result );
// Not sure of a good way to use native glib types (or GObjects etc...)
// so simply hack apart this returned string
gchar *str = json_generator_to_data ( generator, NULL );
//g_debug ( "%s result: %s\n", __FUNCTION__, str );
if ( strlen(str) > 8 ) {
// Not sure how efficient this is; but should suffice for our use cases
gchar *ptr = GINT_TO_POINTER(1);
while ( ptr ) {
// Remove the inbetween brackets
ptr = g_strrstr ( str, "],[" );
if ( ptr )
// NB using memcpy() as compiler moans about not nul-terminated for strncpy(),
// but here we definitely want just a direct substitution
//strncpy ( ptr, " ", 3 );
memcpy ( ptr, " ", 3 );
}
// Tidy up beginning and ending brackets
ptr = g_strrstr ( str, "]]]" );
if ( ptr )
memcpy ( ptr, " ", 3 ); // as above
ptr = g_strrstr ( str, "[[[" );
if ( ptr )
memcpy ( ptr, " ", 3 ); // as above
gchar **coords = g_strsplit ( str, " ", -1 );
gint gi = 0;
gchar *coord = coords[0];
if ( coord ) {
ans = TRUE;
VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( vtl );
VikTrack *trk = vik_track_new ();
trk->is_route = TRUE;
VikTrackpoint *tp;
struct LatLon ll;
while ( coord ) {
gchar **vals = g_strsplit ( coord, ",", -1 );
guint nn = g_strv_length ( vals );
if ( nn == 2 ) {
tp = vik_trackpoint_new ();
// Remember geojson coordinates are in the 'lon,lat' order
ll.lat = g_ascii_strtod ( vals[1], NULL );
ll.lon = g_ascii_strtod ( vals[0], NULL );
vik_coord_load_from_latlon ( &tp->coord, coord_mode, &ll );
trk->trackpoints = g_list_prepend ( trk->trackpoints, tp );
}
g_strfreev ( vals );
//gi++;
coord = coords[gi++];
}
trk->trackpoints = g_list_reverse ( trk->trackpoints );
// Potentially could try to be more clever with the name...
vik_trw_layer_filein_add_track ( vtl, N_("OSRM Route"), trk );
}
g_strfreev ( coords );
}
g_object_unref ( jp );
return ans;
}