forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshp2pgsql-gui.c
1279 lines (1082 loc) · 37.9 KB
/
shp2pgsql-gui.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* $Id$
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.refractions.net
* Copyright 2008 OpenGeo.org
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
* Maintainer: Paul Ramsey <pramsey@opengeo.org>
*
**********************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#ifdef MAC_INTEGRATION
#include <ige-mac-integration.h>
#endif
#include "libpq-fe.h"
#include "shp2pgsql-core.h"
#define GUI_RCSID "shp2pgsql-gui $Revision$"
/*
** Global variables for GUI only
*/
/* Main window */
static GtkWidget *window_main = NULL;
static GtkWidget *entry_pg_user = NULL;
static GtkWidget *entry_pg_pass = NULL;
static GtkWidget *entry_pg_host = NULL;
static GtkWidget *entry_pg_port = NULL;
static GtkWidget *entry_pg_db = NULL;
static GtkWidget *entry_config_table = NULL;
static GtkWidget *entry_config_schema = NULL;
static GtkWidget *entry_config_srid = NULL;
static GtkWidget *entry_config_geocolumn = NULL;
static GtkWidget *label_pg_connection_test = NULL;
static GtkWidget *textview_log = NULL;
static GtkWidget *file_chooser_button_shape = NULL;
static GtkWidget *progress = NULL;
static GtkTextBuffer *textbuffer_log = NULL;
/* Options window */
static GtkWidget *entry_options_encoding = NULL;
static GtkWidget *checkbutton_options_preservecase = NULL;
static GtkWidget *checkbutton_options_forceint = NULL;
static GtkWidget *checkbutton_options_autoindex = NULL;
static GtkWidget *checkbutton_options_dbfonly = NULL;
static GtkWidget *checkbutton_options_dumpformat = NULL;
static GtkWidget *checkbutton_options_geography = NULL;
/* Other */
static char *pgui_errmsg = NULL;
static PGconn *pg_connection = NULL;
static SHPLOADERCONFIG *config = NULL;
static SHPLOADERSTATE *state = NULL;
static SHPCONNECTIONCONFIG *conn = NULL;
static volatile int import_running = 0;
/* Local prototypes */
static void pgui_create_options_dialogue(void);
/*
** Write a message to the Import Log text area.
*/
static void
pgui_log_va(const char *fmt, va_list ap)
{
char *msg;
if (!lw_vasprintf (&msg, fmt, ap)) return;
gtk_text_buffer_insert_at_cursor(textbuffer_log, msg, -1);
gtk_text_buffer_insert_at_cursor(textbuffer_log, "\n", -1);
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(textview_log), gtk_text_buffer_get_insert(textbuffer_log) );
/* Allow GTK to process events */
while (gtk_events_pending())
gtk_main_iteration();
free(msg);
return;
}
/*
** Write a message to the Import Log text area.
*/
static void
pgui_logf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pgui_log_va(fmt, ap);
va_end(ap);
return;
}
static void
pgui_seterr(const char *errmsg)
{
if ( pgui_errmsg )
{
free(pgui_errmsg);
}
pgui_errmsg = strdup(errmsg);
return;
}
static void
pgui_raise_error_dialogue(void)
{
GtkWidget *dialog, *label;
gint result;
label = gtk_label_new(pgui_errmsg);
dialog = gtk_dialog_new_with_buttons("Error", GTK_WINDOW(window_main),
GTK_DIALOG_MODAL & GTK_DIALOG_NO_SEPARATOR & GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
gtk_dialog_set_has_separator ( GTK_DIALOG(dialog), FALSE );
gtk_container_set_border_width (GTK_CONTAINER(dialog), 5);
gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), 15);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
gtk_widget_show_all (dialog);
result = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
/* Terminate the main loop and exit the application. */
static void
pgui_quit (GtkWidget *widget, gpointer data)
{
if ( pg_connection) PQfinish(pg_connection);
pg_connection = NULL;
gtk_main_quit ();
}
/* Set the global config variables controlled by the options dialogue */
static void
pgui_set_config_from_options_ui()
{
const char *entry_encoding = gtk_entry_get_text(GTK_ENTRY(entry_options_encoding));
gboolean preservecase = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_preservecase));
gboolean forceint = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_forceint));
gboolean createindex = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_autoindex));
gboolean dbfonly = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_dbfonly));
gboolean dumpformat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_dumpformat));
gboolean geography = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton_options_geography));
if ( geography )
{
config->geography = 1;
/* Flip the geometry column name to match the load type */
if ( ! strcmp(gtk_entry_get_text(GTK_ENTRY(entry_config_geocolumn)), GEOMETRY_DEFAULT) )
{
gtk_entry_set_text(GTK_ENTRY(entry_config_geocolumn), GEOGRAPHY_DEFAULT );
free(config->geom);
config->geom = strdup(GEOGRAPHY_DEFAULT);
}
}
/* Encoding */
if ( entry_encoding && strlen(entry_encoding) > 0 )
{
if (config->encoding)
free(config->encoding);
config->encoding = strdup(entry_encoding);
}
/* Preserve case */
if ( preservecase )
config->quoteidentifiers = 1;
else
config->quoteidentifiers = 0;
/* No long integers in table */
if ( forceint )
config->forceint4 = 1;
else
config->forceint4 = 0;
/* Create spatial index after load */
if ( createindex )
config->createindex = 1;
else
config->createindex = 0;
/* Read the .shp file, don't ignore it */
if ( dbfonly )
config->readshape = 0;
else
config->readshape = 1;
/* Use COPY rather than INSERT format */
if ( dumpformat )
config->dump_format = 1;
else
config->dump_format = 0;
return;
}
/* Set the global configuration based upon the current UI */
static void
pgui_set_config_from_ui()
{
const char *pg_table = gtk_entry_get_text(GTK_ENTRY(entry_config_table));
const char *pg_schema = gtk_entry_get_text(GTK_ENTRY(entry_config_schema));
const char *pg_geom = gtk_entry_get_text(GTK_ENTRY(entry_config_geocolumn));
const char *source_file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser_button_shape));
const char *entry_srid = gtk_entry_get_text(GTK_ENTRY(entry_config_srid));
char *c;
/* Set the destination schema, table and column parameters */
if (config->table)
free(config->table);
config->table = strdup(pg_table);
if (config->schema)
free(config->schema);
if (strlen(pg_schema) == 0)
config->schema = strdup("public");
else
config->schema = strdup(pg_schema);
if (strlen(pg_geom) == 0)
config->geom = strdup(GEOMETRY_DEFAULT);
else
config->geom = strdup(pg_geom);
/* Set the destination filename: note the shp2pgsql core engine simply wants the file
without the .shp extension */
if (config->shp_file)
free(config->shp_file);
/* Handle empty selection */
if (source_file == NULL)
config->shp_file = strdup("");
else
config->shp_file = strdup(source_file);
/* NULL-terminate the file name before the .shp extension */
for (c = config->shp_file + strlen(config->shp_file); c >= config->shp_file; c--)
{
if (*c == '.')
{
*c = '\0';
break;
}
}
/* SRID */
if ( ! ( config->sr_id = atoi(entry_srid) ) )
{
config->sr_id = -1;
}
return;
}
/* Validate the configuration, returning true or false */
static int
pgui_validate_config()
{
/* Validate table parameters */
if ( ! config->table || strlen(config->table) == 0 )
{
pgui_seterr("Fill in the destination table.");
return 0;
}
if ( ! config->schema || strlen(config->schema) == 0 )
{
pgui_seterr("Fill in the destination schema.");
return 0;
}
if ( ! config->geom || strlen(config->geom) == 0 )
{
pgui_seterr("Fill in the destination column.");
return 0;
}
if ( ! config->shp_file || strlen(config->shp_file) == 0 )
{
pgui_seterr("Select a shape file to import.");
return 0;
}
return 1;
}
/*
** Run a SQL command against the current connection.
*/
static int
pgui_exec(const char *sql)
{
PGresult *res = NULL;
ExecStatusType status;
char sql_trunc[256];
/* We need a connection to do anything. */
if ( ! pg_connection ) return 0;
if ( ! sql ) return 0;
res = PQexec(pg_connection, sql);
status = PQresultStatus(res);
PQclear(res);
/* Did something unexpected happen? */
if ( ! ( status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK ) )
{
/* Log notices and return success. */
if ( status == PGRES_NONFATAL_ERROR )
{
pgui_logf("%s", PQerrorMessage(pg_connection));
return 1;
}
/* Log errors and return failure. */
snprintf(sql_trunc, 255, "%s", sql);
pgui_logf("Failed SQL begins: \"%s\"", sql_trunc);
pgui_logf("Failed in pgui_exec(): %s", PQerrorMessage(pg_connection));
return 0;
}
return 1;
}
/*
** Start the COPY process.
*/
static int
pgui_copy_start(const char *sql)
{
PGresult *res = NULL;
ExecStatusType status;
char sql_trunc[256];
/* We need a connection to do anything. */
if ( ! pg_connection ) return 0;
if ( ! sql ) return 0;
res = PQexec(pg_connection, sql);
status = PQresultStatus(res);
PQclear(res);
/* Did something unexpected happen? */
if ( status != PGRES_COPY_IN )
{
/* Log errors and return failure. */
snprintf(sql_trunc, 255, "%s", sql);
pgui_logf("Failed SQL begins: \"%s\"", sql_trunc);
pgui_logf("Failed in pgui_copy_start(): %s", PQerrorMessage(pg_connection));
return 0;
}
return 1;
}
/*
** Send a line (row) of data into the COPY procedure.
*/
static int
pgui_copy_write(const char *line)
{
char line_trunc[256];
/* We need a connection to do anything. */
if ( ! pg_connection ) return 0;
if ( ! line ) return 0;
/* Did something unexpected happen? */
if ( PQputCopyData(pg_connection, line, strlen(line)) < 0 )
{
/* Log errors and return failure. */
snprintf(line_trunc, 255, "%s", line);
pgui_logf("Failed row begins: \"%s\"", line_trunc);
pgui_logf("Failed in pgui_copy_write(): %s", PQerrorMessage(pg_connection));
return 0;
}
/* Send linefeed to signify end of line */
PQputCopyData(pg_connection, "\n", 1);
return 1;
}
/*
** Finish the COPY process.
*/
static int
pgui_copy_end(const int rollback)
{
char *errmsg = NULL;
/* We need a connection to do anything. */
if ( ! pg_connection ) return 0;
if ( rollback ) errmsg = "Roll back the copy.";
/* Did something unexpected happen? */
if ( PQputCopyEnd(pg_connection, errmsg) < 0 )
{
/* Log errors and return failure. */
pgui_logf("Failed in pgui_copy_end(): %s", PQerrorMessage(pg_connection));
return 0;
}
return 1;
}
static char *
pgui_read_connection(void)
{
const char *pg_host = gtk_entry_get_text(GTK_ENTRY(entry_pg_host));
const char *pg_port = gtk_entry_get_text(GTK_ENTRY(entry_pg_port));
const char *pg_user = gtk_entry_get_text(GTK_ENTRY(entry_pg_user));
const char *pg_pass = gtk_entry_get_text(GTK_ENTRY(entry_pg_pass));
const char *pg_db = gtk_entry_get_text(GTK_ENTRY(entry_pg_db));
char *connection_string = NULL;
stringbuffer_t *sb = stringbuffer_create();
/* Read the host */
if ( pg_host && strlen(pg_host) > 0 )
{
stringbuffer_aprintf(sb, "host=%s ", pg_host);
}
/* Read the port */
if ( pg_port && strlen(pg_port) > 0 )
{
if ( ! atoi(pg_port) )
{
pgui_seterr("Server port must be a number.");
stringbuffer_destroy(sb);
return NULL;
}
stringbuffer_aprintf(sb, "port=%s ", pg_port);
}
/* Read the user name */
if ( pg_user && strlen(pg_user) > 0 )
{
stringbuffer_aprintf(sb, "user=%s ", pg_user);
}
/* Read the database name */
if ( pg_db && strlen(pg_db) > 0 )
{
stringbuffer_aprintf(sb, "dbname=%s ", pg_db);
}
/* Read the password */
if ( pg_pass && strlen(pg_pass) > 0 )
{
stringbuffer_aprintf(sb, "password=%s ", pg_pass);
}
/* Return the connection string */
connection_string = strdup(stringbuffer_getstring(sb));
stringbuffer_destroy(sb);
return connection_string;
}
static void
pgui_action_cancel(GtkWidget *widget, gpointer data)
{
if (!import_running)
pgui_quit(widget, data); /* quit if we're not running */
else
import_running = FALSE;
}
static void
pgui_sanitize_connection_string(char *connection_string)
{
char *ptr = strstr(connection_string, "password");
if ( ptr )
{
ptr += 9;
while ( *ptr != ' ' && *ptr != '\0' )
{
*ptr = '*';
ptr++;
}
}
return;
}
static void
pgui_action_connection_test(GtkWidget *widget, gpointer data)
{
char *connection_string = NULL;
char *connection_sanitized = NULL;
if ( ! (connection_string = pgui_read_connection()) )
{
pgui_raise_error_dialogue();
return;
}
connection_sanitized = strdup(connection_string);
pgui_sanitize_connection_string(connection_sanitized);
pgui_logf("Connecting: %s", connection_sanitized);
free(connection_sanitized);
if ( pg_connection )
PQfinish(pg_connection);
pg_connection = PQconnectdb(connection_string);
if (PQstatus(pg_connection) == CONNECTION_BAD)
{
pgui_logf( "Connection failed: %s", PQerrorMessage(pg_connection));
gtk_label_set_text(GTK_LABEL(label_pg_connection_test), "Connection failed.");
free(connection_string);
PQfinish(pg_connection);
pg_connection = NULL;
return;
}
gtk_label_set_text(GTK_LABEL(label_pg_connection_test), "Connection succeeded.");
pgui_logf( "Connection succeeded." );
gtk_widget_show(label_pg_connection_test);
PQfinish(pg_connection);
pg_connection = NULL;
free(connection_string);
return;
}
static void
pgui_action_options_open(GtkWidget *widget, gpointer data)
{
pgui_create_options_dialogue();
return;
}
static void
pgui_action_options_close(GtkWidget *widget, gpointer data)
{
pgui_set_config_from_options_ui();
gtk_widget_destroy(widget);
return;
}
static void
pgui_action_shape_file_set(GtkWidget *widget, gpointer data)
{
char *shp_file;
int shp_file_len;
char *table_start;
char *table_end;
char *table;
const char *gtk_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
if ( gtk_filename )
{
shp_file = strdup(gtk_filename);
shp_file_len = strlen(shp_file);
}
else
{
return;
}
/* Roll back from end to first slash character. */
table_start = shp_file + shp_file_len;
while ( *table_start != '/' && *table_start != '\\' && table_start > shp_file)
{
table_start--;
}
table_start++; /* Forward one to start of actual characters. */
/* Roll back from end to first . character. */
table_end = shp_file + shp_file_len;
while ( *table_end != '.' && table_end > shp_file && table_end > table_start )
{
table_end--;
}
/* Copy the table name into a fresh memory slot. */
table = malloc(table_end - table_start + 1);
memcpy(table, table_start, table_end - table_start);
table[table_end - table_start] = '\0';
/* Set the table name into the configuration */
config->table = table;
gtk_entry_set_text(GTK_ENTRY(entry_config_table), table);
free(shp_file);
}
static void
pgui_action_import(GtkWidget *widget, gpointer data)
{
char *connection_string = NULL;
char *connection_sanitized = NULL;
char *dest_string = NULL;
int ret, record_count = 0, i = 0, progress_bar_update_frequency = 1;
char *header, *footer, *record;
PGresult *result;
if ( ! (connection_string = pgui_read_connection() ) )
{
pgui_raise_error_dialogue();
return;
}
/*
** Set the configuration from the UI and validate
*/
pgui_set_config_from_ui();
if (! pgui_validate_config() )
{
pgui_raise_error_dialogue();
free(connection_string);
return;
}
/* Log what we know so far */
connection_sanitized = strdup(connection_string);
pgui_sanitize_connection_string(connection_sanitized);
pgui_logf("Connection: %s", connection_sanitized);
pgui_logf("Destination: %s.%s", config->schema, config->table);
pgui_logf("Source File: %s", config->shp_file);
free(connection_sanitized);
/* Connect to the database. */
if ( pg_connection ) PQfinish(pg_connection);
pg_connection = PQconnectdb(connection_string);
if (PQstatus(pg_connection) == CONNECTION_BAD)
{
pgui_logf( "Connection failed: %s", PQerrorMessage(pg_connection));
gtk_label_set_text(GTK_LABEL(label_pg_connection_test), "Connection failed.");
free(connection_string);
free(dest_string);
PQfinish(pg_connection);
pg_connection = NULL;
return;
}
/*
* Loop through the items in the shapefile
*/
/* Disable the button to prevent multiple imports running at the same time */
gtk_widget_set_sensitive(widget, FALSE);
/* Allow GTK events to get a look in */
while (gtk_events_pending())
gtk_main_iteration();
/* Create the shapefile state object */
state = ShpLoaderCreate(config);
/* Open the shapefile */
ret = ShpLoaderOpenShape(state);
if (ret != SHPLOADEROK)
{
pgui_logf("%s", state->message);
if (ret == SHPLOADERERR)
goto import_cleanup;
}
/* If reading the whole shapefile, display its type */
if (state->config->readshape)
{
pgui_logf("Shapefile type: %s", SHPTypeName(state->shpfiletype));
pgui_logf("Postgis type: %s[%d]", state->pgtype, state->pgdims);
}
/* Get the header */
ret = ShpLoaderGetSQLHeader(state, &header);
if (ret != SHPLOADEROK)
{
pgui_logf("%s", state->message);
if (ret == SHPLOADERERR)
goto import_cleanup;
}
/* Send the header to the remote server: if we are in COPY mode then the last
statement will be a COPY and so will change connection mode */
ret = pgui_exec(header);
free(header);
if (!ret)
goto import_cleanup;
/* If we are in COPY (dump format) mode, output the COPY statement and enter COPY mode */
if (state->config->dump_format)
{
ret = ShpLoaderGetSQLCopyStatement(state, &header);
if (ret != SHPLOADEROK)
{
pgui_logf("%s", state->message);
if (ret == SHPLOADERERR)
goto import_cleanup;
}
/* Send the result to the remote server: this should put us in COPY mode */
ret = pgui_copy_start(header);
free(header);
if (!ret)
goto import_cleanup;
}
/* Main loop: iterate through all of the records and send them to stdout */
pgui_logf("Importing shapefile (%d records)...", ShpLoaderGetRecordCount(state));
/* Number of records in this file */
record_count = ShpLoaderGetRecordCount(state);
/* Assume a progress bar 1000 pixels across, how we only want to update it every N records */
progress_bar_update_frequency = 1 + record_count / 1000;
import_running = TRUE;
for (i = 0; (i < record_count) && import_running; i++)
{
ret = ShpLoaderGenerateSQLRowStatement(state, i, &record);
switch (ret)
{
case SHPLOADEROK:
/* Simply send the statement */
if (state->config->dump_format)
ret = pgui_copy_write(record);
else
ret = pgui_exec(record);
/* Display a record number if we failed */
if (!ret)
pgui_logf("Failed record number #%d", i);
free(record);
break;
case SHPLOADERERR:
/* Display the error message then stop */
pgui_logf("%s\n", state->message);
goto import_cleanup;
break;
case SHPLOADERWARN:
/* Display the warning, but continue */
pgui_logf("%s\n", state->message);
if (state->config->dump_format)
ret = pgui_copy_write(record);
else
ret = pgui_exec(record);
/* Display a record number if we failed */
if (!ret)
pgui_logf("Failed record number #%d", i);
free(record);
break;
case SHPLOADERRECDELETED:
/* Record is marked as deleted - ignore */
break;
case SHPLOADERRECISNULL:
/* Record is NULL and should be ignored according to NULL policy */
break;
}
/* Update the progress bar */
if( i % progress_bar_update_frequency == 0 )
{
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (float)i / record_count);
}
/* Allow GTK events to get a look in */
while (gtk_events_pending())
gtk_main_iteration();
}
/* If we are in COPY (dump format) mode, leave COPY mode */
if (state->config->dump_format)
{
if (! pgui_copy_end(0) )
goto import_cleanup;
result = PQgetResult(pg_connection);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
pgui_logf("COPY failed with the following error: %s", PQerrorMessage(pg_connection));
ret = SHPLOADERERR;
goto import_cleanup;
}
}
/* Only continue if we didn't abort part way through */
if (import_running)
{
/* Get the footer */
ret = ShpLoaderGetSQLFooter(state, &footer);
if (ret != SHPLOADEROK)
{
pgui_logf("%s\n", state->message);
if (ret == SHPLOADERERR)
goto import_cleanup;
}
if ( state->config->createindex )
{
pgui_logf("Creating spatial index...\n");
}
/* Send the footer to the server */
ret = pgui_exec(footer);
free(footer);
if (!ret)
goto import_cleanup;
}
import_cleanup:
/* If we didn't finish inserting all of the items, an error occurred */
if (i != ShpLoaderGetRecordCount(state) || !ret)
pgui_logf("Shapefile import failed.");
else
pgui_logf("Shapefile import completed.");
/* Free the state object */
ShpLoaderDestroy(state);
/* Import has definitely finished */
import_running = FALSE;
/* Enable the button once again */
gtk_widget_set_sensitive(widget, TRUE);
/* Silly GTK bug means we have to hide and show the button for it to work again! */
gtk_widget_hide(widget);
gtk_widget_show(widget);
/* Reset the progress bar */
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), 0.0);
/* Allow GTK events to get a look in */
while (gtk_events_pending())
gtk_main_iteration();
/* Tidy up */
free(connection_string);
free(dest_string);
connection_string = dest_string = NULL;
/* Disconnect from the database */
PQfinish(pg_connection);
pg_connection = NULL;
return;
}
static void
pgui_create_options_dialogue_add_label(GtkWidget *table, const char *str, gfloat alignment, int row)
{
GtkWidget *align = gtk_alignment_new( alignment, 0.5, 0.0, 1.0 );
GtkWidget *label = gtk_label_new( str );
gtk_table_attach_defaults(GTK_TABLE(table), align, 1, 3, row, row+1 );
gtk_container_add (GTK_CONTAINER (align), label);
}
static void
pgui_action_about_open()
{
GtkWidget *dlg;
const char *authors[] =
{
"Paul Ramsey <pramsey@opengeo.org>",
"Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>",
NULL
};
dlg = gtk_about_dialog_new ();
gtk_about_dialog_set_name (GTK_ABOUT_DIALOG(dlg), "Shape to PostGIS");
gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(dlg), GUI_RCSID);
/* gtk_about_dialog_set_version (GTK_ABOUT_DIALOG(dlg), GUI_RCSID); */
gtk_about_dialog_set_website (GTK_ABOUT_DIALOG(dlg), "http://postgis.org/");
gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(dlg), authors);
g_signal_connect_swapped(dlg, "response", G_CALLBACK(gtk_widget_destroy), dlg);
gtk_widget_show (dlg);
}
static void
pgui_create_options_dialogue()
{
GtkWidget *table_options;
GtkWidget *align_options_center;
GtkWidget *dialog_options;
static int text_width = 12;
dialog_options = gtk_dialog_new_with_buttons ("Import Options", GTK_WINDOW(window_main), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);
gtk_window_set_modal (GTK_WINDOW(dialog_options), TRUE);
gtk_window_set_keep_above (GTK_WINDOW(dialog_options), TRUE);
gtk_window_set_default_size (GTK_WINDOW(dialog_options), 180, 200);
table_options = gtk_table_new(7, 3, TRUE);
gtk_container_set_border_width (GTK_CONTAINER (table_options), 12);
gtk_table_set_row_spacings(GTK_TABLE(table_options), 5);
gtk_table_set_col_spacings(GTK_TABLE(table_options), 10);
pgui_create_options_dialogue_add_label(table_options, "DBF file character encoding", 0.0, 0);
entry_options_encoding = gtk_entry_new();
gtk_entry_set_width_chars(GTK_ENTRY(entry_options_encoding), text_width);
gtk_entry_set_text(GTK_ENTRY(entry_options_encoding), config->encoding);
gtk_table_attach_defaults(GTK_TABLE(table_options), entry_options_encoding, 0, 1, 0, 1 );
pgui_create_options_dialogue_add_label(table_options, "Preserve case of column names", 0.0, 1);
checkbutton_options_preservecase = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_options_preservecase), config->quoteidentifiers ? TRUE : FALSE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 1.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 1, 2 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_preservecase);
pgui_create_options_dialogue_add_label(table_options, "Do not create 'bigint' columns", 0.0, 2);
checkbutton_options_forceint = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_options_forceint), config->forceint4 ? TRUE : FALSE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 1.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 2, 3 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_forceint);
pgui_create_options_dialogue_add_label(table_options, "Create spatial index automatically after load", 0.0, 3);
checkbutton_options_autoindex = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_options_autoindex), config->createindex ? TRUE : FALSE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 1.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 3, 4 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_autoindex);
pgui_create_options_dialogue_add_label(table_options, "Load only attribute (dbf) data", 0.0, 4);
checkbutton_options_dbfonly = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (checkbutton_options_dbfonly), config->readshape ? FALSE : TRUE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 1.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 4, 5 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_dbfonly);
pgui_create_options_dialogue_add_label(table_options, "Load data using COPY rather than INSERT", 0.0, 5);
checkbutton_options_dumpformat = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (checkbutton_options_dumpformat), config->dump_format ? TRUE : FALSE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 0.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 5, 6 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_dumpformat);
pgui_create_options_dialogue_add_label(table_options, "Load into GEOGRAPHY column", 0.0, 6);
checkbutton_options_geography = gtk_check_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_options_geography), config->geography ? TRUE : FALSE);
align_options_center = gtk_alignment_new( 0.5, 0.5, 0.0, 1.0 );
gtk_table_attach_defaults(GTK_TABLE(table_options), align_options_center, 0, 1, 6, 7 );
gtk_container_add (GTK_CONTAINER (align_options_center), checkbutton_options_geography);
g_signal_connect(dialog_options, "response", G_CALLBACK(pgui_action_options_close), dialog_options);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_options)->vbox), table_options, FALSE, FALSE, 0);
gtk_widget_show_all (dialog_options);
}
static void
pgui_create_main_window(const SHPCONNECTIONCONFIG *conn)
{
static int text_width = 12;
/* Reusable label handle */
GtkWidget *label;
/* Main widgets */
GtkWidget *vbox_main;
/* Shape file section */
GtkWidget *file_chooser_dialog_shape;
GtkFileFilter *file_filter_shape;
/* PgSQL section */
GtkWidget *frame_pg, *frame_shape, *frame_log, *frame_config;