forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse-xml2.c
410 lines (360 loc) · 14.3 KB
/
parse-xml2.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
/*
#-----------------------------------------------------------------------------
# osm2pgsql - converts planet.osm file into PostgreSQL
# compatible output suitable to be rendered by mapnik
#-----------------------------------------------------------------------------
# Original Python implementation by Artem Pavlenko
# Re-implementation by Jon Burgess, Copyright 2006
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-----------------------------------------------------------------------------
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <libxml/xmlstring.h>
#include <libxml/xmlreader.h>
#include "osmtypes.h"
#include "sanitizer.h"
#include "reprojection.h"
#include "input.h"
#include "output.h"
#include "parse-xml2.h"
/* Parses the action="foo" tags in JOSM change files. Obvisouly not useful from osmChange files */
static actions_t ParseAction( xmlTextReaderPtr reader, struct osmdata_t *osmdata )
{
actions_t new_action;
xmlChar *action;
if( osmdata->filetype == FILETYPE_OSMCHANGE || osmdata->filetype == FILETYPE_PLANETDIFF )
return osmdata->action;
new_action = ACTION_NONE;
action = xmlTextReaderGetAttribute( reader, BAD_CAST "action" );
if( action == NULL )
new_action = ACTION_CREATE;
else if( strcmp((char *)action, "modify") == 0 )
new_action = ACTION_MODIFY;
else if( strcmp((char *)action, "delete") == 0 )
new_action = ACTION_DELETE;
else
{
fprintf( stderr, "Unknown value for action: %s\n", (char*)action );
exit_nicely();
}
return new_action;
}
static void StartElement(xmlTextReaderPtr reader, const xmlChar *name, struct osmdata_t *osmdata)
{
xmlChar *xid, *xlat, *xlon, *xk, *xv, *xrole, *xtype;
char *k;
if (osmdata->filetype == FILETYPE_NONE)
{
if (xmlStrEqual(name, BAD_CAST "osm"))
{
osmdata->filetype = FILETYPE_OSM;
osmdata->action = ACTION_CREATE;
}
else if (xmlStrEqual(name, BAD_CAST "osmChange"))
{
osmdata->filetype = FILETYPE_OSMCHANGE;
osmdata->action = ACTION_NONE;
}
else if (xmlStrEqual(name, BAD_CAST "planetdiff"))
{
osmdata->filetype = FILETYPE_PLANETDIFF;
osmdata->action = ACTION_NONE;
}
else
{
fprintf( stderr, "Unknown XML document type: %s\n", name );
exit_nicely();
}
return;
}
if (xmlStrEqual(name, BAD_CAST "node")) {
xid = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
xlon = xmlTextReaderGetAttribute(reader, BAD_CAST "lon");
xlat = xmlTextReaderGetAttribute(reader, BAD_CAST "lat");
assert(xid);
osmdata->osm_id = strtoosmid((char *)xid, NULL, 10);
osmdata->action = ParseAction( reader , osmdata);
if (osmdata->action != ACTION_DELETE) {
assert(xlon); assert(xlat);
osmdata->node_lon = strtod((char *)xlon, NULL);
osmdata->node_lat = strtod((char *)xlat, NULL);
}
if (osmdata->osm_id > osmdata->max_node)
osmdata->max_node = osmdata->osm_id;
if (osmdata->count_node == 0) {
time(&osmdata->start_node);
}
osmdata->count_node++;
if (osmdata->count_node%10000 == 0)
printStatus(osmdata);
xmlFree(xid);
xmlFree(xlon);
xmlFree(xlat);
} else if (xmlStrEqual(name, BAD_CAST "tag")) {
xk = xmlTextReaderGetAttribute(reader, BAD_CAST "k");
assert(xk);
/* 'created_by' and 'source' are common and not interesting to mapnik renderer */
if (strcmp((char *)xk, "created_by") && strcmp((char *)xk, "source")) {
char *p;
xv = xmlTextReaderGetAttribute(reader, BAD_CAST "v");
assert(xv);
k = (char *)xmlStrdup(xk);
while ((p = strchr(k, ' ')))
*p = '_';
addItem(&(osmdata->tags), k, (char *)xv, 0);
xmlFree(k);
xmlFree(xv);
}
xmlFree(xk);
} else if (xmlStrEqual(name, BAD_CAST "way")) {
xid = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
assert(xid);
osmdata->osm_id = strtoosmid((char *)xid, NULL, 10);
osmdata->action = ParseAction( reader, osmdata );
if (osmdata->osm_id > osmdata->max_way)
osmdata->max_way = osmdata->osm_id;
if (osmdata->count_way == 0) {
time(&osmdata->start_way);
}
osmdata->count_way++;
if (osmdata->count_way%1000 == 0)
printStatus(osmdata);
osmdata->nd_count = 0;
xmlFree(xid);
} else if (xmlStrEqual(name, BAD_CAST "nd")) {
xid = xmlTextReaderGetAttribute(reader, BAD_CAST "ref");
assert(xid);
osmdata->nds[osmdata->nd_count++] = strtoosmid( (char *)xid, NULL, 10 );
if( osmdata->nd_count >= osmdata->nd_max )
realloc_nodes(osmdata);
xmlFree(xid);
} else if (xmlStrEqual(name, BAD_CAST "relation")) {
xid = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
assert(xid);
osmdata->osm_id = strtoosmid((char *)xid, NULL, 10);
osmdata->action = ParseAction( reader, osmdata );
if (osmdata->osm_id > osmdata->max_rel)
osmdata->max_rel = osmdata->osm_id;
if (osmdata->count_rel == 0) {
time(&osmdata->start_rel);
}
osmdata->count_rel++;
if (osmdata->count_rel%10 == 0)
printStatus(osmdata);
osmdata->member_count = 0;
xmlFree(xid);
} else if (xmlStrEqual(name, BAD_CAST "member")) {
xrole = xmlTextReaderGetAttribute(reader, BAD_CAST "role");
assert(xrole);
xtype = xmlTextReaderGetAttribute(reader, BAD_CAST "type");
assert(xtype);
xid = xmlTextReaderGetAttribute(reader, BAD_CAST "ref");
assert(xid);
osmdata->members[osmdata->member_count].id = strtoosmid( (char *)xid, NULL, 0 );
osmdata->members[osmdata->member_count].role = strdup( (char *)xrole );
/* Currently we are only interested in 'way' members since these form polygons with holes */
if (xmlStrEqual(xtype, BAD_CAST "way"))
osmdata->members[osmdata->member_count].type = OSMTYPE_WAY;
if (xmlStrEqual(xtype, BAD_CAST "node"))
osmdata->members[osmdata->member_count].type = OSMTYPE_NODE;
if (xmlStrEqual(xtype, BAD_CAST "relation"))
osmdata->members[osmdata->member_count].type = OSMTYPE_RELATION;
osmdata->member_count++;
if( osmdata->member_count >= osmdata->member_max )
realloc_members(osmdata);
xmlFree(xid);
xmlFree(xrole);
xmlFree(xtype);
} else if (xmlStrEqual(name, BAD_CAST "add") ||
xmlStrEqual(name, BAD_CAST "create")) {
osmdata->action = ACTION_MODIFY; /* Turns all creates into modifies, makes it resiliant against inconsistant snapshots. */
} else if (xmlStrEqual(name, BAD_CAST "modify")) {
osmdata->action = ACTION_MODIFY;
} else if (xmlStrEqual(name, BAD_CAST "delete")) {
osmdata->action = ACTION_DELETE;
} else if (xmlStrEqual(name, BAD_CAST "bound")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "bounds")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "changeset")) {
/* ignore */
} else {
fprintf(stderr, "%s: Unknown element name: %s\n", __FUNCTION__, name);
}
/* Collect extra attribute information and add as tags */
if (osmdata->extra_attributes && (xmlStrEqual(name, BAD_CAST "node") ||
xmlStrEqual(name, BAD_CAST "way") ||
xmlStrEqual(name, BAD_CAST "relation")))
{
xmlChar *xtmp;
xtmp = xmlTextReaderGetAttribute(reader, BAD_CAST "user");
if (xtmp) {
addItem(&(osmdata->tags), "osm_user", (char *)xtmp, 0);
xmlFree(xtmp);
}
xtmp = xmlTextReaderGetAttribute(reader, BAD_CAST "uid");
if (xtmp) {
addItem(&(osmdata->tags), "osm_uid", (char *)xtmp, 0);
xmlFree(xtmp);
}
xtmp = xmlTextReaderGetAttribute(reader, BAD_CAST "version");
if (xtmp) {
addItem(&(osmdata->tags), "osm_version", (char *)xtmp, 0);
xmlFree(xtmp);
}
xtmp = xmlTextReaderGetAttribute(reader, BAD_CAST "timestamp");
if (xtmp) {
addItem(&(osmdata->tags), "osm_timestamp", (char *)xtmp, 0);
xmlFree(xtmp);
}
xtmp = xmlTextReaderGetAttribute(reader, BAD_CAST "changeset");
if (xtmp) {
addItem(&(osmdata->tags), "osm_changeset", (char *)xtmp, 0);
xmlFree(xtmp);
}
}
}
static void EndElement(const xmlChar *name, struct osmdata_t *osmdata)
{
if (xmlStrEqual(name, BAD_CAST "node")) {
if (node_wanted(osmdata, osmdata->node_lat, osmdata->node_lon)) {
reproject(&(osmdata->node_lat), &(osmdata->node_lon));
if( osmdata->action == ACTION_CREATE )
osmdata->out->node_add(osmdata->osm_id, osmdata->node_lat, osmdata->node_lon, &(osmdata->tags));
else if( osmdata->action == ACTION_MODIFY )
osmdata->out->node_modify(osmdata->osm_id, osmdata->node_lat, osmdata->node_lon, &(osmdata->tags));
else if( osmdata->action == ACTION_DELETE )
osmdata->out->node_delete(osmdata->osm_id);
else
{
fprintf( stderr, "Don't know action for node %" PRIdOSMID "\n", osmdata->osm_id );
exit_nicely();
}
}
resetList(&(osmdata->tags));
} else if (xmlStrEqual(name, BAD_CAST "way")) {
if( osmdata->action == ACTION_CREATE )
osmdata->out->way_add(osmdata->osm_id, osmdata->nds, osmdata->nd_count, &(osmdata->tags) );
else if( osmdata->action == ACTION_MODIFY )
osmdata->out->way_modify(osmdata->osm_id, osmdata->nds, osmdata->nd_count, &(osmdata->tags) );
else if( osmdata->action == ACTION_DELETE )
osmdata->out->way_delete(osmdata->osm_id);
else
{
fprintf( stderr, "Don't know action for way %" PRIdOSMID "\n", osmdata->osm_id );
exit_nicely();
}
resetList(&(osmdata->tags));
} else if (xmlStrEqual(name, BAD_CAST "relation")) {
if( osmdata->action == ACTION_CREATE )
osmdata->out->relation_add(osmdata->osm_id, osmdata->members, osmdata->member_count, &(osmdata->tags));
else if( osmdata->action == ACTION_MODIFY )
osmdata->out->relation_modify(osmdata->osm_id, osmdata->members, osmdata->member_count, &(osmdata->tags));
else if( osmdata->action == ACTION_DELETE )
osmdata->out->relation_delete(osmdata->osm_id);
else
{
fprintf( stderr, "Don't know action for relation %" PRIdOSMID "\n", osmdata->osm_id );
exit_nicely();
}
resetList(&(osmdata->tags));
resetMembers(osmdata);
} else if (xmlStrEqual(name, BAD_CAST "tag")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "nd")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "member")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "osm")) {
printStatus(osmdata);
osmdata->filetype = FILETYPE_NONE;
} else if (xmlStrEqual(name, BAD_CAST "osmChange")) {
printStatus(osmdata);
osmdata->filetype = FILETYPE_NONE;
} else if (xmlStrEqual(name, BAD_CAST "planetdiff")) {
printStatus(osmdata);
osmdata->filetype = FILETYPE_NONE;
} else if (xmlStrEqual(name, BAD_CAST "bound")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "bounds")) {
/* ignore */
} else if (xmlStrEqual(name, BAD_CAST "changeset")) {
/* ignore */
resetList(&(osmdata->tags)); /* We may have accumulated some tags even if we ignored the changeset */
} else if (xmlStrEqual(name, BAD_CAST "add")) {
osmdata->action = ACTION_NONE;
} else if (xmlStrEqual(name, BAD_CAST "create")) {
osmdata->action = ACTION_NONE;
} else if (xmlStrEqual(name, BAD_CAST "modify")) {
osmdata->action = ACTION_NONE;
} else if (xmlStrEqual(name, BAD_CAST "delete")) {
osmdata->action = ACTION_NONE;
} else {
fprintf(stderr, "%s: Unknown element name: %s\n", __FUNCTION__, name);
}
}
static void processNode(xmlTextReaderPtr reader, struct osmdata_t *osmdata) {
xmlChar *name;
name = xmlTextReaderName(reader);
if (name == NULL)
name = xmlStrdup(BAD_CAST "--");
switch(xmlTextReaderNodeType(reader)) {
case XML_READER_TYPE_ELEMENT:
StartElement(reader, name, osmdata);
if (xmlTextReaderIsEmptyElement(reader))
EndElement(name, osmdata); /* No end_element for self closing tags! */
break;
case XML_READER_TYPE_END_ELEMENT:
EndElement(name, osmdata);
break;
case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
/* Ignore */
break;
default:
fprintf(stderr, "Unknown node type %d\n", xmlTextReaderNodeType(reader));
break;
}
xmlFree(name);
}
int streamFileXML2(char *filename, int sanitize, struct osmdata_t *osmdata) {
xmlTextReaderPtr reader;
int ret = 0;
if (sanitize)
reader = sanitizerOpen(filename);
else
reader = inputUTF8(filename);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
processNode(reader, osmdata);
ret = xmlTextReaderRead(reader);
}
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
return ret;
}
xmlFreeTextReader(reader);
} else {
fprintf(stderr, "Unable to open %s\n", filename);
return 1;
}
return 0;
}