return TRUE;
}
+/**
+ * Free way nodes list
+ */
+void
+osm_way_nodes_free(osm_way *w)
+{
+GList *iter;
+
+if (!w->nodes)
+ return;
+
+for (iter=w->nodes; iter!=NULL; iter=iter->next)
+ g_slice_free(osm_way_node, (osm_way_node*)iter->data);
+
+g_list_free(w->nodes);
+}
+
+/**
+ * Free a osm_way structure
+ */
+void
+osm_way_free(osm_way *w)
+{
+osm_way_nodes_free(w);
+if (w->name)
+ g_free(w->name);
+if (w->ref)
+ g_free(w->ref);
+if (w->int_ref)
+ g_free(w->int_ref);
+g_slice_free(osm_way, w);
+}
+
/**
* Get place with given id and distance to current location
*/
n->isin=sqlite3_column_int(sql.select_near_place, 5);
n->type=type;
- g_printf("Place(%d): %s distance: %f\n", type, place, n->dist);
return TRUE;
}
return FALSE;
gboolean distance_point_to_line(gint x, gint y, gint x1, gint y1, gint x2, gint y2, gdouble *d)
{
-gdouble lm,u;
+gdouble lm,u,tmp;
gdouble ix,iy;
-gdouble tmp,tx1,tx2,ty1,ty2;
-
-#if 0
-if (x1>x2) {
- tx1=x2;
- ty1=y2;
- x2=x1;
- y2=y1;
- x1=tx1;
- y1=ty1;
-}
-#endif
lm=magnitude((gdouble)x1,(gdouble)y1,(gdouble)x2,(gdouble)y2);
tmp=(gdouble)((x-x1)*(x2-x1))+((y-y1)*(y2-y1));
u=tmp/(lm*lm);
-g_printf("DPL: tmp %f u %f lm %f\n", tmp, u, lm);
-
-g_printf("DPLX: (%d/%d) (%d %d)-(%d %d)\n",
- x,y,
- x1,y1,
- x2,y2);
-
if (u<0.0f || u>1.0f)
return FALSE;
osm_way *way=(osm_way*)iter->data;
- g_printf("C*** WAY %d (%d) HAS %d NODES, nearest is %d\n",
+ g_printf("WAY %d (%d) HAS %d NODES, nearest is %d\n",
way->id, way->type, way->nodecnt, way->node_num);
if (osm_way_get_nodes(way)==FALSE)
}
wnf=g_list_nth_data(way->nodes, way->node_num);
- if (!wnf)
+ if (!wnf) {
+ osm_way_free(way);
continue;
+ }
- g_printf("----EVAL-START:\n");
- if (way->node_num==way->nodecnt) {
- g_print(" <- Last\n");
- wnt=g_list_nth_data(way->nodes, way->nodecnt-1);
- if (!wnt)
- continue;
- if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE)
+ 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;
- } else if (way->node_num==0) {
- g_print(" First %d ->\n", wnf->num);
- wnt=g_list_nth_data(way->nodes, 1);
- if (!wnt)
- continue;
- if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE)
+ }
+ if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) {
+ osm_way_free(way);
continue;
+ }
} else {
- g_print(" <->\n");
wnt=g_list_nth_data(way->nodes, way->node_num-1);
- if (!wnt)
+ if (!wnt) {
+ osm_way_free(way);
continue;
+ }
if (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) {
wnt=g_list_nth_data(way->nodes, way->node_num+1);
- if (!wnt)
+ 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 (distance_point_to_line(lon, lat, wnf->lon, wnf->lat, wnt->lon, wnt->lat, &pndist)==FALSE) {
+ osm_way_free(way);
continue;
+ }
}
}
- g_printf("----EVAL-DONE: %d <-> %d\n", wnf->num, wnt->num);
- g_printf("WD: New: %f Prev:%f\n", pndist, pdist);
if (pndist<pdist) {
+ g_printf("Found close way, distance: %f (Previous distance: %f)\n", pndist, pdist);
pdist=pndist;
cw=way;
+ } else {
+ g_printf("Way is not closer, freeing\n");
+ osm_way_free(way);
}
}
}
osm_way_get_ref(cw);
}
-g_printf("Way: %d %d %d %d: %s %s %s\n",
+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);
return FALSE;
}
-void
-osm_way_free(osm_way *w)
-{
-if (w->nodes)
- g_list_free(w->nodes);
-if (w->name)
- g_free(w->name);
-if (w->ref)
- g_free(w->ref);
-if (w->int_ref)
- g_free(w->int_ref);
-g_slice_free(osm_way, w);
-}