Diff of /trunk/src/osm.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 40 by harbaum, Sun Jan 18 19:43:20 2009 UTC revision 171 by harbaum, Tue Apr 28 10:29:52 2009 UTC
# Line 17  Line 17 
17   * along with OSM2Go.  If not, see <http://www.gnu.org/licenses/>.   * along with OSM2Go.  If not, see <http://www.gnu.org/licenses/>.
18   */   */
19    
 /* xml parsing has a performance issue */  
 // #define OSM_DOM_PARSER  
 // #define OSM_STREAM_PARSER  
 #define OSM_QND_XML_PARSER  
   
20  #include <stdio.h>  #include <stdio.h>
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <string.h>  #include <string.h>
# Line 40  Line 35 
35  #error "Tree not enabled in libxml"  #error "Tree not enabled in libxml"
36  #endif  #endif
37    
 /* determine where a node/way/relation read from the osm file */  
 /* is inserted into the internal database */  
 // #define OSM_SORT_ID  
 #define OSM_SORT_LAST  
 // #define OSM_SORT_FIRST  
   
38  /* ------------------------- bounds handling --------------------- */  /* ------------------------- bounds handling --------------------- */
39    
40  static void osm_bounds_free(bounds_t *bounds) {  static void osm_bounds_free(bounds_t *bounds) {
# Line 58  static void osm_bounds_dump(bounds_t *bo Line 47  static void osm_bounds_dump(bounds_t *bo
47           bounds->ll_min.lon, bounds->ll_max.lon);           bounds->ll_min.lon, bounds->ll_max.lon);
48  }  }
49    
 #ifdef OSM_DOM_PARSER  
 static bounds_t *osm_parse_osm_bounds(osm_t *osm,  
                      xmlDocPtr doc, xmlNode *a_node) {  
   char *prop;  
   
   if(osm->bounds) {  
     errorf(NULL, "Doubly defined bounds");  
     return NULL;  
   }  
   
   bounds_t *bounds = g_new0(bounds_t, 1);  
   
   bounds->ll_min.lat = bounds->ll_min.lon = NAN;  
   bounds->ll_max.lat = bounds->ll_max.lon = NAN;  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"minlat"))) {  
     bounds->ll_min.lat = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"maxlat"))) {  
     bounds->ll_max.lat = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"minlon"))) {  
     bounds->ll_min.lon = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"maxlon"))) {  
     bounds->ll_max.lon = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if(isnan(bounds->ll_min.lat) || isnan(bounds->ll_min.lon) ||  
      isnan(bounds->ll_max.lat) || isnan(bounds->ll_max.lon)) {  
     errorf(NULL, "Invalid coordinate in bounds (%f/%f/%f/%f)",  
            bounds->ll_min.lat, bounds->ll_min.lon,  
            bounds->ll_max.lat, bounds->ll_max.lon);  
   
     osm_bounds_free(bounds);  
     return NULL;  
   }  
   
   
   /* calculate map zone which will be used as a reference for all */  
   /* drawing/projection later on */  
   pos_t center = { (bounds->ll_max.lat + bounds->ll_min.lat)/2,  
                    (bounds->ll_max.lon + bounds->ll_min.lon)/2 };  
   
   pos2lpos_center(&center, &bounds->center);  
   
   /* the scale is needed to accomodate for "streching" */  
   /* by the mercartor projection */  
   bounds->scale = cos(DEG2RAD(center.lat));  
   
   pos2lpos_center(&bounds->ll_min, &bounds->min);  
   bounds->min.x -= bounds->center.x;  
   bounds->min.y -= bounds->center.y;  
   bounds->min.x *= bounds->scale;  
   bounds->min.y *= bounds->scale;  
   
   pos2lpos_center(&bounds->ll_max, &bounds->max);  
   bounds->max.x -= bounds->center.x;  
   bounds->max.y -= bounds->center.y;  
   bounds->max.x *= bounds->scale;  
   bounds->max.y *= bounds->scale;  
   
   return bounds;  
 }  
 #endif  
   
50  /* ------------------------- user handling --------------------- */  /* ------------------------- user handling --------------------- */
51    
52  void osm_users_free(user_t *user) {  void osm_users_free(user_t *user) {
# Line 311  void osm_node_dump(node_t *node) { Line 227  void osm_node_dump(node_t *node) {
227    char buf[64];    char buf[64];
228    struct tm tm;    struct tm tm;
229    
230    printf("Id:      %lu\n", node->id);    printf("Id:      "ITEM_ID_FORMAT"\n", node->id);
231    printf("User:    %s\n", node->user?node->user->name:"<unspecified>");    printf("User:    %s\n", node->user?node->user->name:"<unspecified>");
232    printf("Visible: %s\n", node->visible?"yes":"no");    printf("Visible: %s\n", node->visible?"yes":"no");
233    
# Line 330  void osm_nodes_dump(node_t *node) { Line 246  void osm_nodes_dump(node_t *node) {
246    }    }
247  }  }
248    
 #ifdef OSM_DOM_PARSER  
 static node_t *osm_parse_osm_node(osm_t *osm,  
                           xmlDocPtr doc, xmlNode *a_node) {  
   xmlNode *cur_node = NULL;  
   
   /* allocate a new node structure */  
   node_t *node = g_new0(node_t, 1);  
   node->pos.lat = node->pos.lon = NAN;  
   
   char *prop;  
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"id"))) {  
     node->id = strtoul(prop, NULL, 10);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"lat"))) {  
     node->pos.lat = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"lon"))) {  
     node->pos.lon = g_ascii_strtod(prop, NULL);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"user"))) {  
     node->user = osm_user(osm, prop);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"visible"))) {  
     node->visible = (strcasecmp(prop, "true") == 0);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"timestamp"))) {  
     node->time = convert_iso8601(prop);  
     xmlFree(prop);  
   }  
   
   /* scan for tags and attach a list of tags */  
   tag_t **tag = &node->tag;  
   for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {  
     if (cur_node->type == XML_ELEMENT_NODE) {  
       if(strcasecmp((char*)cur_node->name, "tag") == 0) {  
         /* attach tag to node */  
         *tag = osm_parse_osm_tag(osm, doc, cur_node);  
         if(*tag) tag = &((*tag)->next);  
       } else  
         printf("found unhandled osm/node/%s\n", cur_node->name);  
     }  
   }  
   
   pos2lpos(osm->bounds, &node->pos, &node->lpos);  
   
   return node;  
 }  
 #endif  
   
249  /* ------------------- way handling ------------------- */  /* ------------------- way handling ------------------- */
250    
251  void osm_node_chain_free(node_chain_t *node_chain) {  void osm_node_chain_free(node_chain_t *node_chain) {
# Line 403  void osm_node_chain_free(node_chain_t *n Line 260  void osm_node_chain_free(node_chain_t *n
260  }  }
261    
262  void osm_way_free(way_t *way) {  void osm_way_free(way_t *way) {
263    //  printf("freeing way #%ld\n", way->id);    //  printf("freeing way #" ITEM_ID_FORMAT "\n", way->id);
264    
265    osm_node_chain_free(way->node_chain);    osm_node_chain_free(way->node_chain);
266    osm_tags_free(way->tag);    osm_tags_free(way->tag);
# Line 448  void osm_way_dump(way_t *way) { Line 305  void osm_way_dump(way_t *way) {
305    char buf[64];    char buf[64];
306    struct tm tm;    struct tm tm;
307    
308    printf("Id:      %lu\n", way->id);    printf("Id:      "ITEM_ID_FORMAT"\n", way->id);
309    printf("User:    %s\n", way->user?way->user->name:"<unspecified>");    printf("User:    %s\n", way->user?way->user->name:"<unspecified>");
310    printf("Visible: %s\n", way->visible?"yes":"no");    printf("Visible: %s\n", way->visible?"yes":"no");
311    node_chain_t *node_chain = way->node_chain;    node_chain_t *node_chain = way->node_chain;
312    while(node_chain) {    while(node_chain) {
313      printf("  Node:  %lu\n", node_chain->node->id);      printf("  Node:  "ITEM_ID_FORMAT"\n", node_chain->node->id);
314      node_chain = node_chain->next;      node_chain = node_chain->next;
315    }    }
316    
# Line 481  node_chain_t *osm_parse_osm_way_nd(osm_t Line 338  node_chain_t *osm_parse_osm_way_nd(osm_t
338      node_chain_t *node_chain = g_new0(node_chain_t, 1);      node_chain_t *node_chain = g_new0(node_chain_t, 1);
339    
340      /* search matching node */      /* search matching node */
341      node_chain->node = osm->node;      node_chain->node = osm_get_node_by_id(osm, id);
342      while(node_chain->node && node_chain->node->id != id)      if(!node_chain->node) printf("Node id " ITEM_ID_FORMAT " not found\n", id);
343        node_chain->node = node_chain->node->next;      else                  node_chain->node->ways++;
   
     if(!node_chain->node) printf("Node id %lu not found\n", id);  
   
     if(node_chain->node)  
       node_chain->node->ways++;  
344    
345      xmlFree(prop);      xmlFree(prop);
346    
# Line 498  node_chain_t *osm_parse_osm_way_nd(osm_t Line 350  node_chain_t *osm_parse_osm_way_nd(osm_t
350    return NULL;    return NULL;
351  }  }
352    
 #ifdef OSM_DOM_PARSER  
 static way_t *osm_parse_osm_way(osm_t *osm,  
                           xmlDocPtr doc, xmlNode *a_node) {  
   xmlNode *cur_node = NULL;  
   
   /* allocate a new way structure */  
   way_t *way = g_new0(way_t, 1);  
   
   char *prop;  
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"id"))) {  
     way->id = strtoul(prop, NULL, 10);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"user"))) {  
     way->user = osm_user(osm, prop);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"visible"))) {  
     way->visible = (strcasecmp(prop, "true") == 0);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"timestamp"))) {  
     way->time = convert_iso8601(prop);  
     xmlFree(prop);  
   }  
   
   /* scan for tags/nodes and attach their lists */  
   tag_t **tag = &way->tag;  
   node_chain_t **node_chain = &way->node_chain;  
   
   for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {  
     if (cur_node->type == XML_ELEMENT_NODE) {  
       if(strcasecmp((char*)cur_node->name, "tag") == 0) {  
         /* attach tag to node */  
         *tag = osm_parse_osm_tag(osm, doc, cur_node);  
         if(*tag) tag = &((*tag)->next);  
       } else if(strcasecmp((char*)cur_node->name, "nd") == 0) {  
         *node_chain = osm_parse_osm_way_nd(osm, doc, cur_node);  
         if(*node_chain)  
           node_chain = &((*node_chain)->next);  
       } else  
         printf("found unhandled osm/node/%s\n", cur_node->name);  
     }  
   }  
   
   return way;  
 }  
 #endif  
   
353  /* ------------------- relation handling ------------------- */  /* ------------------- relation handling ------------------- */
354    
355  void osm_member_free(member_t *member) {  void osm_member_free(member_t *member) {
# Line 565  void osm_members_free(member_t *member) Line 365  void osm_members_free(member_t *member)
365    }    }
366  }  }
367    
368    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  static void osm_relations_free(relation_t *relation) {  static void osm_relations_free(relation_t *relation) {
376    while(relation) {    while(relation) {
377      relation_t *next = relation->next;      relation_t *next = relation->next;
378        osm_relation_free(relation);
     osm_tags_free(relation->tag);  
     osm_members_free(relation->member);  
   
     g_free(relation);  
379      relation = next;      relation = next;
380    }    }
381  }  }
# Line 583  void osm_relations_dump(relation_t *rela Line 386  void osm_relations_dump(relation_t *rela
386      char buf[64];      char buf[64];
387      struct tm tm;      struct tm tm;
388    
389      printf("Id:      %lu\n", relation->id);      printf("Id:      "ITEM_ID_FORMAT"\n", relation->id);
390      printf("User:    %s\n",      printf("User:    %s\n",
391             relation->user?relation->user->name:"<unspecified>");             relation->user?relation->user->name:"<unspecified>");
392      printf("Visible: %s\n", relation->visible?"yes":"no");      printf("Visible: %s\n", relation->visible?"yes":"no");
393    
394      member_t *member = relation->member;      member_t *member = relation->member;
395      while(member) {      while(member) {
396        switch(member->type) {        switch(member->object.type) {
397        case ILLEGAL:        case ILLEGAL:
398        case NODE_ID:        case NODE_ID:
399        case WAY_ID:        case WAY_ID:
# Line 598  void osm_relations_dump(relation_t *rela Line 401  void osm_relations_dump(relation_t *rela
401          break;          break;
402    
403        case NODE:        case NODE:
404          if(member->node)          if(member->object.node)
405            printf(" Member: Node, id = %lu, role = %s\n",            printf(" Member: Node, id = " ITEM_ID_FORMAT ", role = %s\n",
406                   member->node->id, member->role);                   member->object.node->id, member->role);
407          break;          break;
408    
409        case WAY:        case WAY:
410          if(member->way)          if(member->object.way)
411          printf(" Member: Way, id = %lu, role = %s\n",            printf(" Member: Way, id = " ITEM_ID_FORMAT ", role = %s\n",
412                 member->way->id, member->role);                   member->object.way->id, member->role);
413          break;          break;
414    
415        case RELATION:        case RELATION:
416          if(member->relation)          if(member->object.relation)
417          printf(" Member: Relation, id = %lu, role = %s\n",            printf(" Member: Relation, id = " ITEM_ID_FORMAT ", role = %s\n",
418                 member->relation->id, member->role);                   member->object.relation->id, member->role);
419          break;          break;
420        }        }
421    
# Line 633  member_t *osm_parse_osm_relation_member( Line 436  member_t *osm_parse_osm_relation_member(
436                            xmlDocPtr doc, xmlNode *a_node) {                            xmlDocPtr doc, xmlNode *a_node) {
437    char *prop;    char *prop;
438    member_t *member = g_new0(member_t, 1);    member_t *member = g_new0(member_t, 1);
439    member->type = ILLEGAL;    member->object.type = ILLEGAL;
440    
441    if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"type"))) {    if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"type"))) {
442      if(strcasecmp(prop, "way") == 0)           member->type = WAY;      if(strcasecmp(prop, "way") == 0)           member->object.type = WAY;
443      else if(strcasecmp(prop, "node") == 0)     member->type = NODE;      else if(strcasecmp(prop, "node") == 0)     member->object.type = NODE;
444      else if(strcasecmp(prop, "relation") == 0) member->type = RELATION;      else if(strcasecmp(prop, "relation") == 0) member->object.type = RELATION;
445      xmlFree(prop);      xmlFree(prop);
446    }    }
447    
448    if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"ref"))) {    if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"ref"))) {
449      item_id_t id = strtoul(prop, NULL, 10);      item_id_t id = strtoul(prop, NULL, 10);
450    
451      switch(member->type) {      switch(member->object.type) {
452      case ILLEGAL:      case ILLEGAL:
453        printf("Unable to store illegal type\n");        printf("Unable to store illegal type\n");
454        break;        break;
455    
456      case WAY:      case WAY:
457        /* search matching way */        /* search matching way */
458        member->way = osm->way;        member->object.way = osm_get_way_by_id(osm, id);
459        while(member->way && member->way->id != id)        if(!member->object.way) {
460          member->way = member->way->next;          member->object.type = WAY_ID;
461            member->object.id = id;
       if(!member->way) {  
         member->type = WAY_ID;  
         member->id = id;  
462        }        }
463        break;        break;
464    
465      case NODE:      case NODE:
466        /* search matching node */        /* search matching node */
467        member->node = osm->node;        member->object.node = osm_get_node_by_id(osm, id);
468        while(member->node && member->node->id != id)        if(!member->object.node) {
469          member->node = member->node->next;          member->object.type = NODE_ID;
470            member->object.id = id;
       if(!member->node) {  
         member->type = NODE_ID;  
         member->id = id;  
471        }        }
472        break;        break;
473    
474      case RELATION:      case RELATION:
475        /* search matching relation */        /* search matching relation */
476        member->relation = osm->relation;        member->object.relation = osm_get_relation_by_id(osm, id);
477        while(member->relation && member->relation->id != id)        if(!member->object.relation) {
478          member->relation = member->relation->next;          member->object.type = NODE_ID;
479            member->object.id = id;
       if(!member->relation) {  
         member->type = NODE_ID;  
         member->id = id;  
480        }        }
481        break;        break;
482    
# Line 703  member_t *osm_parse_osm_relation_member( Line 497  member_t *osm_parse_osm_relation_member(
497    return member;    return member;
498  }  }
499    
500  #ifdef OSM_DOM_PARSER  /* ------------------ osm handling ----------------- */
 static relation_t *osm_parse_osm_relation(osm_t *osm,  
                           xmlDocPtr doc, xmlNode *a_node) {  
   xmlNode *cur_node = NULL;  
   
   /* allocate a new relation structure */  
   relation_t *relation = g_new0(relation_t, 1);  
   
   char *prop;  
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"id"))) {  
     relation->id = strtoul(prop, NULL, 10);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"user"))) {  
     relation->user = osm_user(osm, prop);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"visible"))) {  
     relation->visible = (strcasecmp(prop, "true") == 0);  
     xmlFree(prop);  
   }  
   
   if((prop = (char*)xmlGetProp(a_node, (unsigned char*)"timestamp"))) {  
     relation->time = convert_iso8601(prop);  
     xmlFree(prop);  
   }  
   
   /* scan for tags and attach a list of tags */  
   tag_t **tag = &relation->tag;  
   member_t **member = &relation->member;  
   
   for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {  
     if (cur_node->type == XML_ELEMENT_NODE) {  
       if(strcasecmp((char*)cur_node->name, "tag") == 0) {  
         /* attach tag to node */  
         *tag = osm_parse_osm_tag(osm, doc, cur_node);  
         if(*tag) tag = &((*tag)->next);  
       } else if(strcasecmp((char*)cur_node->name, "member") == 0) {  
         *member = osm_parse_osm_relation_member(osm, doc, cur_node);  
         if(*member) member = &((*member)->next);  
       } else  
         printf("found unhandled osm/node/%s\n", cur_node->name);  
     }  
   }  
   
   return relation;  
 }  
   
 /* ----------------------- generic xml handling -------------------------- */  
   
 /* parse loc entry */  
 static void osm_parse_osm(osm_t *osm, xmlDocPtr doc, xmlNode * a_node) {  
   xmlNode *cur_node = NULL;  
   
   for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {  
     if (cur_node->type == XML_ELEMENT_NODE) {  
       if(strcasecmp((char*)cur_node->name, "bounds") == 0)  
         osm->bounds = osm_parse_osm_bounds(osm, doc, cur_node);  
       else if(strcasecmp((char*)cur_node->name, "node") == 0) {  
         /* parse node and attach it to chain */  
         node_t *new = osm_parse_osm_node(osm, doc, cur_node);  
         if(new) {  
           node_t **node = &osm->node;  
   
 #ifdef OSM_SORT_ID  
           /* search chain of nodes */  
           while(*node && ((*node)->id < new->id))  
             node = &(*node)->next;  
 #endif  
   
 #ifdef OSM_SORT_LAST  
           while(*node) node = &(*node)->next;  
 #endif  
   
           /* insert into chain */  
           new->next = *node;  
           *node = new;  
         }  
       } else if(strcasecmp((char*)cur_node->name, "way") == 0) {  
         /* parse way and attach it to chain */  
         way_t *new = osm_parse_osm_way(osm, doc, cur_node);  
         if(new) {  
           way_t **way = &osm->way;  
   
 #ifdef OSM_SORT_ID  
           /* insert into chain */  
           while(*way && ((*way)->id < new->id))  
             way = &(*way)->next;  
 #endif  
   
 #ifdef OSM_SORT_LAST  
           while(*way) way = &(*way)->next;  
 #endif  
   
           /* insert into chain */  
           new->next = *way;  
           *way = new;  
         }  
       } else if(strcasecmp((char*)cur_node->name, "relation") == 0) {  
         /* parse relation and attach it to chain */  
         relation_t *new = osm_parse_osm_relation(osm, doc, cur_node);  
         if(new) {  
           relation_t **relation = &osm->relation;  
   
 #ifdef OSM_SORT_ID  
           /* search chain of ways */  
           while(*relation && ((*relation)->id < new->id))  
             relation = &(*relation)->next;  
 #endif  
   
 #ifdef OSM_SORT_LAST  
           while(*relation) relation = &(*relation)->next;  
 #endif  
   
           /* insert into chain */  
           new->next = *relation;  
           *relation = new;  
         }  
       } else  
         printf("found unhandled osm/%s\n", cur_node->name);  
   
     }  
   }  
 }  
   
 /* parse root element and search for "osm" */  
 static osm_t *osm_parse_root(xmlDocPtr doc, xmlNode * a_node) {  
   osm_t *osm;  
   xmlNode *cur_node = NULL;  
   
   /* allocate memory to hold osm file description */  
   osm = g_new0(osm_t, 1);  
501    
502    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {  /* the two hash tables eat over 512kBytes memory and may thus be */
503      if (cur_node->type == XML_ELEMENT_NODE) {  /* freed at any time. osm2go can work without them (albeit slower) */
504        /* parse osm osm file ... */  static void hash_table_free(hash_table_t *table) {
505        if(strcasecmp((char*)cur_node->name, "osm") == 0)    if(!table) return;
506          osm_parse_osm(osm, doc, cur_node);  
507        else    int i;
508          printf("found unhandled %s\n", cur_node->name);    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    }    }
   
   return osm;  
516  }  }
517    
518  static osm_t *osm_parse_doc(xmlDocPtr doc) {  void osm_hash_tables_free(osm_t *osm) {
519    osm_t *osm;    hash_table_free(osm->node_hash);
520      osm->node_hash = NULL;
521    /* Get the root element node */    hash_table_free(osm->way_hash);
522    xmlNode *root_element = xmlDocGetRootElement(doc);    osm->way_hash = NULL;
   
   osm = osm_parse_root(doc, root_element);  
   
   /*free the document */  
   xmlFreeDoc(doc);  
   
   /*  
    * Free the global variables that may  
    * have been allocated by the parser.  
    */  
   xmlCleanupParser();  
   
   return osm;  
523  }  }
 #endif  
   
 /* ------------------ osm handling ----------------- */  
524    
525  void osm_free(icon_t **icon, osm_t *osm) {  void osm_free(icon_t **icon, osm_t *osm) {
526    if(!osm) return;    if(!osm) return;
527    
528      osm_hash_tables_free(osm);
529    
530    if(osm->bounds)   osm_bounds_free(osm->bounds);    if(osm->bounds)   osm_bounds_free(osm->bounds);
531    if(osm->user)     osm_users_free(osm->user);    if(osm->user)     osm_users_free(osm->user);
532    if(osm->way)      osm_ways_free(osm->way);    if(osm->way)      osm_ways_free(osm->way);
# Line 893  void osm_dump(osm_t *osm) { Line 543  void osm_dump(osm_t *osm) {
543    osm_relations_dump(osm->relation);    osm_relations_dump(osm->relation);
544  }  }
545    
546  #ifdef OSM_STREAM_PARSER  /* -------------------------- stream parser ------------------- */
 /* -------------------------- stream parser tests ------------------- */  
547    
548  #include <libxml/xmlreader.h>  #include <libxml/xmlreader.h>
549    
# Line 1030  static node_t *process_node(xmlTextReade Line 679  static node_t *process_node(xmlTextReade
679      xmlFree(prop);      xmlFree(prop);
680    }    }
681    
682      /* 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    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "lat"))) {    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "lat"))) {
689      node->pos.lat = g_ascii_strtod(prop, NULL);      node->pos.lat = g_ascii_strtod(prop, NULL);
690      xmlFree(prop);      xmlFree(prop);
# Line 1057  static node_t *process_node(xmlTextReade Line 712  static node_t *process_node(xmlTextReade
712    
713    pos2lpos(osm->bounds, &node->pos, &node->lpos);    pos2lpos(osm->bounds, &node->pos, &node->lpos);
714    
715      /* 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    /* just an empty element? then return the node as it is */    /* just an empty element? then return the node as it is */
725    if(xmlTextReaderIsEmptyElement(reader))    if(xmlTextReaderIsEmptyElement(reader))
726      return node;      return node;
# Line 1094  static node_chain_t *process_nd(xmlTextR Line 758  static node_chain_t *process_nd(xmlTextR
758      node_chain_t *node_chain = g_new0(node_chain_t, 1);      node_chain_t *node_chain = g_new0(node_chain_t, 1);
759    
760      /* search matching node */      /* search matching node */
761      node_chain->node = osm->node;      node_chain->node = osm_get_node_by_id(osm, id);
762      while(node_chain->node && node_chain->node->id != id)      if(!node_chain->node) printf("Node id " ITEM_ID_FORMAT " not found\n", id);
763        node_chain->node = node_chain->node->next;      else                  node_chain->node->ways++;
   
     if(!node_chain->node) printf("Node id %lu not found\n", id);  
   
     if(node_chain->node)  
       node_chain->node->ways++;  
764    
765      xmlFree(prop);      xmlFree(prop);
766    
# Line 1123  static way_t *process_way(xmlTextReaderP Line 782  static way_t *process_way(xmlTextReaderP
782      xmlFree(prop);      xmlFree(prop);
783    }    }
784    
785      /* 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    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {
792      way->user = osm_user(osm, prop);      way->user = osm_user(osm, prop);
793      xmlFree(prop);      xmlFree(prop);
# Line 1138  static way_t *process_way(xmlTextReaderP Line 803  static way_t *process_way(xmlTextReaderP
803      xmlFree(prop);      xmlFree(prop);
804    }    }
805    
806      /* 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    /* just an empty element? then return the way as it is */    /* 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) */    /* (this should in fact never happen as this would be a way without nodes) */
817    if(xmlTextReaderIsEmptyElement(reader))    if(xmlTextReaderIsEmptyElement(reader))
# Line 1174  static way_t *process_way(xmlTextReaderP Line 848  static way_t *process_way(xmlTextReaderP
848  static member_t *process_member(xmlTextReaderPtr reader, osm_t *osm) {  static member_t *process_member(xmlTextReaderPtr reader, osm_t *osm) {
849    char *prop;    char *prop;
850    member_t *member = g_new0(member_t, 1);    member_t *member = g_new0(member_t, 1);
851    member->type = ILLEGAL;    member->object.type = ILLEGAL;
852    
853    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "type"))) {    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "type"))) {
854      if(strcasecmp(prop, "way") == 0)           member->type = WAY;      if(strcasecmp(prop, "way") == 0)           member->object.type = WAY;
855      else if(strcasecmp(prop, "node") == 0)     member->type = NODE;      else if(strcasecmp(prop, "node") == 0)     member->object.type = NODE;
856      else if(strcasecmp(prop, "relation") == 0) member->type = RELATION;      else if(strcasecmp(prop, "relation") == 0) member->object.type = RELATION;
857      xmlFree(prop);      xmlFree(prop);
858    }    }
859    
860    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) {    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) {
861      item_id_t id = strtoul(prop, NULL, 10);      item_id_t id = strtoul(prop, NULL, 10);
862    
863      switch(member->type) {      switch(member->object.type) {
864      case ILLEGAL:      case ILLEGAL:
865        printf("Unable to store illegal type\n");        printf("Unable to store illegal type\n");
866        break;        break;
867    
868      case WAY:      case WAY:
869        /* search matching way */        /* search matching way */
870        member->way = osm->way;        member->object.way = osm_get_way_by_id(osm, id);
871        while(member->way && member->way->id != id)        if(!member->object.way) {
872          member->way = member->way->next;          member->object.type = WAY_ID;
873            member->object.id = id;
       if(!member->way) {  
         member->type = WAY_ID;  
         member->id = id;  
874        }        }
875        break;        break;
876    
877      case NODE:      case NODE:
878        /* search matching node */        /* search matching node */
879        member->node = osm->node;        member->object.node = osm_get_node_by_id(osm, id);
880        while(member->node && member->node->id != id)        if(!member->object.node) {
881          member->node = member->node->next;          member->object.type = NODE_ID;
882            member->object.id = id;
       if(!member->node) {  
         member->type = NODE_ID;  
         member->id = id;  
883        }        }
884        break;        break;
885    
886      case RELATION:      case RELATION:
887        /* search matching relation */        /* search matching relation */
888        member->relation = osm->relation;        member->object.relation = osm_get_relation_by_id(osm, id);
889        while(member->relation && member->relation->id != id)        if(!member->object.relation) {
890          member->relation = member->relation->next;          member->object.type = NODE_ID;
891            member->object.id = id;
       if(!member->relation) {  
         member->type = NODE_ID;  
         member->id = id;  
892        }        }
893        break;        break;
894    
# Line 1254  static relation_t *process_relation(xmlT Line 919  static relation_t *process_relation(xmlT
919      xmlFree(prop);      xmlFree(prop);
920    }    }
921    
922      /* 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    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {    if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "user"))) {
929      relation->user = osm_user(osm, prop);      relation->user = osm_user(osm, prop);
930      xmlFree(prop);      xmlFree(prop);
# Line 1306  static relation_t *process_relation(xmlT Line 977  static relation_t *process_relation(xmlT
977  static osm_t *process_osm(xmlTextReaderPtr reader) {  static osm_t *process_osm(xmlTextReaderPtr reader) {
978    /* alloc osm structure */    /* alloc osm structure */
979    osm_t *osm = g_new0(osm_t, 1);    osm_t *osm = g_new0(osm_t, 1);
980      osm->node_hash = g_new0(hash_table_t, 1);
981      osm->way_hash = g_new0(hash_table_t, 1);
982    
983    node_t **node = &osm->node;    node_t **node = &osm->node;
984    way_t **way = &osm->way;    way_t **way = &osm->way;
# Line 1388  static osm_t *process_file(const char *f Line 1061  static osm_t *process_file(const char *f
1061    return osm;    return osm;
1062  }  }
1063    
1064  /* ----------------------- end of stream parser tests ------------------- */  /* ----------------------- end of stream parser ------------------- */
 #endif  
1065    
1066  #ifdef OSM_QND_XML_PARSER  #include <sys/time.h>
 /* -------------------------- qnd-xml parser tests ------------------- */  
1067    
1068  #ifdef USE_FLOAT  osm_t *osm_parse(char *filename) {
 #define GET_PROP_POS(a,b,c) qnd_xml_get_prop_float(a, b, c)  
 #else  
 #define GET_PROP_POS(a,b,c) qnd_xml_get_prop_double(a, b, c)  
 #endif  
1069    
1070  gboolean osm_bounds_cb(qnd_xml_stack_t *stack,    struct timeval start;
1071                         qnd_xml_attribute_t *attributes, gpointer data) {    gettimeofday(&start, NULL);
1072    
1073    /* get parent pointer */    LIBXML_TEST_VERSION;
   osm_t *osm = (osm_t*)stack->prev->userdata[0];  
1074    
1075    if(osm->bounds) {    // use stream parser
1076      errorf(NULL, "Doubly defined bounds");    osm_t *osm = process_file(filename);
1077      return FALSE;    xmlCleanupParser();
   }  
1078    
1079    bounds_t *bounds = osm->bounds = g_new0(bounds_t, 1);    struct timeval end;
1080      gettimeofday(&end, NULL);
   bounds->ll_min.lat = bounds->ll_min.lon = NAN;  
   bounds->ll_max.lat = bounds->ll_max.lon = NAN;  
   
   GET_PROP_POS(attributes, "minlat", &bounds->ll_min.lat);  
   GET_PROP_POS(attributes, "minlon", &bounds->ll_min.lon);  
   GET_PROP_POS(attributes, "maxlat", &bounds->ll_max.lat);  
   GET_PROP_POS(attributes, "maxlon", &bounds->ll_max.lon);  
   
   if(isnan(bounds->ll_min.lat) || isnan(bounds->ll_min.lon) ||  
      isnan(bounds->ll_max.lat) || isnan(bounds->ll_max.lon)) {  
     errorf(NULL, "Invalid coordinate in bounds (%f/%f/%f/%f)",  
            bounds->ll_min.lat, bounds->ll_min.lon,  
            bounds->ll_max.lat, bounds->ll_max.lon);  
   
     osm_bounds_free(bounds);  
     osm->bounds = NULL;  
     return FALSE;  
   }  
   
   
   /* calculate map zone which will be used as a reference for all */  
   /* drawing/projection later on */  
   pos_t center = { (bounds->ll_max.lat + bounds->ll_min.lat)/2,  
                    (bounds->ll_max.lon + bounds->ll_min.lon)/2 };  
   
   pos2lpos_center(&center, &bounds->center);  
   
   /* the scale is needed to accomodate for "streching" */  
   /* by the mercartor projection */  
   bounds->scale = cos(DEG2RAD(center.lat));  
   
   pos2lpos_center(&bounds->ll_min, &bounds->min);  
   bounds->min.x -= bounds->center.x;  
   bounds->min.y -= bounds->center.y;  
   bounds->min.x *= bounds->scale;  
   bounds->min.y *= bounds->scale;  
   
   pos2lpos_center(&bounds->ll_max, &bounds->max);  
   bounds->max.x -= bounds->center.x;  
   bounds->max.y -= bounds->center.y;  
   bounds->max.x *= bounds->scale;  
   bounds->max.y *= bounds->scale;  
   
   return TRUE;  
 }  
   
 static gboolean osm_tag_cb(qnd_xml_stack_t *stack,  
                          qnd_xml_attribute_t *attributes, gpointer data) {  
   
   tag_t *tag = *(tag_t**)stack->prev->userdata[1] = g_new0(tag_t, 1);  
   
   tag->key = qnd_xml_get_prop_str(attributes, "k");  
   tag->value = qnd_xml_get_prop_str(attributes, "v");  
   
   if(!tag->key || !tag->value) {  
     printf("incomplete tag key/value %s/%s\n", tag->key, tag->value);  
     osm_tags_free(tag);  
     tag = NULL;  
   } else  
     stack->prev->userdata[1] = &tag->next;  
   
   return TRUE;  
 }  
   
 static gboolean osm_node_cb(qnd_xml_stack_t *stack,  
                      qnd_xml_attribute_t *attributes, gpointer data) {  
   
   osm_t *osm = (osm_t*)stack->prev->userdata[0];  
   
   /* allocate a new node structure. userdata[1] points to the current */  
   /* position a new node is to be stored */  
   node_t *node = *(node_t**)stack->prev->userdata[1] =  
     stack->userdata[0] = g_new0(node_t, 1);  
   stack->prev->userdata[1] = &node->next;  
   
   qnd_xml_get_prop_gulong(attributes, "id", &node->id);  
   GET_PROP_POS(attributes, "lat", &node->pos.lat);  
   GET_PROP_POS(attributes, "lon", &node->pos.lon);  
   node->user = osm_user(osm, qnd_xml_get_prop(attributes, "user"));  
   node->visible = qnd_xml_get_prop_is(attributes, "visible", "true");  
   node->time = convert_iso8601(qnd_xml_get_prop(attributes, "timestamp"));  
   
   pos2lpos(osm->bounds, &node->pos, &node->lpos);  
   
   /* store current tag pointer in userdata for fast access to current tag */  
   stack->userdata[1] = &node->tag;  
   
   return TRUE;  
 }  
   
 static gboolean osm_way_nd_cb(qnd_xml_stack_t *stack,  
                          qnd_xml_attribute_t *attributes, gpointer data) {  
   
   osm_t *osm = (osm_t*)stack->prev->prev->userdata[0];  
   
   item_id_t id;  
   if(qnd_xml_get_prop_gulong(attributes, "ref", &id)) {  
     /* allocate a new node_chain structure */  
     node_chain_t *node_chain = *(node_chain_t**)stack->prev->userdata[2] =  
       g_new0(node_chain_t, 1);  
   
     /* search matching node */  
     node_chain->node = osm->node;  
     while(node_chain->node && node_chain->node->id != id)  
       node_chain->node = node_chain->node->next;  
   
     if(!node_chain->node) printf("Node id %lu not found\n", id);  
   
     if(node_chain->node)  
       node_chain->node->ways++;  
   
     stack->prev->userdata[2] = &node_chain->next;  
   }  
   
   return TRUE;  
 }  
   
 gboolean osm_way_cb(qnd_xml_stack_t *stack,  
                     qnd_xml_attribute_t *attributes, gpointer data) {  
   
   osm_t *osm = (osm_t*)stack->prev->userdata[0];  
   
   /* allocate a new way structure. userdata[2] points to the current */  
   /* position a new way is to be stored in the way list */  
   way_t *way = *(way_t**)stack->prev->userdata[2] =  
     stack->userdata[0] = g_new0(way_t, 1);  
   stack->prev->userdata[2] = &way->next;  
   
   qnd_xml_get_prop_gulong(attributes, "id", &way->id);  
   way->user = osm_user(osm, qnd_xml_get_prop(attributes, "user"));  
   way->visible = qnd_xml_get_prop_is(attributes, "visible", "true");  
   way->time = convert_iso8601(qnd_xml_get_prop(attributes, "timestamp"));  
   
   /* store current tag and node_chain pointers in userdata for fast */  
   /* access to current tag/node_chain entry */  
   stack->userdata[1] = &way->tag;  
   stack->userdata[2] = &way->node_chain;  
   
   return TRUE;  
 }  
   
 static gboolean osm_rel_member_cb(qnd_xml_stack_t *stack,  
                          qnd_xml_attribute_t *attributes, gpointer data) {  
   
   osm_t *osm = (osm_t*)stack->prev->prev->userdata[0];  
   
   member_t *member = *(member_t**)stack->prev->userdata[2] =  
     g_new0(member_t, 1);  
   stack->prev->userdata[2] = &member->next;  
   member->type = ILLEGAL;  
   
   char *type = qnd_xml_get_prop(attributes, "type");  
   if(type) {  
     if(strcasecmp(type, "way") == 0)           member->type = WAY;  
     else if(strcasecmp(type, "node") == 0)     member->type = NODE;  
     else if(strcasecmp(type, "relation") == 0) member->type = RELATION;  
   }  
   
   item_id_t id;  
   if(qnd_xml_get_prop_gulong(attributes, "ref", &id)) {  
     switch(member->type) {  
     case ILLEGAL:  
       printf("Unable to store illegal type\n");  
       break;  
   
     case WAY:  
       /* search matching way */  
       member->way = osm->way;  
       while(member->way && member->way->id != id)  
         member->way = member->way->next;  
   
       if(!member->way) {  
         member->type = WAY_ID;  
         member->id = id;  
       }  
       break;  
   
     case NODE:  
       /* search matching node */  
       member->node = osm->node;  
       while(member->node && member->node->id != id)  
         member->node = member->node->next;  
   
       if(!member->node) {  
         member->type = NODE_ID;  
         member->id = id;  
       }  
       break;  
   
     case RELATION:  
       /* search matching relation */  
       member->relation = osm->relation;  
       while(member->relation && member->relation->id != id)  
         member->relation = member->relation->next;  
   
       if(!member->relation) {  
         member->type = NODE_ID;  
         member->id = id;  
       }  
       break;  
   
     case WAY_ID:  
     case NODE_ID:  
     case RELATION_ID:  
       break;  
     }  
   }  
   
   return TRUE;  
 }  
   
 gboolean osm_rel_cb(qnd_xml_stack_t *stack,  
                     qnd_xml_attribute_t *attributes, gpointer data) {  
   
   osm_t *osm = (osm_t*)stack->prev->userdata[0];  
   
   /* allocate a new relation structure. userdata[3] points to the current */  
   /* position a new relation is to be stored at in the relation list */  
   relation_t *relation = *(relation_t**)stack->prev->userdata[3] =  
     stack->userdata[0] = g_new0(relation_t, 1);  
   stack->prev->userdata[3] = &relation->next;  
   
   qnd_xml_get_prop_gulong(attributes, "id", &relation->id);  
   relation->user = osm_user(osm, qnd_xml_get_prop(attributes, "user"));  
   relation->visible = qnd_xml_get_prop_is(attributes, "visible", "true");  
   relation->time = convert_iso8601(qnd_xml_get_prop(attributes, "timestamp"));  
   
   /* store current tag and member pointers in userdata for fast access */  
   /* to current tag and members in their chains */  
   stack->userdata[1] = &relation->tag;  
   stack->userdata[2] = &relation->member;  
   
   return TRUE;  
 }  
   
 gboolean osm_cb(qnd_xml_stack_t *stack,  
                 qnd_xml_attribute_t *attributes, gpointer data) {  
   
   g_assert(!stack->userdata[0]);  
   
   /* also set parents (roots) userdata as it's the parsers return value */  
   osm_t *osm = stack->prev->userdata[0] =  
     stack->userdata[0] = g_new0(osm_t, 1);  
   
   /* store direct pointers for faster list access */  
   /* (otherwise we'd have to search the end of the lists for every item */  
   /* to be attached) */  
   stack->userdata[1] = &osm->node;  
   stack->userdata[2] = &osm->way;  
   stack->userdata[3] = &osm->relation;  
   
   return TRUE;  
 }  
   
   
 /* these structures describe the content qnd_xml expects while parsing */  
 qnd_xml_entry_t osm_node_tag = { "tag", osm_tag_cb, QND_XML_LEAF };  
   
 qnd_xml_entry_t osm_way_tag = { "tag", osm_tag_cb, QND_XML_LEAF };  
 qnd_xml_entry_t osm_way_nd = { "nd", osm_way_nd_cb, QND_XML_LEAF };  
   
 qnd_xml_entry_t osm_rel_tag = { "tag", osm_tag_cb, QND_XML_LEAF };  
 qnd_xml_entry_t osm_rel_member = { "member", osm_rel_member_cb, QND_XML_LEAF };  
   
 qnd_xml_entry_t osm_bounds = { "bounds", osm_bounds_cb, QND_XML_LEAF };  
   
 qnd_xml_entry_t *node_children[] = { &osm_node_tag },  
   osm_node = { "node", osm_node_cb, QND_XML_CHILDREN(node_children) };  
   
 qnd_xml_entry_t *way_children[] = { &osm_way_tag, &osm_way_nd },  
   osm_way = { "way", osm_way_cb, QND_XML_CHILDREN(way_children) };  
   
 qnd_xml_entry_t *rel_children[] = { &osm_rel_tag, &osm_rel_member },  
   osm_rel = { "rel", osm_rel_cb, QND_XML_CHILDREN(rel_children) };  
   
 /* the osm element */  
 qnd_xml_entry_t *osm_children[] = {  
   &osm_bounds, &osm_node, &osm_way, &osm_rel };  
 qnd_xml_entry_t osm = { "osm", osm_cb, QND_XML_CHILDREN(osm_children) };  
   
 /* the root element */  
 qnd_xml_entry_t *root_children[] = { &osm };  
 qnd_xml_entry_t root = { "<root>", NULL, QND_XML_CHILDREN(root_children) };  
   
 // gcc `pkg-config --cflags --libs glib-2.0` -o qnd_xml qnd_xml.c  
   
   
   
 /* ----------------------- end of qnd-xml parser tests ------------------- */  
 #endif  
   
   
 #include <sys/time.h>  
   
 osm_t *osm_parse(char *filename) {  
   
   struct timeval start;  
   gettimeofday(&start, NULL);  
   
   
 #ifdef OSM_STREAM_PARSER  
   LIBXML_TEST_VERSION;  
   
   // use stream parser  
   osm_t *osm = process_file(filename);  
   xmlCleanupParser();  
 #endif  
   
 #ifdef OSM_DOM_PARSER  
   LIBXML_TEST_VERSION;  
   
   // parse into a tree  
   /* parse the file and get the DOM */  
   xmlDoc *doc = NULL;  
   if ((doc = xmlReadFile(filename, NULL, 0)) == NULL) {  
     xmlErrorPtr errP = xmlGetLastError();  
     errorf(NULL, "While parsing \"%s\":\n\n%s", filename, errP->message);  
     return NULL;  
   }  
   
   osm_t *osm = osm_parse_doc(doc);  
 #endif  
   
 #ifdef OSM_QND_XML_PARSER  
   osm_t *osm = NULL;  
   if(!(osm = qnd_xml_parse(filename, &root, NULL))) {  
     errorf(NULL, "While parsing \"%s\"", filename);  
     return NULL;  
   }  
 #endif  
   
   struct timeval end;  
   gettimeofday(&end, NULL);  
1081    
1082    printf("total parse time: %ldms\n",    printf("total parse time: %ldms\n",
1083           (end.tv_usec - start.tv_usec)/1000 +           (end.tv_usec - start.tv_usec)/1000 +
# Line 1836  gboolean osm_node_has_value(node_t *node Line 1168  gboolean osm_node_has_value(node_t *node
1168  gboolean osm_node_has_tag(node_t *node) {  gboolean osm_node_has_tag(node_t *node) {
1169    tag_t *tag = node->tag;    tag_t *tag = node->tag;
1170    
1171      /* created_by tags don't count as real tags */
1172    if(tag && strcasecmp(tag->key, "created_by") == 0)    if(tag && strcasecmp(tag->key, "created_by") == 0)
1173      tag = tag->next;      tag = tag->next;
1174    
# Line 1858  static void osm_generate_tags(tag_t *tag Line 1191  static void osm_generate_tags(tag_t *tag
1191    while(tag) {    while(tag) {
1192      /* make sure "created_by" tag contains our id */      /* make sure "created_by" tag contains our id */
1193      if(strcasecmp(tag->key, "created_by") == 0) {      if(strcasecmp(tag->key, "created_by") == 0) {
1194        g_free(tag->value);        if(strcasecmp(tag->value, PACKAGE " v" VERSION) != 0) {
1195        tag->value = g_strdup(PACKAGE " v" VERSION);          g_free(tag->value);
1196            tag->value = g_strdup(PACKAGE " v" VERSION);
1197          }
1198      }      }
1199    
1200      xmlNodePtr tag_node = xmlNewChild(node, NULL, BAD_CAST "tag", NULL);      xmlNodePtr tag_node = xmlNewChild(node, NULL, BAD_CAST "tag", NULL);
# Line 1870  static void osm_generate_tags(tag_t *tag Line 1205  static void osm_generate_tags(tag_t *tag
1205  }  }
1206    
1207  /* build xml representation for a way */  /* build xml representation for a way */
1208  char *osm_generate_xml(osm_t *osm, type_t type, void *item) {  static char *osm_generate_xml(osm_t *osm, item_id_t changeset,
1209                           type_t type, void *item) {
1210    char str[32];    char str[32];
1211    xmlChar *result = NULL;    xmlChar *result = NULL;
1212    int len = 0;    int len = 0;
# Line 1879  char *osm_generate_xml(osm_t *osm, type_ Line 1215  char *osm_generate_xml(osm_t *osm, type_
1215    
1216    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
1217    xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "osm");    xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "osm");
   xmlNewProp(root_node, BAD_CAST "version", BAD_CAST "0.5");  
   xmlNewProp(root_node, BAD_CAST "generator", BAD_CAST PACKAGE " V" VERSION);  
1218    xmlDocSetRootElement(doc, root_node);    xmlDocSetRootElement(doc, root_node);
1219    
1220    switch(type) {    switch(type) {
# Line 1894  char *osm_generate_xml(osm_t *osm, type_ Line 1228  char *osm_generate_xml(osm_t *osm, type_
1228          snprintf(str, sizeof(str), "%u", (unsigned)node->id);          snprintf(str, sizeof(str), "%u", (unsigned)node->id);
1229          xmlNewProp(node_node, BAD_CAST "id", BAD_CAST str);          xmlNewProp(node_node, BAD_CAST "id", BAD_CAST str);
1230        }        }
1231        g_ascii_dtostr(str, sizeof(str), node->pos.lat);        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          g_ascii_formatd(str, sizeof(str), LL_FORMAT, node->pos.lat);
1236        xmlNewProp(node_node, BAD_CAST "lat", BAD_CAST str);        xmlNewProp(node_node, BAD_CAST "lat", BAD_CAST str);
1237        g_ascii_dtostr(str, sizeof(str), node->pos.lon);        g_ascii_formatd(str, sizeof(str), LL_FORMAT, node->pos.lon);
1238        xmlNewProp(node_node, BAD_CAST "lon", BAD_CAST str);        xmlNewProp(node_node, BAD_CAST "lon", BAD_CAST str);
1239        osm_generate_tags(node->tag, node_node);        osm_generate_tags(node->tag, node_node);
1240      }      }
# Line 1908  char *osm_generate_xml(osm_t *osm, type_ Line 1246  char *osm_generate_xml(osm_t *osm, type_
1246        xmlNodePtr way_node = xmlNewChild(root_node, NULL, BAD_CAST "way", NULL);        xmlNodePtr way_node = xmlNewChild(root_node, NULL, BAD_CAST "way", NULL);
1247        snprintf(str, sizeof(str), "%u", (unsigned)way->id);        snprintf(str, sizeof(str), "%u", (unsigned)way->id);
1248        xmlNewProp(way_node, BAD_CAST "id", BAD_CAST str);        xmlNewProp(way_node, BAD_CAST "id", BAD_CAST str);
1249          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    
1254        node_chain_t *node_chain = way->node_chain;        node_chain_t *node_chain = way->node_chain;
1255        while(node_chain) {        while(node_chain) {
1256          xmlNodePtr nd_node = xmlNewChild(way_node, NULL, BAD_CAST "nd", NULL);          xmlNodePtr nd_node = xmlNewChild(way_node, NULL, BAD_CAST "nd", NULL);
1257          char *str = g_strdup_printf("%ld", node_chain->node->id);          char *str = g_strdup_printf(ITEM_ID_FORMAT, node_chain->node->id);
1258          xmlNewProp(nd_node, BAD_CAST "ref", BAD_CAST str);          xmlNewProp(nd_node, BAD_CAST "ref", BAD_CAST str);
1259          g_free(str);          g_free(str);
1260          node_chain = node_chain->next;          node_chain = node_chain->next;
# Line 1929  char *osm_generate_xml(osm_t *osm, type_ Line 1271  char *osm_generate_xml(osm_t *osm, type_
1271                                          BAD_CAST "relation", NULL);                                          BAD_CAST "relation", NULL);
1272        snprintf(str, sizeof(str), "%u", (unsigned)relation->id);        snprintf(str, sizeof(str), "%u", (unsigned)relation->id);
1273        xmlNewProp(rel_node, BAD_CAST "id", BAD_CAST str);        xmlNewProp(rel_node, BAD_CAST "id", BAD_CAST str);
1274          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    
1279        member_t *member = relation->member;        member_t *member = relation->member;
1280        while(member) {        while(member) {
1281          xmlNodePtr m_node = xmlNewChild(rel_node,NULL,BAD_CAST "member", NULL);          xmlNodePtr m_node = xmlNewChild(rel_node,NULL,BAD_CAST "member", NULL);
1282          char *str = NULL;          char *str = NULL;
1283    
1284          switch(member->type) {          switch(member->object.type) {
1285          case NODE:          case NODE:
1286            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "node");            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "node");
1287            str = g_strdup_printf("%ld", member->node->id);            str = g_strdup_printf(ITEM_ID_FORMAT, member->object.node->id);
1288            break;            break;
1289    
1290          case WAY:          case WAY:
1291            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "way");            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "way");
1292            str = g_strdup_printf("%ld", member->way->id);            str = g_strdup_printf(ITEM_ID_FORMAT, member->object.way->id);
1293            break;            break;
1294    
1295          case RELATION:          case RELATION:
1296            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "relation");            xmlNewProp(m_node, BAD_CAST "type", BAD_CAST "relation");
1297            str = g_strdup_printf("%ld", member->relation->id);            str = g_strdup_printf(ITEM_ID_FORMAT, member->object.relation->id);
1298            break;            break;
1299    
1300          default:          default:
# Line 1988  char *osm_generate_xml(osm_t *osm, type_ Line 1334  char *osm_generate_xml(osm_t *osm, type_
1334  }  }
1335    
1336  /* build xml representation for a node */  /* build xml representation for a node */
1337  char *osm_generate_xml_node(osm_t *osm, node_t *node) {  char *osm_generate_xml_node(osm_t *osm, item_id_t changeset, node_t *node) {
1338    return osm_generate_xml(osm, NODE, node);    return osm_generate_xml(osm, changeset, NODE, node);
1339  }  }
1340    
1341  /* build xml representation for a way */  /* build xml representation for a way */
1342  char *osm_generate_xml_way(osm_t *osm, way_t *way) {  char *osm_generate_xml_way(osm_t *osm, item_id_t changeset, way_t *way) {
1343    return osm_generate_xml(osm, WAY, way);    return osm_generate_xml(osm, changeset, WAY, way);
1344  }  }
1345    
1346  /* build xml representation for a relation */  /* build xml representation for a relation */
1347  char *osm_generate_xml_relation(osm_t *osm, relation_t *relation) {  char *osm_generate_xml_relation(osm_t *osm, item_id_t changeset,
1348    return osm_generate_xml(osm, RELATION, relation);                                  relation_t *relation) {
1349      return osm_generate_xml(osm, changeset, RELATION, relation);
1350    }
1351    
1352    /* 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    /* the following three functions are eating much CPU power */
1384    /* as they search the objects lists. Hashing is supposed to help */
1385  node_t *osm_get_node_by_id(osm_t *osm, item_id_t id) {  node_t *osm_get_node_by_id(osm_t *osm, item_id_t id) {
1386      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    node_t *node = osm->node;    node_t *node = osm->node;
1399    while(node) {    while(node) {
1400      if(node->id == id)      if(node->id == id)
1401        return node;        return node;
1402    
1403      node = node->next;      node = node->next;
1404    }    }
1405    
1406    return NULL;    return NULL;
1407  }  }
1408    
1409  way_t *osm_get_way_by_id(osm_t *osm, item_id_t id) {  way_t *osm_get_way_by_id(osm_t *osm, item_id_t id) {
1410      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    way_t *way = osm->way;    way_t *way = osm->way;
1423    while(way) {    while(way) {
1424      if(way->id == id)      if(way->id == id)
1425        return way;        return way;
1426    
1427      way = way->next;      way = way->next;
1428    }    }
1429    
1430    return NULL;    return NULL;
1431  }  }
1432    
1433  relation_t *osm_get_relation_by_id(osm_t *osm, item_id_t id) {  relation_t *osm_get_relation_by_id(osm_t *osm, item_id_t id) {
1434      // use linear search
1435    relation_t *relation = osm->relation;    relation_t *relation = osm->relation;
1436    while(relation) {    while(relation) {
1437      if(relation->id == id)      if(relation->id == id)
# Line 2082  item_id_t osm_new_node_id(osm_t *osm) { Line 1489  item_id_t osm_new_node_id(osm_t *osm) {
1489    return 0;    return 0;
1490  }  }
1491    
1492    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  node_t *osm_node_new(osm_t *osm, gint x, gint y) {  node_t *osm_node_new(osm_t *osm, gint x, gint y) {
1515    printf("Creating new node\n");    printf("Creating new node\n");
1516    
1517    node_t *node = g_new0(node_t, 1);    node_t *node = g_new0(node_t, 1);
1518      node->version = 1;
1519    node->lpos.x = x;    node->lpos.x = x;
1520    node->lpos.y = y;    node->lpos.y = y;
1521    node->visible = TRUE;    node->visible = TRUE;
1522    node->time = time(NULL);    node->time = time(NULL);
1523    
   /* add created_by tag */  
   node->tag = g_new0(tag_t, 1);  
   node->tag->key = g_strdup("created_by");  
   node->tag->value = g_strdup(PACKAGE " v" VERSION);  
   
1524    /* convert screen position back to ll */    /* convert screen position back to ll */
1525    lpos2pos(osm->bounds, &node->lpos, &node->pos);    lpos2pos(osm->bounds, &node->lpos, &node->pos);
1526    
# Line 2118  void osm_node_attach(osm_t *osm, node_t Line 1543  void osm_node_attach(osm_t *osm, node_t
1543    *lnode = node;    *lnode = node;
1544  }  }
1545    
1546    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  way_t *osm_way_new(void) {  way_t *osm_way_new(void) {
1556    printf("Creating new way\n");    printf("Creating new way\n");
1557    
1558    way_t *way = g_new0(way_t, 1);    way_t *way = g_new0(way_t, 1);
1559      way->version = 1;
1560    way->visible = TRUE;    way->visible = TRUE;
1561    way->flags = OSM_FLAG_NEW;    way->flags = OSM_FLAG_NEW;
1562    way->time = time(NULL);    way->time = time(NULL);
1563    
   /* add created_by tag */  
   way->tag = g_new0(tag_t, 1);  
   way->tag->key = g_strdup("created_by");  
   way->tag->value = g_strdup(PACKAGE " v" VERSION);  
   
1564    return way;    return way;
1565  }  }
1566    
# Line 2154  way_chain_t *osm_node_delete(osm_t *osm, Line 1584  way_chain_t *osm_node_delete(osm_t *osm,
1584    
1585    /* new nodes aren't stored on the server and are just deleted permanently */    /* new nodes aren't stored on the server and are just deleted permanently */
1586    if(node->flags & OSM_FLAG_NEW) {    if(node->flags & OSM_FLAG_NEW) {
1587      printf("About to delete NEW node #%ld -> force permanent delete\n",      printf("About to delete NEW node #" ITEM_ID_FORMAT
1588             node->id);             " -> force permanent delete\n", node->id);
1589      permanently = TRUE;      permanently = TRUE;
1590    }    }
1591    
# Line 2191  way_chain_t *osm_node_delete(osm_t *osm, Line 1621  way_chain_t *osm_node_delete(osm_t *osm,
1621    }    }
1622    
1623    if(!permanently) {    if(!permanently) {
1624      printf("mark node #%ld as deleted\n", node->id);      printf("mark node #" ITEM_ID_FORMAT " as deleted\n", node->id);
1625      node->flags |= OSM_FLAG_DELETED;      node->flags |= OSM_FLAG_DELETED;
1626    } else {    } else {
1627      printf("permanently delete node #%ld\n", node->id);      printf("permanently delete node #" ITEM_ID_FORMAT "\n", node->id);
1628    
1629      /* remove it from the chain */      /* remove it from the chain */
1630      node_t **cnode = &osm->node;      node_t **cnode = &osm->node;
# Line 2235  relation_chain_t *osm_node_to_relation(o Line 1665  relation_chain_t *osm_node_to_relation(o
1665    
1666      member_t *member = relation->member;      member_t *member = relation->member;
1667      while(member) {      while(member) {
1668        switch(member->type) {        switch(member->object.type) {
1669        case NODE:        case NODE:
1670          /* nodes are checked directly */          /* nodes are checked directly */
1671          if(member->node == node)          if(member->object.node == node)
1672            is_member = TRUE;            is_member = TRUE;
1673          break;          break;
1674    
1675        case WAY: {        case WAY: {
1676          /* ways have to be checked for the nodes they consist of */          /* ways have to be checked for the nodes they consist of */
1677          node_chain_t *chain = member->way->node_chain;          node_chain_t *chain = member->object.way->node_chain;
1678          while(chain && !is_member) {          while(chain && !is_member) {
1679            if(chain->node == node)            if(chain->node == node)
1680              is_member = TRUE;              is_member = TRUE;
# Line 2282  relation_chain_t *osm_way_to_relation(os Line 1712  relation_chain_t *osm_way_to_relation(os
1712    
1713      member_t *member = relation->member;      member_t *member = relation->member;
1714      while(member) {      while(member) {
1715        switch(member->type) {        switch(member->object.type) {
1716        case WAY: {        case WAY: {
1717          /* ways can be check directly */          /* ways can be check directly */
1718          if(member->way == way)          if(member->object.way == way)
1719            is_member = TRUE;            is_member = TRUE;
1720        } break;        } break;
1721    
# Line 2347  gboolean osm_position_within_bounds(osm_ Line 1777  gboolean osm_position_within_bounds(osm_
1777  /* be deleted */  /* be deleted */
1778  void osm_node_remove_from_relation(osm_t *osm, node_t *node) {  void osm_node_remove_from_relation(osm_t *osm, node_t *node) {
1779    relation_t *relation = osm->relation;    relation_t *relation = osm->relation;
1780    printf("removing node #%ld from all relations:\n", node->id);    printf("removing node #" ITEM_ID_FORMAT " from all relations:\n", node->id);
1781    
1782    while(relation) {    while(relation) {
1783      member_t **member = &relation->member;      member_t **member = &relation->member;
1784      while(*member) {      while(*member) {
1785        if(((*member)->type == NODE) &&        if(((*member)->object.type == NODE) &&
1786           ((*member)->node == node)) {           ((*member)->object.node == node)) {
1787    
1788          printf("  from relation #%ld\n", relation->id);          printf("  from relation #" ITEM_ID_FORMAT "\n", relation->id);
1789    
1790          member_t *cur = *member;          member_t *cur = *member;
1791          *member = (*member)->next;          *member = (*member)->next;
# Line 2372  void osm_node_remove_from_relation(osm_t Line 1802  void osm_node_remove_from_relation(osm_t
1802  /* remove the given way from all relations */  /* remove the given way from all relations */
1803  void osm_way_remove_from_relation(osm_t *osm, way_t *way) {  void osm_way_remove_from_relation(osm_t *osm, way_t *way) {
1804    relation_t *relation = osm->relation;    relation_t *relation = osm->relation;
1805    printf("removing way #%ld from all relations:\n", way->id);    printf("removing way #" ITEM_ID_FORMAT " from all relations:\n", way->id);
1806    
1807    while(relation) {    while(relation) {
1808      member_t **member = &relation->member;      member_t **member = &relation->member;
1809      while(*member) {      while(*member) {
1810        if(((*member)->type == WAY) &&        if(((*member)->object.type == WAY) &&
1811           ((*member)->way == way)) {           ((*member)->object.way == way)) {
1812    
1813          printf("  from relation #%ld\n", relation->id);          printf("  from relation #" ITEM_ID_FORMAT "\n", relation->id);
1814    
1815          member_t *cur = *member;          member_t *cur = *member;
1816          *member = (*member)->next;          *member = (*member)->next;
# Line 2394  void osm_way_remove_from_relation(osm_t Line 1824  void osm_way_remove_from_relation(osm_t
1824    }    }
1825  }  }
1826    
1827    relation_t *osm_relation_new(void) {
1828      printf("Creating new relation\n");
1829    
1830      relation_t *relation = g_new0(relation_t, 1);
1831      relation->version = 1;
1832      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  void osm_way_delete(osm_t *osm, icon_t **icon,  void osm_way_delete(osm_t *osm, icon_t **icon,
1853                      way_t *way, gboolean permanently) {                      way_t *way, gboolean permanently) {
1854    
1855    /* new ways aren't stored on the server and are just deleted permanently */    /* new ways aren't stored on the server and are just deleted permanently */
1856    if(way->flags & OSM_FLAG_NEW) {    if(way->flags & OSM_FLAG_NEW) {
1857      printf("About to delete NEW way #%ld -> force permanent delete\n",      printf("About to delete NEW way #" ITEM_ID_FORMAT
1858             way->id);             " -> force permanent delete\n", way->id);
1859      permanently = TRUE;      permanently = TRUE;
1860    }    }
1861    
# Line 2409  void osm_way_delete(osm_t *osm, icon_t * Line 1864  void osm_way_delete(osm_t *osm, icon_t *
1864    while(*chain) {    while(*chain) {
1865    
1866      (*chain)->node->ways--;      (*chain)->node->ways--;
1867      printf("checking node #%ld (still used by %d)\n",      printf("checking node #" ITEM_ID_FORMAT " (still used by %d)\n",
1868             (*chain)->node->id, (*chain)->node->ways);             (*chain)->node->id, (*chain)->node->ways);
1869    
1870      /* this node must only be part of this way */      /* this node must only be part of this way */
# Line 2435  void osm_way_delete(osm_t *osm, icon_t * Line 1890  void osm_way_delete(osm_t *osm, icon_t *
1890    way->node_chain = NULL;    way->node_chain = NULL;
1891    
1892    if(!permanently) {    if(!permanently) {
1893      printf("mark way #%ld as deleted\n", way->id);      printf("mark way #" ITEM_ID_FORMAT " as deleted\n", way->id);
1894      way->flags |= OSM_FLAG_DELETED;      way->flags |= OSM_FLAG_DELETED;
1895    } else {    } else {
1896      printf("permanently delete way #%ld\n", way->id);      printf("permanently delete way #" ITEM_ID_FORMAT "\n", way->id);
1897    
1898      /* remove it from the chain */      /* remove it from the chain */
1899      way_t **cway = &osm->way;      way_t **cway = &osm->way;
# Line 2457  void osm_way_delete(osm_t *osm, icon_t * Line 1912  void osm_way_delete(osm_t *osm, icon_t *
1912    }    }
1913  }  }
1914    
1915  void osm_way_revert(way_t *way) {  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        printf("About to delete NEW relation #" ITEM_ID_FORMAT
1922               " -> force permanent delete\n", relation->id);
1923        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        printf("mark relation #" ITEM_ID_FORMAT " as deleted\n", relation->id);
1931        relation->flags |= OSM_FLAG_DELETED;
1932      } else {
1933        printf("permanently delete relation #" ITEM_ID_FORMAT "\n", relation->id);
1934    
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    void osm_way_reverse(way_t *way) {
1953    node_chain_t *new = NULL;    node_chain_t *new = NULL;
1954    
1955    /* walk old chain first to last */    /* walk old chain first to last */
# Line 2475  void osm_way_revert(way_t *way) { Line 1967  void osm_way_revert(way_t *way) {
1967    way->node_chain = new;    way->node_chain = new;
1968  }  }
1969    
1970    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            if (member->object.type == WAY) {
2061              if (member->object.way == way)
2062                break;
2063            }
2064            if (member->object.type == WAY_ID) {
2065              if (member->object.id == way->id)
2066                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          if (member->role == NULL) {
2073            printf("null role in route relation -> ignore\n");
2074          }
2075          else if (strcasecmp(member->role, DS_ROUTE_FORWARD) == 0) {
2076            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  node_t *osm_way_get_first_node(way_t *way) {  node_t *osm_way_get_first_node(way_t *way) {
2103    node_chain_t *chain = way->node_chain;    node_chain_t *chain = way->node_chain;
2104    if(!chain) return NULL;    if(!chain) return NULL;
# Line 2532  tag_t *osm_tags_copy(tag_t *src_tag, gbo Line 2156  tag_t *osm_tags_copy(tag_t *src_tag, gbo
2156    
2157    return new_tags;    return new_tags;
2158  }  }
2159    
2160    /* return plain text of type */
2161    char *osm_object_type_string(object_t *object) {
2162      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        if(object->type == types[i].type)
2176          return types[i].name;
2177    
2178      return NULL;
2179    }
2180    
2181    char *osm_object_string(object_t *object) {
2182      char *type_str = osm_object_type_string(object);
2183    
2184      if(!object)
2185        return g_strdup_printf("%s #<invalid>", type_str);
2186    
2187      switch(object->type) {
2188      case ILLEGAL:
2189        return g_strdup_printf("%s #<unspec>", type_str);
2190        break;
2191      case NODE:
2192        return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->node->id);
2193        break;
2194      case WAY:
2195        return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->way->id);
2196        break;
2197      case RELATION:
2198        return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str,
2199                               object->relation->id);
2200        break;
2201      case NODE_ID:
2202      case WAY_ID:
2203      case RELATION_ID:
2204        return g_strdup_printf("%s #" ITEM_ID_FORMAT, type_str, object->id);
2205        break;
2206      }
2207      return NULL;
2208    }
2209    
2210    char *osm_object_id_string(object_t *object) {
2211      if(!object) return NULL;
2212    
2213      switch(object->type) {
2214      case ILLEGAL:
2215        return NULL;
2216        break;
2217      case NODE:
2218        return g_strdup_printf("#"ITEM_ID_FORMAT, object->node->id);
2219        break;
2220      case WAY:
2221        return g_strdup_printf("#"ITEM_ID_FORMAT, object->way->id);
2222        break;
2223      case RELATION:
2224        return g_strdup_printf("#"ITEM_ID_FORMAT, object->relation->id);
2225        break;
2226      case NODE_ID:
2227      case WAY_ID:
2228      case RELATION_ID:
2229        return g_strdup_printf("#"ITEM_ID_FORMAT, object->id);
2230        break;
2231      }
2232      return NULL;
2233    }
2234    
2235    tag_t *osm_object_get_tags(object_t *object) {
2236      if(!object) return NULL;
2237    
2238      switch(object->type) {
2239      case ILLEGAL:
2240        return NULL;
2241        break;
2242      case NODE:
2243        return object->node->tag;
2244        break;
2245      case WAY:
2246        return object->way->tag;
2247        break;
2248      case RELATION:
2249        return object->relation->tag;
2250        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    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  // vim:et:ts=8:sw=2:sts=2:ai  // vim:et:ts=8:sw=2:sts=2:ai

Legend:
Removed from v.40  
changed lines
  Added in v.171