]> git.street.me.uk Git - andy/viking.git/blame - src/datasource_geojson.c
Enable importing from GeoJSON files via the usage of the program 'togpx'.
[andy/viking.git] / src / datasource_geojson.c
CommitLineData
c0c5893f
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22#include <glib/gi18n.h>
23#include <glib/gstdio.h>
24#include <gtk/gtk.h>
25
26#include "viking.h"
27#include "acquire.h"
28#include "babel.h"
29#include "geojson.h"
30
31typedef struct {
32 GtkWidget *files;
33 GSList *filelist; // Files selected
34} datasource_geojson_user_data_t;
35
36// The last used directory
37static gchar *last_folder_uri = NULL;
38
39static gpointer datasource_geojson_init ( acq_vik_t *avt );
40static void datasource_geojson_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
41static void datasource_geojson_get_cmd_string ( datasource_geojson_user_data_t *ud, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options );
42static gboolean datasource_geojson_process ( VikTrwLayer *vtl, const gchar *cmd, const gchar *extra, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, gpointer not_used );
43static void datasource_geojson_cleanup ( gpointer data );
44
45VikDataSourceInterface vik_datasource_geojson_interface = {
46 N_("Acquire from GeoJSON"),
47 N_("GeoJSON"),
48 VIK_DATASOURCE_AUTO_LAYER_MANAGEMENT,
49 VIK_DATASOURCE_INPUTTYPE_NONE,
50 TRUE,
51 FALSE, // We should be able to see the data on the screen so no point in keeping the dialog open
52 FALSE, // Not thread method - open each file in the main loop
53 (VikDataSourceInitFunc) datasource_geojson_init,
54 (VikDataSourceCheckExistenceFunc) NULL,
55 (VikDataSourceAddSetupWidgetsFunc) datasource_geojson_add_setup_widgets,
56 (VikDataSourceGetCmdStringFunc) datasource_geojson_get_cmd_string,
57 (VikDataSourceProcessFunc) datasource_geojson_process,
58 (VikDataSourceProgressFunc) NULL,
59 (VikDataSourceAddProgressWidgetsFunc) NULL,
60 (VikDataSourceCleanupFunc) datasource_geojson_cleanup,
61 (VikDataSourceOffFunc) NULL,
62 NULL,
63 0,
64 NULL,
65 NULL,
66 0
67};
68
69static gpointer datasource_geojson_init ( acq_vik_t *avt )
70{
71 datasource_geojson_user_data_t *user_data = g_malloc(sizeof(datasource_geojson_user_data_t));
72 user_data->filelist = NULL;
73 return user_data;
74}
75
76static void datasource_geojson_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
77{
78 datasource_geojson_user_data_t *ud = (datasource_geojson_user_data_t *)user_data;
79
80 ud->files = gtk_file_chooser_widget_new ( GTK_FILE_CHOOSER_ACTION_OPEN );
81
82 // try to make it a nice size - otherwise seems to default to something impractically small
83 gtk_window_set_default_size ( GTK_WINDOW (dialog) , 600, 300 );
84
85 if ( last_folder_uri )
86 gtk_file_chooser_set_current_folder_uri ( GTK_FILE_CHOOSER(ud->files), last_folder_uri );
87
88 GtkFileChooser *chooser = GTK_FILE_CHOOSER ( ud->files );
89
90 // Add filters
91 GtkFileFilter *filter;
92 filter = gtk_file_filter_new ();
93 gtk_file_filter_set_name ( filter, _("All") );
94 gtk_file_filter_add_pattern ( filter, "*" );
95 gtk_file_chooser_add_filter ( chooser, filter );
96
97 filter = gtk_file_filter_new ();
98 gtk_file_filter_set_name ( filter, _("GeoJSON") );
99 gtk_file_filter_add_pattern ( filter, "*.geojson" );
100 gtk_file_chooser_add_filter ( chooser, filter );
101
102 // Default to geojson
103 gtk_file_chooser_set_filter ( chooser, filter );
104
105 // Allow selecting more than one
106 gtk_file_chooser_set_select_multiple ( chooser, TRUE );
107
108 // Packing all widgets
109 GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
110 gtk_box_pack_start ( box, ud->files, TRUE, TRUE, 0 );
111
112 gtk_widget_show_all ( dialog );
113}
114
115static void datasource_geojson_get_cmd_string ( datasource_geojson_user_data_t *userdata, gchar **cmd, gchar **input_file_type, DownloadMapOptions *options )
116{
117 // Retrieve the files selected
118 userdata->filelist = gtk_file_chooser_get_filenames ( GTK_FILE_CHOOSER(userdata->files) ); // Not reusable !!
119
120 // Memorize the directory for later reuse
121 g_free ( last_folder_uri );
122 last_folder_uri = gtk_file_chooser_get_current_folder_uri ( GTK_FILE_CHOOSER(userdata->files) );
123 last_folder_uri = g_strdup ( last_folder_uri );
124
125 // TODO Memorize the file filter for later reuse?
126 //GtkFileFilter *filter = gtk_file_chooser_get_filter ( GTK_FILE_CHOOSER(userdata->files) );
127
128 // return some value so *thread* processing will continue
129 *cmd = g_strdup ("fake command"); // Not really used, thus no translations
130}
131
132/**
133 * Process selected files and try to generate waypoints storing them in the given vtl
134 */
135static gboolean datasource_geojson_process ( VikTrwLayer *vtl, const gchar *cmd, const gchar *extra, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, gpointer not_used )
136{
137 datasource_geojson_user_data_t *user_data = (datasource_geojson_user_data_t *)adw->user_data;
138
139 // Process selected files
140 GSList *cur_file = user_data->filelist;
141 while ( cur_file ) {
142 gchar *filename = cur_file->data;
143
144 gchar *gpx_filename = a_geojson_import_to_gpx ( filename );
145 if ( gpx_filename ) {
146 // Important that this process is run in the main thread
147 vik_window_open_file ( adw->vw, gpx_filename, FALSE );
148 // Delete the temporary file
149 g_remove (gpx_filename);
150 g_free (gpx_filename);
151 }
152 else {
153 gchar* msg = g_strdup_printf ( _("Unable to import from: %s"), filename );
154 vik_window_statusbar_update ( adw->vw, msg, VIK_STATUSBAR_INFO );
155 g_free (msg);
156 }
157
158 g_free ( filename );
159 cur_file = g_slist_next ( cur_file );
160 }
161
162 // Free memory
163 g_slist_free ( user_data->filelist );
164
165 // No failure
166 return TRUE;
167}
168
169static void datasource_geojson_cleanup ( gpointer data )
170{
171 g_free ( data );
172}