forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-pgsql.c
1488 lines (1278 loc) · 50.2 KB
/
output-pgsql.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
/* Implements the mid-layer processing for osm2pgsql
* using several PostgreSQL tables
*
* This layer stores data read in from the planet.osm file
* and is then read by the backend processing code to
* emit the final geometry-enabled output formats
*/
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include <libpq-fe.h>
#include "osmtypes.h"
#include "output.h"
#include "reprojection.h"
#include "output-pgsql.h"
#include "build_geometry.h"
#include "middle.h"
#include "pgsql.h"
#include "expire-tiles.h"
#include "wildcmp.h"
#include "node-ram-cache.h"
#include "tagtransform.h"
#define SRID (project_getprojinfo()->srs)
/* FIXME: Shouldn't malloc this all to begin with but call realloc()
as required. The program will most likely segfault if it reads a
style file with more styles than this */
#define MAX_STYLES 1000
enum table_id {
t_point, t_line, t_poly, t_roads
};
static const struct output_options *Options;
/* enable output of a generated way_area tag to either hstore or its own column */
static int enable_way_area=1;
/* Tables to output */
static struct s_table {
char *name;
const char *type;
PGconn *sql_conn;
char buffer[1024];
unsigned int buflen;
int copyMode;
char *columns;
} tables [] = {
{ .name = "%s_point", .type = "POINT" },
{ .name = "%s_line", .type = "LINESTRING"},
{ .name = "%s_polygon", .type = "GEOMETRY" }, /* Actually POLGYON & MULTIPOLYGON but no way to limit to just these two */
{ .name = "%s_roads", .type = "LINESTRING"}
};
#define NUM_TABLES ((signed)(sizeof(tables) / sizeof(tables[0])))
static struct flagsname {
char *name;
int flag;
} tagflags[] = {
{ .name = "polygon", .flag = FLAG_POLYGON },
{ .name = "linear", .flag = FLAG_LINEAR },
{ .name = "nocache", .flag = FLAG_NOCACHE },
{ .name = "delete", .flag = FLAG_DELETE },
{ .name = "phstore", .flag = FLAG_PHSTORE }
};
#define NUM_FLAGS ((signed)(sizeof(tagflags) / sizeof(tagflags[0])))
struct taginfo *exportList[4]; /* Indexed by enum table_id */
int exportListCount[4];
static int pgsql_delete_way_from_output(osmid_t osm_id);
static int pgsql_delete_relation_from_output(osmid_t osm_id);
static int pgsql_process_relation(osmid_t id, struct member *members, int member_count, struct keyval *tags, int exists);
void read_style_file( const char *filename )
{
FILE *in;
int lineno = 0;
int num_read = 0;
char osmtype[24];
char tag[64];
char datatype[24];
char flags[128];
int i;
char *str;
int fields;
struct taginfo temp;
char buffer[1024];
int flag = 0;
exportList[OSMTYPE_NODE] = malloc( sizeof(struct taginfo) * MAX_STYLES );
exportList[OSMTYPE_WAY] = malloc( sizeof(struct taginfo) * MAX_STYLES );
in = fopen( filename, "rt" );
if( !in )
{
fprintf( stderr, "Couldn't open style file '%s': %s\n", filename, strerror(errno) );
exit_nicely();
}
while( fgets( buffer, sizeof(buffer), in) != NULL )
{
lineno++;
str = strchr( buffer, '#' );
if( str )
*str = '\0';
fields = sscanf( buffer, "%23s %63s %23s %127s", osmtype, tag, datatype, flags );
if( fields <= 0 ) /* Blank line */
continue;
if( fields < 3 )
{
fprintf( stderr, "Error reading style file line %d (fields=%d)\n", lineno, fields );
exit_nicely();
}
temp.name = strdup(tag);
temp.type = strdup(datatype);
temp.flags = 0;
for( str = strtok( flags, ",\r\n" ); str; str = strtok(NULL, ",\r\n") )
{
for( i=0; i<NUM_FLAGS; i++ )
{
if( strcmp( tagflags[i].name, str ) == 0 )
{
temp.flags |= tagflags[i].flag;
break;
}
}
if( i == NUM_FLAGS )
fprintf( stderr, "Unknown flag '%s' line %d, ignored\n", str, lineno );
}
if ((temp.flags!=FLAG_DELETE) && ((strchr(temp.name,'?') != NULL) || (strchr(temp.name,'*') != NULL))) {
fprintf( stderr, "wildcard '%s' in non-delete style entry\n",temp.name);
exit_nicely();
}
if ((0==strcmp(temp.name,"way_area")) && (temp.flags==FLAG_DELETE)) {
enable_way_area=0;
}
temp.count = 0;
/* printf("%s %s %d %d\n", temp.name, temp.type, temp.polygon, offset ); */
if( strstr( osmtype, "node" ) )
{
memcpy( &exportList[ OSMTYPE_NODE ][ exportListCount[ OSMTYPE_NODE ] ], &temp, sizeof(temp) );
exportListCount[ OSMTYPE_NODE ]++;
flag = 1;
}
if( strstr( osmtype, "way" ) )
{
memcpy( &exportList[ OSMTYPE_WAY ][ exportListCount[ OSMTYPE_WAY ] ], &temp, sizeof(temp) );
exportListCount[ OSMTYPE_WAY ]++;
flag = 1;
}
if( !flag )
{
fprintf( stderr, "Weird style line %d\n", lineno );
exit_nicely();
}
num_read++;
}
if (ferror(in)) {
perror(filename);
exit_nicely();
}
if (num_read == 0) {
fprintf(stderr, "Unable to parse any valid columns from the style file. Aborting.\n");
exit_nicely();
}
fclose(in);
}
static void free_style_refs(const char *name, const char *type)
{
/* Find and remove any other references to these pointers
This would be way easier if we kept a single list of styles
Currently this scales with n^2 number of styles */
int i,j;
for (i=0; i<NUM_TABLES; i++) {
for(j=0; j<exportListCount[i]; j++) {
if (exportList[i][j].name == name)
exportList[i][j].name = NULL;
if (exportList[i][j].type == type)
exportList[i][j].type = NULL;
}
}
}
static void free_style(void)
{
int i, j;
for (i=0; i<NUM_TABLES; i++) {
for(j=0; j<exportListCount[i]; j++) {
free(exportList[i][j].name);
free(exportList[i][j].type);
free_style_refs(exportList[i][j].name, exportList[i][j].type);
}
}
for (i=0; i<NUM_TABLES; i++)
free(exportList[i]);
}
/* Handles copying out, but coalesces the data into large chunks for
* efficiency. PostgreSQL doesn't actually need this, but each time you send
* a block of data you get 5 bytes of overhead. Since we go column by column
* with most empty and one byte delimiters, without this optimisation we
* transfer three times the amount of data necessary.
*/
void copy_to_table(enum table_id table, const char *sql)
{
PGconn *sql_conn = tables[table].sql_conn;
unsigned int len = strlen(sql);
unsigned int buflen = tables[table].buflen;
char *buffer = tables[table].buffer;
/* Return to copy mode if we dropped out */
if( !tables[table].copyMode )
{
pgsql_exec(sql_conn, PGRES_COPY_IN, "COPY %s (%s,way) FROM STDIN", tables[table].name, tables[table].columns);
tables[table].copyMode = 1;
}
/* If the combination of old and new data is too big, flush old data */
if( (unsigned)(buflen + len) > sizeof( tables[table].buffer )-10 )
{
pgsql_CopyData(tables[table].name, sql_conn, buffer);
buflen = 0;
/* If new data by itself is also too big, output it immediately */
if( (unsigned)len > sizeof( tables[table].buffer )-10 )
{
pgsql_CopyData(tables[table].name, sql_conn, sql);
len = 0;
}
}
/* Normal case, just append to buffer */
if( len > 0 )
{
strcpy( buffer+buflen, sql );
buflen += len;
len = 0;
}
/* If we have completed a line, output it */
if( buflen > 0 && buffer[buflen-1] == '\n' )
{
pgsql_CopyData(tables[table].name, sql_conn, buffer);
buflen = 0;
}
tables[table].buflen = buflen;
}
static void pgsql_out_cleanup(void)
{
int i;
for (i=0; i<NUM_TABLES; i++) {
if (tables[i].sql_conn) {
PQfinish(tables[i].sql_conn);
tables[i].sql_conn = NULL;
}
}
}
/* Escape data appropriate to the type */
static void escape_type(char *sql, int len, const char *value, const char *type) {
int items;
static int tmplen=0;
static char *tmpstr;
if (len > tmplen) {
tmpstr=realloc(tmpstr,len);
tmplen=len;
}
strcpy(tmpstr,value);
if ( !strcmp(type, "int4") ) {
int from, to;
/* For integers we take the first number, or the average if it's a-b */
items = sscanf(value, "%d-%d", &from, &to);
if ( items == 1 ) {
sprintf(sql, "%d", from);
} else if ( items == 2 ) {
sprintf(sql, "%d", (from + to) / 2);
} else {
sprintf(sql, "\\N");
}
} else {
/*
try to "repair" real values as follows:
* assume "," to be a decimal mark which need to be replaced by "."
* like int4 take the first number, or the average if it's a-b
* assume SI unit (meters)
* convert feet to meters (1 foot = 0.3048 meters)
* reject anything else
*/
if ( !strcmp(type, "real") ) {
int i,slen;
float from,to;
slen=strlen(value);
for (i=0;i<slen;i++) if (tmpstr[i]==',') tmpstr[i]='.';
items = sscanf(tmpstr, "%f-%f", &from, &to);
if ( items == 1 ) {
if ((tmpstr[slen-2]=='f') && (tmpstr[slen-1]=='t')) {
from*=0.3048;
}
sprintf(sql, "%f", from);
} else if ( items == 2 ) {
if ((tmpstr[slen-2]=='f') && (tmpstr[slen-1]=='t')) {
from*=0.3048;
to*=0.3048;
}
sprintf(sql, "%f", (from + to) / 2);
} else {
sprintf(sql, "\\N");
}
} else {
escape(sql, len, value);
}
}
}
static void write_hstore(enum table_id table, struct keyval *tags)
{
static char *sql;
static size_t sqllen=0;
size_t hlen;
/* a clone of the tags pointer */
struct keyval *xtags = tags;
/* sql buffer */
if (sqllen==0) {
sqllen=2048;
sql=malloc(sqllen);
}
/* while this tags has a follow-up.. */
while (xtags->next->key != NULL)
{
/* hard exclude z_order tag and keys which have their own column */
if ((xtags->next->has_column) || (strcmp("z_order",xtags->next->key)==0)) {
/* update the tag-pointer to point to the next tag */
xtags = xtags->next;
continue;
}
/*
hstore ASCII representation looks like
"<key>"=>"<value>"
we need at least strlen(key)+strlen(value)+6+'\0' bytes
in theory any single character could also be escaped
thus we need an additional factor of 2.
The maximum lenght of a single hstore element is thus
calcuated as follows:
*/
hlen=2 * (strlen(xtags->next->key) + strlen(xtags->next->value)) + 7;
/* if the sql buffer is too small */
if (hlen > sqllen) {
sqllen = hlen;
sql = realloc(sql, sqllen);
}
/* pack the tag with its value into the hstore */
keyval2hstore(sql, xtags->next);
copy_to_table(table, sql);
/* update the tag-pointer to point to the next tag */
xtags = xtags->next;
/* if the tag has a follow up, add a comma to the end */
if (xtags->next->key != NULL)
copy_to_table(table, ",");
}
/* finish the hstore column by placing a TAB into the data stream */
copy_to_table(table, "\t");
/* the main hstore-column has now been written */
}
/* write an hstore column to the database */
static void write_hstore_columns(enum table_id table, struct keyval *tags)
{
static char *sql;
static size_t sqllen=0;
char *shortkey;
/* the index of the current hstore column */
int i_hstore_column;
int found;
struct keyval *xtags;
char *pos;
size_t hlen;
/* sql buffer */
if (sqllen==0) {
sqllen=2048;
sql=malloc(sqllen);
}
/* iterate over all configured hstore colums in the options */
for(i_hstore_column = 0; i_hstore_column < Options->n_hstore_columns; i_hstore_column++)
{
/* did this node have a tag that matched the current hstore column */
found = 0;
/* a clone of the tags pointer */
xtags = tags;
/* while this tags has a follow-up.. */
while (xtags->next->key != NULL) {
/* check if the tag's key starts with the name of the hstore column */
pos = strstr(xtags->next->key, Options->hstore_columns[i_hstore_column]);
/* and if it does.. */
if(pos == xtags->next->key)
{
/* remember we found one */
found=1;
/* generate the short key name */
shortkey = xtags->next->key + strlen(Options->hstore_columns[i_hstore_column]);
/* calculate the size needed for this hstore entry */
hlen=2*(strlen(shortkey)+strlen(xtags->next->value))+7;
/* if the sql buffer is too small */
if (hlen > sqllen) {
/* resize it */
sqllen=hlen;
sql=realloc(sql,sqllen);
}
/* and pack the shortkey with its value into the hstore */
keyval2hstore_manual(sql, shortkey, xtags->next->value);
copy_to_table(table, sql);
/* update the tag-pointer to point to the next tag */
xtags=xtags->next;
/* if the tag has a follow up, add a comma to the end */
if (xtags->next->key != NULL)
copy_to_table(table, ",");
}
else
{
/* update the tag-pointer to point to the next tag */
xtags=xtags->next;
}
}
/* if no matching tag has been found, write a NULL */
if(!found)
copy_to_table(table, "\\N");
/* finish the hstore column by placing a TAB into the data stream */
copy_to_table(table, "\t");
}
/* all hstore-columns have now been written */
}
/* example from: pg_dump -F p -t planet_osm gis
COPY planet_osm (osm_id, name, place, landuse, leisure, "natural", man_made, waterway, highway, railway, amenity, tourism, learning, building, bridge, layer, way) FROM stdin;
17959841 \N \N \N \N \N \N \N bus_stop \N \N \N \N \N \N -\N 0101000020E610000030CCA462B6C3D4BF92998C9B38E04940
17401934 The Horn \N \N \N \N \N \N \N \N pub \N \N \N \N -\N 0101000020E6100000C12FC937140FD5BFB4D2F4FB0CE04940
...
mine - 01 01000000 48424298424242424242424256427364
psql - 01 01000020 E6100000 30CCA462B6C3D4BF92998C9B38E04940
01 01000020 E6100000 48424298424242424242424256427364
0x2000_0000 = hasSRID, following 4 bytes = srid, not supported by geos WKBWriter
Workaround - output SRID=4326;<WKB>
*/
static int pgsql_out_node(osmid_t id, struct keyval *tags, double node_lat, double node_lon)
{
int filter = tagtransform_filter_node_tags(tags);
static char *sql;
static size_t sqllen=0;
int i;
struct keyval *tag;
if (filter) return 1;
if (sqllen==0) {
sqllen=2048;
sql=malloc(sqllen);
}
expire_tiles_from_bbox(node_lon, node_lat, node_lon, node_lat);
sprintf(sql, "%" PRIdOSMID "\t", id);
copy_to_table(t_point, sql);
for (i=0; i < exportListCount[OSMTYPE_NODE]; i++) {
if( exportList[OSMTYPE_NODE][i].flags & FLAG_DELETE )
continue;
if( (exportList[OSMTYPE_NODE][i].flags & FLAG_PHSTORE) == FLAG_PHSTORE)
continue;
if ((tag = getTag(tags, exportList[OSMTYPE_NODE][i].name)))
{
escape_type(sql, sqllen, tag->value, exportList[OSMTYPE_NODE][i].type);
exportList[OSMTYPE_NODE][i].count++;
if (HSTORE_NORM==Options->enable_hstore)
tag->has_column=1;
}
else
sprintf(sql, "\\N");
copy_to_table(t_point, sql);
copy_to_table(t_point, "\t");
}
/* hstore columns */
write_hstore_columns(t_point, tags);
/* check if a regular hstore is requested */
if (Options->enable_hstore)
write_hstore(t_point, tags);
#ifdef FIXED_POINT
// guarantee that we use the same values as in the node cache
scale = Options->scale;
node_lon = FIX_TO_DOUBLE(DOUBLE_TO_FIX(node_lon));
node_lat = FIX_TO_DOUBLE(DOUBLE_TO_FIX(node_lat));
#endif
sprintf(sql, "SRID=%d;POINT(%.15g %.15g)", SRID, node_lon, node_lat);
copy_to_table(t_point, sql);
copy_to_table(t_point, "\n");
return 0;
}
static void write_wkts(osmid_t id, struct keyval *tags, const char *wkt, enum table_id table)
{
static char *sql;
static size_t sqllen=0;
int j;
struct keyval *tag;
if (sqllen==0) {
sqllen=2048;
sql=malloc(sqllen);
}
sprintf(sql, "%" PRIdOSMID "\t", id);
copy_to_table(table, sql);
for (j=0; j < exportListCount[OSMTYPE_WAY]; j++) {
if( exportList[OSMTYPE_WAY][j].flags & FLAG_DELETE )
continue;
if( (exportList[OSMTYPE_WAY][j].flags & FLAG_PHSTORE) == FLAG_PHSTORE)
continue;
if ((tag = getTag(tags, exportList[OSMTYPE_WAY][j].name)))
{
exportList[OSMTYPE_WAY][j].count++;
escape_type(sql, sqllen, tag->value, exportList[OSMTYPE_WAY][j].type);
if (HSTORE_NORM==Options->enable_hstore)
tag->has_column=1;
}
else
sprintf(sql, "\\N");
copy_to_table(table, sql);
copy_to_table(table, "\t");
}
/* hstore columns */
write_hstore_columns(table, tags);
/* check if a regular hstore is requested */
if (Options->enable_hstore)
write_hstore(table, tags);
sprintf(sql, "SRID=%d;", SRID);
copy_to_table(table, sql);
copy_to_table(table, wkt);
copy_to_table(table, "\n");
}
/*static int tag_indicates_polygon(enum OsmType type, const char *key)
{
int i;
if (!strcmp(key, "area"))
return 1;
for (i=0; i < exportListCount[type]; i++) {
if( strcmp( exportList[type][i].name, key ) == 0 )
return exportList[type][i].flags & FLAG_POLYGON;
}
return 0;
}*/
/*
COPY planet_osm (osm_id, name, place, landuse, leisure, "natural", man_made, waterway, highway, railway, amenity, tourism, learning, bu
ilding, bridge, layer, way) FROM stdin;
198497 Bedford Road \N \N \N \N \N \N residential \N \N \N \N \N \N \N 0102000020E610000004000000452BF702B342D5BF1C60E63BF8DF49406B9C4D470037D5BF5471E316F3DF4940DFA815A6EF35D5BF9AE95E27F5DF4940B41EB
E4C1421D5BF24D06053E7DF4940
212696 Oswald Road \N \N \N \N \N \N minor \N \N \N \N \N \N \N 0102000020E610000004000000467D923B6C22D5BFA359D93EE4DF4940B3976DA7AD11D5BF84BBB376DBDF4940997FF44D9A06D5BF4223D8B8FEDF49404D158C4AEA04D
5BF5BB39597FCDF4940
*/
static int pgsql_out_way(osmid_t id, struct keyval *tags, struct osmNode *nodes, int count, int exists)
{
int polygon = 0, roads = 0;
int i, wkt_size;
double split_at;
double area;
/* If the flag says this object may exist already, delete it first */
if(exists) {
pgsql_delete_way_from_output(id);
Options->mid->way_changed(id);
}
if (tagtransform_filter_way_tags(tags, &polygon, &roads))
return 0;
/* Split long ways after around 1 degree or 100km */
if (Options->projection == PROJ_LATLONG)
split_at = 1;
else
split_at = 100 * 1000;
wkt_size = get_wkt_split(nodes, count, polygon, split_at);
for (i=0;i<wkt_size;i++)
{
char *wkt = get_wkt(i);
if (wkt && strlen(wkt)) {
/* FIXME: there should be a better way to detect polygons */
if (!strncmp(wkt, "POLYGON", strlen("POLYGON")) || !strncmp(wkt, "MULTIPOLYGON", strlen("MULTIPOLYGON"))) {
expire_tiles_from_nodes_poly(nodes, count, id);
area = get_area(i);
if ((area > 0.0) && enable_way_area) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", area);
addItem(tags, "way_area", tmp, 0);
}
write_wkts(id, tags, wkt, t_poly);
} else {
expire_tiles_from_nodes_line(nodes, count);
write_wkts(id, tags, wkt, t_line);
if (roads)
write_wkts(id, tags, wkt, t_roads);
}
}
free(wkt);
}
clear_wkts();
return 0;
}
static int pgsql_out_relation(osmid_t id, struct keyval *rel_tags, int member_count, struct osmNode **xnodes, struct keyval *xtags, int *xcount, osmid_t *xid, const char **xrole)
{
int i, wkt_size;
int roads = 0;
int make_polygon = 0;
int make_boundary = 0;
int * members_superseeded;
double split_at;
members_superseeded = calloc(sizeof(int), member_count);
if (member_count == 0) {
free(members_superseeded);
return 0;
}
if (tagtransform_filter_rel_member_tags(rel_tags, member_count, xtags, xrole, members_superseeded, &make_boundary, &make_polygon, &roads)) {
free(members_superseeded);
return 0;
}
/* Split long linear ways after around 1 degree or 100km (polygons not effected) */
if (Options->projection == PROJ_LATLONG)
split_at = 1;
else
split_at = 100 * 1000;
wkt_size = build_geometry(id, xnodes, xcount, make_polygon, Options->enable_multi, split_at);
if (!wkt_size) {
free(members_superseeded);
return 0;
}
for (i=0;i<wkt_size;i++) {
char *wkt = get_wkt(i);
if (wkt && strlen(wkt)) {
expire_tiles_from_wkt(wkt, -id);
/* FIXME: there should be a better way to detect polygons */
if (!strncmp(wkt, "POLYGON", strlen("POLYGON")) || !strncmp(wkt, "MULTIPOLYGON", strlen("MULTIPOLYGON"))) {
double area = get_area(i);
if ((area > 0.0) && enable_way_area) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", area);
addItem(rel_tags, "way_area", tmp, 0);
}
write_wkts(-id, rel_tags, wkt, t_poly);
} else {
write_wkts(-id, rel_tags, wkt, t_line);
if (roads)
write_wkts(-id, rel_tags, wkt, t_roads);
}
}
free(wkt);
}
clear_wkts();
/* Tagtransform will have marked those member ways of the relation that
* have fully been dealt with as part of the multi-polygon entry.
* Set them in the database as done and delete their entry to not
* have duplicates */
if (make_polygon) {
for (i=0; xcount[i]; i++) {
if (members_superseeded[i]) {
Options->mid->ways_done(xid[i]);
pgsql_delete_way_from_output(xid[i]);
}
}
}
free(members_superseeded);
/* If we are making a boundary then also try adding any relations which form complete rings
The linear variants will have already been processed above */
if (make_boundary) {
wkt_size = build_geometry(id, xnodes, xcount, 1, Options->enable_multi, split_at);
for (i=0;i<wkt_size;i++)
{
char *wkt = get_wkt(i);
if (strlen(wkt)) {
expire_tiles_from_wkt(wkt, -id);
/* FIXME: there should be a better way to detect polygons */
if (!strncmp(wkt, "POLYGON", strlen("POLYGON")) || !strncmp(wkt, "MULTIPOLYGON", strlen("MULTIPOLYGON"))) {
double area = get_area(i);
if ((area > 0.0) && enable_way_area) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", area);
addItem(rel_tags, "way_area", tmp, 0);
}
write_wkts(-id, rel_tags, wkt, t_poly);
}
}
free(wkt);
}
clear_wkts();
}
return 0;
}
static int pgsql_out_connect(const struct output_options *options, int startTransaction) {
int i;
for (i=0; i<NUM_TABLES; i++) {
PGconn *sql_conn;
sql_conn = PQconnectdb(options->conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(sql_conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(sql_conn));
return 1;
}
tables[i].sql_conn = sql_conn;
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "SET synchronous_commit TO off;");
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "PREPARE get_wkt (" POSTGRES_OSMID_TYPE ") AS SELECT ST_AsText(way) FROM %s WHERE osm_id = $1;\n", tables[i].name);
if (startTransaction)
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "BEGIN");
}
return 0;
}
static int pgsql_out_start(const struct output_options *options)
{
char *sql, tmp[256];
PGresult *res;
int i,j;
unsigned int sql_len;
int their_srid;
int i_hstore_column;
enum OsmType type;
int numTags;
struct taginfo *exportTags;
Options = options;
read_style_file( options->style );
sql_len = 2048;
sql = malloc(sql_len);
assert(sql);
for (i=0; i<NUM_TABLES; i++) {
PGconn *sql_conn;
/* Substitute prefix into name of table */
{
char *temp = malloc( strlen(options->prefix) + strlen(tables[i].name) + 1 );
sprintf( temp, tables[i].name, options->prefix );
tables[i].name = temp;
}
fprintf(stderr, "Setting up table: %s\n", tables[i].name);
sql_conn = PQconnectdb(options->conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(sql_conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(sql_conn));
exit_nicely();
}
tables[i].sql_conn = sql_conn;
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "SET synchronous_commit TO off;");
if (!options->append) {
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS %s", tables[i].name);
}
else
{
sprintf(sql, "SELECT srid FROM geometry_columns WHERE f_table_name='%s';", tables[i].name);
res = PQexec(sql_conn, sql);
if (!((PQntuples(res) == 1) && (PQnfields(res) == 1)))
{
fprintf(stderr, "Problem reading geometry information for table %s - does it exist?\n", tables[i].name);
exit_nicely();
}
their_srid = atoi(PQgetvalue(res, 0, 0));
PQclear(res);
if (their_srid != SRID)
{
fprintf(stderr, "SRID mismatch: cannot append to table %s (SRID %d) using selected SRID %d\n", tables[i].name, their_srid, SRID);
exit_nicely();
}
}
/* These _tmp tables can be left behind if we run out of disk space */
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS %s_tmp", tables[i].name);
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "BEGIN");
type = (i == t_point)?OSMTYPE_NODE:OSMTYPE_WAY;
numTags = exportListCount[type];
exportTags = exportList[type];
if (!options->append) {
sprintf(sql, "CREATE TABLE %s ( osm_id " POSTGRES_OSMID_TYPE, tables[i].name );
for (j=0; j < numTags; j++) {
if( exportTags[j].flags & FLAG_DELETE )
continue;
if( (exportTags[j].flags & FLAG_PHSTORE ) == FLAG_PHSTORE)
continue;
sprintf(tmp, ",\"%s\" %s", exportTags[j].name, exportTags[j].type);
if (strlen(sql) + strlen(tmp) + 1 > sql_len) {
sql_len *= 2;
sql = realloc(sql, sql_len);
assert(sql);
}
strcat(sql, tmp);
}
for(i_hstore_column = 0; i_hstore_column < Options->n_hstore_columns; i_hstore_column++)
{
strcat(sql, ",\"");
strcat(sql, Options->hstore_columns[i_hstore_column]);
strcat(sql, "\" hstore ");
}
if (Options->enable_hstore) {
strcat(sql, ",tags hstore");
}
strcat(sql, ")");
if (Options->tblsmain_data) {
sprintf(sql + strlen(sql), " TABLESPACE %s", Options->tblsmain_data);
}
strcat(sql, "\n");
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", sql);
pgsql_exec(sql_conn, PGRES_TUPLES_OK, "SELECT AddGeometryColumn('%s', 'way', %d, '%s', 2 );\n",
tables[i].name, SRID, tables[i].type );
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ALTER TABLE %s ALTER COLUMN way SET NOT NULL;\n", tables[i].name);
/* slim mode needs this to be able to apply diffs */
if (Options->slim && !Options->droptemp) {
sprintf(sql, "CREATE INDEX %s_pkey ON %s USING BTREE (osm_id)", tables[i].name, tables[i].name);
if (Options->tblsmain_index) {
sprintf(sql + strlen(sql), " TABLESPACE %s\n", Options->tblsmain_index);
}
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", sql);
}
} else {
/* Add any new columns referenced in the default.style */
PGresult *res;
sprintf(sql, "SELECT * FROM %s LIMIT 0;\n", tables[i].name);
res = PQexec(sql_conn, sql);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Error, failed to query table %s\n%s\n", tables[i].name, sql);
exit_nicely();
}
for (j=0; j < numTags; j++) {
if( exportTags[j].flags & FLAG_DELETE )
continue;
if( (exportTags[j].flags & FLAG_PHSTORE) == FLAG_PHSTORE)
continue;
sprintf(tmp, "\"%s\"", exportTags[j].name);
if (PQfnumber(res, tmp) < 0) {
#if 0
fprintf(stderr, "Append failed. Column \"%s\" is missing from \"%s\"\n", exportTags[j].name, tables[i].name);
exit_nicely();
#else
fprintf(stderr, "Adding new column \"%s\" to \"%s\"\n", exportTags[j].name, tables[i].name);
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ALTER TABLE %s ADD COLUMN \"%s\" %s;\n", tables[i].name, exportTags[j].name, exportTags[j].type);
#endif
}
/* Note: we do not verify the type or delete unused columns */
}
PQclear(res);
/* change the type of the geometry column if needed - this can only change to a more permisive type */
}
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "PREPARE get_wkt (" POSTGRES_OSMID_TYPE ") AS SELECT ST_AsText(way) FROM %s WHERE osm_id = $1;\n", tables[i].name);
/* Generate column list for COPY */
strcpy(sql, "osm_id");
for (j=0; j < numTags; j++) {
if( exportTags[j].flags & FLAG_DELETE )
continue;
if( (exportTags[j].flags & FLAG_PHSTORE ) == FLAG_PHSTORE)
continue;
sprintf(tmp, ",\"%s\"", exportTags[j].name);
if (strlen(sql) + strlen(tmp) + 1 > sql_len) {
sql_len *= 2;
sql = realloc(sql, sql_len);
assert(sql);
}
strcat(sql, tmp);
}
for(i_hstore_column = 0; i_hstore_column < Options->n_hstore_columns; i_hstore_column++)
{
strcat(sql, ",\"");
strcat(sql, Options->hstore_columns[i_hstore_column]);
strcat(sql, "\" ");
}
if (Options->enable_hstore) strcat(sql,",tags");
tables[i].columns = strdup(sql);
pgsql_exec(sql_conn, PGRES_COPY_IN, "COPY %s (%s,way) FROM STDIN", tables[i].name, tables[i].columns);
tables[i].copyMode = 1;
}
free(sql);
if (tagtransform_init(options)) {
fprintf(stderr, "Error: Failed to initialise tag processing.\n");
exit_nicely();
}
expire_tiles_init(options);
options->mid->start(options);