From 8a16913420688fc766e2f2782c1e91a570c998b8 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Wed, 18 Jul 2007 01:11:16 +0300 Subject: [PATCH] Use a list for ways instead of a hash table as we don't need to search for ways. Add more checking for null values. Optimize some. --- src/osm.c | 83 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/src/osm.c b/src/osm.c index b6b5bf1..4703187 100644 --- a/src/osm.c +++ b/src/osm.c @@ -37,7 +37,7 @@ struct _node_data { /* Node type */ typedef struct _node node; struct _node { - gint32 id; + guint32 id; gfloat lat; gfloat lon; node_data *data; @@ -46,9 +46,9 @@ struct _node { /* Segment */ typedef struct _segment segment; struct _segment { - gint32 id; - gint32 from; - gint32 to; + guint32 id; + guint32 from; + guint32 to; }; typedef struct _way_data way_data; @@ -63,7 +63,7 @@ struct _way_data { /* Segment/Way structure */ typedef struct _way way; struct _way { - gint32 id; + guint32 id; way_type_t type; guint16 ncnt; guint8 flags; @@ -165,7 +165,7 @@ tag_parent_t tag_parent=IS_NONE; static GHashTable *osm_nodes=NULL; static GHashTable *osm_segments=NULL; -static GHashTable *osm_ways=NULL; +static GSList *osm_ways=NULL; static GSList *osm_poi=NULL; static node *cnode=NULL; @@ -311,6 +311,7 @@ if (n->data->name) if (n->data->desc) sqlite3_bind_text(sql.insert_poi, 5, n->data->desc, -1, SQLITE_TRANSIENT); #endif + sqlite3_bind_int(sql.insert_poi, 6, n->data->type); sqlite3_bind_int(sql.insert_poi, 7, n->data->type/100); @@ -340,6 +341,7 @@ sqlite3_bind_int(sql.insert_way_seg, 3, n->id); sqlite3_step(sql.insert_way_seg); sqlite3_reset(sql.insert_way_seg); sqlite3_clear_bindings(sql.insert_way_seg); +/* XXX: make this sooner ? */ db_update_node_links(n); wsegcnt++; @@ -386,6 +388,16 @@ db_insert_way_segments(segment *s, way *w) node *f; node *t; +if (!s) { + g_printf("ERROR: null segment!\n"); + return; +} + +if (!w) { + g_printf("ERROR: null way!\n"); + return; +} + f=osm_find_node(s->from); t=osm_find_node(s->to); if (!f) @@ -551,13 +563,21 @@ g_slist_foreach(w->nodes, _print_segment_helper, NULL); segment * osm_get_segment_for_way(gint sid) { -return g_hash_table_lookup(osm_segments, GINT_TO_POINTER(sid)); +segment *s; +s=g_hash_table_lookup(osm_segments, GINT_TO_POINTER(sid)); +if (!s) + g_printf("ERROR: Segment %d not found!\n", sid); +return s; } node * osm_find_node(gint nid) { -return g_hash_table_lookup(osm_nodes, GINT_TO_POINTER(nid)); +node *n; +n=g_hash_table_lookup(osm_nodes, GINT_TO_POINTER(nid)); +if (!n) + g_printf("ERROR: Node %d not found!\n", nid); +return n; } void @@ -612,7 +632,7 @@ w->data=NULL; w->ncnt=0; w->flags=0; -g_hash_table_insert(osm_ways, GINT_TO_POINTER(id), w); +osm_ways=g_slist_prepend(osm_ways, w); return w; } @@ -622,7 +642,7 @@ osm_way_new_seg(way *w, gint id) segment *s; s=osm_get_segment_for_way(id); -w->nodes=g_slist_append(w->nodes, s); +w->nodes=g_slist_prepend(w->nodes, s); w->ncnt++; } @@ -633,8 +653,8 @@ osm_node_save_node(gint key, node *value, gpointer user_data) { dbnode_cnt++; db_insert_node(value); -if (dbnode_cnt % 262140==0) - g_printf("Nodes: %d\n", dbnode_cnt); +if (dbnode_cnt % 26214==0) + g_printf("Nodes: %f%%\n",((float)dbnode_cnt/(float)node_cnt)*100); } void @@ -692,12 +712,14 @@ sqlite3_exec(db, "commit;", NULL, NULL, NULL); /*********************************************************************/ void -osm_node_save_way(gint key, way *value, gpointer user_data) +osm_node_save_way(way *value, gpointer user_data) { dbway_cnt++; db_insert_way(value); -if (dbway_cnt % 26214==0) - g_printf("Ways: %d\n", dbway_cnt); +if (dbway_cnt % 8192==0) { + g_printf("Ways: %f%%\n",(((float)dbway_cnt/(float)way_cnt)*100)); + print_way(value); +} } void @@ -705,12 +727,15 @@ osm_planet_save_ways(void) { g_print("Storing ways...\n"); -sqlite3_exec(db, "begin;", NULL, NULL, NULL); +g_print("Clearing old data:\n"); sqlite3_step(sql.delete_way); sqlite3_step(sql.delete_way_seg); sqlite3_step(sql.delete_way_name); sqlite3_step(sql.delete_way_ref); -g_hash_table_foreach(osm_ways, osm_node_save_way, NULL); + +g_print("Inserting new ways:\n"); +sqlite3_exec(db, "begin;", NULL, NULL, NULL); +g_slist_foreach(osm_ways, osm_node_save_way, NULL); sqlite3_exec(db, "commit;", NULL, NULL, NULL); } @@ -734,6 +759,7 @@ _osm_tag_start(void *userData, const char *name, const char **atts) { tag_state_t t; gchar *k, *v; +guint32 id; t=check_tag(name); switch (t) { @@ -743,24 +769,32 @@ switch (t) { case IN_NODE_TAG: tag_parent=IS_NODE; node_cnt++; - cnode=osm_new_node(atoi(get_attr_key_value(atts, "id")), + + id=atoi(get_attr_key_value(atts, "id")); + + cnode=osm_new_node(id, atof(get_attr_key_value(atts, "lat")), atof(get_attr_key_value(atts, "lon"))); break; case IN_SEGMENT_TAG: tag_parent=IS_SEGMENT; seg_cnt++; - osm_new_segment(atoi(get_attr_key_value(atts, "id")), - atof(get_attr_key_value(atts, "from")), - atof(get_attr_key_value(atts, "to"))); + + id=atoi(get_attr_key_value(atts, "id")); + + osm_new_segment(id, + atoi(get_attr_key_value(atts, "from")), + atoi(get_attr_key_value(atts, "to"))); break; case IN_WAY_TAG: tag_parent=IS_WAY; way_cnt++; - cway=osm_new_way(atoi(get_attr_key_value(atts, "id"))); + id=atoi(get_attr_key_value(atts, "id")); + cway=osm_new_way(id); break; case IN_SEG_TAG: - osm_way_new_seg(cway, atoi(get_attr_key_value(atts, "id"))); + id=atoi(get_attr_key_value(atts, "id")); + osm_way_new_seg(cway, id); break; case IN_KEY_TAG: switch (tag_parent) { @@ -869,6 +903,7 @@ switch (t) { if (way_cnt % 32767==0) { g_printf("Ways: %d\n", way_cnt); } + cway->nodes=g_slist_reverse(cway->nodes); if (cway->data && cway->data->name==NULL && cway->data->ref==NULL && cway->data->int_ref==NULL && cway->data->layer==0) osm_free_way_data(cway); @@ -941,7 +976,6 @@ db_prepare(); osm_nodes=g_hash_table_new(g_direct_hash, g_direct_equal); osm_segments=g_hash_table_new(g_direct_hash, g_direct_equal); -osm_ways=g_hash_table_new(g_direct_hash, g_direct_equal); osm_planet_parse_file(argv[1]); @@ -952,7 +986,6 @@ osm_planet_save_to_db(); g_hash_table_unref(osm_nodes); g_hash_table_unref(osm_segments); -g_hash_table_unref(osm_ways); sqlite3_close(db); sync(); -- 2.39.5