Contents of /trunk/src/osm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 171 - (hide annotations)
Tue Apr 28 10:29:52 2009 UTC (15 years ago) by harbaum
File MIME type: text/plain
File size: 58471 byte(s)
Removed api 0.5 support
1 harbaum 1 /*
2     * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3     *
4     * This file is part of OSM2Go.
5     *
6     * OSM2Go is free software: you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * OSM2Go is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>.
18     */
19    
20     #include <stdio.h>
21     #include <stdlib.h>
22     #include <string.h>
23     #include <math.h>
24    
25     #define __USE_XOPEN
26     #include <time.h>
27    
28     #include <libxml/parser.h>
29     #include <libxml/tree.h>
30    
31     #include "appdata.h"
32 achadwick 28 #include "banner.h"
33 harbaum 1
34     #ifndef LIBXML_TREE_ENABLED
35     #error "Tree not enabled in libxml"
36     #endif
37    
38 harbaum 9 /* ------------------------- bounds handling --------------------- */
39 harbaum 1
40     static void osm_bounds_free(bounds_t *bounds) {
41     free(bounds);
42     }
43    
44     static void osm_bounds_dump(bounds_t *bounds) {
45     printf("\nBounds: %f->%f %f->%f\n",
46     bounds->ll_min.lat, bounds->ll_max.lat,
47     bounds->ll_min.lon, bounds->ll_max.lon);
48     }
49    
50     /* ------------------------- user handling --------------------- */
51    
52     void osm_users_free(user_t *user) {
53     while(user) {
54     user_t *next = user->next;
55    
56     if(user->name) g_free(user->name);
57     g_free(user);
58    
59     user = next;
60     }
61     }
62    
63     void osm_users_dump(user_t *user) {
64     printf("\nUser list:\n");
65     while(user) {
66     printf("Name: %s\n", user->name);
67     user = user->next;
68     }
69     }
70    
71     static user_t *osm_user(osm_t *osm, char *name) {
72 harbaum 39 if(!name) return NULL;
73 harbaum 1
74     /* search through user list */
75     user_t **user = &osm->user;
76     while(*user && strcasecmp((*user)->name, name) < 0)
77     user = &(*user)->next;
78    
79     /* end of list or inexact match? create new user entry! */
80     if(!*user || strcasecmp((*user)->name, name)) {
81     user_t *new = g_new0(user_t, 1);
82     new->name = g_strdup(name);
83     new->next = *user;
84     *user = new;
85    
86     return new;
87     }
88    
89     return *user;
90     }
91    
92     static
93     time_t convert_iso8601(const char *str) {
94 harbaum 39 if(!str) return 0;
95    
96 harbaum 1 tzset();
97    
98     struct tm ctime;
99     memset(&ctime, 0, sizeof(struct tm));
100     strptime(str, "%FT%T%z", &ctime);
101    
102     return mktime(&ctime) - timezone;
103     }
104    
105     /* -------------------- tag handling ----------------------- */
106    
107     void osm_tag_free(tag_t *tag) {
108     if(tag->key) g_free(tag->key);
109     if(tag->value) g_free(tag->value);
110     g_free(tag);
111     }
112    
113     void osm_tags_free(tag_t *tag) {
114     while(tag) {
115     tag_t *next = tag->next;
116     osm_tag_free(tag);
117     tag = next;
118     }
119     }
120    
121     static void osm_tags_dump(tag_t *tag) {
122     while(tag) {
123     printf("Key/Val: %s/%s\n", tag->key, tag->value);
124     tag = tag->next;
125     }
126     }
127    
128     tag_t *osm_parse_osm_tag(osm_t *osm, xmlDocPtr doc, xmlNode *a_node) {
129     xmlNode *cur_node = NULL;
130    
131     /* allocate a new tag structure */
132     tag_t *tag = g_new0(tag_t, 1);
133    
134     char *prop;
135     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"k"))) {
136     if(strlen(prop) > 0) tag->key = g_strdup(prop);
137     xmlFree(prop);
138     }
139    
140     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"v"))) {
141     if(strlen(prop) > 0) tag->value = g_strdup(prop);
142     xmlFree(prop);
143     }
144    
145     if(!tag->key || !tag->value) {
146     printf("incomplete tag key/value %s/%s\n", tag->key, tag->value);
147     osm_tags_free(tag);
148     return NULL;
149     }
150    
151     for (cur_node = a_node->children; cur_node; cur_node = cur_node->next)
152     if (cur_node->type == XML_ELEMENT_NODE)
153     printf("found unhandled osm/node/tag/%s\n", cur_node->name);
154    
155     return tag;
156     }
157    
158     gboolean osm_is_creator_tag(tag_t *tag) {
159     if(strcasecmp(tag->key, "created_by") == 0) return TRUE;
160     if(strcasecmp(tag->key, "source") == 0) return TRUE;
161    
162     return FALSE;
163     }
164    
165     gboolean osm_tag_key_and_value_present(tag_t *haystack, tag_t *tag) {
166     while(haystack) {
167     if((strcasecmp(haystack->key, tag->key) == 0) &&
168     (strcasecmp(haystack->value, tag->value) == 0))
169     return TRUE;
170    
171     haystack = haystack->next;
172     }
173     return FALSE;
174     }
175    
176     gboolean osm_tag_key_other_value_present(tag_t *haystack, tag_t *tag) {
177     while(haystack) {
178     if((strcasecmp(haystack->key, tag->key) == 0) &&
179     (strcasecmp(haystack->value, tag->value) != 0))
180     return TRUE;
181    
182     haystack = haystack->next;
183     }
184     return FALSE;
185     }
186    
187     gboolean osm_way_ends_with_node(way_t *way, node_t *node) {
188     /* and deleted way may even not contain any nodes at all */
189     /* so ignore it */
190     if(way->flags & OSM_FLAG_DELETED)
191     return FALSE;
192    
193     /* any valid way must have at least two nodes */
194     g_assert(way->node_chain && way->node_chain->next);
195    
196     node_chain_t *chain = way->node_chain;
197     if(chain->node == node) return TRUE;
198    
199     while(chain->next) chain = chain->next;
200     if(chain->node == node) return TRUE;
201    
202     return FALSE;
203     }
204    
205     /* ------------------- node handling ------------------- */
206    
207     void osm_node_free(icon_t **icon, node_t *node) {
208     if(node->icon_buf)
209     icon_free(icon, node->icon_buf);
210    
211     /* there must not be anything left in this chain */
212     g_assert(!node->map_item_chain);
213    
214     osm_tags_free(node->tag);
215     g_free(node);
216     }
217    
218     static void osm_nodes_free(icon_t **icon, node_t *node) {
219     while(node) {
220     node_t *next = node->next;
221     osm_node_free(icon, node);
222     node = next;
223     }
224     }
225    
226     void osm_node_dump(node_t *node) {
227     char buf[64];
228     struct tm tm;
229    
230 harbaum 161 printf("Id: "ITEM_ID_FORMAT"\n", node->id);
231 harbaum 1 printf("User: %s\n", node->user?node->user->name:"<unspecified>");
232     printf("Visible: %s\n", node->visible?"yes":"no");
233    
234     localtime_r(&node->time, &tm);
235     strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", &tm);
236     printf("Time: %s\n", buf);
237     osm_tags_dump(node->tag);
238     }
239    
240     void osm_nodes_dump(node_t *node) {
241     printf("\nNode list:\n");
242     while(node) {
243     osm_node_dump(node);
244     printf("\n");
245     node = node->next;
246     }
247     }
248    
249     /* ------------------- way handling ------------------- */
250    
251     void osm_node_chain_free(node_chain_t *node_chain) {
252     while(node_chain) {
253     g_assert(node_chain->node->ways);
254    
255     node_chain_t *next = node_chain->next;
256     node_chain->node->ways--;
257     g_free(node_chain);
258     node_chain = next;
259     }
260     }
261    
262     void osm_way_free(way_t *way) {
263 harbaum 161 // printf("freeing way #" ITEM_ID_FORMAT "\n", way->id);
264 harbaum 1
265     osm_node_chain_free(way->node_chain);
266     osm_tags_free(way->tag);
267    
268     /* there must not be anything left in this chain */
269     g_assert(!way->map_item_chain);
270    
271     g_free(way);
272     }
273    
274     static void osm_ways_free(way_t *way) {
275     while(way) {
276     way_t *next = way->next;
277     osm_way_free(way);
278     way = next;
279     }
280     }
281    
282     void osm_way_append_node(way_t *way, node_t *node) {
283     node_chain_t **node_chain = &way->node_chain;
284    
285     while(*node_chain)
286     node_chain = &((*node_chain)->next);
287    
288     *node_chain = g_new0(node_chain_t, 1);
289     (*node_chain)->node = node;
290    
291     node->ways++;
292     }
293    
294     int osm_node_chain_length(node_chain_t *node_chain) {
295     int cnt = 0;
296     while(node_chain) {
297     cnt++;
298     node_chain = node_chain->next;
299     }
300    
301     return cnt;
302     }
303    
304     void osm_way_dump(way_t *way) {
305     char buf[64];
306     struct tm tm;
307    
308 harbaum 161 printf("Id: "ITEM_ID_FORMAT"\n", way->id);
309 harbaum 1 printf("User: %s\n", way->user?way->user->name:"<unspecified>");
310     printf("Visible: %s\n", way->visible?"yes":"no");
311     node_chain_t *node_chain = way->node_chain;
312     while(node_chain) {
313 harbaum 161 printf(" Node: "ITEM_ID_FORMAT"\n", node_chain->node->id);
314 harbaum 1 node_chain = node_chain->next;
315     }
316    
317     localtime_r(&way->time, &tm);
318     strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", &tm);
319     printf("Time: %s\n", buf);
320     osm_tags_dump(way->tag);
321     }
322    
323     void osm_ways_dump(way_t *way) {
324     printf("\nWay list:\n");
325     while(way) {
326     osm_way_dump(way);
327     printf("\n");
328     way = way->next;
329     }
330     }
331    
332     node_chain_t *osm_parse_osm_way_nd(osm_t *osm,
333     xmlDocPtr doc, xmlNode *a_node) {
334     char *prop;
335    
336     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"ref"))) {
337     item_id_t id = strtoul(prop, NULL, 10);
338     node_chain_t *node_chain = g_new0(node_chain_t, 1);
339    
340     /* search matching node */
341 harbaum 42 node_chain->node = osm_get_node_by_id(osm, id);
342 harbaum 161 if(!node_chain->node) printf("Node id " ITEM_ID_FORMAT " not found\n", id);
343 harbaum 42 else node_chain->node->ways++;
344 harbaum 1
345     xmlFree(prop);
346    
347     return node_chain;
348     }
349    
350     return NULL;
351     }
352    
353     /* ------------------- relation handling ------------------- */
354    
355     void osm_member_free(member_t *member) {
356     if(member->role) g_free(member->role);
357     g_free(member);
358     }
359    
360     void osm_members_free(member_t *member) {
361     while(member) {
362     member_t *next = member->next;
363     osm_member_free(member);
364     member = next;
365     }
366     }
367    
368 harbaum 73 void osm_relation_free(relation_t *relation) {
369     osm_tags_free(relation->tag);
370     osm_members_free(relation->member);
371    
372     g_free(relation);
373     }
374    
375 harbaum 1 static void osm_relations_free(relation_t *relation) {
376     while(relation) {
377     relation_t *next = relation->next;
378 harbaum 73 osm_relation_free(relation);
379 harbaum 1 relation = next;
380     }
381     }
382    
383     void osm_relations_dump(relation_t *relation) {
384     printf("\nRelation list:\n");
385     while(relation) {
386     char buf[64];
387     struct tm tm;
388    
389 harbaum 161 printf("Id: "ITEM_ID_FORMAT"\n", relation->id);
390 harbaum 1 printf("User: %s\n",
391     relation->user?relation->user->name:"<unspecified>");
392     printf("Visible: %s\n", relation->visible?"yes":"no");
393    
394     member_t *member = relation->member;
395     while(member) {
396 harbaum 155 switch(member->object.type) {
397 harbaum 1 case ILLEGAL:
398     case NODE_ID:
399     case WAY_ID:
400     case RELATION_ID:
401     break;
402    
403     case NODE:
404 harbaum 155 if(member->object.node)
405 harbaum 161 printf(" Member: Node, id = " ITEM_ID_FORMAT ", role = %s\n",
406 harbaum 155 member->object.node->id, member->role);
407 harbaum 1 break;
408    
409     case WAY:
410 harbaum 155 if(member->object.way)
411 harbaum 161 printf(" Member: Way, id = " ITEM_ID_FORMAT ", role = %s\n",
412     member->object.way->id, member->role);
413 harbaum 1 break;
414 harbaum 161
415 harbaum 1 case RELATION:
416 harbaum 155 if(member->object.relation)
417 harbaum 161 printf(" Member: Relation, id = " ITEM_ID_FORMAT ", role = %s\n",
418     member->object.relation->id, member->role);
419 harbaum 1 break;
420     }
421    
422     member = member->next;
423     }
424    
425     localtime_r(&relation->time, &tm);
426     strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", &tm);
427     printf("Time: %s\n", buf);
428     osm_tags_dump(relation->tag);
429    
430     printf("\n");
431     relation = relation->next;
432     }
433     }
434    
435     member_t *osm_parse_osm_relation_member(osm_t *osm,
436     xmlDocPtr doc, xmlNode *a_node) {
437     char *prop;
438     member_t *member = g_new0(member_t, 1);
439 harbaum 155 member->object.type = ILLEGAL;
440 harbaum 1
441     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"type"))) {
442 harbaum 155 if(strcasecmp(prop, "way") == 0) member->object.type = WAY;
443     else if(strcasecmp(prop, "node") == 0) member->object.type = NODE;
444     else if(strcasecmp(prop, "relation") == 0) member->object.type = RELATION;
445 harbaum 1 xmlFree(prop);
446     }
447    
448     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"ref"))) {
449     item_id_t id = strtoul(prop, NULL, 10);
450    
451 harbaum 155 switch(member->object.type) {
452 harbaum 1 case ILLEGAL:
453     printf("Unable to store illegal type\n");
454     break;
455    
456     case WAY:
457     /* search matching way */
458 harbaum 155 member->object.way = osm_get_way_by_id(osm, id);
459     if(!member->object.way) {
460     member->object.type = WAY_ID;
461     member->object.id = id;
462 harbaum 1 }
463     break;
464    
465     case NODE:
466     /* search matching node */
467 harbaum 155 member->object.node = osm_get_node_by_id(osm, id);
468     if(!member->object.node) {
469     member->object.type = NODE_ID;
470     member->object.id = id;
471 harbaum 1 }
472     break;
473    
474     case RELATION:
475     /* search matching relation */
476 harbaum 155 member->object.relation = osm_get_relation_by_id(osm, id);
477     if(!member->object.relation) {
478     member->object.type = NODE_ID;
479     member->object.id = id;
480 harbaum 1 }
481     break;
482    
483     case WAY_ID:
484     case NODE_ID:
485     case RELATION_ID:
486     break;
487     }
488    
489     xmlFree(prop);
490     }
491    
492     if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"role"))) {
493     if(strlen(prop) > 0) member->role = g_strdup(prop);
494     xmlFree(prop);
495     }
496    
497     return member;
498     }
499    
500     /* ------------------ osm handling ----------------- */
501    
502 harbaum 42 /* the two hash tables eat over 512kBytes memory and may thus be */
503     /* freed at any time. osm2go can work without them (albeit slower) */
504     static void hash_table_free(hash_table_t *table) {
505     if(!table) return;
506    
507     int i;
508     for(i=0;i<65536;i++) {
509     hash_item_t *item = table->hash[i];
510     while(item) {
511     hash_item_t *next = item->next;
512     g_free(item);
513     item = next;
514     }
515     }
516     }
517    
518     void osm_hash_tables_free(osm_t *osm) {
519     hash_table_free(osm->node_hash);
520     osm->node_hash = NULL;
521     hash_table_free(osm->way_hash);
522     osm->way_hash = NULL;
523     }
524    
525 harbaum 1 void osm_free(icon_t **icon, osm_t *osm) {
526     if(!osm) return;
527    
528 harbaum 42 osm_hash_tables_free(osm);
529    
530 harbaum 1 if(osm->bounds) osm_bounds_free(osm->bounds);
531     if(osm->user) osm_users_free(osm->user);
532     if(osm->way) osm_ways_free(osm->way);
533     if(osm->node) osm_nodes_free(icon, osm->node);
534     if(osm->relation) osm_relations_free(osm->relation);
535     g_free(osm);
536     }
537    
538     void osm_dump(osm_t *osm) {
539     osm_bounds_dump(osm->bounds);
540     osm_users_dump(osm->user);
541     osm_nodes_dump(osm->node);
542     osm_ways_dump(osm->way);
543     osm_relations_dump(osm->relation);
544     }
545    
546 harbaum 78 /* -------------------------- stream parser ------------------- */
547 harbaum 8
548     #include <libxml/xmlreader.h>
549    
550     static gint my_strcmp(const xmlChar *a, const xmlChar *b) {
551     if(!a && !b) return 0;
552     if(!a) return -1;
553     if(!b) return +1;
554     return strcmp((char*)a,(char*)b);
555     }
556    
557     /* skip current element incl. everything below (mainly for testing) */
558     /* returns FALSE if something failed */
559     static gboolean skip_element(xmlTextReaderPtr reader) {
560     g_assert(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT);
561     const xmlChar *name = xmlTextReaderConstName(reader);
562     g_assert(name);
563     int depth = xmlTextReaderDepth(reader);
564    
565     if(xmlTextReaderIsEmptyElement(reader))
566     return TRUE;
567    
568     int ret = xmlTextReaderRead(reader);
569     while((ret == 1) &&
570     ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||
571     (xmlTextReaderDepth(reader) > depth) ||
572     (my_strcmp(xmlTextReaderConstName(reader), name) != 0))) {
573     ret = xmlTextReaderRead(reader);
574     }
575     return(ret == 1);
576     }
577    
578     /* parse bounds */
579     static bounds_t *process_bounds(xmlTextReaderPtr reader) {
580     char *prop = NULL;
581     bounds_t *bounds = g_new0(bounds_t, 1);
582    
583     bounds->ll_min.lat = bounds->ll_min.lon = NAN;
584     bounds->ll_max.lat = bounds->ll_max.lon = NAN;
585    
586     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "minlat"))) {
587     bounds->ll_min.lat = g_ascii_strtod(prop, NULL);
588     xmlFree(prop);
589     }
590    
591     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "maxlat"))) {
592     bounds->ll_max.lat = g_ascii_strtod(prop, NULL);
593     xmlFree(prop);
594     }
595    
596     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "minlon"))) {
597     bounds->ll_min.lon = g_ascii_strtod(prop, NULL);
598     xmlFree(prop);
599     }
600    
601     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "maxlon"))) {
602     bounds->ll_max.lon = g_ascii_strtod(prop, NULL);
603     xmlFree(prop);
604     }
605    
606     if(isnan(bounds->ll_min.lat) || isnan(bounds->ll_min.lon) ||
607     isnan(bounds->ll_max.lat) || isnan(bounds->ll_max.lon)) {
608     errorf(NULL, "Invalid coordinate in bounds (%f/%f/%f/%f)",
609     bounds->ll_min.lat, bounds->ll_min.lon,
610     bounds->ll_max.lat, bounds->ll_max.lon);
611    
612     osm_bounds_free(bounds);
613     return NULL;
614     }
615    
616     /* skip everything below */
617     skip_element(reader);
618    
619     /* calculate map zone which will be used as a reference for all */
620     /* drawing/projection later on */
621     pos_t center = { (bounds->ll_max.lat + bounds->ll_min.lat)/2,
622     (bounds->ll_max.lon + bounds->ll_min.lon)/2 };
623    
624     pos2lpos_center(&center, &bounds->center);
625    
626     /* the scale is needed to accomodate for "streching" */
627     /* by the mercartor projection */
628     bounds->scale = cos(DEG2RAD(center.lat));
629    
630     pos2lpos_center(&bounds->ll_min, &bounds->min);
631     bounds->min.x -= bounds->center.x;
632     bounds->min.y -= bounds->center.y;
633     bounds->min.x *= bounds->scale;
634     bounds->min.y *= bounds->scale;
635    
636     pos2lpos_center(&bounds->ll_max, &bounds->max);
637     bounds->max.x -= bounds->center.x;
638     bounds->max.y -= bounds->center.y;
639     bounds->max.x *= bounds->scale;
640     bounds->max.y *= bounds->scale;
641    
642     return bounds;
643     }
644    
645     static tag_t *process_tag(xmlTextReaderPtr reader) {
646     /* allocate a new tag structure */
647     tag_t *tag = g_new0(tag_t, 1);
648    
649     char *prop;
650     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "k"))) {
651     if(strlen(prop) > 0) tag->key = g_strdup(prop);
652     xmlFree(prop);
653     }
654    
655     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "v"))) {
656     if(strlen(prop) > 0) tag->value = g_strdup(prop);
657     xmlFree(prop);
658     }
659    
660     if(!tag->key || !tag->value) {
661     printf("incomplete tag key/value %s/%s\n", tag->key, tag->value);
662     osm_tags_free(tag);
663     tag = NULL;
664     }
665    
666     skip_element(reader);
667     return tag;
668     }
669    
670     static node_t *process_node(xmlTextReaderPtr reader, osm_t *osm) {
671    
672     /* allocate a new node structure */
673     node_t *node = g_new0(node_t, 1);
674     node->pos.lat = node->pos.lon = NAN;
675    
676     char *prop;
677     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"))) {
678     node->id = strtoul(prop, NULL, 10);
679     xmlFree(prop);
680     }
681    
682 harbaum 158 /* new in api 0.6: */
683     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "version"))) {
684     node->version = strtoul(prop, NULL, 10);
685     xmlFree(prop);
686     }
687    
688 harbaum 8 if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "lat"))) {
689     node->pos.lat = g_ascii_strtod(prop, NULL);
690     xmlFree(prop);
691     }
692    
693     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "lon"))) {
694     node->pos.lon = g_ascii_strtod(prop, NULL);
695     xmlFree(prop);
696     }
697    
698     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {
699     node->user = osm_user(osm, prop);
700     xmlFree(prop);
701     }
702    
703     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "visible"))) {
704     node->visible = (strcasecmp(prop, "true") == 0);
705     xmlFree(prop);
706     }
707    
708     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "timestamp"))) {
709     node->time = convert_iso8601(prop);
710     xmlFree(prop);
711     }
712    
713     pos2lpos(osm->bounds, &node->pos, &node->lpos);
714    
715 harbaum 42 /* append node to end of hash table if present */
716     if(osm->node_hash) {
717     hash_item_t **item = &osm->node_hash->hash[ID2HASH(node->id)];
718     while(*item) item = &(*item)->next;
719    
720     *item = g_new0(hash_item_t, 1);
721     (*item)->data.node = node;
722     }
723    
724 harbaum 8 /* just an empty element? then return the node as it is */
725     if(xmlTextReaderIsEmptyElement(reader))
726     return node;
727    
728     /* parse tags if present */
729     int depth = xmlTextReaderDepth(reader);
730    
731     /* scan all elements on same level or its children */
732     tag_t **tag = &node->tag;
733     int ret = xmlTextReaderRead(reader);
734     while((ret == 1) &&
735     ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||
736     (xmlTextReaderDepth(reader) != depth))) {
737    
738     if(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
739     char *subname = (char*)xmlTextReaderConstName(reader);
740     if(strcasecmp(subname, "tag") == 0) {
741     *tag = process_tag(reader);
742     if(*tag) tag = &(*tag)->next;
743     } else
744     skip_element(reader);
745     }
746    
747     ret = xmlTextReaderRead(reader);
748     }
749    
750     return node;
751     }
752    
753     static node_chain_t *process_nd(xmlTextReaderPtr reader, osm_t *osm) {
754     char *prop;
755    
756     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) {
757     item_id_t id = strtoul(prop, NULL, 10);
758     node_chain_t *node_chain = g_new0(node_chain_t, 1);
759    
760     /* search matching node */
761 harbaum 42 node_chain->node = osm_get_node_by_id(osm, id);
762 harbaum 161 if(!node_chain->node) printf("Node id " ITEM_ID_FORMAT " not found\n", id);
763 harbaum 42 else node_chain->node->ways++;
764 harbaum 8
765     xmlFree(prop);
766    
767     skip_element(reader);
768     return node_chain;
769     }
770    
771     skip_element(reader);
772     return NULL;
773     }
774    
775     static way_t *process_way(xmlTextReaderPtr reader, osm_t *osm) {
776     /* allocate a new way structure */
777     way_t *way = g_new0(way_t, 1);
778    
779     char *prop;
780     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"))) {
781     way->id = strtoul(prop, NULL, 10);
782     xmlFree(prop);
783     }
784    
785 harbaum 158 /* new in api 0.6: */
786     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "version"))) {
787     way->version = strtoul(prop, NULL, 10);
788     xmlFree(prop);
789     }
790    
791 harbaum 8 if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {
792     way->user = osm_user(osm, prop);
793     xmlFree(prop);
794     }
795    
796     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "visible"))) {
797     way->visible = (strcasecmp(prop, "true") == 0);
798     xmlFree(prop);
799     }
800    
801     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "timestamp"))) {
802     way->time = convert_iso8601(prop);
803     xmlFree(prop);
804     }
805    
806 harbaum 42 /* append way to end of hash table if present */
807     if(osm->way_hash) {
808     hash_item_t **item = &osm->way_hash->hash[ID2HASH(way->id)];
809     while(*item) item = &(*item)->next;
810    
811     *item = g_new0(hash_item_t, 1);
812     (*item)->data.way = way;
813     }
814    
815 harbaum 8 /* just an empty element? then return the way as it is */
816     /* (this should in fact never happen as this would be a way without nodes) */
817     if(xmlTextReaderIsEmptyElement(reader))
818     return way;
819    
820     /* parse tags/nodes if present */
821     int depth = xmlTextReaderDepth(reader);
822    
823     /* scan all elements on same level or its children */
824     tag_t **tag = &way->tag;
825     node_chain_t **node_chain = &way->node_chain;
826     int ret = xmlTextReaderRead(reader);
827     while((ret == 1) &&
828     ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||
829     (xmlTextReaderDepth(reader) != depth))) {
830    
831     if(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
832     char *subname = (char*)xmlTextReaderConstName(reader);
833     if(strcasecmp(subname, "nd") == 0) {
834     *node_chain = process_nd(reader, osm);
835     if(*node_chain) node_chain = &(*node_chain)->next;
836     } else if(strcasecmp(subname, "tag") == 0) {
837     *tag = process_tag(reader);
838     if(*tag) tag = &(*tag)->next;
839     } else
840     skip_element(reader);
841     }
842     ret = xmlTextReaderRead(reader);
843     }
844    
845     return way;
846     }
847    
848     static member_t *process_member(xmlTextReaderPtr reader, osm_t *osm) {
849     char *prop;
850     member_t *member = g_new0(member_t, 1);
851 harbaum 155 member->object.type = ILLEGAL;
852 harbaum 8
853     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "type"))) {
854 harbaum 155 if(strcasecmp(prop, "way") == 0) member->object.type = WAY;
855     else if(strcasecmp(prop, "node") == 0) member->object.type = NODE;
856     else if(strcasecmp(prop, "relation") == 0) member->object.type = RELATION;
857 harbaum 8 xmlFree(prop);
858     }
859    
860     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) {
861     item_id_t id = strtoul(prop, NULL, 10);
862    
863 harbaum 155 switch(member->object.type) {
864 harbaum 8 case ILLEGAL:
865     printf("Unable to store illegal type\n");
866     break;
867    
868     case WAY:
869     /* search matching way */
870 harbaum 155 member->object.way = osm_get_way_by_id(osm, id);
871     if(!member->object.way) {
872     member->object.type = WAY_ID;
873     member->object.id = id;
874 harbaum 8 }
875     break;
876    
877     case NODE:
878     /* search matching node */
879 harbaum 155 member->object.node = osm_get_node_by_id(osm, id);
880     if(!member->object.node) {
881     member->object.type = NODE_ID;
882     member->object.id = id;
883 harbaum 8 }
884     break;
885    
886     case RELATION:
887     /* search matching relation */
888 harbaum 155 member->object.relation = osm_get_relation_by_id(osm, id);
889     if(!member->object.relation) {
890     member->object.type = NODE_ID;
891     member->object.id = id;
892 harbaum 8 }
893     break;
894    
895     case WAY_ID:
896     case NODE_ID:
897     case RELATION_ID:
898     break;
899     }
900    
901     xmlFree(prop);
902     }
903    
904     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "role"))) {
905     if(strlen(prop) > 0) member->role = g_strdup(prop);
906     xmlFree(prop);
907     }
908    
909     return member;
910     }
911    
912     static relation_t *process_relation(xmlTextReaderPtr reader, osm_t *osm) {
913     /* allocate a new relation structure */
914     relation_t *relation = g_new0(relation_t, 1);
915    
916     char *prop;
917     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"))) {
918     relation->id = strtoul(prop, NULL, 10);
919     xmlFree(prop);
920     }
921    
922 harbaum 158 /* new in api 0.6: */
923     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "version"))) {
924     relation->version = strtoul(prop, NULL, 10);
925     xmlFree(prop);
926     }
927    
928 harbaum 8 if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {
929     relation->user = osm_user(osm, prop);
930     xmlFree(prop);
931     }
932    
933     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "visible"))) {
934     relation->visible = (strcasecmp(prop, "true") == 0);
935     xmlFree(prop);
936     }
937    
938     if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "timestamp"))) {
939     relation->time = convert_iso8601(prop);
940     xmlFree(prop);
941     }
942    
943     /* just an empty element? then return the relation as it is */
944     /* (this should in fact never happen as this would be a relation */
945     /* without members) */
946     if(xmlTextReaderIsEmptyElement(reader))
947     return relation;
948    
949     /* parse tags/member if present */
950     int depth = xmlTextReaderDepth(reader);
951    
952     /* scan all elements on same level or its children */
953     tag_t **tag = &relation->tag;
954     member_t **member = &relation->member;
955     int ret = xmlTextReaderRead(reader);
956     while((ret == 1) &&
957     ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||
958     (xmlTextReaderDepth(reader) != depth))) {
959    
960     if(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
961     char *subname = (char*)xmlTextReaderConstName(reader);
962 achadwick 35 if(strcasecmp(subname, "member") == 0) {
963 harbaum 8 *member = process_member(reader, osm);
964     if(*member) member = &(*member)->next;
965     } else if(strcasecmp(subname, "tag") == 0) {
966     *tag = process_tag(reader);
967     if(*tag) tag = &(*tag)->next;
968     } else
969     skip_element(reader);
970     }
971     ret = xmlTextReaderRead(reader);
972     }
973    
974     return relation;
975     }
976    
977     static osm_t *process_osm(xmlTextReaderPtr reader) {
978     /* alloc osm structure */
979     osm_t *osm = g_new0(osm_t, 1);
980 harbaum 42 osm->node_hash = g_new0(hash_table_t, 1);
981     osm->way_hash = g_new0(hash_table_t, 1);
982 harbaum 8
983     node_t **node = &osm->node;
984     way_t **way = &osm->way;
985     relation_t **relation = &osm->relation;
986    
987     /* no attributes of interest */
988    
989     const xmlChar *name = xmlTextReaderConstName(reader);
990     g_assert(name);
991    
992     /* read next node */
993 achadwick 28 int num_elems = 0;
994     const int tick_every = 50; // Balance responsive appearance with performance.
995 harbaum 8 int ret = xmlTextReaderRead(reader);
996     while(ret == 1) {
997    
998     switch(xmlTextReaderNodeType(reader)) {
999     case XML_READER_TYPE_ELEMENT:
1000    
1001     g_assert(xmlTextReaderDepth(reader) == 1);
1002     char *name = (char*)xmlTextReaderConstName(reader);
1003     if(strcasecmp(name, "bounds") == 0) {
1004     osm->bounds = process_bounds(reader);
1005     } else if(strcasecmp(name, "node") == 0) {
1006     *node = process_node(reader, osm);
1007     if(*node) node = &(*node)->next;
1008     } else if(strcasecmp(name, "way") == 0) {
1009     *way = process_way(reader, osm);
1010     if(*way) way = &(*way)->next;
1011     } else if(strcasecmp(name, "relation") == 0) {
1012     *relation = process_relation(reader, osm);
1013     if(*relation) relation = &(*relation)->next;
1014     } else {
1015     printf("something unknown found\n");
1016     g_assert(0);
1017     skip_element(reader);
1018     }
1019     break;
1020    
1021     case XML_READER_TYPE_END_ELEMENT:
1022     /* end element must be for the current element */
1023     g_assert(xmlTextReaderDepth(reader) == 0);
1024     return osm;
1025     break;
1026    
1027     default:
1028     break;
1029     }
1030     ret = xmlTextReaderRead(reader);
1031 achadwick 28
1032     if (num_elems++ > tick_every) {
1033     num_elems = 0;
1034     banner_busy_tick();
1035     }
1036 harbaum 8 }
1037    
1038     g_assert(0);
1039     return NULL;
1040     }
1041    
1042     static osm_t *process_file(const char *filename) {
1043     osm_t *osm = NULL;
1044     xmlTextReaderPtr reader;
1045     int ret;
1046    
1047     reader = xmlReaderForFile(filename, NULL, 0);
1048     if (reader != NULL) {
1049     ret = xmlTextReaderRead(reader);
1050     if(ret == 1) {
1051     char *name = (char*)xmlTextReaderConstName(reader);
1052     if(name && strcasecmp(name, "osm") == 0)
1053     osm = process_osm(reader);
1054     } else
1055     printf("file empty\n");
1056    
1057     xmlFreeTextReader(reader);
1058     } else {
1059     fprintf(stderr, "Unable to open %s\n", filename);
1060     }
1061     return osm;
1062     }
1063    
1064 harbaum 78 /* ----------------------- end of stream parser ------------------- */
1065 harbaum 8
1066     #include <sys/time.h>
1067    
1068 harbaum 1 osm_t *osm_parse(char *filename) {
1069    
1070 harbaum 8 struct timeval start;
1071     gettimeofday(&start, NULL);
1072    
1073 harbaum 1 LIBXML_TEST_VERSION;
1074    
1075 harbaum 8 // use stream parser
1076     osm_t *osm = process_file(filename);
1077     xmlCleanupParser();
1078    
1079     struct timeval end;
1080     gettimeofday(&end, NULL);
1081    
1082     printf("total parse time: %ldms\n",
1083     (end.tv_usec - start.tv_usec)/1000 +
1084     (end.tv_sec - start.tv_sec)*1000);
1085    
1086     return osm;
1087 harbaum 1 }
1088    
1089     gboolean osm_sanity_check(GtkWidget *parent, osm_t *osm) {
1090     if(!osm->bounds) {
1091 achadwick 6 errorf(parent, _("Invalid data in OSM file:\n"
1092 harbaum 1 "Boundary box missing!"));
1093     return FALSE;
1094     }
1095     if(!osm->node) {
1096 achadwick 6 errorf(parent, _("Invalid data in OSM file:\n"
1097 harbaum 1 "No drawable content found!"));
1098     return FALSE;
1099     }
1100     return TRUE;
1101     }
1102    
1103     /* ------------------------- misc access functions -------------- */
1104    
1105     char *osm_tag_get_by_key(tag_t *tag, char *key) {
1106     if(!tag || !key) return NULL;
1107    
1108     while(tag) {
1109     if(strcasecmp(tag->key, key) == 0)
1110     return tag->value;
1111    
1112     tag = tag->next;
1113     }
1114    
1115     return NULL;
1116     }
1117    
1118     char *osm_way_get_value(way_t *way, char *key) {
1119     tag_t *tag = way->tag;
1120    
1121     while(tag) {
1122     if(strcasecmp(tag->key, key) == 0)
1123     return tag->value;
1124    
1125     tag = tag->next;
1126     }
1127    
1128     return NULL;
1129     }
1130    
1131     char *osm_node_get_value(node_t *node, char *key) {
1132     tag_t *tag = node->tag;
1133    
1134     while(tag) {
1135     if(strcasecmp(tag->key, key) == 0)
1136     return tag->value;
1137    
1138     tag = tag->next;
1139     }
1140    
1141     return NULL;
1142     }
1143    
1144     gboolean osm_way_has_value(way_t *way, char *str) {
1145     tag_t *tag = way->tag;
1146    
1147     while(tag) {
1148     if(tag->value && strcasecmp(tag->value, str) == 0)
1149     return TRUE;
1150    
1151     tag = tag->next;
1152     }
1153     return FALSE;
1154     }
1155    
1156     gboolean osm_node_has_value(node_t *node, char *str) {
1157     tag_t *tag = node->tag;
1158    
1159     while(tag) {
1160     if(tag->value && strcasecmp(tag->value, str) == 0)
1161     return TRUE;
1162    
1163     tag = tag->next;
1164     }
1165     return FALSE;
1166     }
1167    
1168     gboolean osm_node_has_tag(node_t *node) {
1169     tag_t *tag = node->tag;
1170    
1171 harbaum 158 /* created_by tags don't count as real tags */
1172 harbaum 1 if(tag && strcasecmp(tag->key, "created_by") == 0)
1173     tag = tag->next;
1174    
1175     return tag != NULL;
1176     }
1177    
1178     /* return true if node is part of way */
1179     gboolean osm_node_in_way(way_t *way, node_t *node) {
1180     node_chain_t *node_chain = way->node_chain;
1181     while(node_chain) {
1182     if(node_chain->node == node)
1183     return TRUE;
1184    
1185     node_chain = node_chain->next;
1186     }
1187     return FALSE;
1188     }
1189    
1190     static void osm_generate_tags(tag_t *tag, xmlNodePtr node) {
1191     while(tag) {
1192     /* make sure "created_by" tag contains our id */
1193     if(strcasecmp(tag->key, "created_by") == 0) {
1194 harbaum 158 if(strcasecmp(tag->value, PACKAGE " v" VERSION) != 0) {
1195     g_free(tag->value);
1196     tag->value = g_strdup(PACKAGE " v" VERSION);
1197     }
1198 harbaum 1 }
1199    
1200     xmlNodePtr tag_node = xmlNewChild(node, NULL, BAD_CAST "tag", NULL);
1201     xmlNewProp(tag_node, BAD_CAST "k", BAD_CAST tag->key);
1202     xmlNewProp(tag_node, BAD_CAST "v", BAD_CAST tag->value);
1203     tag = tag->next;
1204     }
1205     }
1206    
1207     /* build xml representation for a way */
1208 harbaum 158 static char *osm_generate_xml(osm_t *osm, item_id_t changeset,
1209     type_t type, void *item) {
1210 harbaum 1 char str[32];
1211     xmlChar *result = NULL;
1212     int len = 0;
1213    
1214     LIBXML_TEST_VERSION;
1215    
1216     xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
1217     xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "osm");
1218     xmlDocSetRootElement(doc, root_node);
1219    
1220     switch(type) {
1221     case NODE:
1222     {
1223     node_t *node = (node_t*)item;
1224     xmlNodePtr node_node = xmlNewChild(root_node, NULL,
1225     BAD_CAST "node", NULL);
1226     /* new nodes don't have an id, but get one after the upload */
1227     if(!(node->flags & OSM_FLAG_NEW)) {
1228     snprintf(str, sizeof(str), "%u", (unsigned)node->id);
1229     xmlNewProp(node_node, BAD_CAST "id", BAD_CAST str);
1230     }
1231 harbaum 158 snprintf(str, sizeof(str), "%u", (unsigned)node->version);
1232     xmlNewProp(node_node, BAD_CAST "version", BAD_CAST str);
1233     snprintf(str, sizeof(str), "%u", (unsigned)changeset);
1234     xmlNewProp(node_node, BAD_CAST "changeset", BAD_CAST str);
1235 harbaum 156 g_ascii_formatd(str, sizeof(str), LL_FORMAT, node->pos.lat);
1236 harbaum 1 xmlNewProp(node_node, BAD_CAST "lat", BAD_CAST str);
1237 harbaum 156 g_ascii_formatd(str, sizeof(str), LL_FORMAT, node->pos.lon);
1238 harbaum 1 xmlNewProp(node_node, BAD_CAST "lon", BAD_CAST str);
1239     osm_generate_tags(node->tag, node_node);
1240     }
1241     break;
1242    
1243     case WAY:
1244     {
1245     way_t *way = (way_t*)item;
1246     xmlNodePtr way_node = xmlNewChild(root_node, NULL, BAD_CAST "way", NULL);
1247     snprintf(str, sizeof(str), "%u", (unsigned)way->id);
1248     xmlNewProp(way_node, BAD_CAST "id", BAD_CAST str);
1249 harbaum 158 snprintf(str, sizeof(str), "%u", (unsigned)way->version);
1250     xmlNewProp(way_node, BAD_CAST "version", BAD_CAST str);
1251     snprintf(str, sizeof(str), "%u", (unsigned)changeset);
1252     xmlNewProp(way_node, BAD_CAST "changeset", BAD_CAST str);
1253 harbaum 1
1254     node_chain_t *node_chain = way->node_chain;
1255     while(node_chain) {
1256     xmlNodePtr nd_node = xmlNewChild(way_node, NULL, BAD_CAST "nd", NULL);
1257 harbaum 161 char *str = g_strdup_printf(ITEM_ID_FORMAT, node_chain->node->id);
1258 harbaum 1 xmlNewProp(nd_node, BAD_CAST "ref", BAD_CAST str);
1259     g_free(str);
1260     node_chain = node_chain->next;
1261     }
1262    
1263     osm_generate_tags(way->tag, way_node);
1264     }
1265     break;
1266    
1267     case RELATION:
1268     {
1269     relation_t *relation = (relation_t*)item;
1270     xmlNodePtr rel_node = xmlNewChild(root_node, NULL,
1271     BAD_CAST "relation", NULL);
1272     snprintf(str, sizeof(str), "%u", (unsigned)relation->id);
1273     xmlNewProp(rel_node, BAD_CAST "id", BAD_CAST str);
1274 harbaum 158 snprintf(str, sizeof(str), "%u", (unsigned)relation->version);
1275     xmlNewProp(rel_node, BAD_CAST "version", BAD_CAST str);
1276     snprintf(str, sizeof(str), "%u", (unsigned)changeset);
1277     xmlNewProp(rel_node, BAD_CAST "changeset", BAD_CAST str);
1278 harbaum 1
1279     member_t *member = relation->member;
1280     while(member) {
1281     xmlNodePtr m_node = xmlNewChild(rel_node,NULL,BAD_CAST "member", NULL);
1282     char *str = NULL;
1283    
1284 harbaum 155 switch(member->object.type) {
1285 harbaum 1 case NODE:
1286     xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "node");
1287 harbaum 161 str = g_strdup_printf(ITEM_ID_FORMAT, member->object.node->id);
1288 harbaum 1 break;
1289    
1290     case WAY:
1291     xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "way");
1292 harbaum 161 str = g_strdup_printf(ITEM_ID_FORMAT, member->object.way->id);
1293 harbaum 1 break;
1294    
1295     case RELATION:
1296     xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "relation");
1297 harbaum 161 str = g_strdup_printf(ITEM_ID_FORMAT, member->object.relation->id);
1298 harbaum 1 break;
1299    
1300     default:
1301     break;
1302     }
1303    
1304     if(str) {
1305     xmlNewProp(m_node, BAD_CAST "ref", BAD_CAST str);
1306     g_free(str);
1307     }
1308    
1309     if(member->role)
1310     xmlNewProp(m_node, BAD_CAST "role", BAD_CAST member->role);
1311     else
1312     xmlNewProp(m_node, BAD_CAST "role", BAD_CAST "");
1313    
1314     member = member->next;
1315     }
1316     osm_generate_tags(relation->tag, rel_node);
1317     }
1318     break;
1319    
1320     default:
1321     printf("neither NODE nor WAY nor RELATION\n");
1322     g_assert(0);
1323     break;
1324     }
1325    
1326     xmlDocDumpFormatMemoryEnc(doc, &result, &len, "UTF-8", 1);
1327     xmlFreeDoc(doc);
1328     xmlCleanupParser();
1329    
1330     // puts("xml encoding result:");
1331     // puts((char*)result);
1332    
1333     return (char*)result;
1334     }
1335    
1336     /* build xml representation for a node */
1337 harbaum 158 char *osm_generate_xml_node(osm_t *osm, item_id_t changeset, node_t *node) {
1338     return osm_generate_xml(osm, changeset, NODE, node);
1339 harbaum 1 }
1340    
1341     /* build xml representation for a way */
1342 harbaum 158 char *osm_generate_xml_way(osm_t *osm, item_id_t changeset, way_t *way) {
1343     return osm_generate_xml(osm, changeset, WAY, way);
1344 harbaum 1 }
1345    
1346     /* build xml representation for a relation */
1347 harbaum 158 char *osm_generate_xml_relation(osm_t *osm, item_id_t changeset,
1348     relation_t *relation) {
1349     return osm_generate_xml(osm, changeset, RELATION, relation);
1350 harbaum 1 }
1351    
1352 harbaum 158 /* build xml representation for a changeset */
1353     char *osm_generate_xml_changeset(osm_t *osm, char *comment) {
1354     xmlChar *result = NULL;
1355     int len = 0;
1356    
1357     /* tags for this changeset */
1358     tag_t tag_comment = {
1359     .key = "comment", .value = comment, .next = NULL };
1360     tag_t tag_creator = {
1361     .key = "created_by", .value = PACKAGE " v" VERSION, .next = &tag_comment };
1362    
1363     LIBXML_TEST_VERSION;
1364    
1365     xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
1366     xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "osm");
1367     xmlDocSetRootElement(doc, root_node);
1368    
1369     xmlNodePtr cs_node = xmlNewChild(root_node, NULL, BAD_CAST "changeset", NULL);
1370     osm_generate_tags(&tag_creator, cs_node);
1371    
1372     xmlDocDumpFormatMemoryEnc(doc, &result, &len, "UTF-8", 1);
1373     xmlFreeDoc(doc);
1374     xmlCleanupParser();
1375    
1376     // puts("xml encoding result:");
1377     // puts((char*)result);
1378    
1379     return (char*)result;
1380     }
1381    
1382    
1383 harbaum 42 /* the following three functions are eating much CPU power */
1384     /* as they search the objects lists. Hashing is supposed to help */
1385 harbaum 1 node_t *osm_get_node_by_id(osm_t *osm, item_id_t id) {
1386 harbaum 42 if(id > 0 && osm->node_hash) {
1387     // use hash table if present
1388     hash_item_t *item = osm->node_hash->hash[ID2HASH(id)];
1389     while(item) {
1390     if(item->data.node->id == id)
1391     return item->data.node;
1392    
1393     item = item->next;
1394     }
1395     }
1396    
1397     /* use linear search if no hash tables are present or search in hash table failed */
1398 harbaum 1 node_t *node = osm->node;
1399     while(node) {
1400     if(node->id == id)
1401     return node;
1402 harbaum 42
1403 harbaum 1 node = node->next;
1404     }
1405 harbaum 42
1406 harbaum 1 return NULL;
1407     }
1408    
1409     way_t *osm_get_way_by_id(osm_t *osm, item_id_t id) {
1410 harbaum 42 if(id > 0 && osm->way_hash) {
1411     // use hash table if present
1412     hash_item_t *item = osm->way_hash->hash[ID2HASH(id)];
1413     while(item) {
1414     if(item->data.way->id == id)
1415     return item->data.way;
1416    
1417     item = item->next;
1418     }
1419     }
1420    
1421     /* use linear search if no hash tables are present or search on hash table failed */
1422 harbaum 1 way_t *way = osm->way;
1423     while(way) {
1424     if(way->id == id)
1425     return way;
1426 harbaum 42
1427 harbaum 1 way = way->next;
1428     }
1429 harbaum 42
1430 harbaum 1 return NULL;
1431     }
1432    
1433     relation_t *osm_get_relation_by_id(osm_t *osm, item_id_t id) {
1434 harbaum 42 // use linear search
1435 harbaum 1 relation_t *relation = osm->relation;
1436     while(relation) {
1437     if(relation->id == id)
1438     return relation;
1439    
1440     relation = relation->next;
1441     }
1442    
1443     return NULL;
1444     }
1445    
1446     /* ---------- edit functions ------------- */
1447    
1448     item_id_t osm_new_way_id(osm_t *osm) {
1449     item_id_t id = -1;
1450    
1451     while(TRUE) {
1452     gboolean found = FALSE;
1453     way_t *way = osm->way;
1454     while(way) {
1455     if(way->id == id)
1456     found = TRUE;
1457    
1458     way = way->next;
1459     }
1460    
1461     /* no such id so far -> use it */
1462     if(!found) return id;
1463    
1464     id--;
1465     }
1466     g_assert(0);
1467     return 0;
1468     }
1469    
1470     item_id_t osm_new_node_id(osm_t *osm) {
1471     item_id_t id = -1;
1472    
1473     while(TRUE) {
1474     gboolean found = FALSE;
1475     node_t *node = osm->node;
1476     while(node) {
1477     if(node->id == id)
1478     found = TRUE;
1479    
1480     node = node->next;
1481     }
1482    
1483     /* no such id so far -> use it */
1484     if(!found) return id;
1485    
1486     id--;
1487     }
1488     g_assert(0);
1489     return 0;
1490     }
1491    
1492 harbaum 73 item_id_t osm_new_relation_id(osm_t *osm) {
1493     item_id_t id = -1;
1494    
1495     while(TRUE) {
1496     gboolean found = FALSE;
1497     relation_t *relation = osm->relation;
1498     while(relation) {
1499     if(relation->id == id)
1500     found = TRUE;
1501    
1502     relation = relation->next;
1503     }
1504    
1505     /* no such id so far -> use it */
1506     if(!found) return id;
1507    
1508     id--;
1509     }
1510     g_assert(0);
1511     return 0;
1512     }
1513    
1514 harbaum 1 node_t *osm_node_new(osm_t *osm, gint x, gint y) {
1515     printf("Creating new node\n");
1516    
1517     node_t *node = g_new0(node_t, 1);
1518 harbaum 158 node->version = 1;
1519 harbaum 1 node->lpos.x = x;
1520     node->lpos.y = y;
1521     node->visible = TRUE;
1522     node->time = time(NULL);
1523    
1524     /* convert screen position back to ll */
1525     lpos2pos(osm->bounds, &node->lpos, &node->pos);
1526    
1527     printf(" new at %d %d (%f %f)\n",
1528     node->lpos.x, node->lpos.y, node->pos.lat, node->pos.lon);
1529    
1530     return node;
1531     }
1532    
1533    
1534     void osm_node_attach(osm_t *osm, node_t *node) {
1535     printf("Attaching node\n");
1536    
1537     node->id = osm_new_node_id(osm);
1538     node->flags = OSM_FLAG_NEW;
1539    
1540     /* attach to end of node list */
1541     node_t **lnode = &osm->node;
1542     while(*lnode) lnode = &(*lnode)->next;
1543     *lnode = node;
1544     }
1545    
1546 harbaum 64 void osm_node_restore(osm_t *osm, node_t *node) {
1547     printf("Restoring node\n");
1548    
1549     /* attach to end of node list */
1550     node_t **lnode = &osm->node;
1551     while(*lnode) lnode = &(*lnode)->next;
1552     *lnode = node;
1553     }
1554    
1555 harbaum 1 way_t *osm_way_new(void) {
1556     printf("Creating new way\n");
1557    
1558     way_t *way = g_new0(way_t, 1);
1559 harbaum 158 way->version = 1;
1560 harbaum 1 way->visible = TRUE;
1561     way->flags = OSM_FLAG_NEW;
1562     way->time = time(NULL);
1563    
1564     return way;
1565     }
1566    
1567     void osm_way_attach(osm_t *osm, way_t *way) {
1568     printf("Attaching way\n");
1569    
1570     way->id = osm_new_way_id(osm);
1571     way->flags = OSM_FLAG_NEW;
1572    
1573     /* attach to end of way list */
1574     way_t **lway = &osm->way;
1575     while(*lway) lway = &(*lway)->next;
1576     *lway = way;
1577     }
1578    
1579     /* returns pointer to chain of ways affected by this deletion */
1580     way_chain_t *osm_node_delete(osm_t *osm, icon_t **icon,
1581     node_t *node, gboolean permanently,
1582     gboolean affect_ways) {
1583     way_chain_t *way_chain = NULL, **cur_way_chain = &way_chain;
1584    
1585     /* new nodes aren't stored on the server and are just deleted permanently */
1586     if(node->flags & OSM_FLAG_NEW) {
1587 harbaum 161 printf("About to delete NEW node #" ITEM_ID_FORMAT
1588     " -> force permanent delete\n", node->id);
1589 harbaum 1 permanently = TRUE;
1590     }
1591    
1592     /* first remove node from all ways using it */
1593     way_t *way = osm->way;
1594     while(way) {
1595     node_chain_t **chain = &(way->node_chain);
1596     gboolean modified = FALSE;
1597     while(*chain) {
1598     /* remove node from chain */
1599     if(node == (*chain)->node) {
1600     modified = TRUE;
1601     if(affect_ways) {
1602     node_chain_t *next = (*chain)->next;
1603     g_free(*chain);
1604     *chain = next;
1605     } else
1606     chain = &((*chain)->next);
1607     } else
1608     chain = &((*chain)->next);
1609     }
1610    
1611     if(modified) {
1612     way->flags |= OSM_FLAG_DIRTY;
1613    
1614     /* and add the way to the list of affected ways */
1615     *cur_way_chain = g_new0(way_chain_t, 1);
1616     (*cur_way_chain)->way = way;
1617     cur_way_chain = &((*cur_way_chain)->next);
1618     }
1619    
1620     way = way->next;
1621     }
1622    
1623     if(!permanently) {
1624 harbaum 161 printf("mark node #" ITEM_ID_FORMAT " as deleted\n", node->id);
1625 harbaum 1 node->flags |= OSM_FLAG_DELETED;
1626     } else {
1627 harbaum 161 printf("permanently delete node #" ITEM_ID_FORMAT "\n", node->id);
1628 harbaum 1
1629     /* remove it from the chain */
1630     node_t **cnode = &osm->node;
1631     int found = 0;
1632    
1633     while(*cnode) {
1634     if(*cnode == node) {
1635     found++;
1636     *cnode = (*cnode)->next;
1637    
1638     osm_node_free(icon, node);
1639     } else
1640     cnode = &((*cnode)->next);
1641     }
1642     g_assert(found == 1);
1643     }
1644    
1645     return way_chain;
1646     }
1647    
1648     guint osm_way_number_of_nodes(way_t *way) {
1649     guint nodes = 0;
1650     node_chain_t *chain = way->node_chain;
1651     while(chain) {
1652     nodes++;
1653     chain = chain->next;
1654     }
1655     return nodes;
1656     }
1657    
1658     /* return all relations a node is in */
1659     relation_chain_t *osm_node_to_relation(osm_t *osm, node_t *node) {
1660     relation_chain_t *rel_chain = NULL, **cur_rel_chain = &rel_chain;
1661    
1662     relation_t *relation = osm->relation;
1663     while(relation) {
1664     gboolean is_member = FALSE;
1665    
1666     member_t *member = relation->member;
1667     while(member) {
1668 harbaum 155 switch(member->object.type) {
1669 harbaum 1 case NODE:
1670     /* nodes are checked directly */
1671 harbaum 155 if(member->object.node == node)
1672 harbaum 1 is_member = TRUE;
1673     break;
1674    
1675     case WAY: {
1676     /* ways have to be checked for the nodes they consist of */
1677 harbaum 155 node_chain_t *chain = member->object.way->node_chain;
1678 harbaum 1 while(chain && !is_member) {
1679     if(chain->node == node)
1680     is_member = TRUE;
1681    
1682     chain = chain->next;
1683     }
1684     } break;
1685    
1686     default:
1687     break;
1688     }
1689     member = member->next;
1690     }
1691    
1692     /* node is a member of this relation, so move it to the member chain */
1693     if(is_member) {
1694     *cur_rel_chain = g_new0(relation_chain_t, 1);
1695     (*cur_rel_chain)->relation = relation;
1696     cur_rel_chain = &((*cur_rel_chain)->next);
1697     }
1698    
1699     relation = relation->next;
1700     }
1701    
1702     return rel_chain;
1703     }
1704    
1705     /* return all relations a way is in */
1706     relation_chain_t *osm_way_to_relation(osm_t *osm, way_t *way) {
1707     relation_chain_t *rel_chain = NULL, **cur_rel_chain = &rel_chain;
1708    
1709     relation_t *relation = osm->relation;
1710     while(relation) {
1711     gboolean is_member = FALSE;
1712    
1713     member_t *member = relation->member;
1714     while(member) {
1715 harbaum 155 switch(member->object.type) {
1716 harbaum 1 case WAY: {
1717     /* ways can be check directly */
1718 harbaum 155 if(member->object.way == way)
1719 harbaum 1 is_member = TRUE;
1720     } break;
1721    
1722     default:
1723     break;
1724     }
1725     member = member->next;
1726     }
1727    
1728     /* way is a member of this relation, so move it to the member chain */
1729     if(is_member) {
1730     *cur_rel_chain = g_new0(relation_chain_t, 1);
1731     (*cur_rel_chain)->relation = relation;
1732     cur_rel_chain = &((*cur_rel_chain)->next);
1733     }
1734    
1735     relation = relation->next;
1736     }
1737    
1738     return rel_chain;
1739     }
1740    
1741     /* return all ways a node is in */
1742     way_chain_t *osm_node_to_way(osm_t *osm, node_t *node) {
1743     way_chain_t *chain = NULL, **cur_chain = &chain;
1744    
1745     way_t *way = osm->way;
1746     while(way) {
1747     gboolean is_member = FALSE;
1748    
1749     node_chain_t *node_chain = way->node_chain;
1750     while(node_chain) {
1751     if(node_chain->node == node)
1752     is_member = TRUE;
1753    
1754     node_chain = node_chain->next;
1755     }
1756    
1757     /* node is a member of this relation, so move it to the member chain */
1758     if(is_member) {
1759     *cur_chain = g_new0(way_chain_t, 1);
1760     (*cur_chain)->way = way;
1761     cur_chain = &((*cur_chain)->next);
1762     }
1763    
1764 harbaum 8 way = way->next;
1765 harbaum 1 }
1766    
1767     return chain;
1768     }
1769    
1770     gboolean osm_position_within_bounds(osm_t *osm, gint x, gint y) {
1771     if((x < osm->bounds->min.x) || (x > osm->bounds->max.x)) return FALSE;
1772     if((y < osm->bounds->min.y) || (y > osm->bounds->max.y)) return FALSE;
1773     return TRUE;
1774     }
1775    
1776     /* remove the given node from all relations. used if the node is to */
1777     /* be deleted */
1778     void osm_node_remove_from_relation(osm_t *osm, node_t *node) {
1779     relation_t *relation = osm->relation;
1780 harbaum 161 printf("removing node #" ITEM_ID_FORMAT " from all relations:\n", node->id);
1781 harbaum 1
1782     while(relation) {
1783     member_t **member = &relation->member;
1784     while(*member) {
1785 harbaum 155 if(((*member)->object.type == NODE) &&
1786     ((*member)->object.node == node)) {
1787 harbaum 1
1788 harbaum 161 printf(" from relation #" ITEM_ID_FORMAT "\n", relation->id);
1789 harbaum 1
1790     member_t *cur = *member;
1791     *member = (*member)->next;
1792     osm_member_free(cur);
1793    
1794     relation->flags |= OSM_FLAG_DIRTY;
1795     } else
1796     member = &(*member)->next;
1797     }
1798     relation = relation->next;
1799     }
1800     }
1801    
1802     /* remove the given way from all relations */
1803     void osm_way_remove_from_relation(osm_t *osm, way_t *way) {
1804     relation_t *relation = osm->relation;
1805 harbaum 161 printf("removing way #" ITEM_ID_FORMAT " from all relations:\n", way->id);
1806 harbaum 1
1807     while(relation) {
1808     member_t **member = &relation->member;
1809     while(*member) {
1810 harbaum 155 if(((*member)->object.type == WAY) &&
1811     ((*member)->object.way == way)) {
1812 harbaum 1
1813 harbaum 161 printf(" from relation #" ITEM_ID_FORMAT "\n", relation->id);
1814 harbaum 1
1815     member_t *cur = *member;
1816     *member = (*member)->next;
1817     osm_member_free(cur);
1818    
1819     relation->flags |= OSM_FLAG_DIRTY;
1820     } else
1821     member = &(*member)->next;
1822     }
1823     relation = relation->next;
1824     }
1825     }
1826    
1827 harbaum 73 relation_t *osm_relation_new(void) {
1828     printf("Creating new relation\n");
1829    
1830     relation_t *relation = g_new0(relation_t, 1);
1831 harbaum 158 relation->version = 1;
1832 harbaum 73 relation->visible = TRUE;
1833     relation->flags = OSM_FLAG_NEW;
1834     relation->time = time(NULL);
1835    
1836     return relation;
1837     }
1838    
1839     void osm_relation_attach(osm_t *osm, relation_t *relation) {
1840     printf("Attaching relation\n");
1841    
1842     relation->id = osm_new_relation_id(osm);
1843     relation->flags = OSM_FLAG_NEW;
1844    
1845     /* attach to end of relation list */
1846     relation_t **lrelation = &osm->relation;
1847     while(*lrelation) lrelation = &(*lrelation)->next;
1848     *lrelation = relation;
1849     }
1850    
1851    
1852 harbaum 1 void osm_way_delete(osm_t *osm, icon_t **icon,
1853     way_t *way, gboolean permanently) {
1854    
1855     /* new ways aren't stored on the server and are just deleted permanently */
1856     if(way->flags & OSM_FLAG_NEW) {
1857 harbaum 161 printf("About to delete NEW way #" ITEM_ID_FORMAT
1858     " -> force permanent delete\n", way->id);
1859 harbaum 1 permanently = TRUE;
1860     }
1861    
1862     /* delete all nodes that aren't in other use now */
1863     node_chain_t **chain = &way->node_chain;
1864     while(*chain) {
1865    
1866     (*chain)->node->ways--;
1867 harbaum 161 printf("checking node #" ITEM_ID_FORMAT " (still used by %d)\n",
1868 harbaum 1 (*chain)->node->id, (*chain)->node->ways);
1869    
1870     /* this node must only be part of this way */
1871     if(!(*chain)->node->ways) {
1872     /* delete this node, but don't let this actually affect the */
1873     /* associated ways as the only such way is the oen we are currently */
1874     /* deleting */
1875     way_chain_t *way_chain =
1876     osm_node_delete(osm, icon, (*chain)->node, FALSE, FALSE);
1877     g_assert(way_chain);
1878     while(way_chain) {
1879     way_chain_t *way_next = way_chain->next;
1880     g_assert(way_chain->way == way);
1881     g_free(way_chain);
1882     way_chain = way_next;
1883     }
1884     }
1885    
1886     node_chain_t *cur = (*chain);
1887     *chain = cur->next;
1888     g_free(cur);
1889     }
1890     way->node_chain = NULL;
1891    
1892     if(!permanently) {
1893 harbaum 161 printf("mark way #" ITEM_ID_FORMAT " as deleted\n", way->id);
1894 harbaum 1 way->flags |= OSM_FLAG_DELETED;
1895     } else {
1896 harbaum 161 printf("permanently delete way #" ITEM_ID_FORMAT "\n", way->id);
1897 harbaum 1
1898     /* remove it from the chain */
1899     way_t **cway = &osm->way;
1900     int found = 0;
1901    
1902     while(*cway) {
1903     if(*cway == way) {
1904     found++;
1905     *cway = (*cway)->next;
1906    
1907     osm_way_free(way);
1908     } else
1909     cway = &((*cway)->next);
1910     }
1911     g_assert(found == 1);
1912     }
1913     }
1914    
1915 harbaum 75 void osm_relation_delete(osm_t *osm, relation_t *relation,
1916     gboolean permanently) {
1917    
1918     /* new relations aren't stored on the server and are just */
1919     /* deleted permanently */
1920     if(relation->flags & OSM_FLAG_NEW) {
1921 harbaum 161 printf("About to delete NEW relation #" ITEM_ID_FORMAT
1922     " -> force permanent delete\n", relation->id);
1923 harbaum 75 permanently = TRUE;
1924     }
1925    
1926     /* the deletion of a relation doesn't affect the members as they */
1927     /* don't have any reference to the relation they are part of */
1928    
1929     if(!permanently) {
1930 harbaum 161 printf("mark relation #" ITEM_ID_FORMAT " as deleted\n", relation->id);
1931 harbaum 75 relation->flags |= OSM_FLAG_DELETED;
1932     } else {
1933 harbaum 161 printf("permanently delete relation #" ITEM_ID_FORMAT "\n", relation->id);
1934 harbaum 75
1935     /* remove it from the chain */
1936     relation_t **crelation = &osm->relation;
1937     int found = 0;
1938    
1939     while(*crelation) {
1940     if(*crelation == relation) {
1941     found++;
1942     *crelation = (*crelation)->next;
1943    
1944     osm_relation_free(relation);
1945     } else
1946     crelation = &((*crelation)->next);
1947     }
1948     g_assert(found == 1);
1949     }
1950     }
1951    
1952 achadwick 98 void osm_way_reverse(way_t *way) {
1953 harbaum 1 node_chain_t *new = NULL;
1954    
1955     /* walk old chain first to last */
1956     node_chain_t *old = way->node_chain;
1957     while(old) {
1958     node_chain_t *next = old->next;
1959    
1960     /* and prepend each node to the new chain */
1961     old->next = new;
1962     new = old;
1963    
1964     old = next;
1965     }
1966    
1967     way->node_chain = new;
1968     }
1969    
1970 achadwick 98 static const char *DS_ONEWAY_FWD = "yes";
1971     static const char *DS_ONEWAY_REV = "-1";
1972     static const char *DS_LEFT_SUFFIX = ":left";
1973     static const char *DS_RIGHT_SUFFIX = ":right";
1974    
1975     /* Reverse direction-sensitive tags like "oneway". Marks the way as dirty if
1976     * anything is changed, and returns the number of flipped tags. */
1977    
1978     guint
1979     osm_way_reverse_direction_sensitive_tags (way_t *way) {
1980     tag_t *tag = way->tag;
1981     guint n_tags_altered = 0;
1982     while (tag != NULL) {
1983     char *lc_key = g_ascii_strdown(tag->key, -1);
1984     char *lc_value = g_ascii_strdown(tag->value, -1);
1985    
1986     if (strcmp(lc_key, "oneway") == 0) {
1987     // oneway={yes/true/1/-1} is unusual.
1988     // Favour "yes" and "-1".
1989     if ((strcmp(lc_value, DS_ONEWAY_FWD) == 0) ||
1990     (strcmp(lc_value, "true") == 0) ||
1991     (strcmp(lc_value, "1") == 0)) {
1992     g_free(tag->value);
1993     tag->value = g_strdup(DS_ONEWAY_REV);
1994     n_tags_altered++;
1995     }
1996     else if (strcmp(lc_value, DS_ONEWAY_REV) == 0) {
1997     g_free(tag->value);
1998     tag->value = g_strdup(DS_ONEWAY_FWD);
1999     n_tags_altered++;
2000     }
2001     else {
2002     printf("warning: unknown tag: %s=%s\n", tag->key, tag->value);
2003     }
2004     }
2005    
2006     // :left and :right suffixes
2007     else if (g_str_has_suffix(lc_key, DS_LEFT_SUFFIX)) {
2008     char *key_old = tag->key;
2009     char *lastcolon = rindex(key_old, ':');
2010     g_assert(lastcolon != NULL);
2011     *lastcolon = '\000';
2012     tag->key = g_strconcat(key_old, DS_RIGHT_SUFFIX, NULL);
2013     *lastcolon = ':';
2014     g_free(key_old);
2015     n_tags_altered++;
2016     }
2017     else if (g_str_has_suffix(lc_key, DS_RIGHT_SUFFIX)) {
2018     char *key_old = tag->key;
2019     char *lastcolon = rindex(key_old, ':');
2020     g_assert(lastcolon != NULL);
2021     *lastcolon = '\000';
2022     tag->key = g_strconcat(key_old, DS_LEFT_SUFFIX, NULL);
2023     *lastcolon = ':';
2024     g_free(key_old);
2025     n_tags_altered++;
2026     }
2027    
2028     g_free(lc_key);
2029     g_free(lc_value);
2030     tag = tag->next;
2031     }
2032     if (n_tags_altered > 0) {
2033     way->flags |= OSM_FLAG_DIRTY;
2034     }
2035     return n_tags_altered;
2036     }
2037    
2038     /* Reverse a way's role within relations where the role is direction-sensitive.
2039     * Returns the number of roles flipped, and marks any relations changed as
2040     * dirty. */
2041    
2042     static const char *DS_ROUTE_FORWARD = "forward";
2043     static const char *DS_ROUTE_REVERSE = "reverse";
2044    
2045     guint
2046     osm_way_reverse_direction_sensitive_roles(osm_t *osm, way_t *way) {
2047     relation_chain_t *rel_chain0, *rel_chain;
2048     rel_chain0 = rel_chain = osm_way_to_relation(osm, way);
2049     guint n_roles_flipped = 0;
2050    
2051     for (; rel_chain != NULL; rel_chain = rel_chain->next) {
2052     char *type = osm_tag_get_by_key(rel_chain->relation->tag, "type");
2053    
2054     // Route relations; http://wiki.openstreetmap.org/wiki/Relation:route
2055     if (strcasecmp(type, "route") == 0) {
2056    
2057     // First find the member corresponding to our way:
2058     member_t *member = rel_chain->relation->member;
2059     for (; member != NULL; member = member->next) {
2060 harbaum 155 if (member->object.type == WAY) {
2061     if (member->object.way == way)
2062 achadwick 98 break;
2063     }
2064 harbaum 155 if (member->object.type == WAY_ID) {
2065     if (member->object.id == way->id)
2066 achadwick 98 break;
2067     }
2068     }
2069     g_assert(member); // osm_way_to_relation() broken?
2070    
2071     // Then flip its role if it's one of the direction-sensitive ones
2072 achadwick 141 if (member->role == NULL) {
2073 achadwick 142 printf("null role in route relation -> ignore\n");
2074 achadwick 141 }
2075     else if (strcasecmp(member->role, DS_ROUTE_FORWARD) == 0) {
2076 achadwick 98 g_free(member->role);
2077     member->role = g_strdup(DS_ROUTE_REVERSE);
2078     rel_chain->relation->flags |= OSM_FLAG_DIRTY;
2079     ++n_roles_flipped;
2080     }
2081     else if (strcasecmp(member->role, DS_ROUTE_REVERSE) == 0) {
2082     g_free(member->role);
2083     member->role = g_strdup(DS_ROUTE_FORWARD);
2084     rel_chain->relation->flags |= OSM_FLAG_DIRTY;
2085     ++n_roles_flipped;
2086     }
2087    
2088     // TODO: what about numbered stops? Guess we ignore them; there's no
2089     // consensus about whether they should be placed on the way or to one side
2090     // of it.
2091    
2092     }//if-route
2093    
2094    
2095     }
2096     if (rel_chain0) {
2097     g_free(rel_chain0);
2098     }
2099     return n_roles_flipped;
2100     }
2101    
2102 harbaum 1 node_t *osm_way_get_first_node(way_t *way) {
2103     node_chain_t *chain = way->node_chain;
2104     if(!chain) return NULL;
2105     return chain->node;
2106     }
2107    
2108     node_t *osm_way_get_last_node(way_t *way) {
2109     node_chain_t *chain = way->node_chain;
2110    
2111     while(chain && chain->next) chain=chain->next;
2112    
2113     if(!chain) return NULL;
2114    
2115     return chain->node;
2116     }
2117    
2118     void osm_way_rotate(way_t *way, gint offset) {
2119     if(!offset) return;
2120    
2121     /* needs at least two nodes to work properly */
2122     g_assert(way->node_chain);
2123     g_assert(way->node_chain->next);
2124    
2125     while(offset--) {
2126     node_chain_t *chain = way->node_chain;
2127     chain->node->ways--; // reduce way count of old start/end node
2128    
2129     /* move all nodes ahead one chain element ... */
2130     while(chain->next) {
2131     chain->node = chain->next->node;
2132     chain = chain->next;
2133     }
2134    
2135     /* ... and make last one same as first one */
2136     chain->node = way->node_chain->node;
2137     chain->node->ways++; // increase way count of new start/end node
2138     }
2139     }
2140    
2141     tag_t *osm_tags_copy(tag_t *src_tag, gboolean update_creator) {
2142     tag_t *new_tags = NULL;
2143     tag_t **dst_tag = &new_tags;
2144    
2145     while(src_tag) {
2146     *dst_tag = g_new0(tag_t, 1);
2147     (*dst_tag)->key = g_strdup(src_tag->key);
2148     if(update_creator && (strcasecmp(src_tag->key, "created_by") == 0))
2149     (*dst_tag)->value = g_strdup(PACKAGE " v" VERSION);
2150     else
2151     (*dst_tag)->value = g_strdup(src_tag->value);
2152    
2153     dst_tag = &(*dst_tag)->next;
2154     src_tag = src_tag->next;
2155     }
2156    
2157     return new_tags;
2158     }
2159 harbaum 54
2160     /* return plain text of type */
2161 harbaum 155 char *osm_object_type_string(object_t *object) {
2162 harbaum 54 const struct { type_t type; char *name; } types[] = {
2163     { ILLEGAL, "illegal" },
2164     { NODE, "node" },
2165     { WAY, "way" },
2166     { RELATION, "relation" },
2167     { NODE_ID, "node id" },
2168     { WAY_ID, "way id" },
2169     { RELATION_ID, "relation id" },
2170     { 0, NULL }
2171     };
2172    
2173     int i;
2174     for(i=0;types[i].name;i++)
2175 harbaum 155 if(object->type == types[i].type)
2176 harbaum 54 return types[i].name;
2177    
2178     return NULL;
2179     }
2180    
2181 harbaum 155 char *osm_object_string(object_t *object) {
2182     char *type_str = osm_object_type_string(object);
2183 harbaum 54
2184 harbaum 64 if(!object)
2185     return g_strdup_printf("%s #<invalid>", type_str);
2186    
2187 harbaum 155 switch(object->type) {
2188 harbaum 64 case ILLEGAL:
2189     return g_strdup_printf("%s #<unspec>", type_str);
2190     break;
2191     case NODE:
2192 harbaum 161 return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->node->id);
2193 harbaum 64 break;
2194     case WAY:
2195 harbaum 161 return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->way->id);
2196 harbaum 64 break;
2197     case RELATION:
2198 harbaum 161 return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str,
2199     object->relation->id);
2200 harbaum 64 break;
2201     case NODE_ID:
2202     case WAY_ID:
2203     case RELATION_ID:
2204 harbaum 161 return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->id);
2205 harbaum 64 break;
2206     }
2207     return NULL;
2208     }
2209    
2210 harbaum 155 char *osm_object_id_string(object_t *object) {
2211 harbaum 76 if(!object) return NULL;
2212    
2213 harbaum 155 switch(object->type) {
2214 harbaum 76 case ILLEGAL:
2215     return NULL;
2216     break;
2217     case NODE:
2218 harbaum 161 return g_strdup_printf("#"ITEM_ID_FORMAT, object->node->id);
2219 harbaum 76 break;
2220     case WAY:
2221 harbaum 161 return g_strdup_printf("#"ITEM_ID_FORMAT, object->way->id);
2222 harbaum 76 break;
2223     case RELATION:
2224 harbaum 161 return g_strdup_printf("#"ITEM_ID_FORMAT, object->relation->id);
2225 harbaum 76 break;
2226     case NODE_ID:
2227     case WAY_ID:
2228     case RELATION_ID:
2229 harbaum 161 return g_strdup_printf("#"ITEM_ID_FORMAT, object->id);
2230 harbaum 76 break;
2231     }
2232     return NULL;
2233     }
2234    
2235 harbaum 155 tag_t *osm_object_get_tags(object_t *object) {
2236 harbaum 76 if(!object) return NULL;
2237    
2238 harbaum 155 switch(object->type) {
2239 harbaum 76 case ILLEGAL:
2240     return NULL;
2241     break;
2242     case NODE:
2243 harbaum 155 return object->node->tag;
2244 harbaum 76 break;
2245     case WAY:
2246 harbaum 155 return object->way->tag;
2247 harbaum 76 break;
2248     case RELATION:
2249 harbaum 155 return object->relation->tag;
2250 harbaum 76 break;
2251     case NODE_ID:
2252     case WAY_ID:
2253     case RELATION_ID:
2254     return NULL;
2255     break;
2256     }
2257     return NULL;
2258     }
2259    
2260    
2261     gint osm_relation_members_num(relation_t *relation) {
2262     gint num = 0;
2263     member_t *member = relation->member;
2264     while(member) {
2265     num++;
2266     member = member->next;
2267     }
2268     return num;
2269     }
2270    
2271 harbaum 153 void osm_object_set_flags(object_t *object, int set, int clr) {
2272    
2273     switch(object->type) {
2274     case NODE:
2275     object->node->flags |= set;
2276     object->node->flags &= ~clr;
2277     break;
2278    
2279     case WAY:
2280     object->way->flags |= set;
2281     object->way->flags &= ~clr;
2282     break;
2283    
2284     case RELATION:
2285     object->relation->flags |= set;
2286     object->relation->flags &= ~clr;
2287     break;
2288    
2289     default:
2290     g_assert(0);
2291     break;
2292     }
2293     }
2294    
2295 achadwick 28 // vim:et:ts=8:sw=2:sts=2:ai