15 * <tag k=".." v=".." />
28 * Do something usefull with changesets and version;
32 # define stopcondition (start = ++end) < (range + max)
33 # define nextline start = end + 1
35 # define nextline free(start); start = NULL; tmp = getline(&start, &tmplen, stdin); if (tmp == -1) { goto exit; }
36 # define stopcondition 1
39 #define file_delete_nodes "delete_nodes.csv"
40 #define file_delete_ways "delete_ways.csv"
41 #define file_delete_relations "delete_relations.csv"
43 #define file_modify_nodes "modify_nodes.csv"
44 #define file_modify_ways "modify_ways.csv"
45 #define file_modify_relations "modify_relations.csv"
47 #define file_nodes "nodes.csv"
50 #define file_nodes_uint "nodes_uint.csv"
51 #define file_nodes_gis "nodes_gis.csv"
54 #define file_node_tags "node_tags.csv"
55 #define file_ways "ways.csv"
56 #define file_way_tags "way_tags.csv"
57 #define file_way_nds "way_nds.csv"
58 #define file_relations "relations.csv"
59 #define file_relation_tags "relation_tags.csv"
60 #define file_relation_member_node "relation_member_node.csv"
61 #define file_relation_member_relation "relation_member_relation.csv"
62 #define file_relation_member_way "relation_member_way.csv"
64 /* http://www.cs.utk.edu/~vose/c-stuff/bithacks.html */
65 static unsigned int reverse(unsigned int v
) {
66 unsigned int t
= v
<< 1; // t will have the reversed bits of v
70 for (i
= sizeof(v
) * 8 - 2; i
; i
--)
81 static unsigned long zcurve(char *lon
, char *lat
) {
82 unsigned int array
[2];
85 double temp
= strtod(lon
, (char **) NULL
) + 180.0; /* Longitude */
86 if (temp
< 0.0 || temp
>= 360.0) temp
= 0.0;
87 array
[0] = reverse( 5965232.0 * temp
);
89 temp
= strtod(lat
, (char **) NULL
) + 90.0; /* Latitude */
90 if (temp
< 0.0 || temp
>= 180.0) temp
= 0.0;
91 array
[1] = reverse( 11930464.0 * temp
);
93 for (j
= 0; j
< sizeof(unsigned int)*8; j
++) {
94 for (i
= 0; i
< 2; i
++) {
105 static unsigned int coordtouint(char *input
) {
106 double maxbit
= (double) 4294967296.0 / (double) 360.0;
107 double proper
= strtod(input
, NULL
) * maxbit
;
108 return (unsigned int) proper
;
111 static char * escape_string(char *instr
)
113 unsigned int i
, j
=0, need
= 0;
114 unsigned int len
= strlen(instr
);
118 if (instr
[i
]=='\\' || instr
[i
]=='\'') need
++;
121 outstr
= malloc(len
+ 1);
123 for (i
=0;i
<=strlen(instr
);i
++) {
124 if (instr
[i
]=='\\' || instr
[i
]=='\'')
126 outstr
[j
++]=instr
[i
];
132 static void parser(char *range
, unsigned long int max
) {
134 static void parser() {
136 typedef enum { NONE
= 0, CREATE
= 1, MODIFY
= 2, DELETE
= 3 } ywk_state_t
;
137 typedef enum { OSM
= 0, NODE
= 1, WAY
= 2, RELATION
= 3, TAG
= 4, ND
= 5, MEMBER
= 6 } osm_state_t
;
138 typedef enum { UNKNOWN
= 0, ID
, LAT
, LON
, USER
, UID
, TIMESTAMP
, KEY
, VALUE
, TYPE
, REF
, ROLE
, VERSION
, CHANGESET
} key_state_t
;
139 char *attr_id
= NULL
, *attr_lat
= NULL
, *attr_lon
= NULL
, *attr_user
= NULL
, *attr_uid
= NULL
, *attr_timestamp
= NULL
, *attr_changeset
= NULL
, *attr_key
= NULL
, *attr_value
= NULL
,
140 *attr_type
= NULL
, *attr_ref
= NULL
, *attr_role
= NULL
, *attr_version
= NULL
;
142 unsigned int attr_lat_uint
= 0;
143 unsigned int attr_lon_uint
= 0;
146 unsigned long int count_nodes
= 0, count_node_tags
= 0,
147 count_ways
= 0, count_way_tags
= 0, count_way_nds
= 0,
148 count_relations
= 0, count_relation_tags
= 0, count_members_node
= 0, count_members_relation
= 0, count_members_way
= 0,
149 count_delete_nodes
= 0, count_delete_ways
= 0, count_delete_relations
= 0,
150 count_modify_nodes
= 0, count_modify_ways
= 0, count_modify_relations
= 0;
152 unsigned long int sequence
= 0;
154 ywk_state_t ywk
= NONE
;
155 osm_state_t current_tag
= OSM
;
156 osm_state_t parent_tag
= OSM
;
158 char *start
= NULL
, *end
, *nodename
, *nodename_end
, *nodename_fast_end
;
167 end
= strchrnul((const char*) start
, '\n');
169 if (strncmp(start
, "<?xml", 5) != 0)
173 end
= strchrnul((const char*) start
, '\n');
175 if (strncmp(start
, "<osm", 4) != 0)
178 if (strncmp(&start
[4], "Change", 6) == 0)
193 *fd_members_relation
,
197 *fd_delete_relations
,
203 fd_nodes
= fopen(file_nodes
, "w");
204 if (fd_nodes
== NULL
) { perror("Open:"); exit(-1); }
206 fd_nodes_uint
= fopen(file_nodes_uint
, "w");
207 if (fd_nodes_uint
== NULL
) { perror("Open:"); exit(-1); }
208 fd_nodes_gis
= fopen(file_nodes_gis
, "w");
209 if (fd_nodes_gis
== NULL
) { perror("Open:"); exit(-1); }
211 fd_node_tags
= fopen(file_node_tags
, "w");
212 if (fd_node_tags
== NULL
) { perror("Open:"); exit(-1); }
213 fd_ways
= fopen(file_ways
, "w");
214 if (fd_ways
== NULL
) { perror("Open:"); exit(-1); }
215 fd_way_tags
= fopen(file_way_tags
, "w");
216 if (fd_way_tags
== NULL
) { perror("Open:"); exit(-1); }
217 fd_way_nds
= fopen(file_way_nds
, "w");
218 if (fd_way_nds
== NULL
) { perror("Open:"); exit(-1); }
219 fd_relations
= fopen(file_relations
, "w");
220 if (fd_relations
== NULL
) { perror("Open:"); exit(-1); }
221 fd_relation_tags
= fopen(file_relation_tags
, "w");
222 if (fd_relation_tags
== NULL
) { perror("Open:"); exit(-1); }
223 fd_members_node
= fopen(file_relation_member_node
, "w");
224 if (fd_members_node
== NULL
) { perror("Open:"); exit(-1); }
225 fd_members_relation
= fopen(file_relation_member_relation
, "w");
226 if (fd_members_relation
== NULL
) { perror("Open:"); exit(-1); }
227 fd_members_way
= fopen(file_relation_member_way
, "w");
228 if (fd_members_way
== NULL
) { perror("Open:"); exit(-1); }
231 fd_delete_nodes
= fopen(file_delete_nodes
, "w");
232 if (fd_delete_nodes
== NULL
) { perror("Open:"); exit(-1); }
233 fd_delete_ways
= fopen(file_delete_ways
, "w");
234 if (fd_delete_ways
== NULL
) { perror("Open:"); exit(-1); }
235 fd_delete_relations
= fopen(file_delete_relations
, "w");
236 if (fd_delete_relations
== NULL
) { perror("Open:"); exit(-1); }
238 fd_modify_nodes
= fopen(file_modify_nodes
, "w");
239 if (fd_modify_nodes
== NULL
) { perror("Open:"); exit(-1); }
240 fd_modify_ways
= fopen(file_modify_ways
, "w");
241 if (fd_modify_ways
== NULL
) { perror("Open:"); exit(-1); }
242 fd_modify_relations
= fopen(file_modify_relations
, "w");
243 if (fd_modify_relations
== NULL
) { perror("Open:"); exit(-1); }
249 end
= strchrnul((const char*) start
, '\n');
251 nodename
= strchrnul(start
, '<') + 1;
252 nodename_fast_end
= strchrnul(nodename
, '>');
253 nodename_end
= strchrnul(nodename
, ' ');
255 if (nodename_fast_end
< nodename_end
) nodename_end
= nodename_fast_end
;
257 if (nodename
[0] == '/') {
261 free(attr_timestamp
);
262 free(attr_changeset
);
267 attr_id
= attr_lat
= attr_lon
= attr_user
= attr_uid
= attr_timestamp
= attr_changeset
= attr_version
= NULL
;
275 switch (nodename_end
- nodename
) {
280 switch (nodename
[0]) {
291 fprintf(stderr
, "--> %c%c", nodename
[0], nodename
[1]);
303 switch (nodename
[2]) {
320 current_tag
= MEMBER
;
323 fprintf(stderr
, "--> %c%c", nodename
[0], nodename
[1]);
328 current_tag
= RELATION
;
331 fprintf(stderr
, "--> %c%c", nodename
[0], nodename
[1]);
335 char *key
, *key_end
, *value_end
;
336 key
= nodename_end
+ 1;
340 key_state_t current_key
= UNKNOWN
;
341 key_end
= strchrnul(key
, '=');
343 if (key_end
== NULL
|| key_end
>= end
)
346 switch (key_end
- key
) {
356 current_key
= UNKNOWN
;
378 current_key
= UNKNOWN
;
379 fprintf(stderr
, "--> %c%c\n", key
[0], key
[1]);
395 current_key
= UNKNOWN
;
396 fprintf(stderr
, "--> %c%c\n", key
[0], key
[1]);
401 current_key
= VERSION
;
407 current_key
= TIMESTAMP
;
410 current_key
= CHANGESET
;
413 current_key
= UNKNOWN
;
414 fprintf(stderr
, "--> %c%c\n", key
[0], key
[1]);
419 char *thingie
= strndup(key
, (key_end
- key
));
420 current_key
= UNKNOWN
;
422 fprintf(stderr
, "UNKNOWN ATTR %s-> %c%c\n", thingie
, key
[0], key
[1]);
429 value_end
= strchr(value_end
, '"');
434 switch (current_key
) {
436 if (attr_id
) free(attr_id
);
437 attr_id
= strndup(value
, (value_end
- value
));
441 if (attr_lat
) free(attr_lat
);
442 attr_lat
= strndup(value
, (value_end
- value
));
443 attr_lat_uint
= coordtouint(attr_lat
);
447 if (attr_lon
) free(attr_lon
);
448 attr_lon
= strndup(value
, (value_end
- value
));
449 attr_lon_uint
= coordtouint(attr_lon
);
453 if (attr_timestamp
) free(attr_timestamp
);
454 // attr_timestamp = strndup(value, (value_end - value));
455 attr_timestamp
= strndup(value
, (value_end
- (value
+ 1))); /* another stupid fix */
456 // attr_timestamp[10] = ' '; /* Stupid timestamp fix */
460 if (attr_changeset
) free(attr_changeset
);
461 attr_changeset
= strndup(value
, (value_end
- value
));
466 if (attr_user
) free(attr_user
);
467 attr_user
= strndup(value
, (value_end
- value
));
468 tmp
= escape_string(attr_user
);
475 if (attr_uid
) free(attr_uid
);
476 attr_uid
= strndup(value
, (value_end
- value
));
481 if (attr_version
) free(attr_version
);
482 attr_version
= strndup(value
, (value_end
- value
));
488 if (attr_key
) free(attr_key
);
489 attr_key
= strndup(value
, (value_end
- value
));
490 tmp
= escape_string(attr_key
);
498 if (attr_value
) free(attr_value
);
499 attr_value
= strndup(value
, (value_end
- value
));
500 tmp
= escape_string(attr_value
);
507 if (attr_type
) free(attr_type
);
508 attr_type
= strndup(value
, (value_end
- value
));
512 if (attr_ref
) free(attr_ref
);
513 attr_ref
= strndup(value
, (value_end
- value
));
518 if (attr_role
) free(attr_role
);
519 attr_role
= strndup(value
, (value_end
- value
));
520 tmp
= escape_string(attr_role
);
527 fprintf(stderr
, "--> %c%c\n", value
[0], value
[1]);
533 if (ywk
== DELETE
|| ywk
== MODIFY
) {
535 switch (current_tag
) {
537 if (ywk
== DELETE
) { this = fd_delete_nodes
; count_delete_nodes
++; } else { this = fd_modify_nodes
; count_modify_nodes
++; }
540 if (ywk
== DELETE
) { this = fd_delete_ways
; count_delete_ways
++; } else { this = fd_modify_ways
; count_modify_ways
++; }
543 if (ywk
== DELETE
) { this = fd_delete_relations
; count_delete_relations
++; } else { this = fd_modify_relations
; count_modify_relations
++; }
547 fputs(attr_id
, this);
553 switch (current_tag
) {
555 fprintf(fd_nodes
, "%s, %s, %s, %s, '%s', %lu\n", attr_id
, attr_lon
, attr_lat
, (attr_uid
!= NULL
? attr_uid
: "0"), attr_timestamp
, zcurve(attr_lon
, attr_lat
));
557 fprintf(fd_nodes_uint
, "%s, %d, %d, %s, '%s'\n", attr_id
, attr_lon_uint
, attr_lat_uint
, (attr_uid
!= NULL
? attr_uid
: "0"), attr_timestamp
);
558 fprintf(fd_nodes_gis
, "%s, 'POINT( %s %s )', '%s', '%s'\n", attr_id
, attr_lat
, attr_lon
, (attr_uid
!= NULL
? attr_uid
: "0"), attr_timestamp
);
563 switch (parent_tag
) {
565 fprintf(fd_node_tags
, "%s, '%s', '%s'\n", attr_id
, attr_key
, attr_value
);
569 fprintf(fd_way_tags
, "%s, '%s', '%s'\n", attr_id
, attr_key
, attr_value
);
573 fprintf(fd_relation_tags
, "%s, '%s', '%s'\n", attr_id
, attr_key
, attr_value
);
574 count_relation_tags
++;
582 fprintf(fd_ways
, "%s, %s, '%s'\n", attr_id
, (attr_uid
!= NULL
? attr_uid
: "0"), attr_timestamp
);
584 // fprintf(fd_way_tags, "%s, '%s', '%s'\n", attr_id, "type", "way");
588 fprintf(fd_relations
, "%s, %s, '%s'\n", attr_id
, (attr_uid
!= NULL
? attr_uid
: "0"), attr_timestamp
);
592 if (strcmp(attr_type
, "node") == 0) {
593 fprintf(fd_members_node
, "%s, %lu, %s, '%s'\n", attr_id
, sequence
, attr_ref
, attr_role
);
594 count_members_node
++;
595 } else if (strcmp(attr_type
, "way") == 0) {
596 fprintf(fd_members_way
, "%s, %lu, %s, '%s'\n", attr_id
, sequence
, attr_ref
, attr_role
);
598 } else if (strcmp(attr_type
, "relation") == 0) {
599 fprintf(fd_members_relation
, "%s, %lu, %s, '%s'\n", attr_id
, sequence
, attr_ref
, attr_role
);
600 count_members_relation
++;
605 fprintf(fd_way_nds
, "%s, %lu, %s\n", attr_id
, sequence
, attr_ref
);
614 if (end
[-2] == '/') {
615 switch (current_tag
) {
628 free(attr_timestamp
);
629 free(attr_changeset
);
634 attr_id
= attr_user
= attr_uid
= attr_timestamp
= attr_changeset
= attr_version
= NULL
;
659 } else if (current_tag
== NODE
|| current_tag
== WAY
|| current_tag
== RELATION
) {
660 parent_tag
= current_tag
;
665 } while (stopcondition
);
671 free(attr_timestamp
);
672 free(attr_changeset
);
682 fclose(fd_nodes_uint
);
683 fclose(fd_nodes_gis
);
685 fclose(fd_node_tags
);
689 fclose(fd_relations
);
690 fclose(fd_relation_tags
);
691 fclose(fd_members_node
);
692 fclose(fd_members_relation
);
693 fclose(fd_members_way
);
695 fclose(fd_delete_nodes
);
696 fclose(fd_delete_ways
);
697 fclose(fd_delete_relations
);
698 fclose(fd_modify_nodes
);
699 fclose(fd_modify_ways
);
700 fclose(fd_modify_relations
);
704 char *current
= get_current_dir_name();
706 puts("START TRANSACTION;");
709 puts("CREATE TABLE nodes_legacy (id integer, long double, lat double, uid integer, timestamp timestamptz, zcurve bigint);");
711 puts("CREATE TABLE nodes_legacy_uint (id integer, long integer, lat integer, uid integer, timestamp timestamptz);");
712 puts("CREATE TABLE nodes_legacy_gis (id integer, poi point, uid integer, timestamp timestamptz);");
714 puts("CREATE TABLE node_tags (node integer, k varchar(255), v varchar(1024));");
715 puts("CREATE TABLE ways (id integer,uid integer, timestamp timestamptz);");
716 puts("CREATE TABLE way_tags (way integer, k varchar(255), v varchar(1024));");
717 puts("CREATE TABLE way_nds (way integer, idx integer, to_node integer);");
718 puts("CREATE TABLE relations(id integer, uid integer, timestamp timestamptz);");
719 puts("CREATE TABLE relation_members_node (relation integer, idx integer, to_node integer, role varchar(255));");
720 puts("CREATE TABLE relation_members_relation (relation integer, idx integer, to_relation integer, role varchar(255));");
721 puts("CREATE TABLE relation_members_way (relation integer, idx integer, to_way integer, role varchar(255));");
722 puts("CREATE TABLE relation_tags (relation integer, k varchar(255), v varchar(1024));");
724 if (count_delete_nodes
> 0 || count_delete_ways
> 0 || count_delete_relations
> 0 ||
725 count_modify_nodes
> 0 || count_modify_ways
> 0 || count_modify_relations
> 0) {
726 if (count_delete_relations
> 0 || count_modify_relations
> 0) {
727 puts("CREATE TEMPORARY TABLE delete_relations (id integer);");
729 if (count_delete_ways
> 0 || count_modify_ways
> 0) {
730 puts("CREATE TEMPORARY TABLE delete_ways (id integer);");
732 if (count_delete_nodes
> 0 || count_modify_nodes
> 0) {
733 puts("CREATE TEMPORARY TABLE delete_nodes (id integer);");
736 if (count_delete_nodes
> 0 || count_delete_ways
> 0 || count_delete_relations
> 0) {
737 if (count_delete_relations
> 0) {
738 printf("COPY %lu RECORDS INTO delete_relations FROM '%s/" file_delete_relations
"' USING DELIMITERS ',', '\\n', '''';\n", count_delete_relations
, current
);
741 if (count_delete_ways
> 0) {
742 printf("COPY %lu RECORDS INTO delete_ways FROM '%s/" file_delete_ways
"' USING DELIMITERS ',', '\\n', '''';\n", count_delete_ways
, current
);
745 if (count_delete_nodes
> 0) {
746 printf("COPY %lu RECORDS INTO delete_nodes FROM '%s/" file_delete_nodes
"' USING DELIMITERS ',', '\\n', '''';\n", count_delete_nodes
, current
);
749 if (count_delete_relations
> 0) {
750 puts("DELETE FROM relation_members_relation WHERE to_relation IN (SELECT id FROM delete_relations);");
753 if (count_delete_ways
> 0) {
754 puts("DELETE FROM relation_members_way WHERE to_way IN (SELECT id FROM delete_ways);");
757 if (count_delete_nodes
> 0) {
758 puts("DELETE FROM relation_members_node WHERE to_node IN (SELECT id FROM delete_nodes);\n"
759 "DELETE FROM way_nds WHERE to_node IN (SELECT id FROM delete_nodes);");
763 if (count_modify_relations
> 0) {
764 printf("COPY %lu RECORDS INTO delete_relations FROM '%s/" file_modify_relations
"' USING DELIMITERS ',', '\\n', '''';\n", count_modify_relations
, current
);
767 if (count_modify_ways
> 0) {
768 printf("COPY %lu RECORDS INTO delete_ways FROM '%s/" file_modify_ways
"' USING DELIMITERS ',', '\\n', '''';\n", count_modify_ways
, current
);
771 if (count_modify_nodes
> 0) {
772 printf("COPY %lu RECORDS INTO delete_nodes FROM '%s/" file_modify_nodes
"' USING DELIMITERS ',', '\\n', '''';\n", count_modify_nodes
, current
);
775 if (count_delete_relations
> 0 || count_modify_relations
> 0) {
776 puts("DELETE FROM relation_members_relation WHERE relation IN (SELECT id FROM delete_relations);\n"
777 "DELETE FROM relation_members_way WHERE relation IN (SELECT id FROM delete_relations);\n"
778 "DELETE FROM relation_members_node WHERE relation IN (SELECT id FROM delete_relations);\n"
779 "DELETE FROM relation_tags WHERE relation IN (SELECT id FROM delete_relations);\n"
780 "DELETE FROM relations WHERE id IN (SELECT id FROM delete_relations);\n"
781 "DROP TABLE delete_relations;\n");
784 if (count_delete_ways
> 0 || count_modify_ways
> 0) {
785 puts("DELETE FROM way_nds WHERE way IN (SELECT id FROM delete_ways);\n"
786 "DELETE FROM way_tags WHERE way IN (SELECT id FROM delete_ways);\n"
787 "DELETE FROM ways WHERE id IN (SELECT id FROM delete_ways);\n"
788 "DROP TABLE delete_ways;\n");
791 if (count_delete_nodes
> 0 || count_modify_nodes
> 0) {
792 puts("DELETE FROM node_tags WHERE node IN (SELECT id FROM delete_nodes);\n"
793 "DELETE FROM nodes_legacy WHERE id IN (SELECT id FROM delete_nodes);\n"
794 "DROP TABLE delete_nodes;\n");
799 printf("COPY %lu RECORDS INTO nodes_legacy from '%s/" file_nodes
"' USING DELIMITERS ',', '\\n', '''';\n", count_nodes
, current
);
801 printf("COPY %lu RECORDS INTO nodes_legacy_uint from '%s/" file_nodes_uint
"' USING DELIMITERS ',', '\\n', '''';\n", count_nodes
, current
);
802 printf("COPY %lu RECORDS INTO nodes_legacy_gis from '%s/" file_nodes_gis
"' USING DELIMITERS ',', '\\n', '''';\n", count_nodes
, current
);
804 printf("COPY %lu RECORDS INTO node_tags from '%s/" file_node_tags
"' USING DELIMITERS ',', '\\n', '''';\n", count_node_tags
, current
);
805 printf("COPY %lu RECORDS INTO ways from '%s/" file_ways
"' USING DELIMITERS ',', '\\n', '''';\n", count_ways
, current
);
806 printf("COPY %lu RECORDS INTO way_tags from '%s/" file_way_tags
"' USING DELIMITERS ',', '\\n', '''';\n", count_way_tags
, current
);
807 printf("COPY %lu RECORDS INTO way_nds from '%s/" file_way_nds
"' USING DELIMITERS ',', '\\n', '''';\n", count_way_nds
, current
);
808 printf("COPY %lu RECORDS INTO relations from '%s/" file_relations
"' USING DELIMITERS ',', '\\n', '''';\n", count_relations
, current
);
809 printf("COPY %lu RECORDS INTO relation_tags from '%s/" file_relation_tags
"' USING DELIMITERS ',', '\\n', '''';\n", count_relation_tags
, current
);
810 printf("COPY %lu RECORDS INTO relation_members_node from '%s/" file_relation_member_node
"' USING DELIMITERS ',', '\\n', '''';\n", count_members_node
, current
);
811 printf("COPY %lu RECORDS INTO relation_members_relation from '%s/" file_relation_member_relation
"' USING DELIMITERS ',', '\\n', '''';\n", count_members_relation
, current
);
812 printf("COPY %lu RECORDS INTO relation_members_way from '%s/" file_relation_member_way
"' USING DELIMITERS ',', '\\n', '''';\n", count_members_way
, current
);
819 puts("START TRANSACTION;");
821 puts("CREATE SEQUENCE s_nodes AS INTEGER;");
822 puts("ALTER SEQUENCE s_nodes RESTART WITH (SELECT MAX(id) FROM nodes_legacy);");
823 puts("ALTER TABLE nodes_legacy ALTER COLUMN id SET NOT NULL;");
824 puts("ALTER TABLE nodes_legacy ALTER COLUMN id SET DEFAULT NEXT VALUE FOR \"sys\".\"s_nodes\";");
825 puts("ALTER TABLE nodes_legacy ADD CONSTRAINT pk_nodes_id PRIMARY KEY (id);");
827 puts("CREATE SEQUENCE s_ways AS INTEGER;");
828 puts("ALTER SEQUENCE s_ways RESTART WITH (SELECT MAX(id) FROM ways);");
829 puts("ALTER TABLE ways ALTER COLUMN id SET NOT NULL;");
830 puts("ALTER TABLE ways ALTER COLUMN id SET DEFAULT NEXT VALUE FOR \"sys\".\"s_ways\";");
831 puts("ALTER TABLE ways ADD CONSTRAINT pk_ways_id PRIMARY KEY (id);");
833 puts("CREATE SEQUENCE s_relations AS INTEGER;");
834 puts("ALTER SEQUENCE s_relations RESTART WITH (SELECT MAX(id) FROM relations);");
835 puts("ALTER TABLE relations ALTER COLUMN id SET NOT NULL;");
836 puts("ALTER TABLE relations ALTER COLUMN id SET DEFAULT NEXT VALUE FOR \"sys\".\"s_relations\";");
837 puts("ALTER TABLE relations ADD CONSTRAINT pk_relations_id PRIMARY KEY (id);");
839 puts("ALTER TABLE relation_members_node ADD CONSTRAINT pk_relation_members_node PRIMARY KEY (relation, idx);");
840 puts("ALTER TABLE relation_members_way ADD CONSTRAINT pk_relation_members_way PRIMARY KEY (relation,idx);");
841 puts("ALTER TABLE relation_members_relation ADD CONSTRAINT pk_relation_members_relation PRIMARY KEY (relation,idx);");
846 puts("START TRANSACTION;");
848 puts("ALTER TABLE node_tags ADD CONSTRAINT fk_node_tags_node FOREIGN KEY (node) REFERENCES nodes_legacy (id);");
849 puts("ALTER TABLE node_tags ADD CONSTRAINT pk_node_tags UNIQUE (node, k, v);");
851 puts("ALTER TABLE way_tags ADD CONSTRAINT fk_way_tags_way FOREIGN KEY (way) REFERENCES ways (id);");
852 puts("ALTER TABLE way_tags ADD CONSTRAINT pk_way_tags UNIQUE (way, k, v);");
854 puts("ALTER TABLE way_nds ADD CONSTRAINT pk_way_nds PRIMARY KEY (way, idx);");
855 puts("ALTER TABLE way_nds ADD CONSTRAINT fk_way_nds_way FOREIGN KEY (way) REFERENCES ways (id);");
856 puts("ALTER TABLE way_nds ADD CONSTRAINT fk_way_nds_node FOREIGN KEY (to_node) REFERENCES nodes_legacy (id);");
858 puts("ALTER TABLE relation_tags ADD CONSTRAINT fk_relation_tags FOREIGN KEY (relation) REFERENCES relations (id);");
859 puts("ALTER TABLE relation_tags ADD CONSTRAINT pk_relation_tags UNIQUE (relation, k, v);");
861 puts("ALTER TABLE relation_members_node ADD CONSTRAINT fk_relation_members_node FOREIGN KEY (relation) REFERENCES relations (id);");
862 puts("ALTER TABLE relation_members_node ADD CONSTRAINT fk_relation_members_tonode FOREIGN KEY (to_node) REFERENCES nodes_legacy (id);");
864 puts("ALTER TABLE relation_members_way ADD CONSTRAINT fk_relation_members_way FOREIGN KEY (relation) REFERENCES relations (id);");
865 puts("ALTER TABLE relation_members_way ADD CONSTRAINT fk_relation_members_toway FOREIGN KEY (to_way) REFERENCES ways (id);");
867 puts("ALTER TABLE relation_members_relation ADD CONSTRAINT fk_relation_members_relation FOREIGN KEY (relation) REFERENCES relations (id);");
868 puts("ALTER TABLE relation_members_relation ADD CONSTRAINT fk_relation_members_torelation FOREIGN KEY (to_relation) REFERENCES relations (id);");
875 int main(int argc
, char *argv
[]) {
883 fprintf(stderr
, "Analysing %s...\n", argv
[1]);
885 fd
= open(argv
[1], O_RDONLY
);
890 if (fstat (fd
, &statbuf
) == -1) { perror("fstat:"); exit(-1); }
892 if (statbuf
.st_size
> 0) {
894 range
= mmap(NULL
, statbuf
.st_size
, PROT_READ
, MAP_SHARED
, fd
, (off_t
) 0);
895 if (range
== MAP_FAILED
) { perror("Mmap:"); puts("(did you compile PAE in the kernel?)"); exit(-1); }
896 parser(range
, statbuf
.st_size
/ sizeof(char));
897 munmap(range
, statbuf
.st_size
);