From: Kaj-Michael Lang Date: Tue, 24 Jul 2007 20:41:03 +0000 (+0300) Subject: Use a helper function to check the distance to line from given lat, lon to given... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5e8a55511e09b74675929e312340b2fe0c56006;p=mapper Use a helper function to check the distance to line from given lat, lon to given node pair. Store the used node pair in the way structure so we can use them later. --- diff --git a/src/osm-db.c b/src/osm-db.c index 184023b..2df7a31 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -276,6 +276,14 @@ iy=(gdouble)y1+u*(gdouble)(y2-y1); return TRUE; } +gboolean osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d) +{ +if (!f || !t) + return FALSE; + +return distance_point_to_line(lon, lat, f->lon, f->lat, t->lon, t->lat, d); +} + /** * Search for the nearest way (road) * - First search for ways with nearest node @@ -304,9 +312,6 @@ switch (g_list_length(w)) { case 0: return NULL; break; - case 1: - cw=w->data; - break; default: { gint dist=900000, ndist; @@ -337,27 +342,15 @@ switch (g_list_length(w)) { if ( (way->node_num==way->nodecnt) || (way->node_num==0)) { wnt=g_list_nth_data(way->nodes, way->node_num==way->nodecnt ? way->nodecnt-1 : 1); - if (!wnt) { - osm_way_free(way); - continue; - } - if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) { + if (osm_way_distance(lat, lon, wnf, wnt, &pndist)==FALSE) { osm_way_free(way); continue; } } else { wnt=g_list_nth_data(way->nodes, way->node_num-1); - if (!wnt) { - osm_way_free(way); - continue; - } - if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) { + if (osm_way_distance(lat, lon, wnf, wnt, &pndist)==FALSE) { wnt=g_list_nth_data(way->nodes, way->node_num+1); - if (!wnt) { - osm_way_free(way); - continue; - } - if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) { + if (osm_way_distance(lat, lon, wnf, wnt, &pndist)==FALSE) { osm_way_free(way); continue; } @@ -367,10 +360,14 @@ switch (g_list_length(w)) { if (pndistnode_f=wnf; + way->node_t=wnt; + way->distance=pndist; cw=way; } else { g_printf("Way is not closer, freeing\n"); osm_way_free(way); + way=NULL; } } } @@ -388,10 +385,13 @@ if (cw->type==WAY_MOTORWAY || cw->type==WAY_TRUNK || osm_way_get_ref(cw); } -g_printf("BEST WAY: %d %d %d %d: %s %s %s\n", - cw->id, cw->type, cw->flags, - cw->nodes, cw->dist, cw->name, - cw->ref, cw->int_ref); +g_printf("BEST WAY(%d): %s (%s,%s)\n", + cw->id, cw->name, cw->ref, cw->int_ref); +g_printf("\tT: %d F: %d N: %d D: %f\n", + cw->type, cw->flags, cw->nodes, cw->dist); +g_printf("\tNF: %d NT: %d DT %f\n", + cw->node_f->num, + cw->node_t->num, cw->distance); return cw; } diff --git a/src/osm-db.h b/src/osm-db.h index ef63d54..5df5149 100644 --- a/src/osm-db.h +++ b/src/osm-db.h @@ -3,3 +3,4 @@ gboolean osm_find_nearest_place(node_type_t type, gint lat, gint lon, osm_place *n); osm_way *osm_find_nearest_way(gint lat, gint lon); +gboolean osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d);