From: Kaj-Michael Lang Date: Mon, 23 Jul 2007 12:35:08 +0000 (+0300) Subject: Add bounding box support. Needs to skip segments and ways outside of it when loading... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99088d308a0ca61f1ec8deb5aefd0b1c641e2bf9;p=mapper Add bounding box support. Needs to skip segments and ways outside of it when loading but I don't know.. lookups takes time too... --- diff --git a/src/osm.c b/src/osm.c index 331c20c..eaf60f7 100644 --- a/src/osm.c +++ b/src/osm.c @@ -230,6 +230,15 @@ struct sql_stmt { }; struct sql_stmt sql; +struct map_bbox { + gdouble lat_min; + gdouble lon_min; + gdouble lat_max; + gdouble lon_max; +}; +struct map_bbox bbox; +gboolean use_bbox; + gint wsegcnt; node *osm_find_node(gint nid); @@ -421,7 +430,7 @@ sqlite3_reset(sql.insert_way_name); sqlite3_clear_bindings(sql.insert_way_name); } -void +gboolean db_insert_way_segments(segment *s, way *w) { node *f; @@ -429,24 +438,30 @@ node *t; if (!s) { g_printf("ERROR: null segment!\n"); - return; + return FALSE; } if (!w) { g_printf("ERROR: null way!\n"); - return; + return FALSE; } f=osm_find_node(s->from); t=osm_find_node(s->to); -if (!f) - return; -if (!t) - return; +if (!f) { + w->id=0; + return FALSE; +} +if (!t) { + w->id=0; + return FALSE; +} db_insert_way_seg(f,w); if (w->ncnt==wsegcnt) db_insert_way_seg(t,w); + +return TRUE; } gboolean @@ -458,6 +473,12 @@ if (!w) if (w->type==WAY_UNWAYED) return TRUE; +wsegcnt=0; +g_slist_foreach(w->nodes, db_insert_way_segments, w); +/* Invalid/Skipped segments/nodes in way */ +if (w->id==0) + return FALSE; + sqlite3_bind_int(sql.insert_way_data, 1, w->id); sqlite3_bind_int(sql.insert_way_data, 2, w->ncnt); sqlite3_bind_int(sql.insert_way_data, 3, w->type); @@ -470,8 +491,6 @@ sqlite3_clear_bindings(sql.insert_way_data); db_insert_way_ref(w); db_insert_way_names(w); -wsegcnt=0; -g_slist_foreach(w->nodes, db_insert_way_segments, w); osm_free_way_data(w); return TRUE; @@ -615,8 +634,10 @@ osm_find_node(gint nid) { node *n; n=g_hash_table_lookup(osm_nodes, GINT_TO_POINTER(nid)); +#if 0 if (!n) g_printf("ERROR: Node %d not found!\n", nid); +#endif return n; } @@ -868,6 +889,7 @@ _osm_tag_start(void *userData, const char *name, const char **atts) tag_state_t t; gchar *k, *v; guint32 id; +gfloat nlat, nlon; t=check_tag(name); switch (t) { @@ -880,10 +902,18 @@ switch (t) { 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"))); - osm_node_tags=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + osm_node_tags=NULL; + nlat=atof(get_attr_key_value(atts, "lat")); + nlon=atof(get_attr_key_value(atts, "lon")); + if (use_bbox==TRUE) { + if (nlat > bbox.lat_min && nlat < bbox.lat_max && nlon > bbox.lon_min && nlon < bbox.lon_max) { + cnode=osm_new_node(id, nlat, nlon); + osm_node_tags=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + } + } else { + cnode=osm_new_node(id, nlat, nlon); + osm_node_tags=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + } break; case IN_SEGMENT_TAG: tag_parent=IS_SEGMENT; @@ -914,13 +944,16 @@ switch (t) { { gint i; + if (!osm_node_tags) + return; + k=get_attr_key_value(atts, "k"); v=get_attr_key_value(atts, "v"); if (cnode==NULL) { g_printf("In node tags but current node is NULL\n"); return; - } + } g_hash_table_insert(osm_node_tags, g_strdup(k), g_strdup(v)); } break; @@ -998,6 +1031,9 @@ switch (t) { g_printf("Nodes: %d/%d\n", node_cnt, noded_cnt); } + if (!osm_node_tags) + return; + osm_new_node_data(cnode); for (i=0; nodeinfo[i].k; i++) { @@ -1159,12 +1195,24 @@ g_hash_table_unref(osm_node_isin); int main (int argc, char **argv) { -if (argc!=2) { +if (argc<2) { return print_fail("No planet XML file given", 1); } else { g_printf("Using file: %s\n", argv[1]); } +if (argc==6) { + use_bbox=TRUE; + + bbox.lat_min=atof(argv[2]); + bbox.lon_min=atof(argv[3]); + bbox.lat_max=atof(argv[4]); + bbox.lon_max=atof(argv[5]); + g_printf("Skipping data outside of box: %f,%f - %f,%f\n", + bbox.lat_min, bbox.lon_min, + bbox.lat_max, bbox.lon_max); +} else use_bbox=FALSE; + if (db_open()!=TRUE) return print_fail("Database open failed", 2); db_prepare();