/[zanavi_public1]/navit/navit/search.c
ZANavi

Contents of /navit/navit/search.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations) (download)
Thu Nov 3 16:51:49 2011 UTC (12 years, 4 months ago) by zoff99
File MIME type: text/plain
File size: 52368 byte(s)
better search results, show target on map, improve search bar
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "debug.h"
25 #include "projection.h"
26 #include "item.h"
27 #include "map.h"
28 #include "mapset.h"
29 #include "coord.h"
30 #include "transform.h"
31 #include "search.h"
32 #include "country.h"
33 #include "navit.h"
34
35 #if HAVE_API_ANDROID
36 #include "android.h"
37 #endif
38 #include "layout.h"
39 #include "map.h"
40 #include "linguistics.h"
41
42 struct search_list_level {
43 struct mapset *ms;
44 struct search_list_common *parent;
45 struct attr *attr;
46 int partial;
47 int selected;
48 struct mapset_search *search;
49 GHashTable *hash;
50 GList *list,*curr,*last;
51 };
52
53 struct interpolation {
54 int side, mode, rev;
55 char *first, *last, *curr;
56 };
57
58 struct search_list {
59 struct mapset *ms;
60 struct item *item;
61 int level;
62 struct search_list_level levels[4];
63 struct search_list_result result;
64 struct search_list_result last_result;
65 int last_result_valid;
66 char *postal;
67 struct interpolation inter;
68 };
69
70 static guint
71 search_item_hash_hash(gconstpointer key)
72 {
73 const struct item *itm=key;
74 gconstpointer hashkey=(gconstpointer)GINT_TO_POINTER(itm->id_hi^itm->id_lo);
75 return g_direct_hash(hashkey);
76 }
77
78 static gboolean
79 search_item_hash_equal(gconstpointer a, gconstpointer b)
80 {
81 const struct item *itm_a=a;
82 const struct item *itm_b=b;
83 if (item_is_equal_id(*itm_a, *itm_b))
84 return TRUE;
85 return FALSE;
86 }
87
88 struct search_list *
89 search_list_new(struct mapset *ms)
90 {
91 struct search_list *ret;
92
93 ret=g_new0(struct search_list, 1);
94 ret->ms=ms;
95
96 return ret;
97 }
98
99 static void search_list_search_free(struct search_list *sl, int level);
100
101 static int
102 search_list_level(enum attr_type attr_type)
103 {
104 switch(attr_type) {
105 case attr_country_all:
106 case attr_country_id:
107 case attr_country_iso2:
108 case attr_country_iso3:
109 case attr_country_car:
110 case attr_country_name:
111 return 0;
112 case attr_town_postal:
113 return 1;
114 case attr_town_name:
115 case attr_district_name:
116 case attr_town_or_district_name:
117 return 1;
118 case attr_street_name:
119 return 2;
120 case attr_house_number:
121 return 3;
122 case attr_postal:
123 return -1;
124 default:
125 dbg(0,"unknown search '%s'\n",attr_to_name(attr_type));
126 return -1;
127 }
128 }
129
130 static void
131 interpolation_clear(struct interpolation *inter)
132 {
133 inter->mode=inter->side=0;
134 g_free(inter->first);
135 g_free(inter->last);
136 g_free(inter->curr);
137 inter->first=inter->last=inter->curr=NULL;
138 }
139
140 void
141 search_list_search(struct search_list *this_, struct attr *search_attr, int partial)
142 {
143 struct search_list_level *le;
144 int level=search_list_level(search_attr->type);
145 this_->item=NULL;
146 interpolation_clear(&this_->inter);
147 //dbg(0,"enter\n");
148 //dbg(0,"level=%d\n", level);
149 if (level != -1) {
150 this_->result.id=0;
151 this_->level=level;
152 le=&this_->levels[level];
153 search_list_search_free(this_, level);
154 le->attr=attr_dup(search_attr);
155 le->partial=partial;
156 if (level > 0) {
157 le=&this_->levels[level-1];
158 le->curr=le->list;
159 }
160 //dbg(0,"le=%p partial=%d\n", le, partial);
161 } else if (search_attr->type == attr_postal) {
162 g_free(this_->postal);
163 this_->postal=g_strdup(search_attr->u.str);
164 }
165 //dbg(0,"return\n");
166 }
167
168 struct search_list_common *
169 search_list_select(struct search_list *this_, enum attr_type attr_type, int id, int mode)
170 {
171 int level=search_list_level(attr_type);
172 int num=0;
173 struct search_list_level *le;
174 struct search_list_common *slc;
175 GList *curr;
176 le=&this_->levels[level];
177 curr=le->list;
178 if (mode > 0 || !id)
179 le->selected=mode;
180 //dbg(0,"enter level=%d %d %d %p\n", level, id, mode, curr);
181 while (curr) {
182 num++;
183 if (! id || num == id) {
184 slc=curr->data;
185 slc->selected=mode;
186 if (id) {
187 le->last=curr;
188 //dbg(0,"found\n");
189 return slc;
190 }
191 }
192 curr=g_list_next(curr);
193 }
194 //dbg(0,"not found\n");
195 return NULL;
196 }
197
198 static void
199 search_list_common_new(struct item *item, struct search_list_common *common)
200 {
201 struct attr attr;
202 if (item_attr_get(item, attr_town_name, &attr))
203 common->town_name=map_convert_string(item->map, attr.u.str);
204 else
205 common->town_name=NULL;
206 if (item_attr_get(item, attr_county_name, &attr))
207 common->county_name=map_convert_string(item->map, attr.u.str);
208 else
209 common->county_name=NULL;
210 if (item_attr_get(item, attr_district_name, &attr))
211 common->district_name=map_convert_string(item->map, attr.u.str);
212 else
213 common->district_name=NULL;
214 if (item_attr_get(item, attr_postal, &attr))
215 common->postal=map_convert_string(item->map, attr.u.str);
216 else if (item_attr_get(item, attr_town_postal, &attr))
217 common->postal=map_convert_string(item->map, attr.u.str);
218 else
219 common->postal=NULL;
220 if (item_attr_get(item, attr_postal_mask, &attr))
221 common->postal_mask=map_convert_string(item->map, attr.u.str);
222 else
223 common->postal_mask=NULL;
224 }
225
226 static void
227 search_list_common_destroy(struct search_list_common *common)
228 {
229 map_convert_free(common->town_name);
230 map_convert_free(common->district_name);
231 map_convert_free(common->county_name);
232 map_convert_free(common->postal);
233 map_convert_free(common->postal_mask);
234 }
235
236 static struct search_list_country *
237 search_list_country_new(struct item *item)
238 {
239 struct search_list_country *ret=g_new0(struct search_list_country, 1);
240 struct attr attr;
241
242 ret->common.item=ret->common.unique=*item;
243 if (item_attr_get(item, attr_country_car, &attr))
244 ret->car=g_strdup(attr.u.str);
245 if (item_attr_get(item, attr_country_iso2, &attr)) {
246 #if HAVE_API_ANDROID
247 ret->iso2=g_malloc(strlen(attr.u.str)+1);
248 strtolower(ret->iso2, attr.u.str);
249 #else
250 ret->iso2=g_strdup(attr.u.str);
251 #endif
252 ret->flag=g_strdup_printf("country_%s", ret->iso2);
253 }
254 if (item_attr_get(item, attr_country_iso3, &attr))
255 ret->iso3=g_strdup(attr.u.str);
256 if (item_attr_get(item, attr_country_name, &attr))
257 ret->name=g_strdup(attr.u.str);
258 return ret;
259 }
260
261 static void
262 search_list_country_destroy(struct search_list_country *this_)
263 {
264 g_free(this_->car);
265 g_free(this_->iso2);
266 g_free(this_->iso3);
267 g_free(this_->flag);
268 g_free(this_->name);
269 g_free(this_);
270 }
271
272 static struct search_list_town *
273 search_list_town_new(struct item *item)
274 {
275 struct search_list_town *ret=g_new0(struct search_list_town, 1);
276 struct attr attr;
277 struct coord c;
278
279 ret->itemt=*item;
280 ret->common.item=ret->common.unique=*item;
281 if (item_attr_get(item, attr_town_streets_item, &attr)) {
282 dbg(1,"town_assoc 0x%x 0x%x\n", attr.u.item->id_hi, attr.u.item->id_lo);
283 ret->common.unique=*attr.u.item;
284 }
285 search_list_common_new(item, &ret->common);
286 if (item_attr_get(item, attr_county_name, &attr))
287 ret->county=map_convert_string(item->map,attr.u.str);
288 else
289 ret->county=NULL;
290 if (item_coord_get(item, &c, 1)) {
291 ret->common.c=g_new(struct pcoord, 1);
292 ret->common.c->x=c.x;
293 ret->common.c->y=c.y;
294 ret->common.c->pro = map_projection(item->map);
295 }
296 return ret;
297 }
298
299 static void
300 search_list_town_destroy(struct search_list_town *this_)
301 {
302 map_convert_free(this_->county);
303 search_list_common_destroy(&this_->common);
304 if (this_->common.c)
305 g_free(this_->common.c);
306 g_free(this_);
307 }
308
309
310 static struct search_list_street *
311 search_list_street_new(struct item *item)
312 {
313 struct search_list_street *ret=g_new0(struct search_list_street, 1);
314 struct attr attr;
315 struct coord c;
316
317 ret->common.item=ret->common.unique=*item;
318 if (item_attr_get(item, attr_street_name, &attr))
319 ret->name=map_convert_string(item->map, attr.u.str);
320 else
321 ret->name=NULL;
322 search_list_common_new(item, &ret->common);
323 if (item_coord_get(item, &c, 1)) {
324 ret->common.c=g_new(struct pcoord, 1);
325 ret->common.c->x=c.x;
326 ret->common.c->y=c.y;
327 ret->common.c->pro = map_projection(item->map);
328 }
329 return ret;
330 }
331
332
333 static void
334 search_list_street_destroy(struct search_list_street *this_)
335 {
336 map_convert_free(this_->name);
337 search_list_common_destroy(&this_->common);
338 if (this_->common.c)
339 {
340 g_free(this_->common.c);
341 }
342 g_free(this_);
343 }
344
345 static char *
346 search_interpolate(struct interpolation *inter)
347 {
348 dbg(1,"interpolate %s-%s %s\n",inter->first,inter->last,inter->curr);
349 if (!inter->first || !inter->last)
350 return NULL;
351 if (!inter->curr)
352 inter->curr=g_strdup(inter->first);
353 else {
354 if (strcmp(inter->curr, inter->last)) {
355 int next=atoi(inter->curr)+(inter->mode?2:1);
356 g_free(inter->curr);
357 if (next == atoi(inter->last))
358 inter->curr=g_strdup(inter->last);
359 else
360 inter->curr=g_strdup_printf("%d",next);
361 } else {
362 g_free(inter->curr);
363 inter->curr=NULL;
364 }
365 }
366 dbg(1,"interpolate result %s\n",inter->curr);
367 return inter->curr;
368 }
369
370 static void
371 search_interpolation_split(char *str, struct interpolation *inter)
372 {
373 char *pos=strchr(str,'-');
374 char *first,*last;
375 int len;
376 if (!pos) {
377 inter->first=g_strdup(str);
378 inter->last=g_strdup(str);
379 inter->rev=0;
380 return;
381 }
382 len=pos-str;
383 first=g_malloc(len+1);
384 strncpy(first, str, len);
385 first[len]='\0';
386 last=g_strdup(pos+1);
387 dbg(1,"%s = %s - %s\n",str, first, last);
388 if (atoi(first) > atoi(last)) {
389 inter->first=last;
390 inter->last=first;
391 inter->rev=1;
392 } else {
393 inter->first=first;
394 inter->last=last;
395 inter->rev=0;
396 }
397 }
398
399 static int
400 search_setup_interpolation(struct item *item, enum attr_type i0, enum attr_type i1, enum attr_type i2, struct interpolation *inter)
401 {
402 struct attr attr;
403 g_free(inter->first);
404 g_free(inter->last);
405 g_free(inter->curr);
406 inter->first=inter->last=inter->curr=NULL;
407 dbg(1,"setup %s\n",attr_to_name(i0));
408 if (item_attr_get(item, i0, &attr)) {
409 search_interpolation_split(attr.u.str, inter);
410 inter->mode=0;
411 } else if (item_attr_get(item, i1, &attr)) {
412 search_interpolation_split(attr.u.str, inter);
413 inter->mode=1;
414 } else if (item_attr_get(item, i2, &attr)) {
415 search_interpolation_split(attr.u.str, inter);
416 inter->mode=2;
417 } else
418 return 0;
419 return 1;
420 }
421
422 static int
423 search_match(char *str, char *search, int partial)
424 {
425 if (!partial)
426 return (!g_strcasecmp(str, search));
427 else
428 return (!g_strncasecmp(str, search, strlen(search)));
429 }
430
431 static struct pcoord *
432 search_house_number_coordinate(struct item *item, struct interpolation *inter)
433 {
434 struct pcoord *ret=g_new(struct pcoord, 1);
435 ret->pro = map_projection(item->map);
436 if (item_is_point(*item)) {
437 struct coord c;
438 if (item_coord_get(item, &c, 1)) {
439 ret->x=c.x;
440 ret->y=c.y;
441 } else {
442 g_free(ret);
443 ret=NULL;
444 }
445 } else {
446 int count,max=1024;
447 int hn_pos,hn_length;
448 struct coord *c=g_alloca(sizeof(struct coord)*max);
449 item_coord_rewind(item);
450 count=item_coord_get(item, c, max);
451 hn_length=atoi(inter->last)-atoi(inter->first);
452 if (inter->rev)
453 hn_pos=atoi(inter->last)-atoi(inter->curr);
454 else
455 hn_pos=atoi(inter->curr)-atoi(inter->first);
456 if (count) {
457 int i,distance_sum=0,hn_distance;
458 int *distances=g_alloca(sizeof(int)*(count-1));
459 dbg(1,"count=%d hn_length=%d hn_pos=%d (%s of %s-%s)\n",count,hn_length,hn_pos,inter->curr,inter->first,inter->last);
460 if (!hn_length) {
461 hn_length=2;
462 hn_pos=1;
463 }
464 if (count == max)
465 dbg(0,"coordinate overflow\n");
466 for (i = 0 ; i < count-1 ; i++) {
467 distances[i]=navit_sqrt(transform_distance_sq(&c[i],&c[i+1]));
468 distance_sum+=distances[i];
469 dbg(1,"distance[%d]=%d\n",i,distances[i]);
470 }
471 dbg(1,"sum=%d\n",distance_sum);
472 hn_distance=distance_sum*hn_pos/hn_length;
473 dbg(1,"hn_distance=%d\n",hn_distance);
474 i=0;
475 while (i < count-1 && hn_distance > distances[i])
476 hn_distance-=distances[i++];
477 dbg(1,"remaining distance=%d from %d\n",hn_distance,distances[i]);
478 ret->x=(c[i+1].x-c[i].x)*hn_distance/distances[i]+c[i].x;
479 ret->y=(c[i+1].y-c[i].y)*hn_distance/distances[i]+c[i].y;
480 }
481 }
482 return ret;
483 }
484
485 static struct search_list_house_number *
486 search_list_house_number_new(struct item *item, struct interpolation *inter, char *inter_match, int inter_partial)
487 {
488 struct search_list_house_number *ret=g_new0(struct search_list_house_number, 1);
489 struct attr attr;
490 char *hn;
491
492 // dbg(0,"@@@@ enter @@@@\n");
493
494 ret->common.item=ret->common.unique=*item;
495 //if (item_attr_get(item, attr_street_name, &attr))
496 // dbg(0,"xx1 %s\n",attr.u.str);
497 if (item_attr_get(item, attr_house_number, &attr))
498 ret->house_number=map_convert_string(item->map, attr.u.str);
499 else {
500 //if (item_attr_get(item, attr_street_name, &attr))
501 // dbg(0,"xx2 %s\n",attr.u.str);
502 for (;;) {
503 //dbg(0,"interpolate 11");
504 ret->interpolation=1;
505 switch(inter->side) {
506 case 0:
507 //dbg(0,"interpolate 11 0");
508 inter->side=-1;
509 search_setup_interpolation(item, attr_house_number_left, attr_house_number_left_odd, attr_house_number_left_even, inter);
510 case -1:
511 //dbg(0,"interpolate 11 -1");
512 if ((hn=search_interpolate(inter)))
513 break;
514 inter->side=1;
515 search_setup_interpolation(item, attr_house_number_right, attr_house_number_right_odd, attr_house_number_right_even, inter);
516 case 1:
517 //dbg(0,"interpolate 11 1");
518 if ((hn=search_interpolate(inter)))
519 break;
520 default:
521 //dbg(0,"interpolate 11 default");
522 g_free(ret);
523 return NULL;
524 }
525 if (search_match(hn, inter_match, inter_partial))
526 {
527 //dbg(0,"interpolate 22");
528 //dbg(0,"match %s %s-%s\n",hn, inter->first, inter->last);
529 ret->house_number=map_convert_string(item->map, hn);
530 break;
531 }
532 }
533 }
534 //dbg(0,"interpolate 33");
535 search_list_common_new(item, &ret->common);
536 ret->common.c=search_house_number_coordinate(item, ret->interpolation?inter:NULL);
537 //dbg(0,"interpolate 44");
538 return ret;
539 }
540
541 static void
542 search_list_house_number_destroy(struct search_list_house_number *this_)
543 {
544 map_convert_free(this_->house_number);
545 search_list_common_destroy(&this_->common);
546 if (this_->common.c)
547 g_free(this_->common.c);
548 g_free(this_);
549 }
550
551 static void
552 search_list_result_destroy(int level, void *p)
553 {
554 switch (level) {
555 case 0:
556 search_list_country_destroy(p);
557 break;
558 case 1:
559 search_list_town_destroy(p);
560 break;
561 case 2:
562 search_list_street_destroy(p);
563 break;
564 case 3:
565 search_list_house_number_destroy(p);
566 break;
567 }
568 }
569
570 static void
571 search_list_search_free(struct search_list *sl, int level)
572 {
573 //dbg(0,"enter\n");
574
575 struct search_list_level *le=&sl->levels[level];
576 GList *next,*curr;
577 if (le->search)
578 {
579 mapset_search_destroy(le->search);
580 le->search=NULL;
581 }
582 #if 0 /* FIXME */
583 if (le->hash) {
584 g_hash_table_destroy(le->hash);
585 le->hash=NULL;
586 }
587 #endif
588 curr=le->list;
589 while (curr)
590 {
591 search_list_result_destroy(level, curr->data);
592 next=g_list_next(curr);
593 curr=next;
594 }
595 attr_free(le->attr);
596 g_list_free(le->list);
597 le->list=NULL;
598 le->curr=NULL;
599 le->last=NULL;
600
601 //dbg(0,"return\n");
602 }
603
604 char *
605 search_postal_merge(char *mask, char *new)
606 {
607 int i;
608 char *ret=NULL;
609 dbg(1,"enter %s %s\n", mask, new);
610 if (!new)
611 return NULL;
612 if (!mask)
613 return g_strdup(new);
614 i=0;
615 while (mask[i] && new[i]) {
616 if (mask[i] != '.' && mask[i] != new[i])
617 break;
618 i++;
619
620 }
621 if (mask[i]) {
622 ret=g_strdup(mask);
623 while (mask[i])
624 ret[i++]='.';
625 }
626 dbg(1,"merged %s with %s as %s\n", mask, new, ret);
627 return ret;
628 }
629
630 char *
631 search_postal_merge_replace(char *mask, char *new)
632 {
633 char *ret=search_postal_merge(mask, new);
634 if (!ret)
635 return mask;
636 g_free(mask);
637 return ret;
638 }
639
640
641 static int
642 postal_match(char *postal, char *mask)
643 {
644 for (;;) {
645 if ((*postal != *mask) && (*mask != '.'))
646 return 0;
647 if (!*postal) {
648 if (!*mask)
649 return 1;
650 else
651 return 0;
652 }
653 postal++;
654 mask++;
655 }
656 }
657
658 static int
659 search_add_result(struct search_list_level *le, struct search_list_common *slc)
660 {
661 struct search_list_common *slo;
662 char *merged;
663 slo=g_hash_table_lookup(le->hash, &slc->unique);
664 if (!slo) {
665 g_hash_table_insert(le->hash, &slc->unique, slc);
666 if (slc->postal && !slc->postal_mask) {
667 slc->postal_mask=g_strdup(slc->postal);
668 }
669 le->list=g_list_append(le->list, slc);
670 return 1;
671 }
672 merged=search_postal_merge(slo->postal_mask, slc->postal);
673 if (merged) {
674 g_free(slo->postal_mask);
675 slo->postal_mask=merged;
676 }
677 return 0;
678 }
679
680 struct search_list_result *
681 search_list_get_result(struct search_list *this_)
682 {
683 struct search_list_level *le,*leu;
684 int level=this_->level;
685 struct attr attr2;
686 int has_street_name=0;
687
688 //dbg(0,"******* enter *******\n");
689 le=&this_->levels[level];
690 //dbg(0,"le=%p\n", le);
691 for (;;)
692 {
693 //dbg(0,"le->search=%p\n", le->search);
694 if (! le->search)
695 {
696 //dbg(0,"partial=%d level=%d\n", le->partial, level);
697 if (! level)
698 le->parent=NULL;
699 else
700 {
701 leu=&this_->levels[level-1];
702 //dbg(0,"leu->curr=%p\n", leu->curr);
703 for (;;)
704 {
705 //dbg(0,"*********########");
706
707 struct search_list_common *slc;
708 if (! leu->curr)
709 {
710 return NULL;
711 }
712 le->parent=leu->curr->data;
713 leu->last=leu->curr;
714 leu->curr=g_list_next(leu->curr);
715 slc=(struct search_list_common *)(le->parent);
716 if (!slc)
717 break;
718 if (slc->selected == leu->selected)
719 break;
720 }
721 }
722 //if (le->parent)
723 //{
724 // //dbg(0,"mapset_search_new with item(%d,%d)\n", le->parent->item.id_hi, le->parent->item.id_lo);
725 //}
726 //else
727 //{
728 // dbg(0,"NO parent!!\n");
729 //}
730 // dbg(0,"############## attr=%s\n", attr_to_name(le->attr->type));
731 le->search=mapset_search_new(this_->ms, &le->parent->item, le->attr, le->partial);
732 // ** DOC ** mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
733 le->hash=g_hash_table_new(search_item_hash_hash, search_item_hash_equal);
734 }
735 //dbg(0,"le->search=%p\n", le->search);
736 if (!this_->item)
737 {
738 //dbg(0,"sssss 1");
739 this_->item=mapset_search_get_item(le->search);
740 //dbg(0,"sssss 1 %p\n",this_->item);
741 }
742 if (this_->item)
743 {
744 void *p=NULL;
745 //dbg(0,"id_hi=%d id_lo=%d\n", this_->item->id_hi, this_->item->id_lo);
746 if (this_->postal)
747 {
748 struct attr postal;
749 if (item_attr_get(this_->item, attr_postal_mask, &postal)) {
750 if (!postal_match(this_->postal, postal.u.str))
751 continue;
752 } else if (item_attr_get(this_->item, attr_postal, &postal)) {
753 if (strcmp(this_->postal, postal.u.str))
754 continue;
755 }
756 }
757 this_->result.country=NULL;
758 this_->result.town=NULL;
759 this_->result.street=NULL;
760 this_->result.c=NULL;
761 //dbg(0,"case x LEVEL start %d\n",level);
762 switch (level)
763 {
764 case 0:
765 //dbg(0,"case 0 COUNTRY");
766 p=search_list_country_new(this_->item);
767 this_->result.country=p;
768 this_->result.country->common.parent=NULL;
769 this_->item=NULL;
770 break;
771 case 1:
772 //dbg(0,"case 1 TOWN");
773 p=search_list_town_new(this_->item);
774 this_->result.town=p;
775 this_->result.town->common.parent=this_->levels[0].last->data;
776 this_->result.country=this_->result.town->common.parent;
777 this_->result.c=this_->result.town->common.c;
778 this_->item=NULL;
779 break;
780 case 2:
781 //dbg(0,"case 2 STREET");
782 p=search_list_street_new(this_->item);
783 this_->result.street=p;
784 this_->result.street->common.parent=this_->levels[1].last->data;
785 this_->result.town=this_->result.street->common.parent;
786 this_->result.country=this_->result.town->common.parent;
787 this_->result.c=this_->result.street->common.c;
788 this_->item=NULL;
789 break;
790 case 3:
791 //dbg(0,"case 3 HOUSENUMBER");
792 has_street_name=0;
793
794 // if this housenumber has a streetname tag, set the name now
795 if (item_attr_get(this_->item, attr_street_name, &attr2))
796 {
797 // dbg(0,"streetname: %s\n",attr2.u.str);
798 has_street_name=1;
799 }
800
801 //dbg(0,"case 3 XXXX 1\n");
802 p=search_list_house_number_new(this_->item, &this_->inter, le->attr->u.str, le->partial);
803 //dbg(0,"case 3 XXXX 2\n");
804 if (!p)
805 {
806 interpolation_clear(&this_->inter);
807 this_->item=NULL;
808 continue;
809 }
810 //dbg(0,"case 3 XXXX 3\n");
811 this_->result.house_number=p;
812 if (!this_->result.house_number->interpolation)
813 {
814 this_->item=NULL;
815 }
816
817 this_->result.house_number->common.parent=this_->levels[2].last->data;
818 this_->result.street=this_->result.house_number->common.parent;
819 this_->result.town=this_->result.street->common.parent;
820 this_->result.country=this_->result.town->common.parent;
821 this_->result.c=this_->result.house_number->common.c;
822
823 //dbg(0,"case 3 XXXX 4\n");
824 if (has_street_name==1)
825 {
826 gchar *tmp_name=g_strdup(attr2.u.str);
827 this_->result.street->name=tmp_name;
828 //dbg(0,"res streetname=%s\n",this_->result.street->name);
829 }
830 else
831 {
832 //
833 // this crashes all the time -> so dont use!
834 //static struct search_list_street null_street;
835 //this_->result.street=&null_street;
836 // this crashes all the time -> so dont use!
837 //
838 this_->result.street->name=NULL;
839 }
840 //dbg(0,"case 3 XXXX 5\n");
841 }
842 if (p)
843 {
844 if (search_add_result(le, p))
845 {
846 this_->result.id++;
847 return &this_->result;
848 }
849 else
850 {
851 search_list_result_destroy(level, p);
852 }
853 }
854 } else {
855 mapset_search_destroy(le->search);
856 le->search=NULL;
857 g_hash_table_destroy(le->hash);
858 if (! level)
859 break;
860 }
861 }
862 return NULL;
863 }
864
865 void
866 search_list_destroy(struct search_list *this_)
867 {
868 g_free(this_->postal);
869 g_free(this_);
870 }
871
872 void
873 search_init(void)
874 {
875 }
876
877
878 static char *
879 search_fix_spaces(char *str)
880 {
881 int i;
882 int len=strlen(str);
883 char c,*s,*d,*ret=g_strdup(str);
884
885 for (i = 0 ; i < len ; i++) {
886 if (ret[i] == ',' || ret[i] == ',' || ret[i] == '/')
887 ret[i]=' ';
888 }
889 s=ret;
890 d=ret;
891 len=0;
892 do {
893 c=*s++;
894 if (c != ' ' || len != 0) {
895 *d++=c;
896 len++;
897 }
898 while (c == ' ' && *s == ' ')
899 s++;
900 if (c == ' ' && *s == '\0') {
901 d--;
902 len--;
903 }
904 } while (c);
905 return ret;
906 }
907
908 static GList *
909 search_split_phrases(char *str)
910 {
911 char *tmp,*s,*d;
912 GList *ret=NULL;
913 s=str;
914 do {
915 tmp=g_strdup(s);
916 d=tmp+strlen(s)-1;
917 ret=g_list_append(ret, g_strdup(s));
918 while (d >= tmp) {
919 if (*d == ' ') {
920 *d = '\0';
921 ret=g_list_append(ret, g_strdup(tmp));
922 }
923 d--;
924 }
925 g_free(tmp);
926 do {
927 s++;
928 if (*s == ' ') {
929 s++;
930 break;
931 }
932 } while (*s != '\0');
933 } while (*s != '\0');
934 return ret;
935 }
936
937 static GList *
938 search_address_housenumber_real(GList *result_list, struct search_list *sl, char *street_name, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni)
939 {
940 struct search_list_result *slr;
941 struct coord_geo g;
942 struct coord c;
943
944 //dbg(0,"enter\n");
945
946 while ((slr=search_list_get_result(sl)))
947 {
948 // does the streetname of the housenumber match the street we want?
949 if (slr != NULL)
950 {
951 if (slr->street != NULL)
952 if ((street_name != NULL)&&(slr->street->name != NULL))
953 {
954
955 if (strcmp(slr->street->name, street_name)==0)
956 {
957 char *buffer;
958 // coords of result
959 c.x=slr->house_number->common.c->x;
960 c.y=slr->house_number->common.c->y;
961 transform_to_geo(slr->house_number->common.c->pro, &c, &g);
962 // SHN -> street with house number
963 // return a string like: "SHN:H111L5555:16.766:48.76:full address name is at the end"
964 // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 max. 15 chars -> this sould be max. about 335 chars long
965 if (slr->town->common.postal == NULL)
966 {
967 buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s,%.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name,slr->house_number->house_number);
968 }
969 else
970 {
971 buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s,%.7s %.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name,slr->house_number->house_number);
972 }
973
974 //dbg(0,"res=%s\n",buffer);
975
976 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
977 #ifdef HAVE_API_ANDROID
978 // return results to android as they come in ...
979 android_return_search_result(jni,buffer);
980 #endif
981 g_free(buffer);
982 }
983 }
984 }
985 }
986
987 //dbg(0,"return 2\n");
988 return result_list;
989 }
990
991 static GList *
992 search_address__street(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni)
993 {
994 //dbg(0,"enter\n");
995
996 struct search_list_result *slr = NULL;
997 GList *tmp=phrases;
998 int count=0;
999 struct coord_geo g;
1000 struct coord c;
1001 struct attr attr2;
1002
1003
1004 while ((slr=search_list_get_result(sl)))
1005 {
1006 char *buffer;
1007 char *buffer2;
1008
1009 if (slr->street)
1010 {
1011 // coords of result
1012 c.x=slr->street->common.c->x;
1013 c.y=slr->street->common.c->y;
1014 transform_to_geo(slr->street->common.c->pro, &c, &g);
1015
1016 // STR -> street
1017 // return a string like: "STR:H1111L5555:16.766:-48.76:full address name is at the end"
1018 // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 chars -> this sould be max. about 320 chars long
1019 if (slr->town->common.postal == NULL)
1020 {
1021 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name);
1022 }
1023 else
1024 {
1025 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.7s %.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name);
1026 }
1027 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
1028
1029 //dbg(0,"res=%s\n",buffer);
1030
1031 #ifdef HAVE_API_ANDROID
1032 // return results to android as they come in ...
1033 android_return_search_result(jni,buffer);
1034 #endif
1035 count++;
1036
1037 buffer2=g_strdup_printf("%s", slr->street->name);
1038
1039 while (tmp)
1040 {
1041 //dbg(0,"s0=%s\n",tmp->data);
1042 if (tmp != exclude1 && tmp != exclude2 && tmp != exclude3)
1043 {
1044 //dbg(0,"s=%s\n",tmp->data);
1045
1046 attr2.type=attr_house_number;
1047 attr2.u.str=tmp->data;
1048 search_list_search(sl, &attr2, partial);
1049
1050 result_list=search_address_housenumber_real(result_list, sl, buffer2, phrases, exclude1, exclude2, exclude3, partial, jni);
1051 }
1052 tmp=g_list_next(tmp);
1053 }
1054 }
1055
1056 if (buffer2)
1057 {
1058 g_free(buffer2);
1059 }
1060
1061 if (buffer)
1062 {
1063 g_free(buffer);
1064 }
1065 }
1066
1067 //dbg(0,"return 2\n");
1068 return result_list;
1069 }
1070
1071 static GList *
1072 search_address__town(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, int partial, struct jni_object *jni)
1073 {
1074 //dbg(0,"enter\n");
1075 struct search_list_result *slr;
1076 GList *tmp=phrases;
1077 int count=0;
1078 struct coord_geo g;
1079 struct coord c;
1080 struct attr attr;
1081
1082 while ((slr=search_list_get_result(sl)))
1083 {
1084 char *buffer;
1085 // coords of result
1086 c.x=slr->town->common.c->x;
1087 c.y=slr->town->common.c->y;
1088 transform_to_geo(slr->town->common.c->pro, &c, &g);
1089
1090 // TWN -> town
1091 if (slr->town->common.postal == NULL)
1092 {
1093 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s,%.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name);
1094 }
1095 else
1096 {
1097 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s,%.7s %.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name);
1098 }
1099
1100 //dbg(0,"res=%s\n",buffer);
1101
1102 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
1103 #ifdef HAVE_API_ANDROID
1104 // return results to android as they come in ...
1105 android_return_search_result(jni,buffer);
1106 #endif
1107
1108 count++;
1109 g_free(buffer);
1110
1111 while (tmp)
1112 {
1113 if (tmp != exclude1 && tmp != exclude2)
1114 {
1115 //dbg(0,"s=%s\n",tmp->data);
1116 attr.type=attr_street_name;
1117 attr.u.str=tmp->data;
1118 search_list_search(sl, &attr, partial);
1119 result_list=search_address__street(result_list, sl, phrases, exclude1, exclude2, tmp, partial, jni);
1120 }
1121 tmp=g_list_next(tmp);
1122 }
1123
1124 }
1125
1126 /*
1127 if (!count)
1128 {
1129 dbg(0,"return 1\n");
1130 return result_list;
1131 }
1132 */
1133
1134
1135 //dbg(0,"return 2\n");
1136 return result_list;
1137 }
1138
1139 static GList *
1140 search_address__country(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude, int partial, struct jni_object *jni)
1141 {
1142 GList *tmp=phrases;
1143 int count=0;
1144 struct attr attr;
1145 struct search_list_result *slr;
1146 //dbg(0,"enter\n");
1147
1148 while ((slr=search_list_get_result(sl)))
1149 {
1150 //dbg(0,"1 slr=%p\n",slr->country);
1151 //dbg(0,"2 slr=%s\n",slr->country->name);
1152 //dbg(0,"3 slr=%s\n",slr->country->iso2);
1153 count++;
1154 }
1155 //dbg(0,"count %d\n",count);
1156 if (!count)
1157 {
1158 //dbg(0,"return 1");
1159 return result_list;
1160 }
1161
1162 while (tmp)
1163 {
1164 if (tmp != exclude)
1165 {
1166 //dbg(0,"s=%s\n",tmp->data);
1167 attr.type=attr_town_or_district_name;
1168 attr.u.str=tmp->data;
1169 search_list_search(sl, &attr, partial);
1170 result_list=search_address__town(result_list, sl, phrases, exclude, tmp, partial, jni);
1171 }
1172 tmp=g_list_next(tmp);
1173 }
1174 //dbg(0,"return 2");
1175 return result_list;
1176 }
1177
1178
1179 struct country2 {
1180 int id;
1181 char *car;
1182 char *iso2;
1183 char *iso3;
1184 char *name;
1185 };
1186
1187 static struct country2 all_country_list[]= {
1188 { 20, "AND", "AD", "AND", /* 020 */ "Andorra"},
1189 {784, "UAE", "AE", "ARE", /* 784 */ "United Arab Emirates"},
1190 { 4, "AFG", "AF", "AFG", /* 004 */ "Afghanistan"},
1191 { 28, "AG", "AG", "ATG", /* 028 */ "Antigua and Barbuda"},
1192 {660, NULL, "AI", "AIA", /* 660 */ "Anguilla"},
1193 { 8, "AL", "AL", "ALB", /* 008 */ "Albania"},
1194 { 51, "ARM", "AM", "ARM", /* 051 */ "Armenia"},
1195 {530, "NA", "AN", "ANT", /* 530 */ "Netherlands Antilles"},
1196 { 24, "ANG", "AO", "AGO", /* 024 */ "Angola"},
1197 { 10, NULL, "AQ", "ATA", /* 010 */ "Antarctica"},
1198 { 32, "RA", "AR", "ARG", /* 032 */ "Argentina"},
1199 { 16, NULL, "AS", "ASM", /* 016 */ "American Samoa"},
1200 { 40, "A", "AT", "AUT", /* 040 */ "Austria"},
1201 { 36, "AUS", "AU", "AUS", /* 036 */ "Australia"},
1202 {533, "ARU", "AW", "ABW", /* 533 */ "Aruba"},
1203 {248, "AX", "AX", "ALA", /* 248 */ "Aland Islands"},
1204 { 31, "AZ", "AZ", "AZE", /* 031 */ "Azerbaijan"},
1205 { 70, "BiH", "BA", "BIH", /* 070 */ "Bosnia and Herzegovina"},
1206 { 52, "BDS", "BB", "BRB", /* 052 */ "Barbados"},
1207 { 50, "BD", "BD", "BGD", /* 050 */ "Bangladesh"},
1208 { 56, "B", "BE", "BEL", /* 056 */ "Belgium"},
1209 {854, "BF", "BF", "BFA", /* 854 */ "Burkina Faso"},
1210 {100, "BG", "BG", "BGR", /* 100 */ "Bulgaria"},
1211 { 48, "BRN", "BH", "BHR", /* 048 */ "Bahrain"},
1212 {108, "RU", "BI", "BDI", /* 108 */ "Burundi"},
1213 {204, "BJ", "BJ", "BEN", /* 204 */ "Benin"},
1214 {652, NULL, "BL", "BLM", /* 652 */ "Saint Barthelemy"},
1215 { 60, NULL, "BM", "BMU", /* 060 */ "Bermuda"},
1216 { 96, "BRU", "BN", "BRN", /* 096 */ "Brunei Darussalam"},
1217 { 68, "BOL", "BO", "BOL", /* 068 */ "Bolivia"},
1218 { 76, "BR", "BR", "BRA", /* 076 */ "Brazil"},
1219 { 44, "BS", "BS", "BHS", /* 044 */ "Bahamas"},
1220 { 64, "BHT", "BT", "BTN", /* 064 */ "Bhutan"},
1221 { 74, NULL, "BV", "BVT", /* 074 */ "Bouvet Island"},
1222 { 72, "RB", "BW", "BWA", /* 072 */ "Botswana"},
1223 {112, "BY", "BY", "BLR", /* 112 */ "Belarus"},
1224 { 84, "BZ", "BZ", "BLZ", /* 084 */ "Belize"},
1225 {124, "CDN", "CA", "CAN", /* 124 */ "Canada"},
1226 {166, NULL, "CC", "CCK", /* 166 */ "Cocos (Keeling) Islands"},
1227 {180, "CGO", "CD", "COD", /* 180 */ "Congo, Democratic Republic of the"},
1228 {140, "RCA", "CF", "CAF", /* 140 */ "Central African Republic"},
1229 {178, NULL, "CG", "COG", /* 178 */ "Congo"},
1230 {756, "CH", "CH", "CHE", /* 756 */ "Switzerland"},
1231 {384, "CI", "CI", "CIV", /* 384 */ "Cote d'Ivoire"},
1232 {184, NULL, "CK", "COK", /* 184 */ "Cook Islands"},
1233 {152, "RCH", "CL", "CHL", /* 152 */ "Chile"},
1234 {120, "CAM", "CM", "CMR", /* 120 */ "Cameroon"},
1235 {156, "RC", "CN", "CHN", /* 156 */ "China"},
1236 {170, "CO", "CO", "COL", /* 170 */ "Colombia"},
1237 {188, "CR", "CR", "CRI", /* 188 */ "Costa Rica"},
1238 {192, "C", "CU", "CUB", /* 192 */ "Cuba"},
1239 {132, "CV", "CV", "CPV", /* 132 */ "Cape Verde"},
1240 {162, NULL, "CX", "CXR", /* 162 */ "Christmas Island"},
1241 {196, "CY", "CY", "CYP", /* 196 */ "Cyprus"},
1242 {203, "CZ", "CZ", "CZE", /* 203 */ "Czech Republic"},
1243 {276, "D", "DE", "DEU", /* 276 */ "Germany"},
1244 {262, "DJI", "DJ", "DJI", /* 262 */ "Djibouti"},
1245 {208, "DK", "DK", "DNK", /* 208 */ "Denmark"},
1246 {212, "WD", "DM", "DMA", /* 212 */ "Dominica"},
1247 {214, "DOM", "DO", "DOM", /* 214 */ "Dominican Republic"},
1248 { 12, "DZ", "DZ", "DZA", /* 012 */ "Algeria"},
1249 {218, "EC", "EC", "ECU", /* 218 */ "Ecuador"},
1250 {233, "EST", "EE", "EST", /* 233 */ "Estonia"},
1251 {818, "ET", "EG", "EGY", /* 818 */ "Egypt"},
1252 {732, "WSA", "EH", "ESH", /* 732 */ "Western Sahara"},
1253 {232, "ER", "ER", "ERI", /* 232 */ "Eritrea"},
1254 {724, "E", "ES", "ESP", /* 724 */ "Spain"},
1255 {231, "ETH", "ET", "ETH", /* 231 */ "Ethiopia"},
1256 {246, "FIN", "FI", "FIN", /* 246 */ "Finland"},
1257 {242, "FJI", "FJ", "FJI", /* 242 */ "Fiji"},
1258 {238, NULL, "FK", "FLK", /* 238 */ "Falkland Islands (Malvinas)"},
1259 {583, "FSM", "FM", "FSM", /* 583 */ "Micronesia, Federated States of"},
1260 {234, "FO", "FO", "FRO", /* 234 */ "Faroe Islands"},
1261 {250, "F", "FR", "FRA", /* 250 */ "France"},
1262 {266, "G", "GA", "GAB", /* 266 */ "Gabon"},
1263 {826, "GB", "GB", "GBR", /* 826 */ "United Kingdom"},
1264 {308, "WG", "GD", "GRD", /* 308 */ "Grenada"},
1265 {268, "GE", "GE", "GEO", /* 268 */ "Georgia"},
1266 {254, NULL, "GF", "GUF", /* 254 */ "French Guiana"},
1267 {831, NULL, "GG", "GGY", /* 831 */ "Guernsey"},
1268 {288, "GH", "GH", "GHA", /* 288 */ "Ghana"},
1269 {292, "GBZ", "GI", "GIB", /* 292 */ "Gibraltar"},
1270 {304, "KN", "GL", "GRL", /* 304 */ "Greenland"},
1271 {270, "WAG", "GM", "GMB", /* 270 */ "Gambia"},
1272 {324, "RG", "GN", "GIN", /* 324 */ "Guinea"},
1273 {312, NULL, "GP", "GLP", /* 312 */ "Guadeloupe"},
1274 {226, "GQ", "GQ", "GNQ", /* 226 */ "Equatorial Guinea"},
1275 {300, "GR", "GR", "GRC", /* 300 */ "Greece"},
1276 {239, NULL, "GS", "SGS", /* 239 */ "South Georgia and the South Sandwich Islands"},
1277 {320, "GCA", "GT", "GTM", /* 320 */ "Guatemala"},
1278 {316, NULL, "GU", "GUM", /* 316 */ "Guam"},
1279 {624, "GUB", "GW", "GNB", /* 624 */ "Guinea-Bissau"},
1280 {328, "GUY", "GY", "GUY", /* 328 */ "Guyana"},
1281 {344, "HK", "HK", "HKG", /* 344 */ "Hong Kong"},
1282 {334, NULL, "HM", "HMD", /* 334 */ "Heard Island and McDonald Islands"},
1283 {340, "HN", "HN", "HND", /* 340 */ "Honduras"},
1284 {191, "HR", "HR", "HRV", /* 191 */ "Croatia"},
1285 {332, "RH", "HT", "HTI", /* 332 */ "Haiti"},
1286 {348, "H", "HU", "HUN", /* 348 */ "Hungary"},
1287 {360, "RI", "ID", "IDN", /* 360 */ "Indonesia"},
1288 {372, "IRL", "IE", "IRL", /* 372 */ "Ireland"},
1289 {376, "IL", "IL", "ISR", /* 376 */ "Israel"},
1290 {833, NULL, "IM", "IMN", /* 833 */ "Isle of Man"},
1291 {356, "IND", "IN", "IND", /* 356 */ "India"},
1292 { 86, NULL, "IO", "IOT", /* 086 */ "British Indian Ocean Territory"},
1293 {368, "IRQ", "IQ", "IRQ", /* 368 */ "Iraq"},
1294 {364, "IR", "IR", "IRN", /* 364 */ "Iran, Islamic Republic of"},
1295 {352, "IS", "IS", "ISL", /* 352 */ "Iceland"},
1296 {380, "I", "IT", "ITA", /* 380 */ "Italy"},
1297 {832, NULL, "JE", "JEY", /* 832 */ "Jersey"},
1298 {388, "JA", "JM", "JAM", /* 388 */ "Jamaica"},
1299 {400, "JOR", "JO", "JOR", /* 400 */ "Jordan"},
1300 {392, "J", "JP", "JPN", /* 392 */ "Japan"},
1301 {404, "EAK", "KE", "KEN", /* 404 */ "Kenya"},
1302 {417, "KS", "KG", "KGZ", /* 417 */ "Kyrgyzstan"},
1303 {116, "K", "KH", "KHM", /* 116 */ "Cambodia"},
1304 {296, "KIR", "KI", "KIR", /* 296 */ "Kiribati"},
1305 {174, "COM", "KM", "COM", /* 174 */ "Comoros"},
1306 {659, "KAN", "KN", "KNA", /* 659 */ "Saint Kitts and Nevis"},
1307 {408, "KP", "KP", "PRK", /* 408 */ "Korea, Democratic People's Republic of"},
1308 {410, "ROK", "KR", "KOR", /* 410 */ "Korea, Republic of"},
1309 {414, "KWT", "KW", "KWT", /* 414 */ "Kuwait"},
1310 {136, NULL, "KY", "CYM", /* 136 */ "Cayman Islands"},
1311 {398, "KZ", "KZ", "KAZ", /* 398 */ "Kazakhstan"},
1312 {418, "LAO", "LA", "LAO", /* 418 */ "Lao People's Democratic Republic"},
1313 {422, "RL", "LB", "LBN", /* 422 */ "Lebanon"},
1314 {662, "WL", "LC", "LCA", /* 662 */ "Saint Lucia"},
1315 {438, "FL", "LI", "LIE", /* 438 */ "Liechtenstein"},
1316 {144, "CL", "LK", "LKA", /* 144 */ "Sri Lanka"},
1317 {430, "LB", "LR", "LBR", /* 430 */ "Liberia"},
1318 {426, "LS", "LS", "LSO", /* 426 */ "Lesotho"},
1319 {440, "LT", "LT", "LTU", /* 440 */ "Lithuania"},
1320 {442, "L", "LU", "LUX", /* 442 */ "Luxembourg"},
1321 {428, "LV", "LV", "LVA", /* 428 */ "Latvia"},
1322 {434, "LAR", "LY", "LBY", /* 434 */ "Libyan Arab Jamahiriya"},
1323 {504, "MA", "MA", "MAR", /* 504 */ "Morocco"},
1324 {492, "MC", "MC", "MCO", /* 492 */ "Monaco"},
1325 {498, "MD", "MD", "MDA", /* 498 */ "Moldova, Republic of"},
1326 {499, "MNE", "ME", "MNE", /* 499 */ "Montenegro"},
1327 {663, NULL, "MF", "MAF", /* 663 */ "Saint Martin (French part)"},
1328 {450, "RM", "MG", "MDG", /* 450 */ "Madagascar"},
1329 {584, "MH", "MH", "MHL", /* 584 */ "Marshall Islands"},
1330 {807, "MK", "MK", "MKD", /* 807 */ "Macedonia, the former Yugoslav Republic of"},
1331 {466, "RMM", "ML", "MLI", /* 466 */ "Mali"},
1332 {104, "MYA", "MM", "MMR", /* 104 */ "Myanmar"},
1333 {496, "MGL", "MN", "MNG", /* 496 */ "Mongolia"},
1334 {446, NULL, "MO", "MAC", /* 446 */ "Macao"},
1335 {580, NULL, "MP", "MNP", /* 580 */ "Northern Mariana Islands"},
1336 {474, NULL, "MQ", "MTQ", /* 474 */ "Martinique"},
1337 {478, "RIM", "MR", "MRT", /* 478 */ "Mauritania"},
1338 {500, NULL, "MS", "MSR", /* 500 */ "Montserrat"},
1339 {470, "M", "MT", "MLT", /* 470 */ "Malta"},
1340 {480, "MS", "MU", "MUS", /* 480 */ "Mauritius"},
1341 {462, "MV", "MV", "MDV", /* 462 */ "Maldives"},
1342 {454, "MW", "MW", "MWI", /* 454 */ "Malawi"},
1343 {484, "MEX", "MX", "MEX", /* 484 */ "Mexico"},
1344 {458, "MAL", "MY", "MYS", /* 458 */ "Malaysia"},
1345 {508, "MOC", "MZ", "MOZ", /* 508 */ "Mozambique"},
1346 {516, "NAM", "NA", "NAM", /* 516 */ "Namibia"},
1347 {540, "NCL", "NC", "NCL", /* 540 */ "New Caledonia"},
1348 {562, "RN", "NE", "NER", /* 562 */ "Niger"},
1349 {574, NULL, "NF", "NFK", /* 574 */ "Norfolk Island"},
1350 {566, "NGR", "NG", "NGA", /* 566 */ "Nigeria"},
1351 {558, "NIC", "NI", "NIC", /* 558 */ "Nicaragua"},
1352 {528, "NL", "NL", "NLD", /* 528 */ "Netherlands"},
1353 {578, "N", "NO", "NOR", /* 578 */ "Norway"},
1354 {524, "NEP", "NP", "NPL", /* 524 */ "Nepal"},
1355 {520, "NAU", "NR", "NRU", /* 520 */ "Nauru"},
1356 {570, NULL, "NU", "NIU", /* 570 */ "Niue"},
1357 {554, "NZ", "NZ", "NZL", /* 554 */ "New Zealand"},
1358 {512, "OM", "OM", "OMN", /* 512 */ "Oman"},
1359 {591, "PA", "PA", "PAN", /* 591 */ "Panama"},
1360 {604, "PE", "PE", "PER", /* 604 */ "Peru"},
1361 {258, NULL, "PF", "PYF", /* 258 */ "French Polynesia"},
1362 {598, "PNG", "PG", "PNG", /* 598 */ "Papua New Guinea"},
1363 {608, "RP", "PH", "PHL", /* 608 */ "Philippines"},
1364 {586, "PK", "PK", "PAK", /* 586 */ "Pakistan"},
1365 {616, "PL", "PL", "POL", /* 616 */ "Poland"},
1366 {666, NULL, "PM", "SPM", /* 666 */ "Saint Pierre and Miquelon"},
1367 {612, NULL, "PN", "PCN", /* 612 */ "Pitcairn"},
1368 {630, "PRI", "PR", "PRI", /* 630 */ "Puerto Rico"},
1369 {275, "AUT", "PS", "PSE", /* 275 */ "Palestinian Territory, Occupied"},
1370 {620, "P", "PT", "PRT", /* 620 */ "Portugal"},
1371 {585, "PAL", "PW", "PLW", /* 585 */ "Palau"},
1372 {600, "PY", "PY", "PRY", /* 600 */ "Paraguay"},
1373 {634, "Q", "QA", "QAT", /* 634 */ "Qatar"},
1374 {638, NULL, "RE", "REU", /* 638 */ "Reunion"},
1375 {642, "RO", "RO", "ROU", /* 642 */ "Romania"},
1376 {688, "SRB", "RS", "SRB", /* 688 */ "Serbia"},
1377 {643, "RUS", "RU", "RUS", /* 643 */ "Russian Federation"},
1378 {646, "RWA", "RW", "RWA", /* 646 */ "Rwanda"},
1379 {682, "KSA", "SA", "SAU", /* 682 */ "Saudi Arabia"},
1380 { 90, "SOL", "SB", "SLB", /* 090 */ "Solomon Islands"},
1381 {690, "SY", "SC", "SYC", /* 690 */ "Seychelles"},
1382 {736, "SUD", "SD", "SDN", /* 736 */ "Sudan"},
1383 {752, "S", "SE", "SWE", /* 752 */ "Sweden"},
1384 {702, "SGP", "SG", "SGP", /* 702 */ "Singapore"},
1385 {654, NULL, "SH", "SHN", /* 654 */ "Saint Helena"},
1386 {705, "SLO", "SI", "SVN", /* 705 */ "Slovenia"},
1387 {744, NULL, "SJ", "SJM", /* 744 */ "Svalbard and Jan Mayen"},
1388 {703, "SK", "SK", "SVK", /* 703 */ "Slovakia"},
1389 {694, "WAL", "SL", "SLE", /* 694 */ "Sierra Leone"},
1390 {674, "RSM", "SM", "SMR", /* 674 */ "San Marino"},
1391 {686, "SN", "SN", "SEN", /* 686 */ "Senegal"},
1392 {706, "SO", "SO", "SOM", /* 706 */ "Somalia"},
1393 {740, "SME", "SR", "SUR", /* 740 */ "Suriname"},
1394 {678, "STP", "ST", "STP", /* 678 */ "Sao Tome and Principe"},
1395 {222, "ES", "SV", "SLV", /* 222 */ "El Salvador"},
1396 {760, "SYR", "SY", "SYR", /* 760 */ "Syrian Arab Republic"},
1397 {748, "SD", "SZ", "SWZ", /* 748 */ "Swaziland"},
1398 {796, NULL, "TC", "TCA", /* 796 */ "Turks and Caicos Islands"},
1399 {148, "TD", "TD", "TCD", /* 148 */ "Chad"},
1400 {260, "ARK", "TF", "ATF", /* 260 */ "French Southern Territories"},
1401 {768, "RT", "TG", "TGO", /* 768 */ "Togo"},
1402 {764, "T", "TH", "THA", /* 764 */ "Thailand"},
1403 {762, "TJ", "TJ", "TJK", /* 762 */ "Tajikistan"},
1404 {772, NULL, "TK", "TKL", /* 772 */ "Tokelau"},
1405 {626, "TL", "TL", "TLS", /* 626 */ "Timor-Leste"},
1406 {795, "TM", "TM", "TKM", /* 795 */ "Turkmenistan"},
1407 {788, "TN", "TN", "TUN", /* 788 */ "Tunisia"},
1408 {776, "TON", "TO", "TON", /* 776 */ "Tonga"},
1409 {792, "TR", "TR", "TUR", /* 792 */ "Turkey"},
1410 {780, "TT", "TT", "TTO", /* 780 */ "Trinidad and Tobago"},
1411 {798, "TUV", "TV", "TUV", /* 798 */ "Tuvalu"},
1412 {158, NULL, "TW", "TWN", /* 158 */ "Taiwan, Province of China"},
1413 {834, "EAT", "TZ", "TZA", /* 834 */ "Tanzania, United Republic of"},
1414 {804, "UA", "UA", "UKR", /* 804 */ "Ukraine"},
1415 {800, "EAU", "UG", "UGA", /* 800 */ "Uganda"},
1416 {581, NULL, "UM", "UMI", /* 581 */ "United States Minor Outlying Islands"},
1417 {840, "USA", "US", "USA", /* 840 */ "United States"},
1418 {858, "ROU", "UY", "URY", /* 858 */ "Uruguay"},
1419 {860, "UZ", "UZ", "UZB", /* 860 */ "Uzbekistan"},
1420 {336, "SCV", "VA", "VAT", /* 336 */ "Holy See (Vatican City State)"},
1421 {670, "WV", "VC", "VCT", /* 670 */ "Saint Vincent and the Grenadines"},
1422 {862, "YV", "VE", "VEN", /* 862 */ "Venezuela"},
1423 { 92, NULL, "VG", "VGB", /* 092 */ "Virgin Islands, British"},
1424 {850, NULL, "VI", "VIR", /* 850 */ "Virgin Islands, U.S."},
1425 {704, "VN", "VN", "VNM", /* 704 */ "Viet Nam"},
1426 {548, "VAN", "VU", "VUT", /* 548 */ "Vanuatu"},
1427 {876, NULL, "WF", "WLF", /* 876 */ "Wallis and Futuna"},
1428 {882, "WS", "WS", "WSM", /* 882 */ "Samoa"},
1429 {887, "YAR", "YE", "YEM", /* 887 */ "Yemen"},
1430 {175, NULL, "YT", "MYT", /* 175 */ "Mayotte"},
1431 {710, "ZA", "ZA", "ZAF", /* 710 */ "South Africa"},
1432 {894, "Z", "ZM", "ZMB", /* 894 */ "Zambia"},
1433 {716, "ZW", "ZW", "ZWE", /* 716 */ "Zimbabwe"},
1434 {999, "*", "*", "*", "Unknown"},
1435 };
1436
1437
1438 static int
1439 ascii_cmp_local(char *name, char *match, int partial)
1440 {
1441 char *s=linguistics_casefold(name);
1442 int ret=linguistics_compare(s,match,partial);
1443 g_free(s);
1444 return ret;
1445 }
1446
1447 struct navit *global_navit;
1448
1449
1450 void
1451 search_full_world(char *addr, int partial, int search_order, struct jni_object *jni,struct coord_geo *search_center, int search_radius)
1452 {
1453 struct item *item;
1454 struct map_rect *mr=NULL;
1455 struct mapset *ms;
1456 struct mapset_handle *msh;
1457 struct map* map = NULL;
1458 struct attr map_name_attr;
1459 struct attr attr;
1460
1461 char *str=search_fix_spaces(addr);
1462 GList *phrases=search_split_phrases(str);
1463 GList *phrases_first;
1464 phrases_first=phrases;
1465
1466 ms=global_navit->mapsets->data;
1467 msh=mapset_open(ms);
1468
1469 struct pcoord center99;
1470 int search_radius_this=0;
1471 dbg(0,"in lat=%f,lng=%f\n",search_center->lat,search_center->lng);
1472 if ((search_center->lat == 0)&&(search_center->lng == 0))
1473 {
1474 center99.x=0;
1475 center99.y=0;
1476 search_radius_this=21000000;
1477 }
1478 else
1479 {
1480 struct coord c99;
1481 transform_from_geo(projection_mg, search_center, &c99);
1482 center99.x=c99.x;
1483 center99.y=c99.y;
1484 search_radius_this=search_radius;
1485 }
1486 dbg(0,"out x=%d,y=%d,r=%d\n",center99.x,center99.y,search_radius_this);
1487
1488 struct map_selection *sel=map_selection_rect_new(&center99, search_radius_this, search_order);
1489 sel->range.min=type_town_label;
1490 sel->range.max=type_area;
1491
1492 while (msh && (map=mapset_next(msh, 0)))
1493 {
1494 if(map_get_attr(map,attr_name, &map_name_attr,NULL))
1495 {
1496 if (strncmp("_ms_sdcard_map:", map_name_attr.u.str, 15) == 0)
1497 {
1498 if (strncmp("_ms_sdcard_map:/sdcard/zanavi/maps/navitmap", map_name_attr.u.str, 38) == 0)
1499 {
1500 // its an sdcard map
1501 //dbg(0,"map name=%s",map_name_attr.u.str);
1502 // mr=map_rect_new(map, NULL);
1503 mr=map_rect_new(map, sel);
1504 if (mr)
1505 {
1506 char *streetname_last=NULL;
1507
1508 while ((item=map_rect_get_item(mr)))
1509 {
1510 if ( (item_is_town(*item)) || (item_is_district(*item)) )
1511 {
1512 struct search_list_town *p=NULL;
1513
1514 if (item_attr_get(item, attr_town_name, &attr))
1515 {
1516 p=search_list_town_new(item);
1517 char *buffer=NULL;
1518 // coords of result
1519 struct coord_geo g;
1520 struct coord c;
1521 c.x=p->common.c->x;
1522 c.y=p->common.c->y;
1523 int calc_geo=0;
1524
1525 // dbg(0,"town name=%s\n", attr.u.str);
1526
1527 phrases=phrases_first;
1528 while (phrases)
1529 {
1530 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1531 {
1532 // dbg(0,"matched town name=%s want=%s\n", attr.u.str, phrases->data);
1533 if (calc_geo==0)
1534 {
1535 transform_to_geo(p->common.c->pro, &c, &g);
1536 // TWN -> town
1537 calc_geo=1;
1538 }
1539 if (p->common.postal == NULL)
1540 {
1541 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.town_name);
1542 }
1543 else
1544 {
1545 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,p->common.town_name);
1546 }
1547 #ifdef HAVE_API_ANDROID
1548 // return results to android as they come in ...
1549 android_return_search_result(jni,buffer);
1550 #endif
1551 }
1552 phrases=g_list_next(phrases);
1553
1554 }
1555 if (buffer)
1556 {
1557 g_free(buffer);
1558 }
1559 search_list_town_destroy(p);
1560 }
1561 }
1562 else if (item_is_street(*item))
1563 {
1564
1565 struct search_list_street *p=NULL;
1566
1567 if (item_attr_get(item, attr_label, &attr))
1568 {
1569 // dbg(0,"street1=%s\n",map_convert_string(item->map, attr.u.str));
1570 if ( (streetname_last==NULL) || (strcmp(streetname_last,attr.u.str) != 0) )
1571 {
1572 // dbg(0,"street2=%s\n",map_convert_string(item->map, attr.u.str));
1573 streetname_last=g_strdup_printf("%s",attr.u.str);
1574
1575 p=search_list_street_new(item);
1576 char *buffer=NULL;
1577 // coords of result
1578 struct coord_geo g;
1579 struct coord c;
1580 c.x=p->common.c->x;
1581 c.y=p->common.c->y;
1582 int calc_geo=0;
1583
1584 phrases=phrases_first;
1585 while (phrases)
1586 {
1587 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1588 {
1589 if (calc_geo==0)
1590 {
1591 transform_to_geo(p->common.c->pro, &c, &g);
1592 calc_geo=1;
1593 }
1594 if (p->common.postal == NULL)
1595 {
1596 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,attr.u.str);
1597 }
1598 else
1599 {
1600 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,attr.u.str);
1601 }
1602 //dbg(0,"street3=%s\n",buffer);
1603 #ifdef HAVE_API_ANDROID
1604 // return results to android as they come in ...
1605 android_return_search_result(jni,buffer);
1606 #endif
1607 }
1608 phrases=g_list_next(phrases);
1609 }
1610 if (buffer)
1611 {
1612 g_free(buffer);
1613 }
1614 search_list_street_destroy(p);
1615 }
1616 }
1617 else if (item_attr_get(item, attr_street_name_systematic, &attr))
1618 {
1619 //dbg(0,"street systematic=%s\n",map_convert_string(item->map, attr.u.str));
1620
1621 p=search_list_street_new(item);
1622 char *buffer=NULL;
1623 // coords of result
1624 struct coord_geo g;
1625 struct coord c;
1626 c.x=p->common.c->x;
1627 c.y=p->common.c->y;
1628 int calc_geo=0;
1629
1630 phrases=phrases_first;
1631 while (phrases)
1632 {
1633 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1634 {
1635 if (calc_geo==0)
1636 {
1637 transform_to_geo(p->common.c->pro, &c, &g);
1638 calc_geo=1;
1639 }
1640 if (p->common.postal == NULL)
1641 {
1642 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,attr.u.str);
1643 }
1644 else
1645 {
1646 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,attr.u.str);
1647 }
1648 #ifdef HAVE_API_ANDROID
1649 // return results to android as they come in ...
1650 android_return_search_result(jni,buffer);
1651 #endif
1652 }
1653 phrases=g_list_next(phrases);
1654 }
1655 if (buffer)
1656 {
1657 g_free(buffer);
1658 }
1659 search_list_street_destroy(p);
1660
1661 }
1662 }
1663 }
1664 g_free(streetname_last);
1665 map_rect_destroy(mr);
1666 }
1667 }
1668 }
1669 }
1670 }
1671
1672 map_selection_destroy(sel);
1673
1674 if (phrases)
1675 {
1676 g_list_free(phrases);
1677 }
1678 g_free(str);
1679
1680 mapset_close(msh);
1681 }
1682
1683
1684 GList *
1685 search_by_address(GList *result_list,struct mapset *ms, char *addr, int partial, struct jni_object *jni, int search_country_flags, char *search_country_string)
1686 {
1687 char *str=search_fix_spaces(addr);
1688 GList *tmp,*phrases=search_split_phrases(str);
1689 GList *phrases_first;
1690 GList *ret = NULL;
1691 struct search_list *sl;
1692 struct attr attr;
1693 attr.type=attr_country_all;
1694 tmp=phrases;
1695 phrases_first=phrases;
1696 sl=search_list_new(ms);
1697
1698
1699 // search_full_world(addr,partial,jni,search_country_flags,search_country_string);
1700
1701 dbg(0,"-- START --\n");
1702
1703 // search in "attr_country_all" -> seems to be when item is in no country?? or not
1704 /*
1705 dbg(0,"-- country all start --\n");
1706 while (tmp)
1707 {
1708 dbg(0,"s=%s\n",tmp->data);
1709 attr.u.str=tmp->data;
1710 search_list_search(sl, &attr, partial);
1711 result_list=search_address__country(result_list, sl, phrases, tmp, partial, jni);
1712 tmp=g_list_next(tmp);
1713 }
1714 dbg(0,"-- country all end --\n");
1715 */
1716
1717 // normal search stuff -------- START ----------
1718 if (search_country_flags == 1)
1719 {
1720 dbg(0,"-- country default start --\n");
1721 while (phrases)
1722 {
1723 // dbg(0,"s=%s\n",phrases->data);
1724 // set default country
1725 search_list_search(sl, country_default(), 0);
1726 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1727 phrases=g_list_next(phrases);
1728 }
1729 dbg(0,"-- country default end --\n");
1730 }
1731 else if (search_country_flags == 2)
1732 {
1733 dbg(0,"-- country sel:%s start --\n",search_country_string);
1734 // set a country
1735 struct attr country;
1736 country.type=attr_country_iso2;
1737 country.u.str=search_country_string;
1738 while (phrases)
1739 {
1740 // dbg(0,"s=%s\n",phrases->data);
1741 search_list_search(sl, &country, 0);
1742 // set a country
1743 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1744 phrases=g_list_next(phrases);
1745 }
1746 dbg(0,"-- country sel:%s end --\n",search_country_string);
1747 }
1748 else // flags==3
1749 {
1750 dbg(0,"-- country all start --\n");
1751 // search all countries!! could take a really long time!!
1752 struct attr country;
1753 int j2=sizeof(all_country_list) / sizeof(all_country_list[0]);
1754 int j1;
1755 for(j1=0;j1 < j2;j1++)
1756 {
1757 if (all_country_list[j1].iso2 != NULL)
1758 {
1759 phrases=phrases_first;
1760 while (phrases)
1761 {
1762 // dbg(0,"s country=%s\n",all_country_list[j1].iso2);
1763 // dbg(0,"s=%s\n",phrases->data);
1764 country.type=attr_country_iso2;
1765 country.u.str=all_country_list[j1].iso2;
1766 search_list_search(sl, &country, 0);
1767 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1768 phrases=g_list_next(phrases);
1769 }
1770 }
1771 }
1772 dbg(0,"-- country all end --\n");
1773 }
1774 // normal search stuff -------- END ----------
1775
1776 if (phrases_first)
1777 {
1778 g_list_free(phrases_first);
1779 }
1780
1781 dbg(0,"-- END --\n");
1782
1783 g_free(str);
1784 return ret;
1785 }
1786
1787

   
Visit the ZANavi Wiki