]> err.no Git - mapper/commitdiff
Use a helper function to check the distance to line from given lat, lon to given...
authorKaj-Michael Lang <milang@angel.tal.org>
Tue, 24 Jul 2007 20:41:03 +0000 (23:41 +0300)
committerKaj-Michael Lang <milang@angel.tal.org>
Tue, 24 Jul 2007 20:41:03 +0000 (23:41 +0300)
Store the used node pair in the way structure so we can use them later.

src/osm-db.c
src/osm-db.h

index 184023b173877b182f618b84d6b1ab49b1789596..2df7a313466b128b0fa8f42b5044366a550672d1 100644 (file)
@@ -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 (pndist<pdist) {
                                g_printf("Found close way, distance: %f (Previous distance: %f)\n", pndist, pdist);
                                pdist=pndist;
+                               way->node_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;
 }
index ef63d549c7282ba4b5cd6d4e946deaee3611dd03..5df51498e12c89cc7fcae7a46f62d5a1a523c85b 100644 (file)
@@ -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);