]> err.no Git - mapper/commitdiff
Store way names, ref and int_ref
authorKaj-Michael Lang <milang@onion.tal.org>
Mon, 16 Jul 2007 08:53:02 +0000 (11:53 +0300)
committerKaj-Michael Lang <milang@onion.tal.org>
Mon, 16 Jul 2007 08:53:02 +0000 (11:53 +0300)
Use separate data object for way extra data (name, ref, int_ref)

src/osm.c

index cb88659b840511267c2bd22047e4b3ec95253242..fde0b6b2b7570262b9dc7b33800ed9482f8835f6 100644 (file)
--- a/src/osm.c
+++ b/src/osm.c
@@ -48,6 +48,7 @@ struct _way_data {
        gchar *ref;
        gchar *int_ref;
        gchar *is_in;
+       gint8 layer;
 };
 
 /* Segment/Way structure */
@@ -55,9 +56,9 @@ typedef struct _way way;
 struct _way {
        gint32 id;
        way_type_t type;
-       gshort layer;
-       gchar *name;
+       guint16 ncnt;
        guint flags;
+       way_data *data;
        GSList *nodes;
 };
 
@@ -180,6 +181,8 @@ struct sql_stmt sql;
 gint wsegcnt;
 
 node *osm_find_node(gint nid);
+void osm_free_way_data(way *w);
+void print_way(way *w);
 
 /****************************************************/
 /* Functions */
@@ -222,14 +225,14 @@ sqlite3_prepare(db, "insert into way_seg (wsid,num,node) values (?, ?, ?)",
 sqlite3_prepare(db, "delete from way_seg", -1, 
        &sql.delete_way_seg, NULL);
 
-sqlite3_prepare(db, "insert into name (nid,name,lang) values (?, ?, ?)", 
+sqlite3_prepare(db, "insert into way_names (wid,name) values (?, ?)", 
                    -1, &sql.insert_way_name, NULL);
-sqlite3_prepare(db, "delete from name", -1, 
+sqlite3_prepare(db, "delete from way_names", -1, 
        &sql.delete_way_name, NULL);
 
-sqlite3_prepare(db, "insert into ref (rid,ref,int_ref) values (?, ?, ?)", 
+sqlite3_prepare(db, "insert into way_ref (rid,ref,int_ref) values (?, ?, ?)", 
                    -1, &sql.insert_way_ref, NULL);
-sqlite3_prepare(db, "delete from ref", -1, 
+sqlite3_prepare(db, "delete from way_ref", -1, 
        &sql.delete_way_ref, NULL);
 }
 
@@ -248,6 +251,7 @@ sqlite3_bind_int(sql.insert_node, 2, lat);
 sqlite3_bind_int(sql.insert_node, 3, lon);
 sqlite3_step(sql.insert_node);
 sqlite3_reset(sql.insert_node);
+sqlite3_clear_bindings(sql.insert_node);
 return TRUE;
 }
 
@@ -268,6 +272,7 @@ sqlite3_bind_int(sql.insert_poi, 7, n->data->type/100);
 
 sqlite3_step(sql.insert_poi);
 sqlite3_reset(sql.insert_poi);
+sqlite3_clear_bindings(sql.insert_poi);
 return TRUE;
 }
 
@@ -278,6 +283,7 @@ sqlite3_bind_int(sql.update_node, 1, n->id);
 
 sqlite3_step(sql.update_node);
 sqlite3_reset(sql.update_node);
+sqlite3_clear_bindings(sql.update_node);
 }
 
 void 
@@ -289,12 +295,47 @@ 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);
 db_update_node_links(n);
 
 wsegcnt++;
 }
 
+void 
+db_insert_way_ref(way *w)
+{
+if (!w->data)
+       return;
+
+if (!w->data->ref && !w->data->int_ref)
+       return;
+
+sqlite3_bind_int(sql.insert_way_ref, 1, w->id);
+if (w->data->ref)
+       sqlite3_bind_text(sql.insert_way_ref, 2, w->data->ref, -1, SQLITE_TRANSIENT);
+if (w->data->int_ref)
+       sqlite3_bind_text(sql.insert_way_ref, 3, w->data->int_ref, -1, SQLITE_TRANSIENT);
+
+sqlite3_step(sql.insert_way_ref);
+sqlite3_reset(sql.insert_way_ref);
+sqlite3_clear_bindings(sql.insert_way_ref);
+}
+
+void 
+db_insert_way_names(way *w)
+{
+if (!w->data)
+       return;
+if (!w->data->name)
+       return;
+sqlite3_bind_int(sql.insert_way_name, 1, w->id);
+sqlite3_bind_text(sql.insert_way_name, 2, w->data->name, -1, SQLITE_TRANSIENT);
+
+sqlite3_step(sql.insert_way_name);
+sqlite3_reset(sql.insert_way_name);
+sqlite3_clear_bindings(sql.insert_way_name);
+}
+
 void
 db_insert_way_segments(segment *s, way *w)
 {
@@ -309,7 +350,7 @@ if (!t)
        return;
 
 db_insert_way_seg(f,w);
-if (g_slist_length(w->nodes)==wsegcnt)
+if (w->ncnt==wsegcnt)
        db_insert_way_seg(t,w);
 }
 
@@ -320,24 +361,22 @@ if (w->type==WAY_UNWAYED)
        return TRUE;
 
 sqlite3_bind_int(sql.insert_way_data, 1, w->id);
-sqlite3_bind_int(sql.insert_way_data, 2, g_slist_length(w->nodes));
+sqlite3_bind_int(sql.insert_way_data, 2, w->ncnt);
 sqlite3_bind_int(sql.insert_way_data, 3, w->type);
 sqlite3_bind_int(sql.insert_way_data, 4, w->flags);
 
 sqlite3_step(sql.insert_way_data);
 sqlite3_reset(sql.insert_way_data);
+sqlite3_clear_bindings(sql.insert_way_data);
+
+db_insert_way_ref(w);
+db_insert_way_names(w);
 
 wsegcnt=0;
-if (w->flags & W_AREA) {
-#if 1
-       g_print("Areas not handled yet\n");
-#else
-       g_slist_foreach(w->nodes, db_insert_area_segments, w);
-#endif
-} else {
-       g_slist_foreach(w->nodes, db_insert_way_segments, w);
-}
+g_slist_foreach(w->nodes, db_insert_way_segments, w);
+print_way(w);
 
+osm_free_way_data(w);
 return TRUE;
 }
 
@@ -469,7 +508,7 @@ g_printf("Way #%d(%d/%d): %s [%s:%s:%s]\n",
                w->id,  
                g_slist_length(w->nodes), 
                w->type,
-               w->name ? w->name : "", 
+               w->data ? w->data->name ? w->data->name : "" : "", 
                w->flags & W_ONEWAY ? "-" : "=", 
                w->flags & W_ROUNDABOUT ? "O" : "-", 
                w->flags & W_LINK ? "|" : " ");
@@ -502,6 +541,33 @@ s->to=to;
 g_hash_table_insert(osm_segments, GINT_TO_POINTER(id), s);
 }
 
+void
+osm_new_way_data(way *w)
+{
+if (w==NULL) return;
+if (w->data!=NULL) return;
+w->data=g_slice_new(way_data);
+w->data->name=NULL;
+w->data->ref=NULL;
+w->data->int_ref=NULL;
+w->data->layer=0;
+}
+
+void
+osm_free_way_data(way *w)
+{
+if (!w->data)
+       return;
+if (w->data->name)
+       g_free(w->data->name);
+if (w->data->ref)
+       g_free(w->data->ref);
+if (w->data->int_ref)
+       g_free(w->data->int_ref);
+g_slice_free(way_data, w->data);
+w->data=NULL;
+}
+
 way *
 osm_new_way(gint id)
 {
@@ -511,8 +577,8 @@ w=g_slice_new(way);
 w->id=id;
 w->nodes=NULL;
 w->type=WAY_UNWAYED;
-w->name=NULL;
-w->layer=0;
+w->data=NULL;
+w->ncnt=0;
 w->flags=0;
 
 g_hash_table_insert(osm_ways, GINT_TO_POINTER(id), w);
@@ -526,6 +592,7 @@ segment *s;
 
 s=osm_get_segment_for_way(id);
 w->nodes=g_slist_append(w->nodes, s);
+w->ncnt++;
 }
 
 /***********************************************************************/
@@ -595,6 +662,9 @@ g_print("Storing ways...\n");
 
 sqlite3_exec(db, "begin;", NULL, NULL, NULL);
 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);
 sqlite3_exec(db, "commit;", NULL, NULL, NULL);
 }
@@ -683,16 +753,25 @@ switch (t) {
                        k=get_attr_key_value(atts, "k");
                        v=get_attr_key_value(atts, "v");
 
+                       osm_new_way_data(cway);
+
                        if (strcasecmp(k, "name")==0)
-                               cway->name=g_strdup(v);
-#if 0
+                               cway->data->name=g_strdup(v);
                        else if (strcasecmp(k, "ref")==0)
-                               cway->ref=g_strdup(v);
+                               cway->data->ref=g_strdup(v);
                        else if (strcasecmp(k, "int_ref")==0)
-                               cway->int_ref=g_strdup(v);
-#endif
-
-                       if (cway->type==WAY_UNWAYED) {
+                               cway->data->int_ref=g_strdup(v);
+                       else if (strcasecmp(k, "oneway")==0)
+                               cway->flags|=W_ONEWAY;
+                       else if (strcasecmp(k, "layer")==0)
+                               cway->data->layer=atoi(v);
+                       else if ((strcasecmp(k, "junction")==0) && (strcasecmp(v, "roundabout")==0)) {
+                               cway->flags|=W_ROUNDABOUT;
+                               cway->flags|=W_ONEWAY;
+                       } else if ((strcasecmp(k, "junction")==0) && (strcasecmp(v, "mini_roundabout")==0)) {
+                               cway->flags|=W_ROUNDABOUT;
+                               cway->flags|=W_ONEWAY;
+                       } else if (cway->type==WAY_UNWAYED) {
                        for (i=0; wayinfo[i].k; i++) {
                                if (strcasecmp (wayinfo[i].k, k)==0 && strcasecmp (v, wayinfo[i].v)==0) {
                                        if (wayinfo[i].link==TRUE)
@@ -706,13 +785,6 @@ switch (t) {
                        }
                        }
 
-                       if ((strcasecmp(k, "junction")==0) && (strcasecmp(v, "roundabout")==0)) {
-                               cway->flags|=W_ROUNDABOUT;
-                               cway->flags|=W_ONEWAY;
-                       } else if (strcasecmp(k, "oneway")==0)
-                               cway->flags|=W_ONEWAY;
-                       else if (strcasecmp(k, "layer")==0)
-                               cway->layer=atoi(v);
                }
                break;
                case IS_SEGMENT:
@@ -745,6 +817,9 @@ switch (t) {
                if (way_cnt % 10000==0) {
                        g_printf("Ways: %d\n", way_cnt);
                }
+               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);
                cway=NULL;
        break;
        case IN_SEGMENT_TAG: