/[zanavi_public1]/navit/navit/maptool/osm.c
ZANavi

Contents of /navit/navit/maptool/osm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 57 - (show annotations) (download)
Sun Mar 19 16:46:08 2017 UTC (7 years ago) by zoff99
File MIME type: text/plain
File size: 245141 byte(s)
updates
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2013 Zoff <zoff@zoff.cc>
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 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2011 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #include "maptool.h"
40 #include "linguistics.h"
41
42 #ifdef MAPTOOL_USE_SQL
43 // sqlite 3
44 //#include "sqlite3.h"
45 // sqlite 3
46 #else
47 #ifndef SQL_DUMMY_
48 #define SQL_DUMMY_ 1
49 int sqlite3_reset(void* v)
50 {
51 return 0;
52 }
53 int sqlite3_step(void* v)
54 {
55 return 0;
56 }
57 int sqlite3_exec(void* v1, void* v2, void* v3, void* v4, void* v5)
58 {
59 return 0;
60 }
61 int sqlite3_bind_int64(void* v, int i, long l)
62 {
63 return 0;
64 }
65 int sqlite3_bind_int(void* v, int i, int i2)
66 {
67 return 0;
68 }
69 int sqlite3_bind_double(void* v, int i, double d)
70 {
71 return 0;
72 }
73 long sqlite3_column_int64(void* v, int i)
74 {
75 return 0;
76 }
77 double sqlite3_column_double(void* v, int i)
78 {
79 return 0;
80 }
81 int sqlite3_column_int(void* v, int i)
82 {
83 return 0;
84 }
85 int sqlite3_bind_text(void* v, int i, char* c, int i2, int i3)
86 {
87 return 0;
88 }
89 #endif
90
91 #endif
92
93 #ifdef MAPTOOL_USE_STRINDEX_COMPRESSION
94
95 // compression
96 #define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
97
98 #define MINIZ_NO_STDIO
99 #define MINIZ_NO_ARCHIVE_APIS
100 #define MINIZ_NO_TIME
101 #define MINIZ_NO_ZLIB_APIS
102 #define MINIZ_NO_MALLOC
103
104 #define MINIZ_HEADER_FILE_ONLY
105 #include "miniz.c"
106 // compression
107
108 #endif
109
110 #include <sys/stat.h>
111 #include <unistd.h>
112 #include <time.h>
113 #include <stdio.h>
114 #include <string.h>
115 #include <stdlib.h>
116 #include <math.h>
117 #include "debug.h"
118 #include "linguistics.h"
119 #include "country.h"
120 #include "file.h"
121
122 /* triangulate */
123 /*
124 #ifdef SINGLE
125 #define REAL float
126 #else // not SINGLE
127 #define REAL double
128 #endif // not SINGLE
129 #include "poly_to_tri002/triangle.h"
130 */
131
132 #include <signal.h>
133
134 #ifdef MAPTOOL_TRIANGULATE
135
136 #include "p2t/poly2tri.h"
137 #include "refine/refine.h"
138 #define MAX_HOLES_PER_POLY 50
139
140 #endif
141
142 /* triangulate */
143
144 #define _FILE_OFFSET_BITS 64
145 #define _LARGEFILE_SOURCE
146 #define _LARGEFILE64_SOURCE
147
148
149
150
151 #include "osm_countrytable.h"
152
153
154 // function def --
155 static long long osm_process_street_by_manual_country_borders(GList *bl_manual, struct coord *c);
156 static int osm_check_all_inner_polys_of_country_id(int country_id, struct coord *c);
157 // function def --
158
159
160 extern int doway2poi;
161
162 static int in_way, in_node, in_relation;
163 static osmid nodeid, wayid;
164 osmid current_id;
165
166 static GHashTable *attr_hash, *country_table_hash;
167
168 void add_boundary_to_db(long long current_id, int admin_level, struct item_bin *item_bin_3, struct relation_member *memb);
169 void add_waynode_to_db(osmid ref, int c_count);
170
171 static char *attr_present;
172 static int attr_present_count;
173
174 static struct item_bin item;
175
176 int maxspeed_attr_value;
177
178 char debug_attr_buffer[BUFFER_SIZE];
179
180 int flags[4];
181
182 int flags_attr_value;
183
184 struct attr_bin osmid_attr;
185 osmid osmid_attr_value;
186
187 char is_in_buffer[BUFFER_SIZE];
188
189 char attr_strings_buffer[BUFFER_SIZE * 16];
190 int attr_strings_buffer_len;
191 int alt_name_found = 0;
192 int real_alt_name_found = 0;
193
194 struct coord coord_buffer[65536];
195 struct coord coord_buffer_3[65536];
196
197 struct attr_mapping
198 {
199 enum item_type type;
200 int attr_present_idx_count;
201 int attr_present_idx[0];
202 };
203
204 static void nodes_ref_item_bin(struct item_bin *ib, int local_thread_num, osmid this_way_id);
205
206 static struct attr_mapping **attr_mapping_node;
207 static int attr_mapping_node_count;
208 static struct attr_mapping **attr_mapping_way;
209 static int attr_mapping_way_count;
210 static struct attr_mapping **attr_mapping_way2poi;
211 static int attr_mapping_way2poi_count;
212
213 static char *attr_present;
214 static int attr_present_count;
215
216 static long long seekpos1;
217
218 enum attr_strings
219 {
220 attr_string_phone, attr_string_fax, attr_string_email, attr_string_url, attr_string_street_name, attr_string_street_name_systematic, attr_string_house_number, attr_string_label, attr_string_label_alt, attr_string_label_real_alt, attr_string_postal,
221 attr_string_population, attr_string_county_name, attr_string_colour_, attr_string_capacity,
222 attr_string_street_name_systematic_nat,
223 attr_string_street_name_systematic_int,
224 attr_string_ref,
225 attr_string_exit_to,
226 attr_string_street_destination,
227 attr_string_street_lanes,
228 // ----------
229 attr_string_street_lanes_forward,
230 attr_string_street_turn_lanes,
231 attr_string_street_destination_lanes,
232 // ----- last ----
233 attr_string_last,
234 };
235
236 char *attr_strings[attr_string_last];
237
238 char *osm_types[] = { "unknown", "node", "way", "relation" };
239
240 double save_node_lat;
241 double save_node_lon;
242
243 // #define REF_X 1073741834 --> maptool.h // lat
244 // #define REF_Y 240000000 --> maptool.h // lon
245 #define IS_REF(c) (check_decode_nodeid(c)) // --> new method
246 // #define IS_REF(c) ((c).x >= (1 << 30)) // --> old method again
247
248 // #define SET_REF(c,ref) (encode_nodeid(c, ref))
249 // #define REF(c) (decode_nodeid(c))
250
251 void encode_nodeid(struct coord *c, osmid ref)
252 {
253 //fprintf(stderr, "ref=%lld bb=%lld 1=%lld\n", ref, (1 << 31), ((1 << 31) >> 31));
254 //fprintf(stderr, "11=%lld 22=%lld\n", (ref >> 31), (1 << 30));
255 //fprintf(stderr, "encode_nodeid:ref=%lld\n", ref);
256
257 if (ref > 0xffffffff)
258 {
259 c->x = (1 << 30) | (ref >> 32 );
260 c->y = ref & (0xffffffff);
261 }
262 else
263 {
264 c->x = 1 << 30;
265 c->y = ref;
266 }
267
268 #if 0
269 // lat = y
270 int x = transform_from_geo_lat(-90);
271 fprintf(stderr, "y=%d %x\n", x, x);
272 x = transform_from_geo_lat(0);
273 fprintf(stderr, "y=%d %x\n", x, x);
274 x = transform_from_geo_lat(90);
275 fprintf(stderr, "y=%d %x\n", x, x);
276
277 // lon = x
278 int y = transform_from_geo_lon(-180);
279 fprintf(stderr, "x=%d %x\n", y, y);
280 y = transform_from_geo_lon(0);
281 fprintf(stderr, "x=%d %x\n", y, y);
282 y = transform_from_geo_lon(180);
283 fprintf(stderr, "x=%d %x\n", y, y);
284 #endif
285
286 //fprintf(stderr, "encode_nodeid:cx=%x cy=%x 30th bit=%x ref=%x\n", c->x, c->y, (1 << 30), (ref >> 32) );
287 }
288
289 int check_decode_nodeid(struct coord c)
290 {
291 // fprintf(stderr, "check_decode_nodeid:is ref=%d\n", ((c.x & (1 << 30)) == (1 << 30)) );
292 // fprintf(stderr, "check_decode_nodeid:c.x=%d c.y=%d\n", c.x, c.y);
293
294 // if ((c.x == REF_X) && (c.y == REF_Y))
295 if (c.x == REF_X)
296 {
297 // this number is not a real coordiante, its just a dummy to know we need to lookup
298 return 1;
299 }
300 else
301 {
302 // this is is real GPS coordinate
303 // fprintf(stderr, "check_decode_nodeid:c.x=%d c.y=%d\n", c.x, c.y);
304 return 0;
305 }
306
307 //return ((c.x & (1 << 30)) == (1 << 30));
308 }
309
310 osmid decode_nodeid(struct coord c)
311 {
312 osmid ret;
313
314 //fprintf(stderr, "decode_nodeid:x=%x y=%x val=%x\n", c.x, c.y, ((long long)(c.x ^ 0x40000000)*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2));
315 if ( (c.x & (1 << 30)) == (1 << 30) )
316 {
317 ret = c.y;
318 //fprintf(stderr, "decode_nodeid:1=%x\n", ret);
319 ret = ret | ((long long)(c.x ^ 0x40000000)*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2);
320 }
321 else
322 {
323 ret = (osmid)c.y;
324 }
325 //fprintf(stderr, "decode_nodeid:ret=%lld\n", ret);
326
327 return ret;
328 }
329
330
331
332 // first char - item type
333 // =w - ways
334 // =? - used both for nodes and ways
335 // otherwise - nodes
336
337
338 // for coastline-only map
339 static char *attrmap_cl = { "n *=* point_unkn\n"
340 "w *=* street_unkn\n"
341 "w natural=coastline water_line\n" };
342
343 // for border-only map
344 static char *attrmap_bo = { "n *=* point_unkn\n"
345 "w *=* street_unkn\n"
346 "w boundary=administrative,admin_level=2 border_country\n"
347 "w boundary=territorial,admin_level=2 border_country\n"
348 "w boundary=maritime,admin_level=2 border_country\n"
349 "w administrative=boundary,admin_level=2 border_country\n"
350 "w boundary=administrative,maritime=yes,admin_level=2 border_country\n"
351 "w boundary=administrative,maritime=yes,admin_level=4 border_country\n"
352 "w boundary=administrative,border_type=state,admin_level=4 border_country\n" };
353
354 // ==========================
355 // for USA
356 // ==========================
357 // admin_level = 4
358 // border_type = state
359 // boundary = administrative
360 // ==========================
361
362
363 // for normal map
364 static char *attrmap_normal = { "n *=* point_unkn\n"
365 //// "n Annehmlichkeit=Hochsitz poi_hunting_stand\n"
366 "n addr:housenumber=* house_number\n"
367 "n aeroway=aerodrome poi_airport\n"
368 "n aeroway=airport poi_airport\n"
369 "n aeroway=helipad poi_heliport\n"
370 "n aeroway=terminal poi_airport\n"
371 "n amenity=atm poi_atm\n"
372 "n amenity=bank poi_bank\n"
373 "n amenity=bar poi_bar\n"
374 //"n amenity=bench poi_bench\n"
375 "n amenity=biergarten poi_biergarten\n"
376 "n amenity=bus_station poi_bus_station\n"
377 "n amenity=cafe poi_cafe\n"
378 "n amenity=car_wash poi_car_wash\n"
379 "n amenity=cinema poi_cinema\n"
380 "n amenity=college poi_school_college\n"
381 "n amenity=courthouse poi_justice\n"
382 //"n amenity=drinking_water poi_potable_water\n"
383 "n amenity=fast_food poi_fastfood\n"
384 "n amenity=fire_station poi_firebrigade\n"
385 //"n amenity=fountain poi_fountain\n"
386 "n amenity=fuel poi_fuel\n"
387 "n amenity=grave_yard poi_cemetery\n"
388 "n amenity=hospital poi_hospital\n"
389 //"n amenity=hunting_stand poi_hunting_stand\n"
390 "n amenity=kindergarten poi_kindergarten\n"
391 "n amenity=library poi_library\n"
392 "n amenity=nightclub poi_nightclub\n"
393 //"n amenity=park_bench poi_bench\n"
394 "n amenity=parking poi_car_parking\n"
395 "n amenity=bicycle_parking poi_bicycle_parking\n"
396 "n amenity=bicycle_rental poi_bicycle_rental\n"
397 "n amenity=pharmacy poi_pharmacy\n"
398 "n amenity=place_of_worship,religion=christian poi_church\n"
399 "n amenity=place_of_worship poi_worship\n"
400 "n amenity=police poi_police\n"
401 //"n amenity=post_box poi_post_box\n"
402 "n amenity=post_office poi_post_office\n"
403 "n amenity=prison poi_prison\n"
404 //"n amenity=pub poi_pub\n"
405 "n amenity=public_building poi_public_office\n"
406 //"n amenity=recycling poi_recycling\n"
407 "n amenity=restaurant,cuisine=fine_dining poi_dining\n"
408 "n amenity=restaurant poi_restaurant\n"
409 "n amenity=school poi_school\n"
410 //"n amenity=shelter poi_shelter\n"
411 "n amenity=taxi poi_taxi\n"
412 //"n amenity=tec_common tec_common\n"
413 //"n amenity=telephone poi_telephone\n"
414 "n amenity=theatre poi_theater\n"
415 "n amenity=toilets poi_restroom\n"
416 "n amenity=townhall poi_townhall\n"
417 "n amenity=university poi_school_university\n"
418 //"n amenity=vending_machine poi_vending_machine\n"
419 "n barrier=bollard barrier_bollard\n"
420 "n barrier=cycle_barrier barrier_cycle\n"
421 "n barrier=lift_gate barrier_lift_gate\n"
422 "n car=car_rental poi_car_rent\n"
423 "n highway=bus_station poi_bus_station\n"
424 "n highway=bus_stop poi_bus_stop\n"
425 "n highway=mini_roundabout mini_roundabout\n"
426 "n highway=motorway_junction highway_exit\n"
427 "n highway=stop traffic_sign_stop\n"
428 "n highway=toll_booth poi_toll_booth\n"
429 "n highway=crossing,crossing=traffic_signals traffic_crossing_signal\n"
430 "n highway=crossing,crossing=uncontrolled traffic_crossing_uncontrolled\n"
431 "n highway=crossing traffic_crossing_uncontrolled\n"
432 "n highway=traffic_signals traffic_signals\n"
433 "n highway=turning_circle turning_circle\n"
434 //"n historic=boundary_stone poi_boundary_stone\n"
435 "n historic=castle poi_castle\n"
436 "n historic=memorial poi_memorial\n"
437 "n historic=monument poi_monument\n"
438 "n historic=ruins poi_ruins\n"
439 //// "n historic=* poi_ruins\n"
440 "n landuse=cemetery poi_cemetery\n"
441 //"n leisure=fishing poi_fish\n"
442 "n leisure=golf_course poi_golf\n"
443 "n leisure=marina poi_marine\n"
444 //"n leisure=playground poi_playground\n"
445 //"n leisure=slipway poi_boat_ramp\n"
446 "n leisure=sports_centre poi_sport\n"
447 "n leisure=stadium poi_stadium\n"
448 //"n man_made=tower poi_tower\n"
449 "n military=airfield poi_military\n"
450 "n military=barracks poi_military\n"
451 "n military=bunker poi_military\n"
452 "n military=danger_area poi_danger_area\n"
453 "n military=range poi_military\n"
454 "n natural=bay poi_bay\n"
455 "n natural=peak,ele=* poi_peak\n" // show only major peaks with elevation
456 //"n natural=tree poi_tree\n"
457 "n place=city town_label_2e5\n"
458 "n place=town town_label_2e4\n"
459 "n place=village town_label_2e3\n"
460 "n place=hamlet town_label_2e2\n"
461 "n place=locality town_label_2e0\n"
462 "n place=suburb district_label\n"
463 "n place=quarter district_label_1e2\n"
464 "n place=neighbourhood district_label_1e1\n"
465 //"n power=tower power_tower\n"
466 //"n power=sub_station power_substation\n"
467 "n railway=halt poi_rail_halt\n"
468 "n railway=level_crossing poi_level_crossing\n"
469 "n railway=station poi_rail_station\n"
470 "n railway=tram_stop poi_rail_tram_stop\n"
471 "n shop=baker poi_shop_baker\n"
472 "n shop=bakery poi_shop_baker\n"
473 "n shop=beverages poi_shop_beverages\n"
474 "n shop=bicycle poi_shop_bicycle\n"
475 "n shop=butcher poi_shop_butcher\n"
476 "n shop=car poi_car_dealer_parts\n"
477 "n shop=car_repair poi_repair_service\n"
478 //"n shop=clothes poi_shop_apparel\n"
479 //"n shop=convenience poi_shop_grocery\n"
480 //"n shop=drogist poi_shop_drugstore\n"
481 //"n shop=florist poi_shop_florist\n"
482 //"n shop=fruit poi_shop_fruit\n"
483 //"n shop=furniture poi_shop_furniture\n"
484 //"n shop=garden_centre poi_shop_handg\n"
485 //"n shop=hardware poi_shop_handg\n"
486 "n shop=hairdresser poi_hairdresser\n"
487 //"n shop=kiosk poi_shop_kiosk\n"
488 //"n shop=optician poi_shop_optician\n"
489 //"n shop=parfum poi_shop_parfum\n"
490 //"n shop=photo poi_shop_photo\n"
491 "n shop=shoes poi_shop_shoes\n"
492 "n shop=supermarket poi_shopping\n"
493 //"n sport=10pin poi_bowling\n"
494 //"n sport=baseball poi_baseball\n"
495 //"n sport=basketball poi_basketball\n"
496 //"n sport=climbing poi_climbing\n"
497 "n sport=golf poi_golf\n"
498 //"n sport=motor_sports poi_motor_sport\n"
499 //"n sport=skiing poi_skiing\n"
500 "n sport=soccer poi_soccer\n"
501 "n sport=stadium poi_stadium\n"
502 "n sport=swimming poi_swimming\n"
503 "n sport=tennis poi_tennis\n"
504 "n tourism=attraction poi_attraction\n"
505 "n tourism=camp_site poi_camp_rv\n"
506 "n tourism=caravan_site poi_camp_rv\n"
507 "n tourism=guest_house poi_guesthouse\n"
508 "n tourism=hostel poi_hostel\n"
509 "n tourism=hotel poi_hotel\n"
510 "n tourism=information poi_information\n"
511 "n tourism=motel poi_motel\n"
512 "n tourism=museum poi_museum_history\n"
513 //"n tourism=picnic_site poi_picnic\n"
514 "n tourism=theme_park poi_resort\n"
515 "n tourism=viewpoint poi_viewpoint\n"
516 "n tourism=zoo poi_zoo\n"
517 "n traffic_sign=city_limit traffic_sign_city_limit\n"
518 "n highway=speed_camera tec_common\n"
519 "w *=* street_unkn\n"
520 "w addr:interpolation=even house_number_interpolation_even\n"
521 "w addr:interpolation=odd house_number_interpolation_odd\n"
522 "w addr:interpolation=all house_number_interpolation_all\n"
523 "w addr:interpolation=alphabetic house_number_interpolation_alphabetic\n"
524 //"w aerialway=cable_car lift_cable_car\n"
525 //"w aerialway=chair_lift lift_chair\n"
526 //"w aerialway=drag_lift lift_drag\n"
527 "w aeroway=aerodrome poly_airport\n"
528 "w aeroway=apron poly_apron\n"
529 "w aeroway=runway aeroway_runway\n"
530 "w aeroway=taxiway aeroway_taxiway\n"
531 "w aeroway=terminal poly_terminal\n"
532 "w amenity=college poly_college\n"
533 //"w amenity=grave_yard poly_cemetery\n"
534 "w amenity=parking poly_car_parking\n"
535 "w amenity=place_of_worship poly_building\n"
536 "w amenity=university poly_university\n"
537 // "w boundary=administrative,admin_level=2 border_country\n" --> in border map
538 //"w boundary=civil border_civil\n"
539 "w boundary=national_park border_national_park\n"
540 "w boundary=political border_political\n"
541 "w building=* poly_building\n"
542 "w contour_ext=elevation_major height_line_1\n"
543 "w contour_ext=elevation_medium height_line_2\n"
544 "w contour_ext=elevation_minor height_line_3\n"
545 "w highway=bridleway bridleway\n"
546 "w highway=bus_guideway bus_guideway\n"
547 "w highway=construction street_construction\n"
548 "w highway=cyclepath cycleway\n"
549 "w highway=cycleway cycleway\n"
550 "w highway=footway footway\n"
551 "w highway=footway,piste:type=nordic footway_and_piste_nordic\n"
552 "w highway=living_street living_street\n"
553 "w highway=minor street_1_land\n"
554 "w highway=parking_lane street_parking_lane\n"
555 "w highway=path path\n"
556 "w highway=path,bicycle=designated cycleway\n"
557 "w highway=path,bicycle=official cycleway\n"
558 "w highway=path,foot=designated footway\n"
559 "w highway=path,foot=official footway\n"
560 "w highway=path,horse=designated bridleway\n"
561 "w highway=path,horse=official bridleway\n"
562 //"w highway=path,sac_scale=alpine_hiking hiking_alpine\n"
563 //"w highway=path,sac_scale=demanding_alpine_hiking hiking_alpine_demanding\n"
564 //"w highway=path,sac_scale=demanding_mountain_hiking hiking_mountain_demanding\n"
565 //"w highway=path,sac_scale=difficult_alpine_hiking hiking_alpine_difficult\n"
566 //"w highway=path,sac_scale=hiking hiking\n"
567 //"w highway=path,sac_scale=mountain_hiking hiking_mountain\n"
568 "w highway=pedestrian street_pedestrian\n"
569 "w highway=pedestrian,area=1 poly_pedestrian\n"
570 "w highway=plaza poly_plaza\n"
571 "w highway=motorway highway_land\n"
572 "w highway=motorway,rural=0 highway_city\n"
573 "w highway=motorway_link ramp_highway_land\n"
574 "w highway=trunk street_n_lanes\n"
575 "w highway=trunk,name=*,rural=1 street_4_land\n"
576 "w highway=trunk,name=* street_n_lanes\n"
577 "w highway=trunk,rural=0 street_n_lanes\n"
578 "w highway=trunk_link ramp_street_4_city\n"
579 "w highway=primary street_4_land\n"
580 "w highway=primary,name=*,rural=1 street_4_land\n"
581 "w highway=primary,name=* street_4_city\n"
582 "w highway=primary,rural=0 street_4_city\n"
583 "w highway=primary_link ramp_street_4_city\n"
584 "w highway=secondary street_3_land\n"
585 "w highway=secondary,name=*,rural=1 street_3_land\n"
586 "w highway=secondary,name=* street_3_city\n"
587 "w highway=secondary,rural=0 street_3_city\n"
588 "w highway=secondary,area=1 poly_street_3\n"
589 "w highway=secondary_link ramp_street_3_city\n"
590 "w highway=tertiary street_2_land\n"
591 "w highway=tertiary,name=*,rural=1 street_2_land\n"
592 "w highway=tertiary,name=* street_2_city\n"
593 "w highway=tertiary,rural=0 street_2_city\n"
594 "w highway=tertiary,area=1 poly_street_2\n"
595 "w highway=tertiary_link ramp_street_2_city\n"
596 "w highway=residential street_1_city\n"
597 "w highway=residential,area=1 poly_street_1\n"
598 "w highway=unclassified street_1_city\n"
599 "w highway=unclassified,area=1 poly_street_1\n"
600 "w highway=road street_1_city\n"
601 "w highway=service street_service\n"
602 "w highway=service,area=1 poly_service\n"
603 "w highway=service,service=parking_aisle street_parking_lane\n"
604 "w highway=track track_gravelled\n"
605 "w highway=track,surface=grass track_grass\n"
606 "w highway=track,surface=gravel track_gravelled\n"
607 "w highway=track,surface=ground track_ground\n"
608 "w highway=track,surface=paved track_paved\n"
609 "w highway=track,surface=unpaved track_unpaved\n"
610 "w highway=track,tracktype=grade1 track_paved\n"
611 "w highway=track,tracktype=grade2 track_gravelled\n"
612 "w highway=track,tracktype=grade3 track_unpaved\n"
613 "w highway=track,tracktype=grade4 track_ground\n"
614 "w highway=track,tracktype=grade5 track_grass\n"
615 "w highway=track,surface=paved,tracktype=grade1 track_paved\n"
616 "w highway=track,surface=gravel,tracktype=grade2 track_gravelled\n"
617 "w highway=track,surface=unpaved,tracktype=grade3 track_unpaved\n"
618 "w highway=track,surface=ground,tracktype=grade4 track_ground\n"
619 "w highway=track,surface=grass,tracktype=grade5 track_grass\n"
620 "w highway=unsurfaced track_gravelled\n"
621 "w highway=steps steps\n"
622 "w historic=archaeological_site poly_archaeological_site\n"
623 "w historic=battlefield poly_battlefield\n"
624 "w historic=ruins poly_ruins\n"
625 "w historic=town gate poly_building\n"
626 //"w landuse=allotments poly_allotments\n"
627 //"w landuse=basin poly_basin\n"
628 //"w landuse=brownfield poly_brownfield\n"
629 "w landuse=cemetery poly_cemetery\n"
630 //"w landuse=commercial poly_commercial\n"
631 //"w landuse=construction poly_construction\n"
632 //"w landuse=farm poly_farm\n"
633 //"w landuse=farmland poly_farm\n"
634 //"w landuse=farmyard poly_town\n"
635 "w landuse=forest poly_wood\n"
636 "w landuse=greenfield poly_greenfield\n"
637 //"w landuse=industrial poly_industry\n"
638 //"w landuse=landfill poly_landfill\n"
639 "w landuse=military poly_military\n"
640 "w landuse=plaza poly_plaza\n"
641 //"w landuse=quarry poly_quarry\n"
642 "w landuse=railway poly_railway\n"
643 //"w landuse=recreation_ground poly_recreation_ground\n"
644 "w landuse=reservoir poly_reservoir\n"
645 //"w landuse=residential poly_town\n"
646 //"w landuse=residential,area=1 poly_town\n"
647 //"w landuse=retail poly_retail\n"
648 //"w landuse=village_green poly_village_green\n"
649 //"w landuse=vineyard poly_farm\n"
650 //"w leisure=common poly_common\n"
651 //"w leisure=fishing poly_fishing\n"
652 //"w leisure=garden poly_garden\n"
653 //"w leisure=golf_course poly_golf_course\n"
654 //"w leisure=marina poly_marina\n"
655 //"w leisure=nature_reserve poly_nature_reserve\n"
656 "w leisure=park poly_park\n"
657 //"w leisure=pitch poly_sports_pitch\n"
658 "w leisure=playground poly_playground\n"
659 //"w leisure=sports_centre poly_sport\n"
660 "w leisure=stadium poly_sports_stadium\n"
661 //"w leisure=track poly_sports_track\n"
662 "w leisure=water_park poly_water_park\n"
663 "w military=airfield poly_airfield\n"
664 "w military=barracks poly_barracks\n"
665 "w military=danger_area poly_danger_area\n"
666 "w military=naval_base poly_naval_base\n"
667 "w military=range poly_range\n"
668 "w natural=beach poly_beach\n"
669 // "w natural=coastline water_line\n" --> in coastline map
670 "w natural=fell poly_fell\n"
671 "w natural=glacier poly_glacier\n"
672 "w natural=heath poly_heath\n"
673 "w natural=land poly_land\n"
674 "w natural=marsh poly_marsh\n"
675 "w natural=mud poly_mud\n"
676 "w natural=scree poly_scree\n"
677 "w natural=scrub poly_scrub\n"
678 "w natural=water poly_water\n"
679 "w natural=wood poly_wood\n"
680 //"w piste:type=downhill,piste:difficulty=advanced piste_downhill_advanced\n"
681 //"w piste:type=downhill,piste:difficulty=easy piste_downhill_easy\n"
682 //"w piste:type=downhill,piste:difficulty=expert piste_downhill_expert\n"
683 //"w piste:type=downhill,piste:difficulty=freeride piste_downhill_freeride\n"
684 //"w piste:type=downhill,piste:difficulty=intermediate piste_downhill_intermediate\n"
685 //"w piste:type=downhill,piste:difficulty=novice piste_downhill_novice\n"
686 //"w piste:type=nordic piste_nordic\n"
687 "w place=suburb poly_place1\n"
688 "w place=hamlet poly_place2\n"
689 "w place=village poly_place3\n"
690 "w place=municipality poly_place4\n"
691 "w place=town poly_place5\n"
692 "w place=city poly_place6\n"
693 //"w power=line powerline\n"
694 //"w railway=abandoned rail_abandoned\n"
695 //"w railway=disused rail_disused\n"
696 "w railway=light_rail rail_light\n"
697 "w railway=monorail rail_mono\n"
698 "w railway=narrow_gauge rail_narrow_gauge\n"
699 //"w railway=preserved rail_preserved\n"
700 "w railway=rail rail\n"
701 "w railway=subway rail_subway\n"
702 "w railway=tram rail_tram\n"
703 "w route=ferry ferry\n"
704 //"w route=ski piste_nordic\n"
705 //"w sport=* poly_sport\n"
706 "w tourism=artwork poly_artwork\n"
707 "w tourism=attraction poly_attraction\n"
708 "w tourism=camp_site poly_camp_site\n"
709 "w tourism=caravan_site poly_caravan_site\n"
710 "w tourism=picnic_site poly_picnic_site\n"
711 "w tourism=theme_park poly_theme_park\n"
712 "w tourism=zoo poly_zoo\n"
713 "w waterway=canal water_canal\n"
714 "w waterway=drain water_drain\n"
715 "w waterway=river water_river\n"
716 "w waterway=riverbank poly_water\n"
717 "w waterway=stream water_stream\n"
718 "w barrier=ditch ditch\n"
719 "w barrier=hedge hedge\n"
720 "w barrier=fence fence\n"
721 "w barrier=wall wall\n"
722 "w barrier=retaining_wall retaining_wall\n"
723 "w barrier=city_wall city_wall\n" };
724
725 static void build_attrmap_line(char *line)
726 {
727 char *t = NULL, *kvl = NULL, *i = NULL, *p, *kv;
728 struct attr_mapping *attr_mapping = g_malloc0(sizeof(struct attr_mapping));
729 int idx, attr_mapping_count = 0;
730 t = line;
731 p = strchr(t, '\t');
732 if (p)
733 {
734 while (*p == '\t')
735 *p++ = '\0';
736 kvl = p;
737 p = strchr(kvl, '\t');
738 }
739 if (p)
740 {
741 while (*p == '\t')
742 *p++ = '\0';
743 i = p;
744 }
745 if (t[0] == 'w')
746 {
747 if (!i)
748 i = "street_unkn";
749 }
750 else
751 {
752 if (!i)
753 i = "point_unkn";
754 }
755
756 attr_mapping->type = item_from_name(i);
757
758 if (!attr_mapping->type)
759 {
760 printf("no id found for '%s'\n", i);
761 }
762
763 while ((kv = strtok(kvl, ",")))
764 {
765 kvl = NULL;
766 if (!(idx = (int) (long) g_hash_table_lookup(attr_hash, kv)))
767 {
768 idx = attr_present_count++;
769 g_hash_table_insert(attr_hash, kv, (gpointer) (long) idx);
770 }
771 attr_mapping = g_realloc(attr_mapping, sizeof(struct attr_mapping) + (attr_mapping_count + 1) * sizeof(int));
772 attr_mapping->attr_present_idx[attr_mapping_count++] = idx;
773 attr_mapping->attr_present_idx_count = attr_mapping_count;
774 }
775 if (t[0] == 'w')
776 {
777 attr_mapping_way = g_realloc(attr_mapping_way, sizeof(*attr_mapping_way) * (attr_mapping_way_count + 1));
778 attr_mapping_way[attr_mapping_way_count++] = attr_mapping;
779 }
780 if (t[0] == '?' && doway2poi)
781 {
782 attr_mapping_way2poi = g_realloc(attr_mapping_way2poi, sizeof(*attr_mapping_way2poi) * (attr_mapping_way2poi_count + 1));
783 attr_mapping_way2poi[attr_mapping_way2poi_count++] = attr_mapping;
784 }
785 if (t[0] != 'w')
786 {
787 attr_mapping_node = g_realloc(attr_mapping_node, sizeof(*attr_mapping_node) * (attr_mapping_node_count + 1));
788 attr_mapping_node[attr_mapping_node_count++] = attr_mapping;
789 }
790
791 }
792
793 static void build_attrmap(FILE* rule_file)
794 {
795 attr_hash = g_hash_table_new(g_str_hash, g_str_equal);
796 attr_present_count = 1;
797
798 // build attribute map from rule file if given
799 if (rule_file)
800 {
801 char buffer[200], *p;
802 while (fgets(buffer, 200, rule_file))
803 {
804 p = strchr(buffer, '\n');
805 if (p)
806 *p = 0;
807 build_attrmap_line(g_strdup(buffer));
808 }
809 fclose(rule_file);
810 }
811 // use hardcoded default attributes
812 else
813 {
814 char *p, *map;
815 if (border_only_map == 1)
816 {
817 map = g_strdup(attrmap_bo);
818 }
819 else if (coastline_only_map == 1)
820 {
821 map = g_strdup(attrmap_cl);
822 }
823 else
824 {
825 map = g_strdup(attrmap_normal);
826 }
827
828 while (map)
829 {
830 p = strchr(map, '\n');
831 if (p)
832 *p++ = '\0';
833
834 if (strlen(map))
835 build_attrmap_line(map);
836
837 map = p;
838 }
839 }
840
841 attr_present = g_malloc0(sizeof(*attr_present) * attr_present_count);
842 }
843
844 static void build_countrytable(void)
845 {
846 int i;
847 char *names, *str, *tok;
848 country_table_hash = g_hash_table_new(g_str_hash, g_str_equal);
849 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
850 {
851 names = g_strdup(country_table[i].names);
852 str = names;
853 while ((tok = strtok(str, ",")))
854 {
855 str = NULL;
856 g_hash_table_insert(country_table_hash, tok, (gpointer) & country_table[i]);
857 }
858 }
859 }
860
861 int string_endswith2(const char* ending, const char* instring)
862 {
863 int l1;
864 int l2;
865
866 if (!ending)
867 {
868 return 0;
869 }
870
871 if (!instring)
872 {
873 return 0;
874 }
875
876 l1 = strlen(ending);
877 l2 = strlen(instring);
878
879 if (l1 < 1)
880 {
881 return 0;
882 }
883
884 if (l1 > l2)
885 {
886 return 0;
887 }
888
889 int ret = strcmp(ending, instring + (l2 - l1));
890 //dbg(0, "ending=%s in=%s ret=%d\n", ending, instring + (l2 - l1), (ret == 0));
891 return (ret == 0);
892 }
893
894
895 void osm_warning(char *type, long long id, int cont, char *fmt, ...)
896 {
897 char str[4096];
898 va_list ap;
899 va_start(ap, fmt);
900 vsnprintf(str, sizeof(str), fmt, ap);
901 va_end(ap);
902 if (verbose_mode) fprintf(stderr,"%shttp://www.openstreetmap.org/browse/%s/"LONGLONG_FMT" %s",cont ? "":"OSM Warning:",type,id,str);
903 }
904
905 void osm_info(char *type, long long id, int cont, char *fmt, ...)
906 {
907 char str[4096];
908 va_list ap;
909 va_start(ap, fmt);
910 vsnprintf(str, sizeof(str), fmt, ap);
911 va_end(ap);
912 if (verbose_mode) fprintf(stderr,"%shttp://www.openstreetmap.org/browse/%s/"LONGLONG_FMT" %s",cont ? "":"OSM Info:",type,id,str);
913 }
914
915 static void attr_strings_clear(void)
916 {
917 alt_name_found = 0;
918 real_alt_name_found = 0;
919 attr_strings_buffer_len = 0;
920 memset(attr_strings, 0, sizeof(attr_strings));
921 }
922
923 static void attr_strings_save(enum attr_strings id, char *str)
924 {
925 attr_strings[id] = attr_strings_buffer + attr_strings_buffer_len;
926 strcpy(attr_strings[id], str);
927 attr_strings_buffer_len += strlen(str) + 1;
928 }
929
930 long long item_bin_get_nodeid(struct item_bin *ib)
931 {
932 long long *ret = item_bin_get_attr(ib, attr_osm_nodeid, NULL);
933 if (ret)
934 {
935 return *ret;
936 }
937 return 0;
938 }
939
940 long long item_bin_get_wayid(struct item_bin *ib)
941 {
942 long long *ret = item_bin_get_attr(ib, attr_osm_wayid, NULL);
943 if (ret)
944 {
945 return *ret;
946 }
947 return 0;
948 }
949
950 long long item_bin_get_relationid(struct item_bin *ib)
951 {
952 long long *ret = item_bin_get_attr(ib, attr_osm_relationid, NULL);
953 if (ret)
954 {
955 return *ret;
956 }
957 return 0;
958 }
959
960 long long item_bin_get_id(struct item_bin *ib)
961 {
962 long long ret;
963 if (ib->type < 0x80000000)
964 {
965 return item_bin_get_nodeid(ib);
966 }
967 ret = item_bin_get_wayid(ib);
968 if (!ret)
969 {
970 ret = item_bin_get_relationid(ib);
971 }
972 return ret;
973 }
974
975 static int node_is_tagged;
976 static void relation_add_tag(char *k, char *v);
977 osmid get_waynode_num_have_seekpos(osmid way_id, int coord_num, int local_thread_num, off_t seek_pos);
978
979
980 static int access_value(char *v)
981 {
982 if (!strcmp(v, "1"))
983 return 1;
984 if (!strcmp(v, "yes"))
985 return 1;
986 if (!strcmp(v, "designated"))
987 return 1;
988 if (!strcmp(v, "official"))
989 return 1;
990 if (!strcmp(v, "permissive"))
991 return 1;
992 if (!strcmp(v, "0"))
993 return 2;
994 if (!strcmp(v, "no"))
995 return 2;
996 if (!strcmp(v, "agricultural"))
997 return 2;
998 if (!strcmp(v, "forestry"))
999 return 2;
1000 if (!strcmp(v, "private"))
1001 return 2;
1002 if (!strcmp(v, "delivery"))
1003 return 2;
1004 if (!strcmp(v, "destination"))
1005 return 2;
1006 return 3;
1007 }
1008
1009 void osm_add_tag(char *k, char *v)
1010 {
1011 int idx, level = 2;
1012 char buffer[BUFFER_SIZE * 2 + 2];
1013
1014 if (in_relation)
1015 {
1016 relation_add_tag(k, v);
1017 return;
1018 }
1019
1020 if (!strcmp(k, "ele"))
1021 level = 9;
1022
1023 if (!strcmp(k, "time"))
1024 level = 9;
1025
1026 if (!strcmp(k, "created_by"))
1027 level = 9;
1028
1029 if (!strncmp(k, "tiger:", 6) || !strcmp(k, "AND_nodes"))
1030 level = 9;
1031
1032 if (!strcmp(k, "converted_by") || !strcmp(k, "source"))
1033 level = 8;
1034
1035 if (!strncmp(k, "osmarender:", 11) || !strncmp(k, "svg:", 4))
1036 level = 8;
1037
1038 if (!strcmp(k, "layer"))
1039 level = 7;
1040
1041 if (!strcasecmp(v, "true") || !strcasecmp(v, "yes"))
1042 v = "1";
1043
1044 if (!strcasecmp(v, "false") || !strcasecmp(v, "no"))
1045 v = "0";
1046
1047
1048
1049
1050
1051 if (!strcmp(k, "oneway"))
1052 {
1053 if (!strcmp(v, "1"))
1054 {
1055 flags[0] |= NAVIT_AF_ONEWAY | NAVIT_AF_ROUNDABOUT_VALID;
1056 }
1057
1058 if (!strcmp(v, "-1"))
1059 {
1060 flags[0] |= NAVIT_AF_ONEWAYREV | NAVIT_AF_ROUNDABOUT_VALID;
1061 }
1062
1063 if (!in_way)
1064 {
1065 level = 6;
1066 }
1067 else
1068 {
1069 level = 5;
1070 }
1071 }
1072
1073 if (!strcmp(k, "cycleway"))
1074 {
1075 if (!strcmp(v, "opposite"))
1076 {
1077 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_NO;
1078 }
1079 if (!strcmp(v, "opposite_lane"))
1080 {
1081 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_NO;
1082 }
1083
1084 if (!in_way)
1085 {
1086 level = 6;
1087 }
1088 else
1089 {
1090 level = 5;
1091 }
1092 }
1093
1094
1095 if (!strcmp(k, "bicycle:oneway"))
1096 {
1097 if (!strcmp(v, "0"))
1098 {
1099 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_NO;
1100 }
1101 if (!strcmp(v, "1"))
1102 {
1103 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_YES;
1104 }
1105
1106 if (!in_way)
1107 {
1108 level = 6;
1109 }
1110 else
1111 {
1112 level = 5;
1113 }
1114 }
1115
1116 if (!strcmp(k, "oneway:bicycle"))
1117 {
1118 if (!strcmp(v, "0"))
1119 {
1120 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_NO;
1121 }
1122 if (!strcmp(v, "1"))
1123 {
1124 flags[0] |= NAVIT_AF_ONEWAY_BICYCLE_YES;
1125 }
1126
1127 if (!in_way)
1128 {
1129 level = 6;
1130 }
1131 else
1132 {
1133 level = 5;
1134 }
1135 }
1136
1137
1138 /*
1139 blue: cycleway=lane + oneway=yes
1140 blue: cycleway=lane + oneway=yes + bicycle=yes
1141
1142 xxxx: cycleway=opposite_lane + cycleway:left=lane + oneway=yes
1143 xxxx: bicycle=yes + cycleway:right=lane + oneway=yes
1144 xxxx: cycleway:left=track + cycleway:right=lane + oneway=yes + oneway:bicycle=no
1145 xxxx: cycleway:left=share_busway + cycleway:right=lane + oneway=yes + oneway:bicycle=no
1146
1147 dots: cycleway=track + oneway=yes
1148 dots: cycleway=track
1149
1150 xxxx: cycleway:left=track + oneway=yes
1151 xxxx: cycleway:right=track + oneway=yes
1152
1153 xxxx: cycleway:right=track
1154 xxxx: cycleway:right=track + cycleway:right:oneway=no
1155 */
1156
1157 if (!strcmp(k, "cycleway"))
1158 {
1159 if (!strcmp(v, "track"))
1160 {
1161 flags[0] |= NAVIT_AF_BICYCLE_TRACK;
1162 }
1163
1164 if (!in_way)
1165 {
1166 level = 6;
1167 }
1168 else
1169 {
1170 level = 5;
1171 }
1172 }
1173
1174
1175
1176 /*
1177 cycleway=lane
1178 cycleway:right=lane
1179 cycleway:left=lane
1180
1181 cycleway=opposite_lane
1182 cycleway:left=opposite_lane
1183 cycleway:right=opposite_lane
1184 */
1185
1186 if (!strcmp(k, "cycleway"))
1187 {
1188 if (!strcmp(v, "lane"))
1189 {
1190 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1191 }
1192 else if (!strcmp(v, "opposite_lane"))
1193 {
1194 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1195 }
1196
1197 if (!in_way)
1198 {
1199 level = 6;
1200 }
1201 else
1202 {
1203 level = 5;
1204 }
1205 }
1206
1207
1208 if (!strcmp(k, "cycleway:left"))
1209 {
1210 if (!strcmp(v, "lane"))
1211 {
1212 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1213 }
1214 else if (!strcmp(v, "opposite_lane"))
1215 {
1216 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1217 }
1218
1219 if (!in_way)
1220 {
1221 level = 6;
1222 }
1223 else
1224 {
1225 level = 5;
1226 }
1227 }
1228
1229 if (!strcmp(k, "cycleway:right"))
1230 {
1231 if (!strcmp(v, "lane"))
1232 {
1233 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1234 }
1235 else if (!strcmp(v, "opposite_lane"))
1236 {
1237 flags[0] |= NAVIT_AF_BICYCLE_LANE;
1238 }
1239
1240 if (!in_way)
1241 {
1242 level = 6;
1243 }
1244 else
1245 {
1246 level = 5;
1247 }
1248 }
1249
1250
1251
1252
1253 if (!strcmp(k, "junction"))
1254 {
1255 if (!strcmp(v, "roundabout"))
1256 flags[0] |= NAVIT_AF_ONEWAY | NAVIT_AF_ROUNDABOUT | NAVIT_AF_ROUNDABOUT_VALID;
1257 }
1258
1259 if (!strcmp(k, "maxspeed"))
1260 {
1261 if (strstr(v, "mph"))
1262 {
1263 maxspeed_attr_value = (int) floor(atof(v) * 1.609344);
1264 }
1265 else
1266 {
1267 maxspeed_attr_value = atoi(v);
1268 }
1269
1270 if (maxspeed_attr_value)
1271
1272 flags[0] |= NAVIT_AF_SPEED_LIMIT;
1273 level = 5;
1274 }
1275 if (!strcmp(k, "toll"))
1276 {
1277 if (!strcmp(v, "1"))
1278 {
1279 flags[0] |= NAVIT_AF_TOLL;
1280 }
1281 }
1282 if (!strcmp(k, "access"))
1283 {
1284 flags[access_value(v)] |= NAVIT_AF_DANGEROUS_GOODS | NAVIT_AF_EMERGENCY_VEHICLES | NAVIT_AF_TRANSPORT_TRUCK | NAVIT_AF_DELIVERY_TRUCK | NAVIT_AF_PUBLIC_BUS | NAVIT_AF_TAXI | NAVIT_AF_HIGH_OCCUPANCY_CAR | NAVIT_AF_CAR | NAVIT_AF_MOTORCYCLE | NAVIT_AF_MOPED | NAVIT_AF_HORSE | NAVIT_AF_BIKE | NAVIT_AF_PEDESTRIAN;
1285 level = 5;
1286 }
1287 if (!strcmp(k, "vehicle"))
1288 {
1289 flags[access_value(v)] |= NAVIT_AF_DANGEROUS_GOODS | NAVIT_AF_EMERGENCY_VEHICLES | NAVIT_AF_TRANSPORT_TRUCK | NAVIT_AF_DELIVERY_TRUCK | NAVIT_AF_PUBLIC_BUS | NAVIT_AF_TAXI | NAVIT_AF_HIGH_OCCUPANCY_CAR | NAVIT_AF_CAR | NAVIT_AF_MOTORCYCLE | NAVIT_AF_MOPED | NAVIT_AF_BIKE;
1290 level = 5;
1291 }
1292 if (!strcmp(k, "motorvehicle"))
1293 {
1294 flags[access_value(v)] |= NAVIT_AF_DANGEROUS_GOODS | NAVIT_AF_EMERGENCY_VEHICLES | NAVIT_AF_TRANSPORT_TRUCK | NAVIT_AF_DELIVERY_TRUCK | NAVIT_AF_PUBLIC_BUS | NAVIT_AF_TAXI | NAVIT_AF_HIGH_OCCUPANCY_CAR | NAVIT_AF_CAR | NAVIT_AF_MOTORCYCLE | NAVIT_AF_MOPED;
1295 level = 5;
1296 }
1297 if (!strcmp(k, "bicycle"))
1298 {
1299 flags[access_value(v)] |= NAVIT_AF_BIKE;
1300 level = 5;
1301 }
1302 if (!strcmp(k, "foot"))
1303 {
1304 flags[access_value(v)] |= NAVIT_AF_PEDESTRIAN;
1305 level = 5;
1306 }
1307 if (!strcmp(k, "horse"))
1308 {
1309 flags[access_value(v)] |= NAVIT_AF_HORSE;
1310 level = 5;
1311 }
1312 if (!strcmp(k, "moped"))
1313 {
1314 flags[access_value(v)] |= NAVIT_AF_MOPED;
1315 level = 5;
1316 }
1317 if (!strcmp(k, "motorcycle"))
1318 {
1319 flags[access_value(v)] |= NAVIT_AF_MOTORCYCLE;
1320 level = 5;
1321 }
1322 if (!strcmp(k, "motorcar"))
1323 {
1324 flags[access_value(v)] |= NAVIT_AF_CAR;
1325 level = 5;
1326 }
1327 if (!strcmp(k, "hov"))
1328 {
1329 flags[access_value(v)] |= NAVIT_AF_HIGH_OCCUPANCY_CAR;
1330 level = 5;
1331 }
1332 if (!strcmp(k, "bus"))
1333 {
1334 flags[access_value(v)] |= NAVIT_AF_PUBLIC_BUS;
1335 level = 5;
1336 }
1337 if (!strcmp(k, "taxi"))
1338 {
1339 flags[access_value(v)] |= NAVIT_AF_TAXI;
1340 level = 5;
1341 }
1342 if (!strcmp(k, "goods"))
1343 {
1344 flags[access_value(v)] |= NAVIT_AF_DELIVERY_TRUCK;
1345 level = 5;
1346 }
1347 if (!strcmp(k, "hgv"))
1348 {
1349 flags[access_value(v)] |= NAVIT_AF_TRANSPORT_TRUCK;
1350 level = 5;
1351 }
1352 if (!strcmp(k, "emergency"))
1353 {
1354 flags[access_value(v)] |= NAVIT_AF_EMERGENCY_VEHICLES;
1355 level = 5;
1356 }
1357 if (!strcmp(k, "hazmat"))
1358 {
1359 flags[access_value(v)] |= NAVIT_AF_DANGEROUS_GOODS;
1360 level = 5;
1361 }
1362 if (!strcmp(k, "tunnel") && !strcmp(v, "1"))
1363 {
1364 flags[0] |= NAVIT_AF_UNDERGROUND;
1365 }
1366 if (!strcmp(k, "bridge") && !strcmp(v, "1"))
1367 {
1368 flags[0] |= NAVIT_AF_BRIDGE;
1369 }
1370
1371 if (!strcmp(k, "note"))
1372 {
1373 level = 5;
1374 }
1375
1376 if (!strcmp(k, "name"))
1377 {
1378 attr_strings_save(attr_string_label, v);
1379 level = 5;
1380 }
1381
1382 if (!strcmp(k, "capacity"))
1383 {
1384 attr_strings_save(attr_string_capacity, v);
1385 level = 5;
1386 }
1387
1388 if (!strcmp(k, "alt_name"))
1389 {
1390 // alternative name for some places (is sometimes what people call it in the local area)
1391 attr_strings_save(attr_string_label_real_alt, v);
1392 real_alt_name_found = 1;
1393 level = 5;
1394 }
1395
1396 if ((!strcmp(k, "int_name")) && (alt_name_found == 0))
1397 {
1398 // only use "int_name" if we dont have "name:en"
1399 attr_strings_save(attr_string_label_alt, v);
1400 alt_name_found = 1; // lowest priority
1401 level = 5;
1402 }
1403 if (!strcmp(k, "name:en"))
1404 {
1405 attr_strings_save(attr_string_label_alt, v);
1406 alt_name_found = 2; // highest priority
1407 level = 5;
1408 }
1409 if (!strcmp(k, "addr:email"))
1410 {
1411 attr_strings_save(attr_string_email, v);
1412 level = 5;
1413 }
1414 if (!strcmp(k, "addr:housenumber"))
1415 {
1416 attr_strings_save(attr_string_house_number, v);
1417 level = 5;
1418 }
1419 if (!strcmp(k, "addr:street"))
1420 {
1421 attr_strings_save(attr_string_street_name, v);
1422 level = 5;
1423 }
1424 if (!strcmp(k, "colour"))
1425 {
1426 attr_strings_save(attr_string_colour_, v);
1427 level = 5;
1428 }
1429 if (!strcmp(k, "phone"))
1430 {
1431 attr_strings_save(attr_string_phone, v);
1432 level = 5;
1433 }
1434 if (!strcmp(k, "fax"))
1435 {
1436 attr_strings_save(attr_string_fax, v);
1437 level = 5;
1438 }
1439 if (!strcmp(k, "postal_code"))
1440 {
1441 attr_strings_save(attr_string_postal, v);
1442 level = 5;
1443 }
1444 if (!strcmp(k, "openGeoDB:postal_codes") && !attr_strings[attr_string_postal])
1445 {
1446 attr_strings_save(attr_string_postal, v);
1447 level = 5;
1448 }
1449 if (!strcmp(k, "population"))
1450 {
1451 attr_strings_save(attr_string_population, v);
1452 level = 5;
1453 }
1454 if (!strcmp(k, "openGeoDB:population") && !attr_strings[attr_string_population])
1455 {
1456 attr_strings_save(attr_string_population, v);
1457 level = 5;
1458 }
1459 if (!strcmp(k, "ref"))
1460 {
1461 if (in_way)
1462 {
1463 attr_strings_save(attr_string_street_name_systematic, v);
1464 }
1465 else
1466 {
1467 attr_strings_save(attr_string_ref, v);
1468 }
1469 level = 5;
1470 }
1471 if (! strcmp(k,"nat_ref"))
1472 {
1473 if (in_way)
1474 {
1475 attr_strings_save(attr_string_street_name_systematic_nat, v);
1476 //fprintf(stderr, "XYZ123!?:nat_ref=%s\n", v);
1477 }
1478 level=5;
1479 }
1480 if (! strcmp(k,"int_ref"))
1481 {
1482 if (in_way)
1483 {
1484 attr_strings_save(attr_string_street_name_systematic_int, v);
1485 //fprintf(stderr, "XYZ123!?:int_ref=%s\n", v);
1486 }
1487 level=5;
1488 }
1489 if (! strcmp(k,"destination:lanes"))
1490 {
1491 if (in_way)
1492 {
1493 attr_strings_save(attr_string_street_destination_lanes, v);
1494 //fprintf(stderr, "XYZ123!?:destination:lanes=%s\n", v);
1495 }
1496 level=5;
1497 }
1498 if (! strcmp(k,"destination"))
1499 {
1500 if (in_way)
1501 {
1502 attr_strings_save(attr_string_street_destination, v);
1503 //fprintf(stderr, "XYZ123!?:destination=%s\n", v);
1504 }
1505 level=5;
1506 }
1507 if (! strcmp(k,"exit_to"))
1508 {
1509 attr_strings_save(attr_string_exit_to, v);
1510 //fprintf(stderr, "XYZ123!?:exit_to=%s\n", v);
1511 level=5;
1512 }
1513 if (!strcmp(k, "openGeoDB:is_in"))
1514 {
1515 if (!is_in_buffer[0])
1516 {
1517 strcpy(is_in_buffer, v);
1518 }
1519 level = 5;
1520 }
1521 if (!strcmp(k, "is_in"))
1522 {
1523 if (!is_in_buffer[0])
1524 {
1525 strcpy(is_in_buffer, v);
1526 }
1527 level = 5;
1528 }
1529 if (!strcmp(k, "is_in:country"))
1530 {
1531 /**
1532 * Sometimes there is no is_in tag, only is_in:country.
1533 * I put this here so it can be overwritten by the previous if clause if there IS an is_in tag.
1534 */
1535 strcpy(is_in_buffer, v);
1536 level = 5;
1537 }
1538 if (!strcmp(k, "place_county"))
1539 {
1540 /**
1541 * Ireland uses the place_county OSM tag to describe what county a town is in.
1542 * This would be equivalent to is_in: Town; Locality; Country
1543 * A real world example would be Node: Moycullen (52234625)
1544 * The tag is processed as Moycullen; Galway; Ireland
1545 * where Galway is the county
1546 */
1547 strcpy(is_in_buffer, "Ireland");
1548 attr_strings_save(attr_string_county_name, v);
1549 level = 5;
1550 }
1551 if (!strcmp(k, "gnis:ST_alpha"))
1552 {
1553 /* assume a gnis tag means it is part of the USA:
1554 http://en.wikipedia.org/wiki/Geographic_Names_Information_System
1555 many US towns do not have is_in tags
1556 */
1557 strcpy(is_in_buffer, "USA");
1558 level = 5;
1559 }
1560
1561 if (!strcmp(k, "turn:lanes"))
1562 {
1563 if (in_way)
1564 {
1565 attr_strings_save(attr_string_street_turn_lanes, v);
1566 //fprintf(stderr, "XYZ123!?:turn:lanes=%s\n", v);
1567 }
1568 level = 5;
1569 }
1570
1571
1572 if (!strcmp(k, "lanes:forward"))
1573 {
1574 if (in_way)
1575 {
1576 attr_strings_save(attr_string_street_lanes_forward, v);
1577 //fprintf(stderr, "XYZ123!?:lanes:forward=%s\n", v);
1578 }
1579 level = 5;
1580 }
1581
1582 if (!strcmp(k, "lanes"))
1583 {
1584 if (in_way)
1585 {
1586 attr_strings_save(attr_string_street_lanes, v);
1587 //fprintf(stderr, "XYZ123!?:lanes=%s\n", v);
1588 }
1589 level = 5;
1590 }
1591
1592
1593 if (attr_debug_level >= level)
1594 {
1595 int bytes_left = sizeof(debug_attr_buffer) - strlen(debug_attr_buffer) - 1;
1596 if (bytes_left > 0)
1597 {
1598 snprintf(debug_attr_buffer + strlen(debug_attr_buffer), bytes_left, " %s=%s", k, v);
1599 debug_attr_buffer[sizeof(debug_attr_buffer) - 1] = '\0';
1600 node_is_tagged = 1;
1601 }
1602 }
1603
1604 if (level < 6)
1605 {
1606 node_is_tagged = 1;
1607 }
1608
1609 strcpy(buffer, "*=*");
1610 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1611 {
1612 attr_present[idx] = 1;
1613 }
1614
1615 sprintf(buffer, "%s=*", k);
1616 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1617 {
1618 attr_present[idx] = 2;
1619 }
1620
1621 sprintf(buffer, "*=%s", v);
1622 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1623 {
1624 attr_present[idx] = 2;
1625 }
1626
1627 sprintf(buffer, "%s=%s", k, v);
1628 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1629 {
1630 attr_present[idx] = 4;
1631 }
1632 }
1633
1634 int coord_count;
1635
1636 static void extend_buffer(struct buffer *b)
1637 {
1638 b->malloced += b->malloced_step;
1639 b->base = realloc(b->base, (size_t) b->malloced);
1640
1641 if (b->base == NULL)
1642 {
1643 fprintf(stderr, "realloc of "LONGLONG_FMT" bytes failed\n", (size_t) b->malloced);
1644 exit(1);
1645 }
1646
1647 if (verbose_mode) fprintf(stderr, "extend_buffer: "LONGLONG_FMT" bytes\n", (size_t) b->malloced);
1648 }
1649
1650 osmid nodeid_last;
1651 GHashTable *way_hash, *waytag_hash;
1652 cfuhash_table_t *way_hash_cfu = NULL;
1653
1654 typedef struct dummy_cfuhash_entry
1655 {
1656 long long key;
1657 int data;
1658 struct dummy_cfuhash_entry *next;
1659 } dummy_cfuhash_entry;
1660
1661 static void node_buffer_to_hash(int local_thread_num)
1662 {
1663 if (verbose_mode)
1664 {
1665 fprintf(stderr, "node_buffer_to_hash t:%d nb->size:%lu\n", local_thread_num, node_buffer[local_thread_num].size);
1666 fprintf(stderr, "node_buffer_to_hash t:%d nb->base:%lu\n", local_thread_num, node_buffer[local_thread_num].base);
1667 fprintf(stderr, "node_buffer_to_hash t:%d nh:%p\n", local_thread_num, node_hash[local_thread_num]);
1668 }
1669
1670 int i = 0;
1671 int count2 = node_buffer[local_thread_num].size / sizeof(struct node_item);
1672 struct node_item *ni = (struct node_item *) node_buffer[local_thread_num].base;
1673
1674 #if 0
1675 fprintf(stderr, "[THREAD] #%d fill hash node: count=%d size=%d\n", local_thread_num, count2, sizeof(dummy_cfuhash_entry));
1676 #endif
1677
1678 struct dummy_dummy_entry
1679 {
1680 long long key;
1681 int value_ptr;
1682 };
1683
1684 fprintf_(stderr, "[THREAD] #%d fill hash node: count=%d size=%d\n", local_thread_num, count2, sizeof(struct dummy_dummy_entry));
1685
1686 for (i = 0; i < count2; i++)
1687 {
1688 /*
1689 if (i % 5000)
1690 {
1691 }
1692 */
1693
1694 // fprintf_(stderr, "thread #%d fill #%d/%d\n", local_thread_num, i, count2);
1695
1696 // g_hash_table_insert(node_hash[local_thread_num], (gpointer) (long) (ni[i].id), (gpointer) (long) i);
1697 //cfuhash_put(node_hash_cfu[local_thread_num], &(long long)ni[i].id, xxx);
1698
1699 #if 0
1700 //fprintf(stderr, "node id:%lld i:%d\n", (long long)ni[i].id, i);
1701 cfuhash_put_data(node_hash_cfu[local_thread_num], (long long)ni[i].id, sizeof(long long), i, sizeof(int), NULL);
1702 #endif
1703
1704 // fprintf(stderr, "node id:%lld mem:%p\n", (long long)ni->id, ni);
1705 quick_hash_add_entry(node_hash_cfu[local_thread_num], (long long)ni[i].id, i);
1706 // ni++;
1707 }
1708
1709 // fprintf_(stderr, "thread #%d node_buffer_to_hash ready\n", local_thread_num);
1710
1711 if (verbose_mode)
1712 {
1713 fprintf(stderr, "node_buffer_to_hash ready\n");
1714 }
1715 }
1716
1717 static void waytag_buffer_to_hash(void)
1718 {
1719 int i, count = waytag_buffer.size / sizeof(struct way_tag);
1720 struct way_tag *wt = (struct way_tag *) waytag_buffer.base;
1721 for (i = 0; i < count; i++)
1722 {
1723 // fprintf(stderr, "waytag_hash insert:%lu %lu\n", (long) (wt[i].way_id), (long) i);
1724 g_hash_table_insert(waytag_hash, (gpointer) (long) (wt[i].way_id), (gpointer) (long) i);
1725 }
1726 }
1727
1728 static struct node_item *ni;
1729
1730 void init_node_hash(int threads, int clear)
1731 {
1732 int i;
1733 for (i = 0; i < threads; i++)
1734 {
1735 //if (clear)
1736 //{
1737 //*node_hash[i] = NULL;
1738 //}
1739 //else
1740 //{
1741 //*node_hash[i] = g_hash_table_new(NULL, NULL);
1742 //}
1743 #if 0
1744 node_hash_cfu[i] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_NODES);
1745 cfuhash_set_flag(node_hash_cfu[i], CFUHASH_FROZEN);
1746 #endif
1747 node_hash_cfu[i] = quick_hash_init(CFUHASH_BUCKETS_NODES);
1748 }
1749 }
1750
1751 void fill_hash_node(int local_thread_num)
1752 {
1753 //fprintf(stderr, "001t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1754
1755 if (verbose_mode)
1756 {
1757 fprintf(stderr, "fill_hash_node - START\n");
1758 }
1759 //if (node_hash[local_thread_num])
1760 if (node_hash_cfu[local_thread_num])
1761 {
1762 #if 0
1763 //g_hash_table_destroy(node_hash[local_thread_num]);
1764 //node_hash[local_thread_num] = NULL;
1765 cfuhash_clear(node_hash_cfu[local_thread_num]);
1766 cfuhash_destroy(node_hash_cfu[local_thread_num]);
1767 node_hash_cfu[local_thread_num] = NULL;
1768 #endif
1769 quick_hash_destroy(node_hash_cfu[local_thread_num]);
1770 node_hash_cfu[local_thread_num] = NULL;
1771 }
1772
1773 #if 0
1774 // node_hash[local_thread_num] = g_hash_table_new(NULL, NULL);
1775 node_hash_cfu[local_thread_num] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_NODES);
1776 cfuhash_set_flag(node_hash_cfu[local_thread_num], CFUHASH_FROZEN);
1777 #endif
1778 node_hash_cfu[local_thread_num] = quick_hash_init(CFUHASH_BUCKETS_NODES);
1779
1780
1781 //fprintf(stderr, "002t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1782 node_buffer_to_hash(local_thread_num);
1783
1784 quick_hash_print_stats(node_hash_cfu[local_thread_num]);
1785
1786 //fprintf(stderr, "003t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1787 if (verbose_mode)
1788 {
1789 fprintf(stderr, "fill_hash_node - END\n");
1790 }
1791 }
1792
1793 void flush_nodes(int final, int local_thread_num)
1794 {
1795 save_buffer("coords.tmp", &node_buffer[local_thread_num], slices * slice_size);
1796 //fprintf(stderr, "004t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1797
1798 if (verbose_mode)
1799 {
1800 fprintf(stderr, "flush_nodes %d\n", final);
1801 }
1802
1803 if (!final)
1804 {
1805 //fprintf(stderr, "005t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1806 node_buffer[local_thread_num].size = 0;
1807 //fprintf(stderr, "006t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1808
1809 //if (node_hash[local_thread_num])
1810 if (node_hash_cfu[local_thread_num])
1811 {
1812 if (verbose_mode) fprintf(stderr,"node_hash size="LONGLONG_FMT"\n", g_hash_table_size (node_hash[local_thread_num]));
1813 #if 0
1814 //g_hash_table_destroy(node_hash[local_thread_num]);
1815 //node_hash[local_thread_num] = NULL;
1816 cfuhash_clear(node_hash_cfu[local_thread_num]);
1817 cfuhash_destroy(node_hash_cfu[local_thread_num]);
1818 node_hash_cfu[local_thread_num] = NULL;
1819 #endif
1820 quick_hash_destroy(node_hash_cfu[local_thread_num]);
1821 node_hash_cfu[local_thread_num] = NULL;
1822 }
1823 }
1824
1825 if (verbose_mode) fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer[local_thread_num].size);
1826 if (verbose_mode) fprintf(stderr,"node: node_buffer malloced="LONGLONG_FMT"\n", node_buffer[local_thread_num].malloced);
1827 //fprintf(stderr, "007t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1828
1829 slices++;
1830 }
1831
1832
1833
1834 inline double transform_to_geo_lat(int y)
1835 {
1836 /* ZZ GEO TRANS ZZ */
1837 return TO_GEO_LAT_(y);
1838 }
1839
1840 inline double transform_to_geo_lon(int x)
1841 {
1842 /* ZZ GEO TRANS ZZ */
1843 return TO_GEO_LON_(x);
1844 }
1845
1846 inline int transform_from_geo_lat(double lat)
1847 {
1848 /* ZZ GEO TRANS ZZ */
1849 return FROM_GEO_LAT_(lat);
1850 }
1851
1852 inline int transform_from_geo_lon(double lon)
1853 {
1854 /* ZZ GEO TRANS ZZ */
1855 return FROM_GEO_LON_(lon);
1856 }
1857
1858 void osm_add_node(osmid id, double lat, double lon)
1859 {
1860 in_node = 1;
1861 if (node_buffer[0].size + sizeof(struct node_item) > node_buffer[0].malloced)
1862 {
1863 extend_buffer(&node_buffer[0]);
1864 }
1865 attr_strings_clear();
1866 node_is_tagged = 0;
1867 nodeid = id;
1868 item.type = type_point_unkn;
1869 debug_attr_buffer[0] = '\0';
1870 is_in_buffer[0] = '\0';
1871 debug_attr_buffer[0] = '\0';
1872 osmid_attr.type = attr_osm_nodeid;
1873 osmid_attr.len = 3;
1874 osmid_attr_value = id;
1875 if (node_buffer[0].size + sizeof(struct node_item) > slice_size)
1876 {
1877 flush_nodes(0, 0);
1878 }
1879 ni = (struct node_item *) (node_buffer[0].base + node_buffer[0].size);
1880 ni->id = id;
1881 ni->ref_node = 0;
1882 ni->ref_way = 0;
1883 ni->ref_ref = 0;
1884 ni->dummy = 0;
1885
1886 // fprintf(stderr, "NNDD:000:%f %f\n", lat, lon);
1887
1888 /* ZZ GEO TRANS ZZ */
1889 ni->c.x = FROM_GEO_LON_(lon);
1890 ni->c.y = FROM_GEO_LAT_(lat);
1891 /* ZZ GEO TRANS ZZ */
1892
1893 // fprintf(stderr, "NNDD:002:%d %d\n", ni->c.x, ni->c.y);
1894
1895
1896 // save values -----
1897 save_node_lat = lat;
1898 save_node_lon = lon;
1899 // save values -----
1900
1901
1902 #ifdef MAPTOOL_USE_SQL
1903
1904 if (sql_counter2 == 0)
1905 {
1906 sqlite3_exec(sql_handle002a, "BEGIN", 0, 0, 0);
1907 sqlite3_exec(sql_handle003a, "BEGIN", 0, 0, 0);
1908 sqlite3_exec(sql_handle002b, "BEGIN", 0, 0, 0);
1909 sqlite3_exec(sql_handle003b, "BEGIN", 0, 0, 0);
1910 }
1911 sql_counter2++;
1912
1913 #ifdef MAPTOOL_SPLIT_NODE_DB
1914 if (nodeid % 2)
1915 {
1916 if ((nodeid & MAPTOOL_SPLIT_NODE_BIT) > 0)
1917 {
1918 sqlite3_bind_int64(stmt_node__2a, 1, nodeid);
1919 sqlite3_bind_double(stmt_node__2a, 2, lat);
1920 sqlite3_bind_double(stmt_node__2a, 3, lon);
1921 sqlite3_step(stmt_node__2a);
1922 sqlite3_reset(stmt_node__2a);
1923 }
1924 else
1925 {
1926 sqlite3_bind_int64(stmt_node__2b, 1, nodeid);
1927 sqlite3_bind_double(stmt_node__2b, 2, lat);
1928 sqlite3_bind_double(stmt_node__2b, 3, lon);
1929 sqlite3_step(stmt_node__2b);
1930 sqlite3_reset(stmt_node__2b);
1931 }
1932 }
1933 else
1934 {
1935 if ((nodeid & MAPTOOL_SPLIT_NODE_BIT) > 0)
1936 {
1937 #endif
1938 sqlite3_bind_int64(stmt_nodea, 1, nodeid);
1939 sqlite3_bind_double(stmt_nodea, 2, lat);
1940 sqlite3_bind_double(stmt_nodea, 3, lon);
1941 sqlite3_step(stmt_nodea);
1942 sqlite3_reset(stmt_nodea);
1943 #ifdef MAPTOOL_SPLIT_NODE_DB
1944 }
1945 else
1946 {
1947 sqlite3_bind_int64(stmt_nodeb, 1, nodeid);
1948 sqlite3_bind_double(stmt_nodeb, 2, lat);
1949 sqlite3_bind_double(stmt_nodeb, 3, lon);
1950 sqlite3_step(stmt_nodeb);
1951 sqlite3_reset(stmt_nodeb);
1952 }
1953 }
1954 #endif
1955
1956 if (sql_counter2 > MAX_ROWS_WO_COMMIT)
1957 {
1958 sql_counter2 = 0;
1959 sqlite3_exec(sql_handle002a, "COMMIT", 0, 0, 0);
1960 sqlite3_exec(sql_handle003a, "COMMIT", 0, 0, 0);
1961 sqlite3_exec(sql_handle002b, "COMMIT", 0, 0, 0);
1962 sqlite3_exec(sql_handle003b, "COMMIT", 0, 0, 0);
1963 //fprintf(stderr, "SQL: COMMIT\n");
1964 }
1965
1966 #endif
1967
1968 //fprintf(stderr,"node: %d %d\n", ni->c.x, ni->c.y);
1969 //fprintf(stderr,"node: %f %f\n", lat, lon);
1970
1971
1972 node_buffer[0].size += sizeof(struct node_item);
1973 //fprintf(stderr,"node: inc node_buffer by %d\n", sizeof(struct node_item));
1974 //fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer.size);
1975
1976 #if 0
1977 //if (!node_hash[0])
1978 if (!node_hash_cfu[0])
1979 {
1980 if (ni->id > nodeid_last)
1981 {
1982 nodeid_last = ni->id;
1983 }
1984 else
1985 {
1986 //fprintf(stderr,"INFO: Nodes out of sequence (new %d vs old %d), adding hash\n", ni->id, nodeid_last);
1987 //node_hash[0] = g_hash_table_new(NULL, NULL);
1988
1989 node_hash_cfu[0] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_OTHER);
1990 cfuhash_set_flag(node_hash_cfu[0], CFUHASH_FROZEN);
1991
1992 node_buffer_to_hash(0);
1993 }
1994 }
1995 //else if (!g_hash_table_lookup(node_hash[0], (gpointer) (long) (ni->id)))
1996 // else if (!cfuhash_exists(node_hash_cfu[0], (long long) ni->id ))
1997
1998 else if (!cfuhash_exists_data(node_hash_cfu[0], (long long) ni->id, sizeof(long long)))
1999 {
2000 //g_hash_table_insert(node_hash[0], (gpointer) (long) (ni->id), (gpointer) (long) (ni - (struct node_item *) node_buffer[0].base));
2001 //cfuhash_put(node_hash_cfu[0], (gpointer)ni->id, (gpointer)(long) (ni - (struct node_item *) node_buffer[0].base) );
2002 cfuhash_put_data(node_hash_cfu[0], (long long)ni->id, sizeof(long long), (long) (ni - (struct node_item *) node_buffer[0].base), 1, NULL);
2003
2004 }
2005 else
2006 {
2007 node_buffer[0].size -= sizeof(struct node_item);
2008 if (verbose_mode)
2009 fprintf(stderr, "node: Decr. node_buffer by %d\n", sizeof(struct node_item));
2010 if (verbose_mode) fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer[0].size);
2011 nodeid = 0;
2012 }
2013 #endif
2014
2015 }
2016
2017 void clear_node_item_buffer(void)
2018 {
2019 int j, count = node_buffer[0].size / sizeof(struct node_item);
2020 struct node_item *ni = (struct node_item *) (node_buffer[0].base);
2021 for (j = 0; j < count; j++)
2022 {
2023 ni[j].ref_way = 0;
2024 }
2025 }
2026
2027 static struct node_item *
2028 node_item_get_fast(osmid id, int local_thread_num)
2029 {
2030 #if 1
2031 int i;
2032 //void *p_tmp;
2033 //int rr;
2034 //size_t data_size2 = sizeof(int);
2035 struct node_item *ni = (struct node_item *) (node_buffer[local_thread_num].base);
2036 #endif
2037
2038 //if (node_hash[local_thread_num])
2039 if (node_hash_cfu[local_thread_num])
2040 {
2041 #if 0
2042 //p_tmp = (gpointer)(g_hash_table_lookup(node_hash[local_thread_num], (gpointer) (long) id));
2043 rr = cfuhash_get_data(node_hash_cfu[local_thread_num], (long long) id, sizeof(long long), &p_tmp, &data_size2);
2044 if (rr == 1)
2045 {
2046 //fprintf(stderr, "got key=%lld value=%d\n", (long long) id, p_tmp);
2047 i = p_tmp;
2048 return ni + i;
2049 }
2050 #endif
2051
2052 //struct node_item *ni = quick_hash_lookup(node_hash_cfu[local_thread_num], id);
2053 //fprintf(stderr, "got key=%lld value=%p\n", (long long)id, ni);
2054 //if (ni)
2055 //{
2056 // fprintf(stderr, "x=%d y=%d\n", ni->c.x, ni->c.y);
2057 //}
2058 i = quick_hash_lookup(node_hash_cfu[local_thread_num], id);
2059 if (i != -1)
2060 {
2061 return ni + i; // move pointer "i" number of items forward
2062 }
2063
2064 //if (p_tmp != NULL)
2065 //{
2066 // // only if node is found in hash!
2067 // i = (int) (long) (p_tmp);
2068 // return ni + i;
2069 //}
2070 }
2071 return NULL;
2072 }
2073
2074 static int load_node(FILE *coords, int p, struct node_item *ret)
2075 {
2076 fseek(coords, p * sizeof(struct node_item), SEEK_SET);
2077 if (fread(ret, sizeof(*ret), 1, coords) != 1)
2078 {
2079 fprintf(stderr, "read failed\n");
2080 return 0;
2081 }
2082 return 1;
2083 }
2084
2085 static osmid first_node_of_current_way;
2086
2087 void osm_add_way(osmid id)
2088 {
2089 static osmid wayid_last;
2090
2091 in_way = 1;
2092 wayid = id;
2093 coord_count = 0;
2094 attr_strings_clear();
2095 item.type = type_street_unkn;
2096 debug_attr_buffer[0] = '\0';
2097 maxspeed_attr_value = 0;
2098 flags_attr_value = 0;
2099 memset(flags, 0, sizeof(flags));
2100 debug_attr_buffer[0] = '\0';
2101 osmid_attr_value = id;
2102
2103 if (wayid < wayid_last && !way_hash_cfu)
2104 {
2105 if (verbose_mode)
2106 {
2107 fprintf(stderr, "INFO: Ways out of sequence (new %d vs old %d), adding hash\n", wayid, wayid_last);
2108 }
2109 // way_hash = g_hash_table_new(NULL, NULL);
2110 way_hash_cfu = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_WAYS);
2111 cfuhash_set_flag(way_hash_cfu, CFUHASH_FROZEN);
2112
2113 }
2114 wayid_last = wayid;
2115 }
2116
2117 char relation_type[BUFFER_SIZE];
2118 char iso_code[BUFFER_SIZE];
2119 int admin_level;
2120 int boundary;
2121
2122 void osm_add_relation(osmid id)
2123 {
2124 current_id = id;
2125 in_relation = 1;
2126 debug_attr_buffer[0] = '\0';
2127 relation_type[0] = '\0';
2128 iso_code[0] = '\0';
2129 admin_level = -1;
2130 boundary = 0;
2131
2132 // zero out buffer -----------------------
2133 // bzero(item_bin_2, MAX_ITEMBIN_BYTES_);
2134 // zero out buffer -----------------------
2135
2136 item_bin_init(item_bin_2, type_none);
2137 item_bin_add_attr_longlong(item_bin_2, attr_osm_relationid, current_id);
2138 }
2139
2140 static int country_id_from_iso2(char *iso)
2141 {
2142 int ret = 0;
2143 if (iso)
2144 {
2145 // make uppercase
2146 int i99;
2147 for (i99 = 0; iso[i99]; i99++)
2148 {
2149 iso[i99] = toupper(iso[i99]);
2150 }
2151 struct country_search *search;
2152 struct attr country_iso2, country_id;
2153 struct item *item;
2154 country_iso2.type = attr_country_iso2;
2155 country_iso2.u.str = iso;
2156 search = country_search_new(&country_iso2, 0);
2157 if ((item = country_search_get_item(search)) && item_attr_get(item, attr_country_id, &country_id))
2158 {
2159 ret = country_id.u.num;
2160 if (debug_itembin(3))
2161 {
2162 fprintf(stderr, "country id(1)=%d\n", ret);
2163 }
2164 }
2165 else
2166 {
2167 if (debug_itembin(3))
2168 {
2169 fprintf(stderr, "country id(2)=%d\n", ret);
2170 }
2171 }
2172 country_search_destroy(search);
2173 }
2174 return ret;
2175 }
2176
2177 struct country_table *country_from_countryid(int id)
2178 {
2179 int i;
2180 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
2181 {
2182 //if (debug_itembin(3))
2183 //{
2184 // fprintf(stderr,"== country_from_countryid: %d ==\n", country_table[i].countryid);
2185 //}
2186
2187 if (country_table[i].countryid == id)
2188 {
2189 return &country_table[i];
2190 }
2191 }
2192
2193 //if (debug_itembin(3))
2194 //{
2195 // fprintf(stderr,"== country_from_countryid: return NULL ==\n");
2196 //}
2197
2198 return NULL;
2199 }
2200
2201 struct country_table *
2202 country_from_iso2(char *iso)
2203 {
2204 return country_from_countryid(country_id_from_iso2(iso));
2205 }
2206
2207 static int get_relation_member(char *str, struct relation_member *memb)
2208 {
2209 int len;
2210 sscanf(str,"%d:"LONGLONG_FMT":%n",&memb->type,&memb->id,&len);
2211 memb->role = str + len;
2212 return 1;
2213 }
2214
2215 #if 0
2216 void report(io, markers, reporttriangles, reportneighbors, reportsegments, reportedges, reportnorms)
2217 struct triangulateio *io;int markers;int reporttriangles;int reportneighbors;int reportsegments;int reportedges;int reportnorms;
2218 {
2219 int i, j;
2220
2221 for (i = 0; i < io->numberofpoints; i++)
2222 {
2223 fprintf(stderr, "Point %4d:", i);
2224 for (j = 0; j < 2; j++)
2225 {
2226 fprintf(stderr, " %.6g", io->pointlist[i * 2 + j]);
2227 }
2228 if (io->numberofpointattributes > 0)
2229 {
2230 fprintf(stderr, " attributes");
2231 }
2232 for (j = 0; j < io->numberofpointattributes; j++)
2233 {
2234 fprintf(stderr, " %.6g", io->pointattributelist[i * io->numberofpointattributes + j]);
2235 }
2236 if (markers)
2237 {
2238 fprintf(stderr, " marker %d\n", io->pointmarkerlist[i]);
2239 }
2240 else
2241 {
2242 fprintf(stderr, "\n");
2243 }
2244 }
2245 fprintf(stderr, "\n");
2246
2247 if (reporttriangles || reportneighbors)
2248 {
2249 for (i = 0; i < io->numberoftriangles; i++)
2250 {
2251 if (reporttriangles)
2252 {
2253 fprintf(stderr, "Triangle %4d points:", i);
2254 for (j = 0; j < io->numberofcorners; j++)
2255 {
2256 fprintf(stderr, " %4d", io->trianglelist[i * io->numberofcorners + j]);
2257 }
2258 if (io->numberoftriangleattributes > 0)
2259 {
2260 fprintf(stderr, " attributes");
2261 }
2262 for (j = 0; j < io->numberoftriangleattributes; j++)
2263 {
2264 fprintf(stderr, " %.6g", io->triangleattributelist[i * io->numberoftriangleattributes + j]);
2265 }
2266 fprintf(stderr, "\n");
2267 }
2268 if (reportneighbors)
2269 {
2270 fprintf(stderr, "Triangle %4d neighbors:", i);
2271 for (j = 0; j < 3; j++)
2272 {
2273 fprintf(stderr, " %4d", io->neighborlist[i * 3 + j]);
2274 }
2275 fprintf(stderr, "\n");
2276 }
2277 }
2278 fprintf(stderr, "\n");
2279 }
2280
2281 if (reportsegments)
2282 {
2283 for (i = 0; i < io->numberofsegments; i++)
2284 {
2285 fprintf(stderr, "Segment %4d points:", i);
2286 for (j = 0; j < 2; j++)
2287 {
2288 fprintf(stderr, " %4d", io->segmentlist[i * 2 + j]);
2289 }
2290 if (markers)
2291 {
2292 fprintf(stderr, " marker %d\n", io->segmentmarkerlist[i]);
2293 }
2294 else
2295 {
2296 fprintf(stderr, "\n");
2297 }
2298 }
2299 fprintf(stderr, "\n");
2300 }
2301
2302 if (reportedges)
2303 {
2304 for (i = 0; i < io->numberofedges; i++)
2305 {
2306 fprintf(stderr, "Edge %4d points:", i);
2307 for (j = 0; j < 2; j++)
2308 {
2309 fprintf(stderr, " %4d", io->edgelist[i * 2 + j]);
2310 }
2311 if (reportnorms && (io->edgelist[i * 2 + 1] == -1))
2312 {
2313 for (j = 0; j < 2; j++)
2314 {
2315 fprintf(stderr, " %.6g", io->normlist[i * 2 + j]);
2316 }
2317 }
2318 if (markers)
2319 {
2320 fprintf(stderr, " marker %d\n", io->edgemarkerlist[i]);
2321 }
2322 else
2323 {
2324 fprintf(stderr, "\n");
2325 }
2326 }
2327 fprintf(stderr, "\n");
2328 }
2329 }
2330
2331 #endif
2332
2333 void get_lat_lon_for_node(long long nd, struct node_lat_lon *node);
2334
2335 void get_lat_lon_way_first_node(long long way_id, struct node_lat_lon *node)
2336 {
2337 int rc2;
2338 sqlite3_stmt *st;
2339 long long nd = 0;
2340
2341
2342 node->valid = 0;
2343
2344 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2345 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
2346 {
2347 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2348 {
2349 #endif
2350 st = stmt_sel001;
2351 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2352 }
2353 else
2354 {
2355 st = stmt_sel001b;
2356 }
2357 }
2358 else
2359 {
2360 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2361 {
2362 st = stmt_sel001__2;
2363 }
2364 else
2365 {
2366 st = stmt_sel001__2b;
2367 }
2368 }
2369 #endif
2370 sqlite3_bind_int64(st, 1, way_id);
2371
2372 // execute the statement
2373 rc2 = sqlite3_step(st);
2374 switch (rc2)
2375 {
2376 case SQLITE_DONE:
2377 break;
2378 case SQLITE_ROW:
2379 nd = sqlite3_column_int64(st, 0);
2380 break;
2381 default:
2382 fprintf(stderr, "Error: %d\n", rc2);
2383 break;
2384 }
2385 sqlite3_reset(st);
2386 // sqlite3_finalize(st);
2387
2388 if (nd != 0)
2389 {
2390 get_lat_lon_for_node(nd, node);
2391 }
2392 }
2393
2394 void get_lat_lon_for_node(long long nd, struct node_lat_lon *node)
2395 {
2396 int rc2;
2397
2398 // now read the coords of this node
2399 node->valid = 0;
2400
2401 #ifdef MAPTOOL_USE_SQL
2402
2403 #ifdef MAPTOOL_SPLIT_NODE_DB
2404 if (nd % 2)
2405 {
2406 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2407 {
2408 sqlite3_bind_int64(stmt_sel002__2a, 1, nd);
2409 }
2410 else
2411 {
2412 sqlite3_bind_int64(stmt_sel002__2b, 1, nd);
2413 }
2414 }
2415 else
2416 {
2417 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2418 {
2419 #endif
2420 sqlite3_bind_int64(stmt_sel002a, 1, nd);
2421 #ifdef MAPTOOL_SPLIT_NODE_DB
2422 }
2423 else
2424 {
2425 sqlite3_bind_int64(stmt_sel002b, 1, nd);
2426 }
2427 }
2428 #endif
2429
2430 rc2 = 0;
2431 //do
2432 //{
2433 #ifdef MAPTOOL_SPLIT_NODE_DB
2434 if (nd % 2)
2435 {
2436 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2437 {
2438 rc2 = sqlite3_step(stmt_sel002__2a);
2439 }
2440 else
2441 {
2442 rc2 = sqlite3_step(stmt_sel002__2b);
2443 }
2444 }
2445 else
2446 {
2447 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2448 {
2449 #endif
2450 rc2 = sqlite3_step(stmt_sel002a);
2451 #ifdef MAPTOOL_SPLIT_NODE_DB
2452 }
2453 else
2454 {
2455 rc2 = sqlite3_step(stmt_sel002b);
2456 }
2457 }
2458 #endif
2459 //cols2 = sqlite3_column_count(stmt_sel002);
2460 switch (rc2)
2461 {
2462 //case SQLITE_DONE:
2463 // break;
2464 case SQLITE_ROW:
2465
2466 #ifdef MAPTOOL_SPLIT_NODE_DB
2467 if (nd % 2)
2468 {
2469 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2470 {
2471 node->lat = (double) sqlite3_column_double(stmt_sel002__2a, 0);
2472 node->lon = (double) sqlite3_column_double(stmt_sel002__2a, 1);
2473 }
2474 else
2475 {
2476 node->lat = (double) sqlite3_column_double(stmt_sel002__2b, 0);
2477 node->lon = (double) sqlite3_column_double(stmt_sel002__2b, 1);
2478 }
2479 }
2480 else
2481 {
2482 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2483 {
2484 #endif
2485 node->lat = (double) sqlite3_column_double(stmt_sel002a, 0);
2486 node->lon = (double) sqlite3_column_double(stmt_sel002a, 1);
2487 #ifdef MAPTOOL_SPLIT_NODE_DB
2488 }
2489 else
2490 {
2491 node->lat = (double) sqlite3_column_double(stmt_sel002b, 0);
2492 node->lon = (double) sqlite3_column_double(stmt_sel002b, 1);
2493 }
2494 }
2495 #endif
2496 node->valid = 1;
2497 break;
2498 default:
2499 fprintf(stderr, "get_lat_lon_for_node:SQL Error: %d nodeid:%lld\n", rc2, nd);
2500 break;
2501 }
2502 //}
2503 //while (rc2 == SQLITE_ROW);
2504 #ifdef MAPTOOL_SPLIT_NODE_DB
2505 if (nd % 2)
2506 {
2507 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2508 {
2509 sqlite3_reset(stmt_sel002__2a);
2510 }
2511 else
2512 {
2513 sqlite3_reset(stmt_sel002__2b);
2514 }
2515 }
2516 else
2517 {
2518 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2519 {
2520 #endif
2521 sqlite3_reset(stmt_sel002a);
2522 #ifdef MAPTOOL_SPLIT_NODE_DB
2523 }
2524 else
2525 {
2526 sqlite3_reset(stmt_sel002b);
2527 }
2528 }
2529 #endif
2530 #endif
2531
2532 }
2533
2534 void catch_signal(int param)
2535 {
2536 // fprintf (stderr, "Terminating program...\n");
2537 fprintf(stderr, "****** got SIGSEGV ******\n");
2538 THROW(MAPTOOL_00002_EXCEPTION);
2539 // exit(1);
2540 }
2541
2542 struct way_and_endnodes
2543 {
2544 unsigned long long way_id;
2545 unsigned long long first_node_id;
2546 unsigned long long last_node_id;
2547 int reverse;
2548 int role;
2549 GList *nodes;
2550 };
2551
2552 // if same value -> 0
2553 // otherwise -> 1
2554 gint comp_func(gconstpointer item1, gconstpointer item2)
2555 {
2556 long long a;
2557 long long b;
2558
2559 a = *((long long *) item1);
2560 b = *((long long *) item2);
2561
2562 //fprintf(stderr," item1=%lld\n", a);
2563 //fprintf(stderr," item2=%lld\n", b);
2564
2565 if (a == b)
2566 {
2567 return 0;
2568 }
2569
2570 return 1;
2571 }
2572
2573 // return 0 if item is not found in GList
2574 int glist_find_already_inside(GList* list, gconstpointer item)
2575 {
2576 if (g_list_find_custom(list, item, (GCompareFunc) comp_func) == NULL)
2577 {
2578 return 0;
2579 }
2580 return 1;
2581 }
2582
2583 void osm_end_relation(struct maptool_osm *osm)
2584 {
2585 //fprintf(stderr, "XXX000\n");
2586
2587 struct item_bin *item_bin_tri;
2588 struct coord coord_tri;
2589 struct node_lat_lon node_coords;
2590 int i9;
2591 int j9;
2592 int tmp9;
2593
2594 int save_relation = 0;
2595 int save_relations_other_method = 0;
2596 int tag_id = 1;
2597 char *str = NULL;
2598 int triangulate_not_done = 1;
2599 int problem = 0;
2600 struct relation_member memb;
2601 struct relation_member memb2;
2602 struct way_and_endnodes *way_nodes1;
2603 struct way_and_endnodes *way_nodes2;
2604 int found_new_way = 0;
2605 gpointer *gptr;
2606 int rc = 0;
2607 int rc2 = 0;
2608 int cols;
2609 int col;
2610 int first_node_found = 0;
2611 long long nd;
2612 long long first_nd;
2613 long long search_nd;
2614 long long *ndptr;
2615
2616 /*
2617 long lat_nd;
2618 long lon_nd;
2619 */
2620
2621 double lat_nd;
2622 double lon_nd;
2623
2624 int number_of_points_in_poly = 0;
2625 int number_of_points_current_poly = 0;
2626 int exception = 0;
2627 int role_ = 0;
2628
2629 double factor = 10000000;
2630
2631 struct node_lat_lon* lat_lon_c;
2632
2633 // triangulate vars
2634 #if 0
2635 struct triangulateio in, mid, out, vorout;
2636 #endif
2637
2638 #ifdef MAPTOOL_TRIANGULATE
2639 GPtrArray *current_points;
2640 GPtrArray *current_hole;
2641 P2tCDT *cdt;
2642 P2trCDT *rcdt;
2643 P2trRefiner *refiner;
2644 P2tTrianglePtrArray triangles;
2645 gint refine_max_steps = 1000;
2646 double x;
2647 double y;
2648 int i;
2649 int j;
2650 GHashTable *point_map;
2651 P2trCDT *rmesh;
2652 #endif
2653 GList *all_holes;
2654
2655 int count_of_outer_ways = 0;
2656
2657 GList *unsorted_outer_ways = NULL;
2658 GList *unsorted_inner_ways = NULL;
2659 GList *sorted_outer_ways = NULL;
2660 GList *temp_nodes_list = NULL;
2661 all_holes = NULL;
2662
2663 // -- save relations for riverbanks --
2664 while ((str = item_bin_get_attr(item_bin_2, attr_osm_tag, str)))
2665 {
2666
2667 if (!strcmp(str, "waterway=riverbank"))
2668 {
2669 // fprintf(stderr,"k:v=%s\n", str);
2670 save_relation = 1;
2671 save_relations_other_method = 0;
2672 tag_id = 1;
2673 }
2674 else if (!strcmp(str, "natural=water"))
2675 {
2676 save_relation = 1;
2677 save_relations_other_method = 0;
2678 tag_id = 2;
2679 }
2680 /*
2681 else if (!strcmp(str, "water=river"))
2682 {
2683 save_relation = 1;
2684 tag_id = 3;
2685 }
2686 */
2687 else if (!strcmp(str, "natural=lake"))
2688 {
2689 save_relation = 1;
2690 save_relations_other_method = 0;
2691 tag_id = 4;
2692 }
2693 else if (!strcmp(str, "landuse=forest"))
2694 {
2695 // ------ DISABLED ------
2696 // save_relation = 1;
2697 // save_relations_other_method = 1;
2698 // tag_id = 5;
2699 // ------ DISABLED ------
2700 }
2701 else if (!strcmp(str, "natural=wood"))
2702 {
2703 // ------ DISABLED ------
2704 // save_relation = 1;
2705 // save_relations_other_method = 1;
2706 // tag_id = 6;
2707 // ------ DISABLED ------
2708 }
2709 else if (!strcmp(str, "building=yes"))
2710 {
2711 save_relation = 1;
2712 save_relations_other_method = 0;
2713 tag_id = 9;
2714 //fprintf(stderr,"k:v=%s id=%lld\n", str, current_id);
2715 }
2716
2717 }
2718
2719 if (save_relation == 1)
2720 {
2721
2722 #ifdef MAPTOOL_TRIANGULATE
2723
2724 if (save_relations_other_method == 0)
2725 {
2726
2727 //fprintf(stderr, "START relation id=%lld tag=%d\n", current_id, tag_id);
2728
2729 sqlite3_stmt *st;
2730
2731 triangulate_not_done = 1;
2732 str = NULL;
2733 number_of_points_in_poly = 0;
2734 count_of_outer_ways = 0;
2735 //fprintf(stderr, "\n\n----------START---------\n");
2736 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
2737 {
2738 if (get_relation_member(str, &memb))
2739 {
2740 if (!strcmp(memb.role, "outer"))
2741 {
2742 if (memb.type == 2)
2743 {
2744 count_of_outer_ways++;
2745 }
2746 }
2747 }
2748 }
2749 //fprintf(stderr, "number of outer ways: %d\n", count_of_outer_ways);
2750
2751 str = NULL;
2752 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
2753 {
2754 if (get_relation_member(str, &memb))
2755 {
2756 role_=0;
2757 if (!strcmp(memb.role, "outer"))
2758 {
2759 role_=1;
2760 //fprintf(stderr, "1:role=%s id=%lld\n", memb.role, memb.id);
2761 }
2762 else if (!strcmp(memb.role, "inner"))
2763 {
2764 role_=2;
2765 //fprintf(stderr, "2:role=%s id=%lld\n", memb.role, memb.id);
2766 }
2767
2768 if ((memb.type == 2) && ( (role_ == 1)||(role_ == 2) )) // use "outer" and "inner" ways
2769 {
2770 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2771 way_nodes1->nodes = NULL;
2772 // get id for last and first nodes
2773
2774 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2775 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
2776 {
2777 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2778 {
2779 #endif
2780 st = stmt_sel001;
2781 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2782 }
2783 else
2784 {
2785 st = stmt_sel001b;
2786 }
2787 }
2788 else
2789 {
2790 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2791 {
2792 st = stmt_sel001__2;
2793 }
2794 else
2795 {
2796 st = stmt_sel001__2b;
2797 }
2798 }
2799 #endif
2800 sqlite3_bind_int64(st, 1, memb.id);
2801
2802 first_node_found = 0;
2803 way_nodes1->first_node_id = 0;
2804 nd = 0;
2805 number_of_points_current_poly = 0;
2806 // execute the statement
2807 do
2808 {
2809 rc = sqlite3_step(st);
2810 // cols = sqlite3_column_count(st);
2811 switch (rc)
2812 {
2813 case SQLITE_DONE:
2814 break;
2815 case SQLITE_ROW:
2816 nd = sqlite3_column_int64(st, 0);
2817 number_of_points_current_poly++;
2818 //fprintf(stderr, "------ %s = %d\n", sqlite3_column_name(st, 0), nd ? nd : "NULL");
2819 if (first_node_found == 0)
2820 {
2821 way_nodes1->first_node_id = nd;
2822 way_nodes1->role = role_;
2823 first_node_found = 1;
2824 //fprintf(stderr, "------ first node found\n");
2825 }
2826 ndptr = malloc(sizeof(long long));
2827 *ndptr = nd;
2828 way_nodes1->nodes = g_list_append(way_nodes1->nodes, ndptr);
2829 break;
2830 default:
2831 fprintf(stderr, "Error: %d\n", rc);
2832 break;
2833 }
2834 }
2835 while (rc == SQLITE_ROW);
2836
2837 // dont count the last node of each way
2838 number_of_points_current_poly--;
2839
2840 sqlite3_reset(st);
2841
2842 way_nodes1->last_node_id = nd;
2843 way_nodes1->way_id = memb.id;
2844
2845 if (way_nodes1->last_node_id == 0)
2846 {
2847 if (way_nodes1->nodes)
2848 {
2849 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2850 while (way_nodes1->nodes)
2851 {
2852 ndptr = way_nodes1->nodes->data;
2853 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
2854 g_free(ndptr);
2855 ndptr = NULL;
2856 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2857 }
2858 }
2859 g_free(way_nodes1);
2860 }
2861 else if (way_nodes1->last_node_id == way_nodes1->first_node_id)
2862 {
2863 //fprintf(stderr, "ln id=%lld, fn id=%lld\n", way_nodes1->last_node_id, way_nodes1->first_node_id);
2864
2865 if (way_nodes1->nodes)
2866 {
2867 if (role_ == 2)
2868 {
2869 //fprintf(stderr, "role=2\n");
2870 unsorted_inner_ways = g_list_append(unsorted_inner_ways, way_nodes1);
2871 }
2872 else if (role_ == 1)
2873 {
2874 if (count_of_outer_ways == 1)
2875 {
2876 // we have only 1 outer way, it must be a closed loop. add it
2877 unsorted_outer_ways = g_list_append(unsorted_outer_ways, way_nodes1);
2878 number_of_points_in_poly = number_of_points_in_poly + number_of_points_current_poly;
2879 //fprintf(stderr, "w,sn,en %lld,%lld,%lld\n", way_nodes1->way_id, way_nodes1->first_node_id, way_nodes1->last_node_id);
2880 //fprintf(stderr, "num points=%d\n", number_of_points_current_poly);
2881 //fprintf(stderr, "SUM points=%d\n", number_of_points_in_poly);
2882 }
2883 else
2884 {
2885 // we have an outer way that is a loop (remove it)
2886 //fprintf(stderr, "remove node ... start\n");
2887 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2888 while (way_nodes1->nodes)
2889 {
2890 //fprintf(stderr, "remove node\n");
2891 ndptr = way_nodes1->nodes->data;
2892 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
2893 g_free(ndptr);
2894 ndptr = NULL;
2895 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2896 }
2897 g_free(way_nodes1);
2898 //fprintf(stderr, "remove node ... ready\n");
2899 }
2900 }
2901 }
2902 }
2903 else
2904 {
2905 if (role_ == 1)
2906 {
2907 unsorted_outer_ways = g_list_append(unsorted_outer_ways, way_nodes1);
2908 number_of_points_in_poly = number_of_points_in_poly + number_of_points_current_poly;
2909 }
2910 //fprintf(stderr, "w,sn,en %lld,%lld,%lld\n", way_nodes1->way_id, way_nodes1->first_node_id, way_nodes1->last_node_id);
2911 //fprintf(stderr, "num points=%d\n", number_of_points_current_poly);
2912 //fprintf(stderr, "SUM points=%d\n", number_of_points_in_poly);
2913 }
2914 }
2915 }
2916 }
2917
2918 // now sort ways to have a linked polygon
2919 if (unsorted_outer_ways)
2920 {
2921 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2922 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2923 way_nodes2 = (struct way_and_endnodes *) unsorted_outer_ways->data;
2924 way_nodes1->way_id = way_nodes2->way_id;
2925 //fprintf(stderr, "(START)way id=%llu\n", way_nodes1->way_id);
2926 way_nodes1->last_node_id = way_nodes2->last_node_id;
2927 way_nodes1->first_node_id = way_nodes2->first_node_id;
2928 // only copy over the pointer to the node list!!
2929 way_nodes1->nodes = way_nodes2->nodes;
2930 // first node is always in the right order
2931 way_nodes1->reverse = 0;
2932
2933 gptr = unsorted_outer_ways->data;
2934 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
2935 // save the very first node of the poly
2936 first_nd = way_nodes1->first_node_id;
2937 // what node we want to find
2938 search_nd = way_nodes1->last_node_id;
2939 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, gptr);
2940 g_free(gptr);
2941 gptr = NULL;
2942
2943 //fprintf(stderr,"remaining=%d sorted=%d\n", g_list_length(unsorted_outer_ways), g_list_length(sorted_outer_ways));
2944 }
2945
2946 if (unsorted_outer_ways)
2947 {
2948
2949 problem = 0;
2950 //fprintf(stderr, "start length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
2951
2952 while ((g_list_length(unsorted_outer_ways) > 0) && (problem == 0))
2953 {
2954 //fprintf(stderr, "loop thru remaining ways 1\n");
2955
2956 // loop thru all the remaining ways
2957 found_new_way = 0;
2958 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2959 while (unsorted_outer_ways)
2960 {
2961 //fprintf(stderr, "loop thru remaining ways 2 search=%lld\n", search_nd);
2962
2963 if (unsorted_outer_ways->data)
2964 {
2965 way_nodes2 = (struct way_and_endnodes *) unsorted_outer_ways->data;
2966 if (way_nodes2->first_node_id == search_nd)
2967 {
2968
2969 if (way_nodes2->last_node_id == first_nd)
2970 {
2971 if (g_list_length(unsorted_outer_ways) > 1)
2972 {
2973 // we have closed the poly, but there are still "ways" left
2974 // sounds broken
2975 problem = 1;
2976 break;
2977 }
2978 }
2979
2980 // what node we want to find
2981 search_nd = way_nodes2->last_node_id;
2982
2983 // now move the way from unsorted to sorted list
2984 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2985 way_nodes1->way_id = way_nodes2->way_id;
2986
2987 // ok next way found
2988 //fprintf(stderr, "(F)way id=%llu\n", way_nodes1->way_id);
2989 //fprintf(stderr, "now search id=%llu\n", search_nd);
2990
2991 way_nodes1->last_node_id = way_nodes2->last_node_id;
2992 way_nodes1->first_node_id = way_nodes2->first_node_id;
2993 // only copy over the pointer to the node list!!
2994 way_nodes1->nodes = way_nodes2->nodes;
2995 // first node is always in the right order
2996 way_nodes1->reverse = 0;
2997 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
2998 if (unsorted_outer_ways)
2999 {
3000 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
3001 }
3002 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, way_nodes2);
3003 g_free(way_nodes2);
3004 way_nodes2 = NULL;
3005
3006 found_new_way = 1;
3007 //fprintf(stderr, "length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
3008 break;
3009 }
3010 else if (way_nodes2->last_node_id == search_nd)
3011 {
3012 if (way_nodes2->first_node_id == first_nd)
3013 {
3014 if (g_list_length(unsorted_outer_ways) > 1)
3015 {
3016 // we have closed the poly, but there are still "ways" left
3017 // sounds broken
3018 problem = 1;
3019 break;
3020 }
3021 }
3022
3023 // what node we want to find
3024 search_nd = way_nodes2->first_node_id;
3025
3026 // now move the way from unsorted to sorted list
3027 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
3028 way_nodes1->way_id = way_nodes2->way_id;
3029
3030 // ok next way found
3031 //fprintf(stderr, "(R)way id=%llu\n", way_nodes1->way_id);
3032 //fprintf(stderr, "now search id=%llu\n", search_nd);
3033
3034 way_nodes1->first_node_id = way_nodes2->last_node_id;
3035 way_nodes1->last_node_id = way_nodes2->first_node_id;
3036 // only copy over the pointer to the node list!!
3037 way_nodes1->nodes = way_nodes2->nodes;
3038 way_nodes1->nodes = g_list_reverse(way_nodes1->nodes);
3039 // first node is always in the right order
3040 way_nodes1->reverse = 1;
3041 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
3042 if (unsorted_outer_ways)
3043 {
3044 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
3045 }
3046 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, way_nodes2);
3047 g_free(way_nodes2);
3048 way_nodes2 = NULL;
3049
3050 found_new_way = 1;
3051 //fprintf(stderr, "length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
3052 break;
3053 }
3054 }
3055 unsorted_outer_ways = g_list_next(unsorted_outer_ways);
3056 }
3057 //fprintf(stderr, "after while loop 2\n");
3058
3059 if (found_new_way == 0)
3060 {
3061 // some problem with this multipolygon
3062 fprintf_(stderr, "relation id=%lld\n", current_id);
3063 fprintf_(stderr, "some problem with this multipolygon\n");
3064 triangulate_not_done = 1;
3065 problem = 1;
3066 break;
3067 }
3068 }
3069 //fprintf(stderr, "after while loop 1\n");
3070
3071 }
3072
3073 // now at last check if very first node matches the last found node
3074 // and less than xxx 1000 points in polygon
3075 // if ((search_nd == first_nd) && (number_of_points_in_poly > 2) && (number_of_points_in_poly < 1000))
3076 if ((problem == 0) && ((search_nd == first_nd) && (number_of_points_in_poly > 2) && (number_of_points_in_poly < 30000)))
3077 {
3078
3079 //fprintf(stderr, "in relation id=%lld\n", current_id);
3080
3081 point_map = g_hash_table_new(g_direct_hash, g_direct_equal);
3082 current_points = g_ptr_array_new();
3083
3084 number_of_points_in_poly = 0;
3085
3086 sorted_outer_ways = g_list_first(sorted_outer_ways);
3087 while (sorted_outer_ways)
3088 {
3089 if (sorted_outer_ways->data)
3090 {
3091 way_nodes1 = (struct way_and_endnodes *) sorted_outer_ways->data;
3092 // now read all the nodes of this way
3093 //**alread in GList** //sqlite3_bind_int64(stmt_sel001, 1, way_nodes1->way_id);
3094 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3095 first_node_found = 0;
3096 while (way_nodes1->nodes)
3097 {
3098 ndptr = (long long *) way_nodes1->nodes->data;
3099 nd = *ndptr;
3100
3101 get_lat_lon_for_node(nd, &node_coords);
3102 if (node_coords.valid == 1)
3103 {
3104 // lat
3105 lat_nd = node_coords.lat;
3106 // lon
3107 lon_nd = node_coords.lon;
3108
3109 if (first_node_found == 0)
3110 {
3111 first_node_found = 1;
3112 }
3113 else
3114 {
3115 y = lat_nd * factor;
3116 x = lon_nd * factor;
3117
3118 // ------ ok use this node and coords for triangulation ----
3119 //fprintf(stderr, "nd,lat,lon %lld,%f,%f\n", nd,lat_nd,lon_nd);
3120 //fprintf(stderr, "x,y %f,%f\n", x, y);
3121
3122 g_ptr_array_add(current_points, p2t_point_new_dd(x, y));
3123 }
3124 }
3125 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3126 }
3127 }
3128 //sqlite3_reset(st);
3129
3130 sorted_outer_ways = g_list_next(sorted_outer_ways);
3131 }
3132
3133 //fprintf(stderr, "after sorting ways\n");
3134
3135
3136 exception = 0;
3137
3138 TRY
3139 {
3140 // catch SIGSEGV !!
3141 signal(SIGSEGV, catch_signal);
3142
3143 // alternate signal stack ------------
3144 //ss.ss_size = SIGSTKSZ;
3145 //ss.ss_sp = stack;
3146 //sa.sa_handler = catch_signal;
3147 //sa.sa_flags = SA_ONSTACK;
3148 //sigfillset(&sa.sa_mask);
3149
3150 //sigaltstack(&ss, 0);
3151 //sigfillset(&sa.sa_mask);
3152 //sigaction(SIGSEGV, &sa, 0);
3153 // alternate signal stack ------------
3154
3155 // catch SIGSEGV !!
3156
3157 //fprintf(stderr, "In Try Statement (p2t_cdt_new)\n");
3158 cdt = p2t_cdt_new(current_points);
3159 //fprintf(stderr, "all OK (p2t_cdt_new)\n");
3160 }
3161 CATCH( MAPTOOL_00001_EXCEPTION )
3162 {
3163 fprintf_(stderr, "relation id=%lld\n", current_id);
3164 fprintf_(stderr, "Got Exception 1!\n");
3165 exception = 1;
3166 }
3167 CATCH( MAPTOOL_00002_EXCEPTION )
3168 {
3169 fprintf_(stderr, "relation id=%lld\n", current_id);
3170 fprintf_(stderr, "Got segv Exception 1!\n");
3171 exception = 1;
3172 }
3173 FINALLY
3174 {
3175 // set default
3176 signal (SIGSEGV, SIG_DFL);
3177 // set default
3178
3179 //fprintf(stderr, "finally\n");
3180 }
3181 ETRY;
3182
3183 //fprintf(stderr, "after cdt_new\n");
3184
3185 GList* save = unsorted_inner_ways;
3186
3187 int no_holes = 0;
3188 if (exception == 0)
3189 {
3190
3191 // first run a check loop
3192 int number_of_nodes_in_this_way;
3193 int cur_node_count_in_way;
3194 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
3195
3196 int number_of_nodes_in_this_way3 = g_list_length(unsorted_inner_ways);
3197 //fprintf(stderr, "uiw 001.0 c=%d\n", number_of_nodes_in_this_way3);
3198 while (unsorted_inner_ways)
3199 {
3200 if ((unsorted_inner_ways->data)&&(no_holes == 0))
3201 {
3202 way_nodes1 = (struct way_and_endnodes *) unsorted_inner_ways->data;
3203 number_of_nodes_in_this_way = g_list_length(way_nodes1->nodes);
3204 // now read all the nodes of this "hole"-way
3205 GList* save0 = way_nodes1->nodes;
3206 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3207 first_node_found = 0;
3208 cur_node_count_in_way = 1;
3209 while (way_nodes1->nodes)
3210 {
3211 ndptr = (long long *) way_nodes1->nodes->data;
3212 // fprintf(stderr, "hole node:%lld\n", (long long)*ndptr);
3213
3214 if (temp_nodes_list != NULL)
3215 {
3216 //fprintf(stderr, "cur_node_count_in_way %d number_of_nodes_in_this_way %d\n", cur_node_count_in_way, number_of_nodes_in_this_way);
3217 // the last node should always be same as first node (for now we only support holes that dont span over multiple ways!)
3218 if (cur_node_count_in_way < number_of_nodes_in_this_way)
3219 {
3220 if (glist_find_already_inside(temp_nodes_list, ndptr) != 0)
3221 {
3222 fprintf_(stderr, "relation id=%lld\n", current_id);
3223 fprintf_(stderr, "problem with hole!\n");
3224 no_holes = 1;
3225 break;
3226 }
3227 else
3228 {
3229 //fprintf(stderr, "hole check ok\n");
3230 }
3231 }
3232 }
3233
3234 temp_nodes_list = g_list_append(temp_nodes_list, ndptr);
3235
3236 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3237 cur_node_count_in_way++;
3238 }
3239 way_nodes1->nodes = save0;
3240 }
3241 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
3242 }
3243
3244 }
3245
3246 unsorted_inner_ways = save;
3247
3248 //fprintf(stderr, "ex=%d nohole=%d\n", exception, no_holes);
3249
3250
3251
3252 if ((exception == 0) && (no_holes == 0))
3253 {
3254 //fprintf(stderr, "uiw 000\n");
3255
3256 // now do the real loop
3257 int number_of_added_holes = 0;
3258
3259 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
3260 //int number_of_nodes_in_this_way4 = g_list_length(unsorted_inner_ways);
3261 //fprintf(stderr, "uiw 001.2 c=%d\n", number_of_nodes_in_this_way4);
3262 while (unsorted_inner_ways)
3263 {
3264 //fprintf(stderr, "uiw 001\n");
3265
3266 if (unsorted_inner_ways->data)
3267 {
3268 //fprintf(stderr, "uiw 002\n");
3269
3270 current_hole = g_ptr_array_new();
3271
3272 //fprintf(stderr, "uiw 003\n");
3273
3274 way_nodes1 = (struct way_and_endnodes *) unsorted_inner_ways->data;
3275 // now read all the nodes of this "hole"-way
3276 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3277 first_node_found = 0;
3278 while (way_nodes1->nodes)
3279 {
3280 //fprintf(stderr, "uiw 004\n");
3281
3282 ndptr = (long long *) way_nodes1->nodes->data;
3283 nd = *ndptr;
3284
3285 // now read the coords of this node
3286
3287 #ifdef MAPTOOL_SPLIT_NODE_DB
3288 if (nd % 2)
3289 {
3290 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3291 {
3292 sqlite3_bind_int64(stmt_sel002__2a, 1, nd);
3293 }
3294 else
3295 {
3296 sqlite3_bind_int64(stmt_sel002__2b, 1, nd);
3297 }
3298 }
3299 else
3300 {
3301 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3302 {
3303 #endif
3304 sqlite3_bind_int64(stmt_sel002a, 1, nd);
3305 #ifdef MAPTOOL_SPLIT_NODE_DB
3306 }
3307 else
3308 {
3309 sqlite3_bind_int64(stmt_sel002b, 1, nd);
3310 }
3311 }
3312 #endif
3313 rc2 = 0;
3314 do
3315 {
3316 #ifdef MAPTOOL_SPLIT_NODE_DB
3317 if (nd % 2)
3318 {
3319 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3320 {
3321 rc2 = sqlite3_step(stmt_sel002__2a);
3322 }
3323 else
3324 {
3325 rc2 = sqlite3_step(stmt_sel002__2b);
3326 }
3327 }
3328 else
3329 {
3330 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3331 {
3332 #endif
3333 rc2 = sqlite3_step(stmt_sel002a);
3334 #ifdef MAPTOOL_SPLIT_NODE_DB
3335 }
3336 else
3337 {
3338 rc2 = sqlite3_step(stmt_sel002b);
3339 }
3340 }
3341 #endif
3342
3343 switch (rc2)
3344 {
3345 case SQLITE_DONE:
3346 break;
3347 case SQLITE_ROW:
3348
3349 #ifdef MAPTOOL_SPLIT_NODE_DB
3350 if (nd % 2)
3351 {
3352 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3353 {
3354 lat_nd = (double) sqlite3_column_double(stmt_sel002__2a, 0);
3355 lon_nd = (double) sqlite3_column_double(stmt_sel002__2a, 1);
3356 }
3357 else
3358 {
3359 lat_nd = (double) sqlite3_column_double(stmt_sel002__2b, 0);
3360 lon_nd = (double) sqlite3_column_double(stmt_sel002__2b, 1);
3361 }
3362 }
3363 else
3364 {
3365 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3366 {
3367 #endif
3368 lat_nd = (double) sqlite3_column_double(stmt_sel002a, 0);
3369 lon_nd = (double) sqlite3_column_double(stmt_sel002a, 1);
3370 #ifdef MAPTOOL_SPLIT_NODE_DB
3371 }
3372 else
3373 {
3374 lat_nd = (double) sqlite3_column_double(stmt_sel002b, 0);
3375 lon_nd = (double) sqlite3_column_double(stmt_sel002b, 1);
3376 }
3377 }
3378 #endif
3379
3380 if (first_node_found == 0)
3381 {
3382 first_node_found = 1;
3383 }
3384 else
3385 {
3386 // ------ ok use this node and coords for triangulation ----
3387 //fprintf(stderr, "HOLE:nd,lat,lon %lld,%f,%f\n", nd,lat_nd,lon_nd);
3388
3389 //fprintf(stderr, "np=%d\n", number_of_points_in_poly);
3390 //in.pointlist[number_of_points_in_poly] = (REAL) (lat_nd*10);
3391 //number_of_points_in_poly++;
3392 //fprintf(stderr, "np=%d\n", number_of_points_in_poly);
3393 //in.pointlist[number_of_points_in_poly] = (REAL) (lon_nd*10);
3394 //number_of_points_in_poly++;
3395 #if 1
3396 y = lat_nd * factor;
3397 x = lon_nd * factor;
3398
3399 TRY
3400 {
3401 signal(SIGSEGV, catch_signal);
3402
3403 // hole complete, now add it
3404 //fprintf(stderr, "hole new\n");
3405 g_ptr_array_add(current_hole, p2t_point_new_dd(x, y));
3406 //fprintf(stderr, "hole new done\n");
3407 }
3408 CATCH( MAPTOOL_00001_EXCEPTION )
3409 {
3410 fprintf_(stderr, "relation id=%lld\n", current_id);
3411 fprintf_(stderr, "Got Exception A!\n");
3412 exception = 1;
3413 }
3414 CATCH( MAPTOOL_00002_EXCEPTION )
3415 {
3416 fprintf_(stderr, "relation id=%lld\n", current_id);
3417 fprintf_(stderr, "Got segv Exception A!\n");
3418 exception = 1;
3419 }
3420 FINALLY
3421 {
3422 // set default
3423 signal (SIGSEGV, SIG_DFL);
3424 // set default
3425
3426 //fprintf(stderr, "finally\n");
3427 }
3428 ETRY;
3429
3430 #endif
3431 }
3432 break;
3433 default:
3434 fprintf(stderr, "Error: %d\n", rc2);
3435 break;
3436 }
3437 }
3438 while (rc2 == SQLITE_ROW);
3439
3440 #ifdef MAPTOOL_SPLIT_NODE_DB
3441 if (nd % 2)
3442 {
3443 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3444 {
3445 sqlite3_reset(stmt_sel002__2a);
3446 }
3447 else
3448 {
3449 sqlite3_reset(stmt_sel002__2b);
3450 }
3451 }
3452 else
3453 {
3454 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3455 {
3456 #endif
3457 sqlite3_reset(stmt_sel002a);
3458 #ifdef MAPTOOL_SPLIT_NODE_DB
3459 }
3460 else
3461 {
3462 sqlite3_reset(stmt_sel002b);
3463 }
3464 }
3465 #endif
3466
3467 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3468 }
3469
3470 //fprintf(stderr, "uiw 005\n");
3471
3472 // remember the pointer, to free the memory later
3473 all_holes = g_list_append(all_holes, current_hole);
3474
3475 //fprintf(stderr, "uiw 005.1\n");
3476
3477 TRY
3478 {
3479 signal(SIGSEGV, catch_signal);
3480
3481 // hole complete, now add it
3482 //fprintf(stderr, "adding hole\n");
3483 if (number_of_added_holes < MAX_HOLES_PER_POLY)
3484 {
3485 p2t_cdt_add_hole(cdt, current_hole);
3486 number_of_added_holes++;
3487 }
3488 //fprintf(stderr, "adding done\n");
3489 }
3490 CATCH( MAPTOOL_00001_EXCEPTION )
3491 {
3492 fprintf_(stderr, "relation id=%lld\n", current_id);
3493 fprintf_(stderr, "Got Exception 8!\n");
3494 exception = 1;
3495 }
3496 CATCH( MAPTOOL_00002_EXCEPTION )
3497 {
3498 fprintf_(stderr, "relation id=%lld\n", current_id);
3499 fprintf_(stderr, "Got segv Exception 8!\n");
3500 exception = 1;
3501 }
3502 FINALLY
3503 {
3504 // set default
3505 signal (SIGSEGV, SIG_DFL);
3506 // set default
3507
3508 //fprintf(stderr, "finally\n");
3509 }
3510 ETRY;
3511
3512 }
3513
3514 //fprintf(stderr, "uiw 006\n");
3515 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
3516
3517 }
3518 //sqlite3_reset(st);
3519
3520 //fprintf(stderr, "uiw 007\n");
3521 }
3522
3523 //fprintf(stderr, "after adding holes\n");
3524
3525 #if 1
3526
3527 if (exception == 0)
3528 {
3529 exception = 0;
3530
3531 TRY
3532 {
3533 // catch SIGSEGV !!
3534 signal(SIGSEGV, catch_signal);
3535 //signal(SIGILL, catch_signal);
3536
3537 // alternate signal stack ------------
3538 //ss.ss_size = SIGSTKSZ;
3539 //ss.ss_sp = stack;
3540 //sa.sa_handler = catch_signal;
3541 //sa.sa_flags = SA_ONSTACK;
3542 //sigfillset(&sa.sa_mask);
3543
3544 //sigaltstack(&ss, 0);
3545 //sigfillset(&sa.sa_mask);
3546 //sigaction(SIGSEGV, &sa, 0);
3547 // alternate signal stack ------------
3548
3549 // catch SIGSEGV !!
3550
3551 //fprintf(stderr, "triangulate start ...\n");
3552 p2t_cdt_triangulate(cdt);
3553 //fprintf(stderr, "triangulate end\n");
3554 }
3555 CATCH( MAPTOOL_00001_EXCEPTION )
3556 {
3557 fprintf_(stderr, "relation id=%lld\n", current_id);
3558 fprintf_(stderr, "Got Exception 2! (in triangulate)\n");
3559 exception = 1;
3560 }
3561 CATCH( MAPTOOL_00002_EXCEPTION )
3562 {
3563 fprintf_(stderr, "relation id=%lld\n", current_id);
3564 fprintf_(stderr, "Got segv Exception 2! (in triangulate)\n");
3565 exception = 1;
3566 }
3567 FINALLY
3568 {
3569 //fprintf(stderr, "finally 2 (in triangulate)\n");
3570
3571 //sa.sa_handler = SIG_DFL;
3572 // sigfillset(&sa.sa_mask);
3573 //sigaction(SIGSEGV, &sa, 0);
3574
3575 // set default
3576 signal (SIGSEGV, SIG_DFL);
3577 // set default
3578 }
3579 ETRY;
3580
3581 if (exception == 0)
3582 {
3583 rmesh = g_slice_new (P2trCDT);
3584 rmesh->mesh = p2tr_mesh_new ();
3585 // rmesh->outline = p2tr_pslg_new ();
3586
3587 /* First iteration over the CDT - create all the points */
3588 //fprintf(stderr, "trinew 006\n");
3589 triangles = p2t_cdt_get_triangles(cdt);
3590 //fprintf(stderr, "trinew 007\n");
3591 for (i = 0; i < triangles->len; i++)
3592 {
3593 P2tTriangle *cdt_tri = triangle_index (triangles, i);
3594 for (j = 0; j < 3; j++)
3595 {
3596 P2tPoint *cdt_pt = p2t_triangle_get_point(cdt_tri, j);
3597 P2trPoint *new_pt = (P2trPoint*) g_hash_table_lookup (point_map, cdt_pt);
3598
3599 if (new_pt == NULL)
3600 {
3601 //fprintf(stderr, "p1 x=%d, y=%d\n",(int)cdt_pt->x, (int)cdt_pt->y);
3602 new_pt = p2tr_mesh_new_point2 (rmesh->mesh, (cdt_pt->x / factor), (cdt_pt->y / factor));
3603 g_hash_table_insert (point_map, cdt_pt, new_pt);
3604 }
3605 }
3606 }
3607
3608 for (i = 0; i < triangles->len; i++)
3609 {
3610 P2tTriangle *cdt_tri = triangle_index(triangles, i);
3611 //fprintf(stderr, "tri# %d\n", i);
3612 gboolean ii = p2t_triangle_is_interior(cdt_tri);
3613 //fprintf(stderr, "is int=%d\n", ii);
3614
3615 P2trPoint *pt1 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 0));
3616 P2trPoint *pt2 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 1));
3617 P2trPoint *pt3 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 2));
3618
3619 //fprintf(stderr, "0 x=%f, y=%f\n",pt1->c.x, pt1->c.y);
3620 //fprintf(stderr, "1 x=%f, y=%f\n",pt2->c.x, pt2->c.y);
3621 //fprintf(stderr, "2 x=%f, y=%f\n",pt3->c.x, pt3->c.y);
3622
3623 // make item and write it to ways file -------------------
3624 // make item and write it to ways file -------------------
3625 if (tag_id == 1)
3626 {
3627 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3628 //fprintf(stderr, "T:water\n");
3629 }
3630 else if (tag_id == 2)
3631 {
3632 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3633 //fprintf(stderr, "T:water\n");
3634 }
3635 else if (tag_id == 4)
3636 {
3637 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3638 //fprintf(stderr, "T:water\n");
3639 }
3640 else if (tag_id == 5)
3641 {
3642 item_bin_tri = init_item(type_poly_wood_from_triang, 0);
3643 //fprintf(stderr, "T:forest\n");
3644 }
3645 else if (tag_id == 6)
3646 {
3647 item_bin_tri = init_item(type_poly_wood_from_triang, 0);
3648 //fprintf(stderr, "T:forest\n");
3649 }
3650 else if (tag_id == 9)
3651 {
3652 item_bin_tri = init_item(type_poly_building_from_triang, 0);
3653 //fprintf(stderr, "T:building\n");
3654 }
3655 else
3656 {
3657 // default
3658 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3659 //fprintf(stderr, "T:water\n");
3660 }
3661
3662 // first add coords
3663 coord_tri.x = transform_from_geo_lon(pt1->c.x);
3664 coord_tri.y = transform_from_geo_lat(pt1->c.y);
3665 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3666 coord_tri.x = transform_from_geo_lon(pt2->c.x);
3667 coord_tri.y = transform_from_geo_lat(pt2->c.y);
3668 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3669 coord_tri.x = transform_from_geo_lon(pt3->c.x);
3670 coord_tri.y = transform_from_geo_lat(pt3->c.y);
3671 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3672 // now add dummy osm wayid
3673 item_bin_add_attr_longlong(item_bin_tri, attr_osm_wayid, current_id);
3674
3675 // DEBUG -- DEBUG -- DEBUG --
3676 // DEBUG -- DEBUG -- DEBUG --
3677 // dump_itembin(item_bin_tri);
3678 // DEBUG -- DEBUG -- DEBUG --
3679 // DEBUG -- DEBUG -- DEBUG --
3680 item_bin_write(item_bin_tri, osm->ways_with_coords);
3681
3682 // make item and write it to ways file -------------------
3683 // make item and write it to ways file -------------------
3684
3685 }
3686
3687 //fprintf(stderr, "trinew 091\n");
3688
3689 }
3690
3691 //fprintf(stderr, "trinew 092.0\n");
3692
3693
3694 TRY
3695 {
3696 signal(SIGSEGV, catch_signal);
3697
3698 /* Now finally unref the points we added into the map */
3699 if (point_map != NULL)
3700 {
3701 GHashTableIter iter;
3702 P2trPoint *pt_iter = NULL;
3703 g_hash_table_iter_init (&iter, point_map);
3704 while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&pt_iter))
3705 {
3706 p2tr_point_unref(pt_iter);
3707 }
3708 g_hash_table_destroy(point_map);
3709 }
3710 }
3711 CATCH( MAPTOOL_00001_EXCEPTION )
3712 {
3713 fprintf_(stderr, "relation id=%lld\n", current_id);
3714 fprintf_(stderr, "Got Exception 3!\n");
3715 exception = 1;
3716 }
3717 CATCH( MAPTOOL_00002_EXCEPTION )
3718 {
3719 fprintf_(stderr, "relation id=%lld\n", current_id);
3720 fprintf_(stderr, "Got segv Exception 3!\n");
3721 exception = 1;
3722 }
3723 FINALLY
3724 {
3725 signal (SIGSEGV, SIG_DFL);
3726 }
3727 ETRY;
3728
3729 //fprintf(stderr, "trinew 092.1\n");
3730
3731
3732 TRY
3733 {
3734 signal(SIGSEGV, catch_signal);
3735 // p2tr_mesh_clear(rmesh->mesh);
3736 // --- maybe need this?? --- p2tr_mesh_unref(rmesh->mesh);
3737 }
3738 CATCH( MAPTOOL_00001_EXCEPTION )
3739 {
3740 fprintf_(stderr, "relation id=%lld\n", current_id);
3741 fprintf_(stderr, "Got Exception 4!\n");
3742 exception = 1;
3743 }
3744 CATCH( MAPTOOL_00002_EXCEPTION )
3745 {
3746 fprintf_(stderr, "relation id=%lld\n", current_id);
3747 fprintf_(stderr, "Got segv Exception 4!\n");
3748 exception = 1;
3749 }
3750 FINALLY
3751 {
3752 signal (SIGSEGV, SIG_DFL);
3753 }
3754 ETRY;
3755
3756 //fprintf(stderr, "trinew 092.2\n");
3757
3758 TRY
3759 {
3760 signal(SIGSEGV, catch_signal);
3761 p2t_cdt_free(cdt);
3762 }
3763 CATCH( MAPTOOL_00001_EXCEPTION )
3764 {
3765 fprintf_(stderr, "relation id=%lld\n", current_id);
3766 fprintf_(stderr, "Got Exception 5!\n");
3767 exception = 1;
3768 }
3769 CATCH( MAPTOOL_00002_EXCEPTION )
3770 {
3771 fprintf_(stderr, "relation id=%lld\n", current_id);
3772 fprintf_(stderr, "Got segv Exception 5!\n");
3773 exception = 1;
3774 }
3775 FINALLY
3776 {
3777 signal (SIGSEGV, SIG_DFL);
3778 }
3779 ETRY;
3780
3781 //fprintf(stderr, "trinew 092.3\n");
3782
3783 } // exception == 0 ? -- END
3784
3785 // rcdt = p2tr_cdt_new(cdt);
3786 /*
3787 if (refine_max_steps > 0)
3788 {
3789 fprintf(stderr, "Refining the mesh!\n");
3790 refiner = p2tr_refiner_new(G_PI / 6, p2tr_refiner_false_too_big, rcdt);
3791 p2tr_refiner_refine(refiner, refine_max_steps, NULL);
3792 p2tr_refiner_free(refiner);
3793
3794 p2tr_cdt_validate_edges(rcdt);
3795 }
3796 */
3797
3798 //fprintf(stderr, "free holes\n");
3799
3800 if (all_holes)
3801 {
3802 all_holes = g_list_first(all_holes);
3803 while (all_holes)
3804 {
3805 current_hole = all_holes->data;
3806 for (i = 0; i < current_hole->len; ++i)
3807 {
3808 p2t_point_free(g_ptr_array_index(current_hole, i));
3809 }
3810 g_ptr_array_free(current_hole, TRUE);
3811
3812 all_holes = g_list_next(all_holes);
3813 }
3814 g_list_free(all_holes);
3815 all_holes = NULL;
3816 }
3817
3818 //fprintf(stderr, "free points\n");
3819
3820 for (i = 0; i < current_points->len; ++i)
3821 {
3822 p2t_point_free(g_ptr_array_index(current_points, i));
3823 }
3824 g_ptr_array_free(current_points, TRUE);
3825
3826 // p2tr_cdt_free(rcdt);
3827
3828 #endif
3829
3830 #if 0
3831 item_bin_write(item_bin_tri, osm->ways_with_coords);
3832 #endif
3833 triangulate_not_done = 0;
3834 }
3835
3836 // free everything!! ----------------
3837 if (unsorted_outer_ways)
3838 {
3839 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
3840 while (unsorted_outer_ways)
3841 {
3842 gptr = unsorted_outer_ways->data;
3843 way_nodes1 = unsorted_outer_ways->data;
3844 if (way_nodes1->nodes)
3845 {
3846 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3847 while (way_nodes1->nodes)
3848 {
3849 ndptr = way_nodes1->nodes->data;
3850 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3851 g_free(ndptr);
3852 ndptr = NULL;
3853 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3854 }
3855 }
3856 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, gptr);
3857 g_free(gptr);
3858 gptr = NULL;
3859 unsorted_outer_ways = g_list_next(unsorted_outer_ways);
3860 }
3861 }
3862
3863 if (unsorted_inner_ways)
3864 {
3865 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
3866 while (unsorted_inner_ways)
3867 {
3868 gptr = unsorted_inner_ways->data;
3869 way_nodes1 = unsorted_inner_ways->data;
3870 if (way_nodes1->nodes)
3871 {
3872 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3873 while (way_nodes1->nodes)
3874 {
3875 ndptr = way_nodes1->nodes->data;
3876 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3877 g_free(ndptr);
3878 ndptr = NULL;
3879 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3880 }
3881 }
3882 unsorted_inner_ways = g_list_remove(unsorted_inner_ways, gptr);
3883 g_free(gptr);
3884 gptr = NULL;
3885 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
3886 }
3887 }
3888
3889 if (sorted_outer_ways)
3890 {
3891 sorted_outer_ways = g_list_first(sorted_outer_ways);
3892 while (sorted_outer_ways)
3893 {
3894 gptr = sorted_outer_ways->data;
3895 way_nodes1 = sorted_outer_ways->data;
3896 if (way_nodes1->nodes)
3897 {
3898 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3899 while (way_nodes1->nodes)
3900 {
3901 ndptr = way_nodes1->nodes->data;
3902 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3903 g_free(ndptr);
3904 ndptr = NULL;
3905 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3906 }
3907 }
3908 sorted_outer_ways = g_list_remove(sorted_outer_ways, gptr);
3909 g_free(gptr);
3910 gptr = NULL;
3911 sorted_outer_ways = g_list_next(sorted_outer_ways);
3912 }
3913 }
3914 // free everything!! ----------------
3915 }
3916 // save relations other method --------------
3917
3918 #endif
3919
3920 if (triangulate_not_done == 1)
3921 {
3922 str = NULL;
3923 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3924 {
3925 if (get_relation_member(str, &memb))
3926 {
3927 if ((memb.type == 2) && (!strcmp(memb.role, "outer"))) // use only "outer" "ways" here!!
3928 {
3929 struct way_tag wt;
3930 wt.way_id = memb.id;
3931 wt.tag_id = tag_id;
3932 // only use "water" related ways here
3933 if ((tag_id == 1) || (tag_id == 2) || (tag_id == 4) || (tag_id == 5) || (tag_id == 6))
3934 {
3935 //fprintf(stderr,"rel way=%llu\n", memb.id);
3936 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3937 }
3938 }
3939 else if ((memb.type == 2) && (!strcmp(memb.role, "inner"))) // use only "outer" "ways" here!!
3940 {
3941 struct way_tag wt;
3942 wt.way_id = memb.id;
3943 wt.tag_id = tag_id;
3944 // only use "water" related ways here
3945 if (tag_id == 6)
3946 {
3947 wt.tag_id = 8;
3948 //fprintf(stderr,"rel way(8)=%llu\n", memb.id);
3949 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3950 }
3951 else if (tag_id == 5)
3952 {
3953 wt.tag_id = 7;
3954 //fprintf(stderr,"rel way(7)=%llu\n", memb.id);
3955 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3956 }
3957 }
3958
3959 }
3960 }
3961 }
3962 }
3963 // -- save relations for riverbanks --
3964
3965
3966 in_relation = 0;
3967
3968 if ((!strcmp(relation_type, "multipolygon") || !strcmp(relation_type, "boundary")) && boundary)
3969 {
3970 int found_town_rel = 0;
3971
3972 fprintf_(stderr, "town to relation 00: r:%lld\n", current_id);
3973
3974 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
3975 {
3976 // save relation id for towns into DB
3977 str = NULL;
3978 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3979 {
3980 if (get_relation_member(str, &memb))
3981 {
3982 if (memb.type == 1)
3983 {
3984 fprintf_(stderr, "role=%s\n", memb.role);
3985
3986 if (!strcmp(memb.role, "label"))
3987 {
3988 fprintf_(stderr, "town to relation 1: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
3989 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3990 // fprintf(stderr, "ret=%d\n", rr);
3991 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
3992 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
3993 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
3994 sqlite3_step(stmt_town_sel008);
3995 //fprintf(stderr, "ret=%d\n", rr);
3996 sqlite3_reset(stmt_town_sel008);
3997 //fprintf(stderr, "ret=%d\n", rr);
3998
3999 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
4000
4001 lat_lon_c = get_first_coord_of_boundary(item_bin_2, &memb);
4002 if (lat_lon_c)
4003 {
4004 sqlite3_bind_double(stmt_town, 6, lat_lon_c->lat);
4005 sqlite3_bind_double(stmt_town, 7, lat_lon_c->lon);
4006 g_free(lat_lon_c);
4007 }
4008
4009 sqlite3_bind_int64(stmt_town, 1, dummy_town_id);
4010 sqlite3_bind_int(stmt_town, 2, 999);
4011 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_level) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
4012 sqlite3_bind_text(stmt_town, 3, osm_tag_value(item_bin_2, "name"), -1, SQLITE_STATIC);
4013 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
4014 sqlite3_bind_int64(stmt_town, 8, current_id);
4015 sqlite3_step(stmt_town);
4016 sqlite3_reset(stmt_town);
4017 dummy_town_id--;
4018
4019 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4020 //fprintf(stderr, "ret=%d\n", rr);
4021
4022 found_town_rel = 1;
4023 }
4024 }
4025 }
4026 }
4027 }
4028
4029
4030
4031 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
4032 {
4033 // save relation id for towns into DB
4034 str = NULL;
4035 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
4036 {
4037 if (get_relation_member(str, &memb))
4038 {
4039 if (memb.type == 1)
4040 {
4041 fprintf_(stderr, "role=%s\n", memb.role);
4042
4043 if (!strcmp(memb.role, "admin_centre"))
4044 {
4045 fprintf_(stderr, "town to relation 2: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
4046 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4047 // fprintf(stderr, "ret=%d\n", rr);
4048 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
4049 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
4050 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
4051 sqlite3_step(stmt_town_sel008);
4052 //fprintf(stderr, "ret=%d\n", rr);
4053 sqlite3_reset(stmt_town_sel008);
4054 //fprintf(stderr, "ret=%d\n", rr);
4055
4056 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
4057
4058 lat_lon_c = get_first_coord_of_boundary(item_bin_2, &memb);
4059 if (lat_lon_c)
4060 {
4061 sqlite3_bind_double(stmt_town, 6, lat_lon_c->lat);
4062 sqlite3_bind_double(stmt_town, 7, lat_lon_c->lon);
4063 g_free(lat_lon_c);
4064 }
4065
4066 sqlite3_bind_int64(stmt_town, 1, dummy_town_id);
4067 sqlite3_bind_int(stmt_town, 2, 999);
4068 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_level) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
4069 sqlite3_bind_text(stmt_town, 3, osm_tag_value(item_bin_2, "name"), -1, SQLITE_STATIC);
4070 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
4071 sqlite3_bind_int64(stmt_town, 8, current_id);
4072 sqlite3_step(stmt_town);
4073 sqlite3_reset(stmt_town);
4074 dummy_town_id--;
4075
4076 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4077 //fprintf(stderr, "ret=%d\n", rr);
4078
4079 found_town_rel = 1;
4080 }
4081 }
4082 }
4083 }
4084 }
4085
4086 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
4087 {
4088 // save relation id for towns into DB
4089 str = NULL;
4090 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
4091 {
4092 if (get_relation_member(str, &memb))
4093 {
4094 if (memb.type == 1)
4095 {
4096 //fprintf(stderr, "role=%s\n", memb.role);
4097
4098 if (!strcmp(memb.role, "admin_center"))
4099 {
4100 fprintf_(stderr, "town to relation 3: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
4101 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4102 // fprintf(stderr, "ret=%d\n", rr);
4103 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
4104 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
4105 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
4106 sqlite3_step(stmt_town_sel008);
4107 //fprintf(stderr, "ret=%d\n", rr);
4108 sqlite3_reset(stmt_town_sel008);
4109 //fprintf(stderr, "ret=%d\n", rr);
4110
4111 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
4112
4113 lat_lon_c = get_first_coord_of_boundary(item_bin_2, &memb);
4114 if (lat_lon_c)
4115 {
4116 sqlite3_bind_double(stmt_town, 6, lat_lon_c->lat);
4117 sqlite3_bind_double(stmt_town, 7, lat_lon_c->lon);
4118 g_free(lat_lon_c);
4119 }
4120
4121 sqlite3_bind_int64(stmt_town, 1, dummy_town_id);
4122 sqlite3_bind_int(stmt_town, 2, 999);
4123 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_level) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
4124 sqlite3_bind_text(stmt_town, 3, osm_tag_value(item_bin_2, "name"), -1, SQLITE_STATIC);
4125 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
4126 sqlite3_bind_int64(stmt_town, 8, current_id);
4127 sqlite3_step(stmt_town);
4128 sqlite3_reset(stmt_town);
4129 dummy_town_id--;
4130
4131 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4132 //fprintf(stderr, "ret=%d\n", rr);
4133
4134 found_town_rel = 1;
4135 }
4136 }
4137 }
4138 }
4139 }
4140
4141
4142
4143 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
4144 {
4145 // save relation id for towns into DB (use dummy town data)
4146 fprintf_(stderr, "town to relation(*): rel-name:%s admlevel:%d r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), admin_level, current_id, dummy_town_id);
4147 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4148 // fprintf(stderr, "ret=%d\n", rr);
4149 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
4150 sqlite3_bind_int64(stmt_town_sel008, 2, dummy_town_id);
4151 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
4152 sqlite3_step(stmt_town_sel008);
4153 //fprintf(stderr, "ret=%d\n", rr);
4154 sqlite3_reset(stmt_town_sel008);
4155 //fprintf(stderr, "ret=%d\n", rr);
4156
4157 int did_write_boundary_to_db = 0;
4158 str = NULL;
4159 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
4160 {
4161 if (get_relation_member(str, &memb))
4162 {
4163 if ((memb.type == 2) && (strcmp(memb.role, "inner")))
4164 {
4165 // way-id is "memb.id"
4166 // use the first nodes coords
4167 //get_lat_lon_way_first_node(memb.id, &node_coords);
4168 //if (node_coords.valid == 1)
4169 //{
4170 // sqlite3_bind_double(stmt_town, 6, node_coords.lat);
4171 // sqlite3_bind_double(stmt_town, 7, node_coords.lon);
4172 //}
4173
4174 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
4175 did_write_boundary_to_db = 1;
4176
4177 break;
4178 }
4179 }
4180 }
4181
4182 lat_lon_c = get_first_coord_of_boundary(item_bin_2, &memb);
4183 if (lat_lon_c)
4184 {
4185 sqlite3_bind_double(stmt_town, 6, lat_lon_c->lat);
4186 sqlite3_bind_double(stmt_town, 7, lat_lon_c->lon);
4187 fprintf_(stderr, "town to relation(*):coords %f %f\n", lat_lon_c->lat, lat_lon_c->lon);
4188 g_free(lat_lon_c);
4189 }
4190
4191
4192 sqlite3_bind_int64(stmt_town, 1, dummy_town_id);
4193 sqlite3_bind_int(stmt_town, 2, 999);
4194 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_level) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
4195 sqlite3_bind_text(stmt_town, 3, osm_tag_value(item_bin_2, "name"), -1, SQLITE_STATIC);
4196 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
4197
4198 if (did_write_boundary_to_db == 1)
4199 {
4200 sqlite3_bind_int64(stmt_town, 8, current_id);
4201 }
4202 else
4203 {
4204 // boundary not in DB
4205 // ----- what? i have no idea what to do here -----
4206 }
4207 sqlite3_step(stmt_town);
4208 sqlite3_reset(stmt_town);
4209
4210 found_town_rel = 1;
4211
4212 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4213 //fprintf(stderr, "ret=%d\n", rr);
4214
4215 dummy_town_id--;
4216 }
4217
4218 // rest of the boundaries!!!
4219 if (found_town_rel == 0)
4220 {
4221 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4222
4223 str = NULL;
4224 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
4225 {
4226 if (get_relation_member(str, &memb))
4227 {
4228 if ((memb.type == 2) && (!strcmp(memb.role, "outer")))
4229 {
4230 fprintf_(stderr, "town to relation(+): rel-name:%s admlevel:%d r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), admin_level, current_id, 9797979797);
4231
4232 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
4233 break;
4234 }
4235 }
4236 }
4237 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4238
4239 found_town_rel = 0;
4240 }
4241
4242
4243 #if 0
4244 if (admin_level == 2)
4245 {
4246 FILE *f;
4247 fprintf(stderr,"Multipolygon for %s\n", iso_code);
4248 char *name=g_strdup_printf("country_%s.tmp",iso_code);
4249 f=fopen(name,"w");
4250 item_bin_write(item_bin_2, f);
4251 fclose(f);
4252 }
4253 #endif
4254 item_bin_write(item_bin_2, osm->boundaries);
4255 }
4256
4257 if (!strcmp(relation_type, "restriction") && (item_bin_2->type == type_street_turn_restriction_no || item_bin_2->type == type_street_turn_restriction_only))
4258 {
4259 item_bin_write(item_bin_2, osm->turn_restrictions);
4260 }
4261 }
4262
4263
4264 // #define DEBUG_CORRECT_BOUNDARY_REF_POINT 1
4265
4266 void correct_boundary_ref_point(GList *bl)
4267 {
4268 long long rid;
4269 int retval;
4270 int rc = 0;
4271 int commit_after = 20000;
4272 int count = 0;
4273 int admin_l = 99;
4274 double lat, lon;
4275 struct coord c;
4276 GList *l = NULL;
4277 GList *l2 = NULL;
4278 GList *match = NULL;
4279 struct boundary *b = NULL;
4280 int admin_l_boundary;
4281 int max_admin_l;
4282
4283 //fprintf(stderr, "BRP:001\n");
4284
4285 // loop thru all the boundaries
4286 do
4287 {
4288
4289 //fprintf(stderr, "BRP:002\n");
4290
4291 rc = sqlite3_step(stmt_bd_002);
4292
4293 //fprintf(stderr, "BRP:003\n");
4294
4295 switch (rc)
4296 {
4297 case SQLITE_DONE:
4298 break;
4299 case SQLITE_ROW:
4300
4301 //fprintf(stderr, "BRP:004\n");
4302 // select rel_id, admin_level, lat, lon, name from boundary where done = 0;
4303 rid = sqlite3_column_int64(stmt_bd_002, 0);
4304 //fprintf(stderr, "BRP:005\n");
4305 admin_l = sqlite3_column_int(stmt_bd_002, 1);
4306 //fprintf(stderr, "BRP:006\n");
4307 lat = sqlite3_column_double(stmt_bd_002, 2);
4308 //fprintf(stderr, "BRP:007\n");
4309 lon = sqlite3_column_double(stmt_bd_002, 3);
4310 //fprintf(stderr, "BRP:008\n");
4311 c.x = transform_from_geo_lon(lon);
4312 //fprintf(stderr, "BRP:009\n");
4313 c.y = transform_from_geo_lat(lat);
4314 //fprintf(stderr, "BRP:010\n");
4315
4316 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4317 fprintf(stderr, "correct_boundary_ref_point: relid:%lld(%d) x=%d y=%d\n", rid, admin_l, c.x, c.y);
4318 #endif
4319
4320 // find this boundary in list
4321 l = bl;
4322 match = NULL;
4323
4324 //fprintf(stderr, "BRP:011\n");
4325
4326 while (l)
4327 {
4328 // fprintf(stderr, "BRP:012 l=%p l->data=%p\n", l, l->data);
4329
4330 b = l->data;
4331
4332 // fprintf(stderr, "BRP:012a b=%p\n", b);
4333
4334 if (b)
4335 {
4336 // fprintf(stderr, "BRP:012b\n");
4337 if (b->ib)
4338 {
4339 // fprintf(stderr, "BRP:012c\n");
4340
4341 // fprintf(stderr, "BRP:013a %p\n", b);
4342 // fprintf(stderr, "BRP:013b %p\n", b->ib);
4343 // fprintf(stderr, "BRP:013c %lld\n", (long long)rid);
4344
4345 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4346 fprintf(stderr, "correct_boundary_ref_point: %lld %lld\n", item_bin_get_relationid(b->ib), rid);
4347 #endif
4348 if (item_bin_get_relationid(b->ib) == rid)
4349 {
4350 // fprintf(stderr, "BRP:014\n");
4351 match = l;
4352 break;
4353 }
4354 // fprintf(stderr, "BRP:015\n");
4355
4356 }
4357 }
4358
4359 l = g_list_next(l);
4360
4361 // fprintf(stderr, "BRP:016\n");
4362
4363 }
4364
4365 //fprintf(stderr, "BRP:017\n");
4366
4367 if (match)
4368 {
4369 //fprintf(stderr, "BRP:018\n");
4370
4371 b = match->data;
4372
4373 if (b)
4374 {
4375
4376 //fprintf(stderr, "BRP:019\n");
4377
4378 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4379 fprintf(stderr, "correct_boundary_ref_point: relid:%lld(%d) parentid:%lld(%d)\n", rid, admin_l, item_bin_get_relationid(b->ib), max_admin_l);
4380 #endif
4381 // -------------------------------------------
4382 /*
4383 GList *sl;
4384 sl = b->sorted_segments;
4385 if (sl)
4386 {
4387 struct geom_poly_segment *gs = sl->data;
4388 struct coord *c2 = gs->first;
4389 fprintf(stderr, "1a c=%d y=%d\n", c2->x, c2->y);
4390 }
4391 */
4392 // -------------------------------------------
4393
4394 if (find_correct_point_in_boundary(match, &lat, &lon) == 1)
4395 {
4396
4397 //fprintf(stderr, "BRP:020\n");
4398 sqlite3_bind_double(stmt_bd_004, 1, lat);
4399 //fprintf(stderr, "BRP:021\n");
4400 sqlite3_bind_double(stmt_bd_004, 2, lon);
4401 //fprintf(stderr, "BRP:022\n");
4402 sqlite3_bind_int64(stmt_bd_004, 3, rid);
4403 //fprintf(stderr, "BRP:023\n");
4404 sqlite3_step(stmt_bd_004);
4405 //fprintf(stderr, "BRP:024\n");
4406 sqlite3_reset(stmt_bd_004);
4407 //fprintf(stderr, "BRP:025\n");
4408 }
4409
4410 //fprintf(stderr, "BRP:026\n");
4411
4412 }
4413
4414 }
4415
4416 //fprintf(stderr, "BRP:027\n");
4417
4418 if (count == 0)
4419 {
4420 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4421 }
4422 count++;
4423
4424 //fprintf(stderr, "BRP:028\n");
4425
4426 if (count > commit_after)
4427 {
4428 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4429 count = 0;
4430 }
4431
4432 //fprintf(stderr, "BRP:029\n");
4433
4434 break;
4435 default:
4436 fprintf(stderr, "SQL Error: %d\n", rc);
4437 break;
4438 }
4439 }
4440 while (rc == SQLITE_ROW);
4441
4442 //fprintf(stderr, "BRP:091\n");
4443
4444 sqlite3_reset(stmt_bd_002);
4445
4446 //fprintf(stderr, "BRP:092\n");
4447
4448 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4449
4450 //fprintf(stderr, "BRP:093\n");
4451
4452 }
4453
4454
4455 int find_correct_point_in_boundary(GList *bl, double* lat, double* lon)
4456 {
4457 double lat2, lon2;
4458 struct coord c;
4459 struct boundary *b;
4460 GList *matches;
4461 double lat_d = 0.00006;
4462 double lon_d = 0.00006;
4463
4464 lat2 = *lat;
4465 lon2 = *lon;
4466
4467 c.x = transform_from_geo_lon(lon2 + lon_d);
4468 c.y = transform_from_geo_lat(lat2 + lat_d);
4469
4470 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4471 fprintf(stderr, "2 c=%d y=%d\n", c.x, c.y);
4472 #endif
4473
4474 matches = NULL;
4475 matches = boundary_find_matches_single(bl, &c);
4476 if (g_list_length(matches) > 0)
4477 {
4478 *lon = lon2 + lon_d;
4479 *lat = lat2 + lat_d;
4480 return 1;
4481 }
4482
4483 c.x = transform_from_geo_lon(lon2 + lon_d);
4484 c.y = transform_from_geo_lat(lat2 - lat_d);
4485
4486 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4487 fprintf(stderr, "3 c=%d y=%d\n", c.x, c.y);
4488 #endif
4489
4490 matches = NULL;
4491 matches = boundary_find_matches_single(bl, &c);
4492 if (g_list_length(matches) > 0)
4493 {
4494 *lon = lon2 + lon_d;
4495 *lat = lat2 - lat_d;
4496 return 1;
4497 }
4498
4499 c.x = transform_from_geo_lon(lon2 - lon_d);
4500 c.y = transform_from_geo_lat(lat2 + lat_d);
4501
4502 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4503 fprintf(stderr, "4 c=%d y=%d\n", c.x, c.y);
4504 #endif
4505
4506 matches = NULL;
4507 matches = boundary_find_matches_single(bl, &c);
4508 if (g_list_length(matches) > 0)
4509 {
4510 *lon = lon2 - lon_d;
4511 *lat = lat2 + lat_d;
4512 return 1;
4513 }
4514
4515 c.x = transform_from_geo_lon(lon2 - lon_d);
4516 c.y = transform_from_geo_lat(lat2 - lat_d);
4517
4518 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4519 fprintf(stderr, "5 c=%d y=%d\n", c.x, c.y);
4520 #endif
4521
4522 matches = NULL;
4523 matches = boundary_find_matches_single(bl, &c);
4524 if (g_list_length(matches) > 0)
4525 {
4526 *lon = lon2 - lon_d;
4527 *lat = lat2 - lat_d;
4528 return 1;
4529 }
4530
4531
4532 c.x = transform_from_geo_lon(lon2 + lon_d);
4533 c.y = transform_from_geo_lat(lat2);
4534
4535 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4536 fprintf(stderr, "6 c=%d y=%d\n", c.x, c.y);
4537 #endif
4538
4539 matches = NULL;
4540 matches = boundary_find_matches_single(bl, &c);
4541 if (g_list_length(matches) > 0)
4542 {
4543 *lon = lon2 + lon_d;
4544 *lat = lat2;
4545 return 1;
4546 }
4547
4548 c.x = transform_from_geo_lon(lon2 - lon_d);
4549 c.y = transform_from_geo_lat(lat2);
4550
4551 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4552 fprintf(stderr, "7 c=%d y=%d\n", c.x, c.y);
4553 #endif
4554
4555 matches = NULL;
4556 matches = boundary_find_matches_single(bl, &c);
4557 if (g_list_length(matches) > 0)
4558 {
4559 *lon = lon2 - lon_d;
4560 *lat = lat2;
4561 return 1;
4562 }
4563
4564 c.x = transform_from_geo_lon(lon2);
4565 c.y = transform_from_geo_lat(lat2 + lat_d);
4566
4567 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4568 fprintf(stderr, "8 c=%d y=%d\n", c.x, c.y);
4569 #endif
4570
4571 matches = NULL;
4572 matches = boundary_find_matches_single(bl, &c);
4573 if (g_list_length(matches) > 0)
4574 {
4575 *lon = lon2;
4576 *lat = lat2 + lat_d;
4577 return 1;
4578 }
4579
4580
4581 c.x = transform_from_geo_lon(lon2);
4582 c.y = transform_from_geo_lat(lat2 - lat_d);
4583
4584 #ifdef DEBUG_CORRECT_BOUNDARY_REF_POINT
4585 fprintf(stderr, "9 c=%d y=%d\n", c.x, c.y);
4586 #endif
4587
4588 matches = NULL;
4589 matches = boundary_find_matches_single(bl, &c);
4590 if (g_list_length(matches) > 0)
4591 {
4592 *lon = lon2;
4593 *lat = lat2 - lat_d;
4594 return 1;
4595 }
4596
4597 return 0;
4598
4599 }
4600
4601 void build_boundary_tree(GList *bl, GList *man_bl)
4602 {
4603 long long rid;
4604 int retval;
4605 int rc = 0;
4606 int commit_after = 20000;
4607 int count = 0;
4608 int admin_l = 99;
4609 double lat, lon;
4610 struct coord c;
4611 GList *l = NULL;
4612 GList *l2 = NULL;
4613 GList *match = NULL;
4614 struct boundary *b;
4615 int admin_l_boundary;
4616 int max_admin_l;
4617 long long b_count = 0;
4618 long long b_max_count = 0;
4619
4620
4621 sqlite3_stmt *stmt_mm_1;
4622 int retval8 = sqlite3_prepare_v2(sql_handle, "select count(*) from boundary where done = 0;", -1, &stmt_mm_1, NULL);
4623 fprintf_(stderr, "prep:%d\n", retval8);
4624 sqlite3_step(stmt_mm_1);
4625 b_max_count = sqlite3_column_int64(stmt_mm_1, 0);
4626 fprintf_(stderr, "b_max_count:%lld\n", b_max_count);
4627 sqlite3_reset(stmt_mm_1);
4628 sqlite3_finalize(stmt_mm_1);
4629
4630
4631 // loop thru all the boundaries
4632 do
4633 {
4634 rc = sqlite3_step(stmt_bd_002);
4635 switch (rc)
4636 {
4637 case SQLITE_DONE:
4638 break;
4639 case SQLITE_ROW:
4640
4641 // select rel_id, admin_level, lat, lon, name from boundary where done = 0;
4642 rid = sqlite3_column_int64(stmt_bd_002, 0);
4643 admin_l = sqlite3_column_int(stmt_bd_002, 1);
4644 lat = sqlite3_column_double(stmt_bd_002, 2);
4645 lon = sqlite3_column_double(stmt_bd_002, 3);
4646 c.x = transform_from_geo_lon(lon);
4647 c.y = transform_from_geo_lat(lat);
4648
4649 b_count++;
4650 if ((b_count % 100) == 0)
4651 {
4652 fprintf_(stderr, "rel:%lld/%lld\n", b_count, b_max_count);
4653 // fprintf(stderr, "relid:%lld(%d) x=%d y=%d\n", rid, admin_l, c.x, c.y);
4654 }
4655
4656 max_admin_l = 0;
4657 l2 = boundary_find_matches_level(bl, &c, 0, (admin_l - 1));
4658 l = l2;
4659 match = NULL;
4660 while (l)
4661 {
4662 b = l->data;
4663 char *admin_l_boundary_str = osm_tag_value(b->ib, "admin_level");
4664 if (!admin_l_boundary_str)
4665 {
4666 admin_l_boundary = 9999;
4667 }
4668 else
4669 {
4670 admin_l_boundary = atoi(admin_l_boundary_str);
4671 }
4672
4673 if (admin_l_boundary < 2)
4674 {
4675 admin_l_boundary = 9999;
4676 }
4677
4678 //fprintf(stderr, "b=%d\n", admin_l_boundary);
4679 if (admin_l_boundary > max_admin_l)
4680 {
4681 max_admin_l = admin_l_boundary;
4682 match = l;
4683 //fprintf(stderr, "b2=%d\n", max_admin_l);
4684 }
4685 l = g_list_next(l);
4686 }
4687
4688 if (match)
4689 {
4690 b = match->data;
4691 //fprintf(stderr, "relid:%lld(%d) parentid:%lld(%d)\n", rid, admin_l, item_bin_get_relationid(b->ib), max_admin_l);
4692
4693 sqlite3_bind_int64(stmt_bd_003, 1, item_bin_get_relationid(b->ib));
4694 sqlite3_bind_int64(stmt_bd_003, 2, rid);
4695 sqlite3_step(stmt_bd_003);
4696 sqlite3_reset(stmt_bd_003);
4697 }
4698
4699
4700 if (count == 0)
4701 {
4702 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4703 }
4704 count++;
4705
4706 if (count > commit_after)
4707 {
4708 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4709 count = 0;
4710 }
4711
4712 break;
4713 default:
4714 fprintf(stderr, "SQL Error: %d\n", rc);
4715 break;
4716 }
4717 }
4718 while (rc == SQLITE_ROW);
4719
4720 sqlite3_reset(stmt_bd_002);
4721
4722 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4723
4724
4725 // ------- enter manual countries into boundary DB ----------
4726 GList *bl2 = man_bl;
4727 struct boundary_manual *bb3;
4728
4729 struct country_table3
4730 {
4731 int countryid;
4732 char *names;
4733 char *admin_levels;
4734 FILE *file;
4735 int size;
4736 struct rect r;
4737 };
4738
4739
4740 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4741 while (bl2)
4742 {
4743 bb3 = bl2->data;
4744 if (bb3)
4745 {
4746 struct country_table3* cc3 = bb3->country;
4747 sqlite3_bind_int64(stmt_bd_001, 1, bb3->town_id); // dummy id
4748 sqlite3_bind_int(stmt_bd_001, 2, 2); // admin level
4749 sqlite3_bind_text(stmt_bd_001, 5, cc3->names, -1, SQLITE_STATIC); // name
4750 sqlite3_step(stmt_bd_001);
4751 sqlite3_reset(stmt_bd_001);
4752 }
4753 bl2 = g_list_next(bl2);
4754 }
4755 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4756 count = 0;
4757
4758
4759 // ------- now assign all the left overs towns/boundaries to countries ----------
4760 long long dummy_town_id2 = 0;
4761 count = 0;
4762 admin_l = 99;
4763 rc = 0;
4764
4765 // loop thru all the boundaries
4766 do
4767 {
4768 rc = sqlite3_step(stmt_bd_002);
4769 switch (rc)
4770 {
4771 case SQLITE_DONE:
4772 break;
4773 case SQLITE_ROW:
4774
4775 // select rel_id, admin_level, lat, lon, name from boundary where done = 0;
4776 rid = sqlite3_column_int64(stmt_bd_002, 0);
4777 admin_l = sqlite3_column_int(stmt_bd_002, 1);
4778 lat = sqlite3_column_double(stmt_bd_002, 2);
4779 lon = sqlite3_column_double(stmt_bd_002, 3);
4780 c.x = transform_from_geo_lon(lon);
4781 c.y = transform_from_geo_lat(lat);
4782
4783 //fprintf(stderr, "relid:%lld(%d) x=%d y=%d\n", rid, admin_l, c.x, c.y);
4784 if (admin_l > 2)
4785 {
4786
4787 // check if c is inside manual country border
4788 dummy_town_id2 = osm_process_street_by_manual_country_borders(man_bl, &c);
4789
4790 if (dummy_town_id2 != -1)
4791 {
4792 sqlite3_bind_int64(stmt_bd_003, 1, dummy_town_id2); // parent rel_id
4793 sqlite3_bind_int64(stmt_bd_003, 2, rid); // rel_id
4794 sqlite3_step(stmt_bd_003);
4795 sqlite3_reset(stmt_bd_003);
4796 }
4797
4798
4799 if (count == 0)
4800 {
4801 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4802 }
4803 count++;
4804
4805 if (count > commit_after)
4806 {
4807 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4808 count = 0;
4809 }
4810
4811 }
4812 break;
4813 default:
4814 fprintf(stderr, "SQL Error: %d\n", rc);
4815 break;
4816 }
4817 }
4818 while (rc == SQLITE_ROW);
4819 sqlite3_reset(stmt_bd_002);
4820
4821 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4822
4823 }
4824
4825 struct node_lat_lon* get_first_coord_of_boundary(struct item_bin *item_bin_3, struct relation_member *memb)
4826 {
4827 char *str = NULL;
4828 struct node_lat_lon* node_coords=g_new0(struct node_lat_lon, 1);
4829
4830 while ((str = item_bin_get_attr(item_bin_3, attr_osm_member, str)))
4831 {
4832 if (get_relation_member(str, memb))
4833 {
4834 if (memb->type == 2)
4835 {
4836 // way-id is "memb.id"
4837 // use the first nodes coords
4838 get_lat_lon_way_first_node(memb->id, node_coords);
4839 if (node_coords->valid == 1)
4840 {
4841 return node_coords;
4842 }
4843 break;
4844 }
4845 }
4846 }
4847
4848 return NULL;
4849 }
4850
4851 void add_boundary_to_db(long long current_id, int admin_level, struct item_bin *item_bin_3, struct relation_member *memb)
4852 {
4853 char *str = NULL;
4854 struct node_lat_lon node_coords;
4855
4856 // fprintf(stderr, "add_boundary_to_db:bid:%lld admin_level:%d\n", current_id, admin_level);
4857
4858 sqlite3_bind_int64(stmt_bd_001, 1, current_id);
4859
4860 if (admin_level < 2)
4861 {
4862 sqlite3_bind_int(stmt_bd_001, 2, 9999);
4863 }
4864 else
4865 {
4866 sqlite3_bind_int(stmt_bd_001, 2, admin_level);
4867 }
4868
4869 while ((str = item_bin_get_attr(item_bin_3, attr_osm_member, str)))
4870 {
4871 if (get_relation_member(str, memb))
4872 {
4873 if (memb->type == 2)
4874 {
4875 // way-id is "memb.id"
4876 // use the first nodes coords
4877 get_lat_lon_way_first_node(memb->id, &node_coords);
4878 if (node_coords.valid == 1)
4879 {
4880 sqlite3_bind_double(stmt_bd_001, 3, node_coords.lat);
4881 sqlite3_bind_double(stmt_bd_001, 4, node_coords.lon);
4882 }
4883 break;
4884 }
4885 }
4886 }
4887 sqlite3_bind_text(stmt_bd_001, 5, osm_tag_value(item_bin_3, "name"), -1, SQLITE_STATIC);
4888 sqlite3_step(stmt_bd_001);
4889 sqlite3_reset(stmt_bd_001);
4890 }
4891
4892
4893 void osm_add_member(int type, osmid ref, char *role)
4894 {
4895 char member_buffer[BUFFER_SIZE * 3 + 3];
4896 struct attr memberattr = { attr_osm_member };
4897
4898 sprintf(member_buffer,"%d:"LONGLONG_FMT":%s", type, (long long) ref, role);
4899 memberattr.u.str = member_buffer;
4900 item_bin_add_attr(item_bin_2, &memberattr);
4901 }
4902
4903 static void relation_add_tag(char *k, char *v)
4904 {
4905 int add_tag = 1;
4906 #if 0
4907 fprintf(stderr,"add tag %s %s\n",k,v);
4908 #endif
4909 if (!strcmp(k, "type"))
4910 {
4911 strcpy(relation_type, v);
4912 add_tag = 0;
4913 }
4914 else if (!strcmp(k, "restriction"))
4915 {
4916 if (!strncmp(v, "no_", 3))
4917 {
4918 item_bin_2->type = type_street_turn_restriction_no;
4919 add_tag = 0;
4920 }
4921 else if (!strncmp(v, "only_", 5))
4922 {
4923 item_bin_2->type = type_street_turn_restriction_only;
4924 add_tag = 0;
4925 }
4926 else
4927 {
4928 item_bin_2->type = type_none;
4929 //osm_warning("relation", current_id, 0, "Unknown restriction %s\n", v);
4930 }
4931 }
4932 else if (!strcmp(k, "admin_level"))
4933 {
4934 admin_level = atoi(v);
4935 }
4936 else if (!strcmp(k, "boundary"))
4937 {
4938 if (!strcmp(v, "administrative") || (experimental && !strcmp(v, "postal_code")))
4939 {
4940 boundary = 1;
4941 }
4942 }
4943 else if (!strcmp(k, "ISO3166-1"))
4944 {
4945 strcpy(iso_code, v);
4946 }
4947
4948 if (add_tag)
4949 {
4950 //fprintf(stderr,"*TAG*%s=%s\n", k, v);
4951 char tag[strlen(k) + strlen(v) + 2];
4952 sprintf(tag, "%s=%s", k, v);
4953 item_bin_add_attr_string(item_bin_2, attr_osm_tag, tag);
4954 }
4955 }
4956
4957 static int attr_longest_match(struct attr_mapping **mapping, int mapping_count, enum item_type *types, int types_count)
4958 {
4959 int i, j, longest = 0, ret = 0, sum, val;
4960 struct attr_mapping *curr;
4961 for (i = 0; i < mapping_count; i++)
4962 {
4963 sum = 0;
4964 curr = mapping[i];
4965 for (j = 0; j < curr->attr_present_idx_count; j++)
4966 {
4967 val = attr_present[curr->attr_present_idx[j]];
4968 if (val)
4969 sum += val;
4970 else
4971 {
4972 sum = -1;
4973 break;
4974 }
4975 }
4976 if (sum > longest)
4977 {
4978 longest = sum;
4979 ret = 0;
4980 }
4981 if (sum > 0 && sum == longest && ret < types_count)
4982 types[ret++] = curr->type;
4983 }
4984 return ret;
4985 }
4986
4987 static void attr_longest_match_clear(void)
4988 {
4989 memset(attr_present, 0, sizeof(*attr_present) * attr_present_count);
4990 }
4991
4992 char* remove_all_spaces_non_utf8(char *str)
4993 {
4994 char *write = str, *read = str;
4995
4996 if (str == NULL)
4997 {
4998 return NULL;
4999 }
5000
5001 do
5002 {
5003 if (!g_ascii_isspace(*read))
5004 {
5005 *write++ = *read;
5006 }
5007 } while (*read++);
5008
5009 return str;
5010 }
5011
5012
5013 char* get_index_for_string(const char *in)
5014 {
5015 // first letters for indexing faster later
5016 char *sub = g_strndup(in, 2);
5017
5018
5019 // check first letter
5020 if (strlen(sub) > 0)
5021 {
5022 if ((sub[0] < 97) || (sub[0]) > 122)
5023 {
5024 sub[0]='\0';
5025 // fprintf(stderr, "(1)in=%s sub=%s\n", in, sub);
5026 return sub;
5027 }
5028 }
5029
5030 // if second letter != a-z then delete it from index
5031 if (strlen(sub) > 1)
5032 {
5033 // 'a' 'z'
5034 if ((sub[1] < 97) || (sub[1]) > 122)
5035 {
5036 sub[1]='\0';
5037 }
5038 }
5039
5040 // fprintf(stderr, "(2)in=%s sub=%s\n", in, sub);
5041 return sub;
5042 }
5043
5044
5045
5046 static int tab_atan[]=
5047 { 0,262,524,787,1051,1317,1584,1853,2126,2401,2679,2962,3249,3541,3839,4142,4452,4770,5095,5430,5774,6128,6494,6873,7265,7673,8098,8541,9004,9490,10000,10538};
5048
5049 static int
5050 atan2_int_lookup(int val)
5051 {
5052 int len=sizeof(tab_atan)/sizeof(int);
5053 int i=len/2;
5054 int p=i-1;
5055
5056 for (;;)
5057 {
5058 i>>=1;
5059 if (val < tab_atan[p])
5060 {
5061 p-=i;
5062 }
5063 else
5064 {
5065 if (val < tab_atan[p+1])
5066 {
5067 return p+(p>>1);
5068 }
5069 else
5070 {
5071 p+=i;
5072 }
5073 }
5074 }
5075 }
5076
5077
5078 inline double my_atan2_double(double x, double y)
5079 {
5080 double result;
5081
5082 asm (
5083 "fpatan\n\t"
5084 : "=t" (result) // outputs; t = top of fpu stack
5085 : "0" (x), // inputs; 0 = same as result
5086 "u" (y) // u = 2nd floating point register
5087 );
5088
5089 return result;
5090 }
5091
5092
5093 static int
5094 atan2_int(int dx, int dy)
5095 {
5096 int mul=1,add=0,ret;
5097
5098 if (! dx)
5099 {
5100 return dy < 0 ? 180 : 0;
5101 }
5102
5103 if (! dy)
5104 {
5105 return dx < 0 ? -90 : 90;
5106 }
5107
5108 if (dx < 0)
5109 {
5110 dx=-dx;
5111 mul=-1;
5112 }
5113
5114 if (dy < 0)
5115 {
5116 dy=-dy;
5117 add=180*mul;
5118 mul*=-1;
5119 }
5120
5121 while (dx > 20000 || dy > 20000)
5122 {
5123 dx/=10;
5124 dy/=10;
5125 }
5126
5127 if (dx > dy)
5128 {
5129 ret=90-atan2_int_lookup(dy*10000/dx);
5130 }
5131 else
5132 {
5133 ret=atan2_int_lookup(dx*10000/dy);
5134 }
5135
5136 return ret*mul+add;
5137 }
5138
5139
5140
5141 static int maptool_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
5142 {
5143 int dx = c2->x - c1->x;
5144 int dy = c2->y - c1->y;
5145
5146 // fprintf(stderr, "angle: %d %d %d %d\n", c1->x, c1->y, c2->x, c2->y);
5147
5148 #if 0
5149 double angle;
5150 angle = my_atan2_double(dy, dx);
5151 angle *= 57.29577952019129709110; // ( 180/ M_PI ) = 57.29577952019129709110
5152 //fprintf(stderr, "1 angle=%lf\n", angle);
5153 #endif
5154
5155 #if 0
5156 double angle;
5157 angle = atan2(dx, dy);
5158 angle *= 57.29577952019129709110; // ( 180/ M_PI ) = 57.29577952019129709110
5159 fprintf(stderr, "2 angle=%lf\n", angle);
5160 #endif
5161
5162 #if 1
5163 int angle;
5164 angle=atan2_int(dx,dy);
5165 //fprintf(stderr, "3 angle=%d\n", (int)(angle));
5166 #endif
5167
5168 if (dir == -1)
5169 {
5170 angle = angle - 180;
5171 }
5172
5173 if (angle < 0)
5174 {
5175 angle += 360;
5176 }
5177
5178 return angle;
5179 }
5180
5181
5182 /**
5183 * @brief Calculates the delta between two angles
5184 * @param angle1 The first angle
5185 * @param angle2 The second angle
5186 * @return The difference between the angles: -179..-1=angle2 is left of angle1,0=same,1..179=angle2 is right of angle1,180=angle1 is opposite of angle2
5187 */
5188 static int maptool_delta_angle(int angle1, int angle2)
5189 {
5190 int delta = angle2 - angle1;
5191
5192 if (delta <= -180)
5193 {
5194 delta += 360;
5195 }
5196
5197 if (delta > 180)
5198 {
5199 delta -= 360;
5200 }
5201
5202 return delta;
5203 }
5204
5205
5206 clock_t maptool_debug_measure_start(void)
5207 {
5208 clock_t start = clock();
5209 return start;
5210 }
5211
5212 clock_t maptool_debug_measure_end(clock_t start_time)
5213 {
5214 clock_t diff_time = clock() - start_time;
5215 return diff_time;
5216 }
5217
5218 int maptool_debug_measure_end_tsecs(clock_t start_time)
5219 {
5220 clock_t diff_time = clock() - start_time;
5221 return (int) (((double) diff_time / (double) CLOCKS_PER_SEC) * 1000);
5222 }
5223
5224 void maptool_debug_measure_result_str(clock_t diff, char *buffer)
5225 {
5226 sprintf(buffer, "elapsed: %fs\n", (diff / CLOCKS_PER_SEC));
5227 }
5228
5229 void maptool_debug_mrp(clock_t diff)
5230 {
5231 fprintf(stderr, "angle el:%fs\n", (double) ((double) diff / (double) CLOCKS_PER_SEC));
5232 }
5233
5234
5235
5236 static int maptool_get_node_x_y(long long way_id, int node_num_in_way, struct coord *c_ret)
5237 {
5238 int ret = 0;
5239 struct node_lat_lon node_coords2;
5240
5241 // fprintf(stderr, "angle way id=%lld\n", way_id);
5242
5243 long long way_node = get_waynode_num_have_seekpos(way_id, node_num_in_way, 0, (off_t)seekpos1);
5244
5245 if (way_node > 0)
5246 {
5247 // fprintf(stderr, "angle way node id=%lld\n", way_node);
5248
5249 get_lat_lon_for_node(way_node, &node_coords2);
5250 if (node_coords2.valid == 1)
5251 {
5252 //c_ret->x = transform_from_geo_lon(node_coords2.lon);
5253 c_ret->x = (int)(node_coords2.lon * 100000.0);
5254
5255 //c_ret->y = transform_from_geo_lat(node_coords2.lat);
5256 c_ret->y = (int)(node_coords2.lat * 100000.0);
5257
5258 // save coord of way_node
5259 // coord_buffer_3[node_num_in_way].x = c_ret->x;
5260 // coord_buffer_3[node_num_in_way].y = c_ret->y;
5261
5262 // fprintf(stderr, "angle node_num=%d x=%d y=%d lon=%f lat=%f\n", node_num_in_way, c_ret->x, c_ret->y, node_coords2.lon, node_coords2.lat);
5263 ret = 1;
5264 }
5265 }
5266
5267 return ret;
5268 }
5269
5270
5271 static void osm_write_cycle_way(FILE *ways_file, int i, int type, struct coord *coord_buffer_2, int start_node_num_in_way, int node_count, int part_num)
5272 {
5273 int *def_flags, add_flags;
5274 struct item_bin *item_bin;
5275
5276 item_bin = init_item(type, 0);
5277 def_flags = item_get_default_flags(type);
5278
5279 // struct coord *coord_buffer_4 = &coord_buffer_3[start_node_num_in_way];
5280 struct coord *coord_buffer_4 = &coord_buffer[start_node_num_in_way];
5281 item_bin_add_coord(item_bin, coord_buffer_4, node_count);
5282
5283 // fprintf(stderr, "Xangle node_num=%d count=%d wayid=%lld\n", start_node_num_in_way, node_count, osmid_attr_value);
5284 //int ii;
5285 //for (ii=0;ii<node_count;ii++)
5286 //{
5287 // fprintf(stderr, "Yangle node_num=%d x=%d y=%d\n", ii, coord_buffer_4[ii].x, coord_buffer_4[ii].y);
5288 //}
5289
5290 if (def_flags)
5291 {
5292 flags_attr_value = (*def_flags | flags[0] | flags[1]) & ~flags[2];
5293 // only add flags if they differ from the default flags!!
5294 if (flags_attr_value != *def_flags)
5295 {
5296 add_flags = 1;
5297 }
5298 }
5299 else // maybe should explicitly specify which "types" to consider? now its all "types"
5300 {
5301 def_flags = 0;
5302 flags_attr_value = (flags[0] | flags[1]) & ~flags[2];
5303
5304 // only add flags if we really have a value
5305 if (flags_attr_value != 0)
5306 {
5307 add_flags = 1;
5308 }
5309 }
5310
5311 item_bin_add_attr_string(item_bin, def_flags ? attr_street_name : attr_label, attr_strings[attr_string_label]);
5312
5313 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value);
5314
5315 if (add_flags)
5316 {
5317 item_bin_add_attr_int(item_bin, attr_flags, flags_attr_value);
5318 }
5319
5320 // ------- add DEBUG ATTR --------
5321 item_bin_add_attr_int(item_bin, attr_debugsplitway, start_node_num_in_way);
5322 // ------- add DEBUG ATTR --------
5323
5324 if (maxspeed_attr_value)
5325 {
5326 item_bin_add_attr_int(item_bin, attr_maxspeed, maxspeed_attr_value);
5327 }
5328
5329 // custom color attribute
5330 if (attr_strings[attr_string_colour_])
5331 {
5332 unsigned int cc = color_int_value_from_string(attr_strings[attr_string_colour_]);
5333 //fprintf(stderr, "col:ret=%d\n", cc);
5334 if (cc != 0)
5335 {
5336 cc = (cc >> 8);
5337 //fprintf(stderr, "col:in map=%d\n", (int)cc);
5338 item_bin_add_attr_int(item_bin, attr_colour2, (int)cc);
5339 }
5340 }
5341
5342 item_bin_write(item_bin, ways_file);
5343 }
5344
5345 long long cycle_w_processed_count = 0;
5346
5347 void osm_end_way(struct maptool_osm *osm)
5348 {
5349 int i, count;
5350 int *def_flags, add_flags;
5351 enum item_type types[10];
5352 struct item_bin *item_bin;
5353 int count_lines = 0, count_areas = 0;
5354 int dont_save_to_db = 1;
5355 int first = 1;
5356 int retval;
5357 char *name_folded = NULL;
5358 char *name_folded2 = NULL;
5359 struct node_lat_lon node_coords;
5360 osmid w_node;
5361 int write_this_item_to_binfile;
5362 off_t seek_posx = 0;
5363
5364 in_way = 0;
5365
5366 if (!osm->ways)
5367 {
5368 return;
5369 }
5370
5371 ways_processed_count++;
5372
5373 write_this_item_to_binfile = 1;
5374
5375 count = attr_longest_match(attr_mapping_way, attr_mapping_way_count, types, sizeof(types) / sizeof(enum item_type));
5376 if (!count)
5377 {
5378 count = 1;
5379 types[0] = type_street_unkn;
5380 }
5381
5382 if (count >= 10)
5383 {
5384 fprintf_(stderr, "way id %lld\n", osmid_attr_value);
5385 dbg_assert(count < 10);
5386 }
5387
5388 if (attr_strings[attr_string_label] != NULL)
5389 {
5390 if (strlen(attr_strings[attr_string_label]) > 0)
5391 {
5392 dont_save_to_db = 0;
5393
5394 // check if "alt name" is same as normal name
5395 if (attr_strings[attr_string_label_alt] != NULL)
5396 {
5397 if (!strcmp(attr_strings[attr_string_label], attr_strings[attr_string_label_alt]))
5398 {
5399 // strings are the same, clear "alt name"
5400 attr_strings[attr_string_label_alt] = NULL;
5401 }
5402 }
5403 }
5404 }
5405
5406 // required for fread to work!
5407 fseeko(ways_ref_file, 0L, SEEK_CUR);
5408
5409
5410 // correct bicycle flags ------------------------------------------------------------------------
5411 if ((flags[0] & NAVIT_AF_ONEWAY) || (flags[0] & NAVIT_AF_ONEWAYREV)) // it's a one way road
5412 {
5413 if (flags[0] & NAVIT_AF_ONEWAY_BICYCLE_NO) // bike against oneway
5414 {
5415 flags[0] = (flags[0] & ~NAVIT_AF_BICYCLE_LANE); // remove the lane flag
5416 flags[0] = (flags[0] & ~NAVIT_AF_BICYCLE_TRACK); // remove the track flag
5417 }
5418 flags[0] = (flags[0] & ~NAVIT_AF_ONEWAY_BICYCLE_YES); // its already oneway, so remove additional oneway-bike flag
5419 }
5420 else // no oneway
5421 {
5422 flags[0] = (flags[0] & ~NAVIT_AF_ONEWAY_BICYCLE_NO); // remove the bike against-oneway-road flag, if no oneway road
5423 }
5424 // correct bicycle flags ------------------------------------------------------------------------
5425
5426
5427
5428
5429
5430 // check if its a bicycle way and also a footway, remove footway then!! -------------------------
5431 int is_cycleway = -1;
5432 int is_footway = -1;
5433 for (i = 0; i < count; i++)
5434 {
5435 // cycleway
5436 // footway
5437 if (types[i] == type_cycleway)
5438 {
5439 is_cycleway = i;
5440 }
5441 else if (types[i] == type_footway)
5442 {
5443 is_footway = i;
5444 }
5445 }
5446
5447 if ((is_footway > -1) && (is_cycleway > -1))
5448 {
5449 types[is_footway] = type_none; // remove the footway
5450 }
5451 // check if its a bicycle way and also a footway, remove footway then!! -------------------------
5452
5453
5454
5455 for (i = 0; i < count; i++) // loop thru all the attributes of this way (and add a way for every attribute known -> so ways get duplicated here!!)
5456 {
5457 add_flags = 0;
5458 write_this_item_to_binfile = 1;
5459 if (types[i] == type_none)
5460 {
5461 continue;
5462 }
5463
5464 //if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn))
5465 // continue;
5466
5467 // DO NOT exclude unknown streets here, or city-to-countrypolygon matching won't work!!
5468 if (ignore_unkown && (types[i] == type_point_unkn))
5469 {
5470 continue;
5471 }
5472
5473
5474 if (types[i] == type_cycleway)
5475 {
5476
5477 // fprintf(stderr, "C=%lld W=%lld\n", cycle_w_processed_count, ways_processed_count);
5478
5479 // check if we have a turn inside this way with angle greater than ROAD_ANGLE_MIN_FOR_TURN_BICYCLEMODE
5480 int i_coord;
5481 int i_coord_prev = 0;
5482 struct coord c1_;
5483 struct coord c2_;
5484 struct coord c3_;
5485 int angle_way_1;
5486 int angle_way_2;
5487 int angle_way_delta;
5488 osmid way_node;
5489 int is_valid_;
5490 int fff = 1;
5491 int split_part_num;
5492 struct coord *coord_buffer_2 = NULL;
5493
5494 if (coord_count > 2)
5495 {
5496 if (osmid_attr_value > 0)
5497 {
5498 cycle_w_processed_count++;
5499
5500 coord_buffer_2 = &coord_buffer[0]; // point to first real coordinate of way
5501 split_part_num = 1;
5502
5503 for (i_coord = 1; i_coord < (coord_count - 1); i_coord++)
5504 {
5505 is_valid_ = 0;
5506
5507 //fprintf(stderr, "angle i_coord=%d\n", i_coord);
5508
5509 if (fff == 1)
5510 {
5511 is_valid_ = is_valid_ + (maptool_get_node_x_y(osmid_attr_value, (i_coord - 1), &c1_));
5512 is_valid_ = is_valid_ + (maptool_get_node_x_y(osmid_attr_value, i_coord, &c2_));
5513 fff = 0;
5514 }
5515 else
5516 {
5517 c1_.x = c2_.x;
5518 c1_.y = c2_.y;
5519 c2_.x = c3_.x;
5520 c2_.y = c3_.y;
5521 is_valid_++;
5522 is_valid_++;
5523 }
5524
5525 if (is_valid_ == 2)
5526 {
5527 angle_way_1 = maptool_get_angle_delta(&c1_, &c2_, 1);
5528 //fprintf(stderr, "angle1=%d\n", angle_way_1);
5529 }
5530
5531 is_valid_++;
5532 is_valid_ = is_valid_ + (maptool_get_node_x_y(osmid_attr_value, (i_coord + 1), &c3_));
5533
5534 if (is_valid_ == 4)
5535 {
5536 angle_way_2 = maptool_get_angle_delta(&c2_, &c3_, 1);
5537 //fprintf(stderr, "angle2=%d\n", angle_way_2);
5538
5539 angle_way_delta = abs(maptool_delta_angle(angle_way_1, angle_way_2));
5540 //fprintf(stderr, "angle_way_delta=%d\n", angle_way_delta);
5541
5542 if (angle_way_delta >= ROAD_ANGLE_MIN_FOR_TURN_BICYCLEMODE)
5543 {
5544 // dont write this item to binfile in the normal way
5545 write_this_item_to_binfile = 0;
5546
5547 //fprintf(stderr, "angle cut at node:%d prevnode:%d\n", i_coord, i_coord_prev);
5548 osm_write_cycle_way(osm->ways, i, types[i], coord_buffer_2, i_coord_prev, (i_coord - i_coord_prev + 1), split_part_num);
5549 i_coord_prev = i_coord;
5550 coord_buffer_2 = coord_buffer_2 + (i_coord - i_coord_prev); // move pointer to next starting coord of split-way
5551 split_part_num++;
5552 }
5553 }
5554 }
5555
5556 if (write_this_item_to_binfile == 0)
5557 {
5558 //fprintf(stderr, "angle: write last part node:%d prevnode:%d\n", i_coord, i_coord_prev);
5559 // write last part of split-way
5560 osm_write_cycle_way(osm->ways, i, types[i], coord_buffer_2, i_coord_prev, (i_coord - i_coord_prev + 1), split_part_num);
5561 }
5562 }
5563 }
5564 }
5565
5566
5567 if (types[i] != type_street_unkn)
5568 {
5569 if (types[i] < type_area)
5570 {
5571 count_lines++;
5572 }
5573 else
5574 {
5575 count_areas++;
5576 }
5577 }
5578 item_bin = init_item(types[i], 0);
5579 item_bin_add_coord(item_bin, coord_buffer, coord_count);
5580 def_flags = item_get_default_flags(types[i]);
5581
5582 // save nodes of way -----------------
5583 // save nodes of way -----------------
5584
5585 if (coastline_only_map == 1)
5586 {
5587 first = 0;
5588 }
5589
5590 if (first == 1)
5591 {
5592 first = 0;
5593 int i788 = 0;
5594 struct coord *c788 = (struct coord *) (item_bin + 1); // set pointer to coord struct of this item
5595 for (i788 = 0; i788 < item_bin->clen / 2; i788++)
5596 {
5597 if (dont_save_to_db == 0)
5598 {
5599 // save lat,lon of first node of this way
5600 if (i788 == 0)
5601 {
5602 #ifdef MAPTOOL_USE_SQL
5603 //fprintf(stderr, "insert -w\n");
5604 //fprintf(stderr, "ways DEBUG: wid:%ld\n", osmid_attr_value);
5605 //fprintf(stderr, "ways DEBUG: nid:%lld\n", first_node_of_current_way);
5606 get_lat_lon_for_node(first_node_of_current_way, &node_coords);
5607
5608 if (sql_counter == 0)
5609 {
5610 retval = sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5611 if ((retval > 0) && (retval < 100))
5612 {
5613 fprintf_(stderr, "==================================\n");
5614 fprintf_(stderr, "begin: ways code:%d\n", retval);
5615 retval = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5616 fprintf_(stderr, "begin:(commit) ways code:%d\n", retval);
5617 retval = sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5618 fprintf_(stderr, "begin:(begin) ways code:%d\n", retval);
5619 fprintf_(stderr, "==================================\n");
5620 }
5621 else
5622 {
5623 //fprintf(stderr, "begin: ways\n");
5624 }
5625 }
5626
5627
5628 // only save this way info to way table (used for index search) if its not a nonsearch way (like river, or some district outline)
5629
5630 if (!item_not_for_search_index(types[i]))
5631 {
5632
5633 sql_counter++;
5634
5635 // save ORIG name to DB -------------------------------------
5636 sqlite3_bind_int64(stmt_way, 1, osmid_attr_value);
5637 sqlite3_bind_text(stmt_way, 2, attr_strings[attr_string_label], -1, SQLITE_STATIC);
5638
5639 // streetname folded for later sort/search
5640 char *sub = NULL;
5641 name_folded = linguistics_casefold(attr_strings[attr_string_label]);
5642 if (name_folded)
5643 {
5644 name_folded2 = linguistics_remove_all_specials(name_folded);
5645 if (name_folded2)
5646 {
5647 g_free(name_folded);
5648 name_folded = name_folded2;
5649 }
5650
5651 name_folded2 = linguistics_expand_special(name_folded, 1);
5652 if (name_folded2)
5653 {
5654 sqlite3_bind_text(stmt_way, 6, name_folded2, -1, SQLITE_STATIC);
5655 sub = get_index_for_string(name_folded2);
5656 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5657 }
5658 else
5659 {
5660 sqlite3_bind_text(stmt_way, 6, name_folded, -1, SQLITE_STATIC);
5661 sub = get_index_for_string(name_folded);
5662 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5663 }
5664 }
5665 else
5666 {
5667 // use original string
5668 sqlite3_bind_text(stmt_way, 6, attr_strings[attr_string_label], -1, SQLITE_STATIC);
5669 sub = get_index_for_string(attr_strings[attr_string_label]);
5670 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5671 }
5672
5673 // town id
5674 sqlite3_bind_int(stmt_way, 3, -1);
5675
5676 if (node_coords.valid == 1)
5677 {
5678 // lat
5679 sqlite3_bind_double(stmt_way, 4, node_coords.lat);
5680 // lon
5681 sqlite3_bind_double(stmt_way, 5, node_coords.lon);
5682 }
5683
5684 // waytype
5685 if (types[i] != type_street_unkn)
5686 {
5687 sqlite3_bind_int(stmt_way, 8, 1);
5688 }
5689 else
5690 {
5691 // type -> POI
5692 sqlite3_bind_int(stmt_way, 8, 40);
5693 }
5694
5695 // retval =
5696 sqlite3_step(stmt_way);
5697 //if ((retval > 0) && (retval < 100))
5698 //{
5699 // fprintf(stderr, "ways step: code:%d wid:%lld\n", retval, osmid_attr_value);
5700 //}
5701 sqlite3_reset(stmt_way);
5702
5703 if (name_folded)
5704 {
5705 g_free(name_folded);
5706 }
5707
5708 if (name_folded2)
5709 {
5710 g_free(name_folded2);
5711 }
5712
5713 if (sub)
5714 {
5715 g_free(sub);
5716 }
5717 // save ORIG name to DB -------------------------------------
5718
5719 // save alternate name to DB aswell -------------------------------------
5720 if (attr_strings[attr_string_label_real_alt])
5721 {
5722 sqlite3_bind_int64(stmt_way, 1, osmid_attr_value);
5723 sqlite3_bind_text(stmt_way, 2, attr_strings[attr_string_label_real_alt], -1, SQLITE_STATIC);
5724
5725
5726 // streetname folded for later sort/search
5727 char *sub = NULL;
5728 name_folded = linguistics_casefold(attr_strings[attr_string_label_real_alt]);
5729 if (name_folded)
5730 {
5731 name_folded2 = linguistics_remove_all_specials(name_folded);
5732 if (name_folded2)
5733 {
5734 g_free(name_folded);
5735 name_folded = name_folded2;
5736 }
5737
5738 name_folded2 = linguistics_expand_special(name_folded, 1);
5739 if (name_folded2)
5740 {
5741 sqlite3_bind_text(stmt_way, 6, name_folded2, -1, SQLITE_STATIC);
5742 sub = get_index_for_string(name_folded2);
5743 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5744 }
5745 else
5746 {
5747 sqlite3_bind_text(stmt_way, 6, name_folded, -1, SQLITE_STATIC);
5748 sub = get_index_for_string(name_folded);
5749 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5750 }
5751 }
5752 else
5753 {
5754 // use original string
5755 sqlite3_bind_text(stmt_way, 6, attr_strings[attr_string_label_real_alt], -1, SQLITE_STATIC);
5756 sub = get_index_for_string(attr_strings[attr_string_label_real_alt]);
5757 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5758 }
5759
5760 // town id
5761 sqlite3_bind_int(stmt_way, 3, -1);
5762
5763 if (node_coords.valid == 1)
5764 {
5765 // lat
5766 sqlite3_bind_double(stmt_way, 4, node_coords.lat);
5767 // lon
5768 sqlite3_bind_double(stmt_way, 5, node_coords.lon);
5769 }
5770
5771 // waytype
5772 if (types[i] != type_street_unkn)
5773 {
5774 sqlite3_bind_int(stmt_way, 8, 3);
5775 }
5776 else
5777 {
5778 // type -> POI
5779 sqlite3_bind_int(stmt_way, 8, 40);
5780 }
5781
5782 // retval =
5783 sqlite3_step(stmt_way);
5784 //if ((retval > 0) && (retval < 100))
5785 //{
5786 // fprintf(stderr, "ways step: code:%d wid:%lld\n", retval, osmid_attr_value);
5787 //}
5788 sqlite3_reset(stmt_way);
5789
5790 if (name_folded)
5791 {
5792 g_free(name_folded);
5793 }
5794
5795 if (name_folded2)
5796 {
5797 g_free(name_folded2);
5798 }
5799
5800 if (sub)
5801 {
5802 g_free(sub);
5803 }
5804
5805 }
5806 // save alternate name to DB aswell -------------------------------------
5807
5808
5809 // save english name to DB aswell -------------------------------------
5810 if (attr_strings[attr_string_label_alt])
5811 {
5812 sqlite3_bind_int64(stmt_way, 1, osmid_attr_value);
5813 sqlite3_bind_text(stmt_way, 2, attr_strings[attr_string_label_alt], -1, SQLITE_STATIC);
5814
5815
5816 // streetname folded for later sort/search
5817 char *sub = NULL;
5818 name_folded = linguistics_casefold(attr_strings[attr_string_label_alt]);
5819 if (name_folded)
5820 {
5821 name_folded2 = linguistics_remove_all_specials(name_folded);
5822 if (name_folded2)
5823 {
5824 g_free(name_folded);
5825 name_folded = name_folded2;
5826 }
5827
5828 name_folded2 = linguistics_expand_special(name_folded, 1);
5829 if (name_folded2)
5830 {
5831 sqlite3_bind_text(stmt_way, 6, name_folded2, -1, SQLITE_STATIC);
5832 sub = get_index_for_string(name_folded2);
5833 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5834 }
5835 else
5836 {
5837 sqlite3_bind_text(stmt_way, 6, name_folded, -1, SQLITE_STATIC);
5838 sub = get_index_for_string(name_folded);
5839 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5840 }
5841 }
5842 else
5843 {
5844 // use original string
5845 sqlite3_bind_text(stmt_way, 6, attr_strings[attr_string_label_alt], -1, SQLITE_STATIC);
5846 sub = get_index_for_string(attr_strings[attr_string_label_alt]);
5847 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
5848 }
5849
5850 // town id
5851 sqlite3_bind_int(stmt_way, 3, -1);
5852
5853 if (node_coords.valid == 1)
5854 {
5855 // lat
5856 sqlite3_bind_double(stmt_way, 4, node_coords.lat);
5857 // lon
5858 sqlite3_bind_double(stmt_way, 5, node_coords.lon);
5859 }
5860
5861 // waytype
5862 if (types[i] != type_street_unkn)
5863 {
5864 sqlite3_bind_int(stmt_way, 8, 2);
5865 }
5866 else
5867 {
5868 // type -> POI
5869 sqlite3_bind_int(stmt_way, 8, 40);
5870 }
5871
5872 // retval =
5873 sqlite3_step(stmt_way);
5874 //if ((retval > 0) && (retval < 100))
5875 //{
5876 // fprintf(stderr, "ways step: code:%d wid:%lld\n", retval, osmid_attr_value);
5877 //}
5878 sqlite3_reset(stmt_way);
5879
5880 if (name_folded)
5881 {
5882 g_free(name_folded);
5883 }
5884
5885 if (name_folded2)
5886 {
5887 g_free(name_folded2);
5888 }
5889
5890 if (sub)
5891 {
5892 g_free(sub);
5893 }
5894
5895 }
5896 // save english name to DB aswell -------------------------------------
5897
5898 }
5899
5900
5901 if (sql_counter > MAX_ROWS_WO_COMMIT_3)
5902 {
5903 sql_counter = 0;
5904 retval = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5905 if ((retval > 0) && (retval < 100))
5906 {
5907 fprintf_(stderr, "ways: %lld code:%d\n", ways_processed_count, retval);
5908 }
5909 else
5910 {
5911 //fprintf(stderr, "ways: %lld\n", ways_processed_count);
5912 }
5913 }
5914 #endif
5915 // set ref file to start of waynodes of current way
5916 // fprintf(stderr, "w seek1 pos=%lld\n", seekpos1);
5917 seek_posx = ftello(ways_ref_file);
5918 if ((off_t)seek_posx != (off_t)seekpos1)
5919 {
5920 fseeko(ways_ref_file, (off_t)seekpos1, SEEK_SET);
5921 }
5922 }
5923 // save all the way nodes for streets
5924 int fret = (int)fread(&w_node, sizeof(osmid), 1, ways_ref_file);
5925 if (fret == 0)
5926 {
5927 fprintf(stderr, "**ERROR** at fread 001a: wayid:%lld count=%d\n", osmid_attr_value, (int)i788);
5928 }
5929 else if ((osmid)w_node > 4994968164L)
5930 {
5931 fprintf(stderr, "**ERROR** at fread 001b: wayid:%lld count=%d w_node=%lld\n", osmid_attr_value, (int)i788, (osmid)w_node);
5932 }
5933 // fprintf(stderr, "at fread: w_node:%lld\n", (osmid)w_node);
5934 // save first node of WAYS with name/label into db
5935 add_waynode_to_db((osmid)w_node, (int)i788);
5936 }
5937 else // rest of ways
5938 {
5939 // save all the waynodes to db
5940 if (i788 == 0)
5941 {
5942 // set ref file to start of waynodes of current way
5943 // fprintf(stderr, "w seek1.1 pos=%lld\n", seekpos1);
5944 seek_posx = ftello(ways_ref_file);
5945 if ((off_t)seek_posx != (off_t)seekpos1)
5946 {
5947 fseeko(ways_ref_file, (off_t)seekpos1, SEEK_SET);
5948 }
5949 }
5950
5951 int fret = (int)fread(&w_node, sizeof(osmid), 1, ways_ref_file);
5952 if (fret == 0)
5953 {
5954 fprintf(stderr, "**ERROR** at fread 001.1a: wayid:%lld count=%d\n", osmid_attr_value, (int)i788);
5955 }
5956 else if ((osmid)w_node > 4994968164L)
5957 {
5958 fprintf(stderr, "**ERROR** at fread 001.1b: wayid:%lld count=%d w_node=%lld\n", osmid_attr_value, (int)i788, (osmid)w_node);
5959 }
5960 // save first node of ALL WAYS into db
5961 add_waynode_to_db((osmid)w_node, (int)i788);
5962 }
5963 }
5964
5965 if (dont_save_to_db == 0)
5966 {
5967 // seek to end of file (this should not be needed, because we read values until EOF anyway)
5968 // fprintf(stderr, "w seek2\n");
5969 fseeko(ways_ref_file, 0, SEEK_END);
5970 }
5971 }
5972 // save nodes of way -----------------
5973 // save nodes of way -----------------
5974
5975
5976 if (def_flags)
5977 {
5978 flags_attr_value = (*def_flags | flags[0] | flags[1]) & ~flags[2];
5979 // only add flags if they differ from the default flags!!
5980 if (flags_attr_value != *def_flags)
5981 {
5982 add_flags = 1;
5983 }
5984 }
5985 else // maybe should explicitly specify which "types" to consider? now its all "types"
5986 {
5987 def_flags = 0;
5988 flags_attr_value = (flags[0] | flags[1]) & ~flags[2];
5989
5990 // only add flags if we really have a value
5991 if (flags_attr_value != 0)
5992 {
5993 add_flags = 1;
5994 }
5995 }
5996 item_bin_add_attr_string(item_bin, def_flags ? attr_street_name : attr_label, attr_strings[attr_string_label]);
5997
5998 // housenumber from buildings --------------------
5999 if (types[i] == type_poly_building)
6000 {
6001 // addr:street
6002 item_bin_add_attr_string(item_bin, attr_street_name_dummy, attr_strings[attr_string_street_name]);
6003 // fprintf(stderr, "addr:street :%s\n", attr_strings[attr_string_street_name]);
6004 // addr:housenumber
6005 item_bin_add_attr_string(item_bin, attr_house_number_dummy, attr_strings[attr_string_house_number]);
6006 // fprintf(stderr, "addr:housenumber:%s\n", attr_strings[attr_string_house_number]);
6007 }
6008 // housenumber from buildings --------------------
6009
6010
6011 if (attr_strings[attr_string_label_alt])
6012 {
6013 item_bin_add_attr_string(item_bin, attr_street_name_match, attr_strings[attr_string_label_alt]);
6014 if (debug_itembin(ib))
6015 {
6016 fprintf(stderr, "street name : %s\n", attr_strings[attr_string_label]);
6017 fprintf(stderr, "street name sys: %s\n", attr_strings[attr_string_street_name_systematic]);
6018 fprintf(stderr, "street name alt: %s\n", attr_strings[attr_string_label_alt]);
6019 }
6020 }
6021 item_bin_add_attr_string(item_bin, attr_street_name_systematic, attr_strings[attr_string_street_name_systematic]);
6022
6023 // -- NEW 001 --
6024 item_bin_add_attr_string(item_bin, attr_street_name_systematic_nat, attr_strings[attr_string_street_name_systematic_nat]);
6025 item_bin_add_attr_string(item_bin, attr_street_lanes, attr_strings[attr_string_street_lanes]);
6026 item_bin_add_attr_string(item_bin, attr_street_lanes_forward, attr_strings[attr_string_street_lanes_forward]);
6027 item_bin_add_attr_string(item_bin, attr_street_turn_lanes, attr_strings[attr_string_street_turn_lanes]);
6028 item_bin_add_attr_string(item_bin, attr_street_destination, attr_strings[attr_string_street_destination]);
6029 item_bin_add_attr_string(item_bin, attr_street_destination_lanes, attr_strings[attr_string_street_destination_lanes]);
6030 // -- NEW 001 --
6031
6032 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value);
6033
6034 if (debug_attr_buffer[0])
6035 {
6036 item_bin_add_attr_string(item_bin, attr_debug, debug_attr_buffer);
6037 }
6038
6039 if (add_flags)
6040 {
6041 item_bin_add_attr_int(item_bin, attr_flags, flags_attr_value);
6042 }
6043
6044 if (maxspeed_attr_value)
6045 {
6046 item_bin_add_attr_int(item_bin, attr_maxspeed, maxspeed_attr_value);
6047 }
6048
6049 // custom color attribute
6050 if (attr_strings[attr_string_colour_])
6051 {
6052 unsigned int cc = color_int_value_from_string(attr_strings[attr_string_colour_]);
6053 //fprintf(stderr, "col:ret=%d\n", cc);
6054 if (cc != 0)
6055 {
6056 cc = (cc >> 8);
6057 //fprintf(stderr, "col:in map=%d\n", (int)cc);
6058 item_bin_add_attr_int(item_bin, attr_colour2, (int)cc);
6059 }
6060 }
6061
6062 // if we duplicated this way here (because of multiple attributes), then set "duplicate_way" attr
6063 if (i > 0)
6064 {
6065 item_bin_add_attr_int(item_bin, attr_duplicate_way, 1);
6066 //fprintf(stderr, "attr_duplicate_way:1: dup=true wayid=%lld\n", item_bin_get_wayid(item_bin));
6067 }
6068
6069 if (write_this_item_to_binfile == 1)
6070 {
6071 item_bin_write(item_bin, osm->ways);
6072 }
6073
6074 if (border_only_map_as_xml == 1)
6075 {
6076 item_bin_write_xml(item_bin, "borders.xml");
6077 }
6078 }
6079
6080 /*
6081 if(osm->line2poi) {
6082 count=attr_longest_match(attr_mapping_way2poi, attr_mapping_way2poi_count, types, sizeof(types)/sizeof(enum item_type));
6083 dbg_assert(count < 10);
6084 for (i = 0 ; i < count ; i++) {
6085 if (types[i] == type_none || types[i] == type_point_unkn)
6086 continue;
6087 item_bin=init_item(types[i], 0);
6088 item_bin_add_coord(item_bin, coord_buffer, coord_count);
6089 item_bin_add_attr_string(item_bin, attr_label, attr_strings[attr_string_label]);
6090 item_bin_add_attr_string(item_bin, attr_house_number, attr_strings[attr_string_house_number]);
6091 item_bin_add_attr_string(item_bin, attr_street_name, attr_strings[attr_string_street_name]);
6092 item_bin_add_attr_string(item_bin, attr_phone, attr_strings[attr_string_phone]);
6093 item_bin_add_attr_string(item_bin, attr_fax, attr_strings[attr_string_fax]);
6094 item_bin_add_attr_string(item_bin, attr_email, attr_strings[attr_string_email]);
6095 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
6096 item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]);
6097 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value);
6098 item_bin_write(item_bin, count_areas<count_lines?osm->line2poi:osm->poly2poi);
6099 }
6100 }
6101 */
6102
6103 attr_longest_match_clear();
6104 }
6105
6106 static struct css_color css_cols[]= {
6107 {"aliceblue",0xf0f8ff},
6108 {"antiquewhite",0xfaebd7},
6109 {"aqua",0x00ffff},
6110 {"aquamarine",0x7fffd4},
6111 {"azure",0xf0ffff},
6112 {"beige",0xf5f5dc},
6113 {"bisque",0xffe4c4},
6114 {"black",0x000000},
6115 {"blanchedalmond",0xffebcd},
6116 {"blue",0x0000ff},
6117 {"blueviolet",0x8a2be2},
6118 {"brown",0xa52a2a},
6119 {"burlywood",0xdeb887},
6120 {"cadetblue",0x5f9ea0},
6121 {"chartreuse",0x7fff00},
6122 {"chocolate",0xd2691e},
6123 {"coral",0xff7f50},
6124 {"cornflowerblue",0x6495ed},
6125 {"cornsilk",0xfff8dc},
6126 {"crimson",0xdc143c},
6127 {"cyan",0x00ffff},
6128 {"darkblue",0x00008b},
6129 {"darkcyan",0x008b8b},
6130 {"darkgoldenrod",0xb8860b},
6131 {"darkgray",0xa9a9a9},
6132 {"darkgreen",0x006400},
6133 {"darkkhaki",0xbdb76b},
6134 {"darkmagenta",0x8b008b},
6135 {"darkolivegreen",0x556b2f},
6136 {"darkorange",0xff8c00},
6137 {"darkorchid",0x9932cc},
6138 {"darkred",0x8b0000},
6139 {"darksalmon",0xe9967a},
6140 {"darkseagreen",0x8fbc8f},
6141 {"darkslateblue",0x483d8b},
6142 {"darkslategray",0x2f4f4f},
6143 {"darkturquoise",0x00ced1},
6144 {"darkviolet",0x9400d3},
6145 {"deeppink",0xff1493},
6146 {"deepskyblue",0x00bfff},
6147 {"dimgray",0x696969},
6148 {"dodgerblue",0x1e90ff},
6149 {"firebrick",0xb22222},
6150 {"floralwhite",0xfffaf0},
6151 {"forestgreen",0x228b22},
6152 {"fuchsia",0xff00ff},
6153 {"gainsboro",0xdcdcdc},
6154 {"ghostwhite",0xf8f8ff},
6155 {"gold",0xffd700},
6156 {"goldenrod",0xdaa520},
6157 {"gray",0x808080},
6158 {"green",0x008000},
6159 {"greenyellow",0xadff2f},
6160 {"honeydew",0xf0fff0},
6161 {"hotpink",0xff69b4},
6162 {"indianred",0xcd5c5c},
6163 {"indigo",0x4b0082},
6164 {"ivory",0xfffff0},
6165 {"khaki",0xf0e68c},
6166 {"lavender",0xe6e6fa},
6167 {"lavenderblush",0xfff0f5},
6168 {"lawngreen",0x7cfc00},
6169 {"lemonchiffon",0xfffacd},
6170 {"lightblue",0xadd8e6},
6171 {"lightcoral",0xf08080},
6172 {"lightcyan",0xe0ffff},
6173 {"lightgoldenrodyellow",0xfafad2},
6174 {"lightgrey",0xd3d3d3},
6175 {"lightgreen",0x90ee90},
6176 {"lightpink",0xffb6c1},
6177 {"lightsalmon",0xffa07a},
6178 {"lightseagreen",0x20b2aa},
6179 {"lightskyblue",0x87cefa},
6180 {"lightslategray",0x778899},
6181 {"lightsteelblue",0xb0c4de},
6182 {"lightyellow",0xffffe0},
6183 {"lime",0x00ff00},
6184 {"limegreen",0x32cd32},
6185 {"linen",0xfaf0e6},
6186 {"magenta",0xff00ff},
6187 {"maroon",0x800000},
6188 {"mediumaquamarine",0x66cdaa},
6189 {"mediumblue",0x0000cd},
6190 {"mediumorchid",0xba55d3},
6191 {"mediumpurple",0x9370d8},
6192 {"mediumseagreen",0x3cb371},
6193 {"mediumslateblue",0x7b68ee},
6194 {"mediumspringgreen",0x00fa9a},
6195 {"mediumturquoise",0x48d1cc},
6196 {"mediumvioletred",0xc71585},
6197 {"midnightblue",0x191970},
6198 {"mintcream",0xf5fffa},
6199 {"mistyrose",0xffe4e1},
6200 {"moccasin",0xffe4b5},
6201 {"navajowhite",0xffdead},
6202 {"navy",0x000080},
6203 {"oldlace",0xfdf5e6},
6204 {"olive",0x808000},
6205 {"olivedrab",0x6b8e23},
6206 {"orange",0xffa500},
6207 {"orangered",0xff4500},
6208 {"orchid",0xda70d6},
6209 {"palegoldenrod",0xeee8aa},
6210 {"palegreen",0x98fb98},
6211 {"paleturquoise",0xafeeee},
6212 {"palevioletred",0xd87093},
6213 {"papayawhip",0xffefd5},
6214 {"peachpuff",0xffdab9},
6215 {"peru",0xcd853f},
6216 {"pink",0xffc0cb},
6217 {"plum",0xdda0dd},
6218 {"powderblue",0xb0e0e6},
6219 {"purple",0x800080},
6220 {"red",0xff0000},
6221 {"rosybrown",0xbc8f8f},
6222 {"royalblue",0x4169e1},
6223 {"saddlebrown",0x8b4513},
6224 {"salmon",0xfa8072},
6225 {"sandybrown",0xf4a460},
6226 {"seagreen",0x2e8b57},
6227 {"seashell",0xfff5ee},
6228 {"sienna",0xa0522d},
6229 {"silver",0xc0c0c0},
6230 {"skyblue",0x87ceeb},
6231 {"slateblue",0x6a5acd},
6232 {"slategray",0x708090},
6233 {"snow",0xfffafa},
6234 {"springgreen",0x00ff7f},
6235 {"steelblue",0x4682b4},
6236 {"tan",0xd2b48c},
6237 {"teal",0x008080},
6238 {"thistle",0xd8bfd8},
6239 {"tomato",0xff6347},
6240 {"turquoise",0x40e0d0},
6241 {"violet",0xee82ee},
6242 {"wheat",0xf5deb3},
6243 {"white",0xffffff},
6244 {"whitesmoke",0xf5f5f5},
6245 {"yellow",0xffff00},
6246 {"yellowgreen",0x9acd32},
6247 };
6248
6249
6250 unsigned int color_int_value_from_string(char* color_str)
6251 {
6252 int count = 0;
6253 unsigned int ret = 0; // 0 -> invalid color!
6254 struct css_color* c;
6255
6256 for (count = 0;count < (sizeof(css_cols)/sizeof(struct css_color));count++)
6257 {
6258 c = &css_cols[count];
6259 if (!strcmp(c->col_name, color_str))
6260 {
6261 //fprintf(stderr, "col=%d\n", c->col_value);
6262 ret = (c->col_value << 8) | 0x1; // set alpha to 0x1
6263 //fprintf(stderr, "col ret1=%d\n", ret);
6264 return ret;
6265 }
6266 }
6267
6268 return ret;
6269 }
6270
6271
6272 void osm_append_housenumber_node(FILE *out, struct coord *c, char *house_number, char *street_name)
6273 {
6274
6275 // c is inside the same buffer as the new item will be. so make a copy of c now!
6276 struct coord *c_cpy = coord_new(c->x, c->y);
6277
6278 struct item_bin *item_bin;
6279 item_bin = init_item(type_house_number, 0);
6280 // item_bin->type = type_house_number;
6281
6282 item_bin_add_coord(item_bin, c_cpy, 1);
6283 item_bin_add_attr_string(item_bin, attr_house_number, house_number);
6284 item_bin_add_attr_string(item_bin, attr_street_name, street_name);
6285
6286 // DEBUG -- DEBUG -- DEBUG --
6287 // DEBUG -- DEBUG -- DEBUG --
6288 // dump_itembin(item_bin);
6289 // DEBUG -- DEBUG -- DEBUG --
6290 // DEBUG -- DEBUG -- DEBUG --
6291
6292 item_bin_write(item_bin, out);
6293
6294 g_free(c_cpy);
6295 }
6296
6297
6298 void add_point_as_way_to_db(char *label, osmid id, int waytype, double lat, double lon)
6299 {
6300 char *sub = NULL;
6301 char *name_folded2 = NULL;
6302 char *name_folded = NULL;
6303 int retval;
6304
6305 if (label)
6306 {
6307 if (sql_counter == 0)
6308 {
6309 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
6310 }
6311
6312
6313 sqlite3_bind_int64(stmt_way, 1, id);
6314 sqlite3_bind_text(stmt_way, 2, label, -1, SQLITE_STATIC);
6315
6316
6317 // streetname folded for later sort/search
6318 name_folded = linguistics_casefold(label);
6319 if (name_folded)
6320 {
6321 name_folded2 = linguistics_remove_all_specials(name_folded);
6322 if (name_folded2)
6323 {
6324 g_free(name_folded);
6325 name_folded = name_folded2;
6326 }
6327
6328 name_folded2 = linguistics_expand_special(name_folded, 1);
6329 if (name_folded2)
6330 {
6331 sqlite3_bind_text(stmt_way, 6, name_folded2, -1, SQLITE_STATIC);
6332 sub = get_index_for_string(name_folded2);
6333 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
6334 }
6335 else
6336 {
6337 sqlite3_bind_text(stmt_way, 6, name_folded, -1, SQLITE_STATIC);
6338 sub = get_index_for_string(name_folded);
6339 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
6340 }
6341 }
6342 else
6343 {
6344 // use original string
6345 sqlite3_bind_text(stmt_way, 6, label, -1, SQLITE_STATIC);
6346 sub = get_index_for_string(label);
6347 sqlite3_bind_text(stmt_way, 7, sub, -1, SQLITE_STATIC);
6348 }
6349
6350 // town id
6351 sqlite3_bind_int(stmt_way, 3, -1);
6352
6353 // lat
6354 sqlite3_bind_double(stmt_way, 4, lat);
6355 // lon
6356 sqlite3_bind_double(stmt_way, 5, lon);
6357
6358 // waytype
6359 sqlite3_bind_int(stmt_way, 8, waytype);
6360
6361 // retval =
6362 sqlite3_step(stmt_way);
6363 //if ((retval > 0) && (retval < 100))
6364 //{
6365 // fprintf(stderr, "ways step: code:%d wid:%lld\n", retval, osmid_attr_value);
6366 //}
6367 sqlite3_reset(stmt_way);
6368
6369 if (name_folded)
6370 {
6371 g_free(name_folded);
6372 }
6373
6374 if (name_folded2)
6375 {
6376 g_free(name_folded2);
6377 }
6378
6379 if (sub)
6380 {
6381 g_free(sub);
6382 }
6383
6384 sql_counter++;
6385 if (sql_counter > MAX_ROWS_WO_COMMIT_3)
6386 {
6387 sql_counter = 0;
6388 retval = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
6389 if ((retval > 0) && (retval < 100))
6390 {
6391 fprintf_(stderr, "add_point_as_way_to_db:code:%d\n", retval);
6392 }
6393 }
6394
6395 }
6396
6397 }
6398
6399
6400 void osm_end_node(struct maptool_osm *osm)
6401 {
6402 int count, i;
6403 char *postal;
6404 enum item_type types[10];
6405 struct item_bin *item_bin;
6406 in_node = 0;
6407
6408 if (!osm->nodes || !node_is_tagged || !nodeid)
6409 {
6410 return;
6411 }
6412
6413 count = attr_longest_match(attr_mapping_node, attr_mapping_node_count, types, sizeof(types) / sizeof(enum item_type));
6414
6415 if (!count)
6416 {
6417 types[0] = type_point_unkn;
6418 count = 1;
6419 }
6420
6421 dbg_assert(count < 10);
6422
6423
6424 if (attr_strings[attr_string_label] != NULL)
6425 {
6426 // check if "alt name" is same as normal name
6427 if (attr_strings[attr_string_label_alt] != NULL)
6428 {
6429 if (!strcmp(attr_strings[attr_string_label], attr_strings[attr_string_label_alt]))
6430 {
6431 // strings are the same, clear "alt name"
6432 attr_strings[attr_string_label_alt] = NULL;
6433 }
6434 }
6435 }
6436
6437
6438 for (i = 0; i < count; i++)
6439 {
6440 if (types[i] == type_none)
6441 {
6442 continue;
6443 }
6444
6445 //if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn))
6446 // continue;
6447
6448 if (ignore_unkown && (types[i] == type_point_unkn))
6449 {
6450 continue;
6451 }
6452
6453 item_bin = init_item(types[i], 0);
6454
6455 if (item_is_town(*item_bin) && attr_strings[attr_string_label])
6456 {
6457 if (debug_itembin(ib))
6458 {
6459 fprintf(stderr, "osm_end_node: have town: %s\n", attr_strings[attr_string_label]);
6460 }
6461 }
6462
6463 if (item_is_district(*item_bin) && attr_strings[attr_string_label])
6464 {
6465 if (debug_itembin(ib))
6466 {
6467 fprintf(stderr, "osm_end_node: have district: %s\n", attr_strings[attr_string_label]);
6468 }
6469 }
6470
6471 if (item_is_town(*item_bin) && attr_strings[attr_string_population])
6472 {
6473 item_bin_set_type_by_population(item_bin, atoi(attr_strings[attr_string_population]));
6474 }
6475
6476 // dont forget to qualify districts
6477 if (item_is_district(*item_bin) && attr_strings[attr_string_population])
6478 {
6479 item_bin_set_type_by_population(item_bin, atoi(attr_strings[attr_string_population]));
6480 }
6481
6482 item_bin_add_coord(item_bin, &ni->c, 1);
6483 if (((types[i] == type_poi_bicycle_parking) || (types[i] == type_poi_bicycle_rental)) && (attr_strings[attr_string_capacity] != NULL) && (strlen(attr_strings[attr_string_capacity]) > 0))
6484 {
6485 char *label_new;
6486 if ((attr_strings[attr_string_label] != NULL) && (strlen(attr_strings[attr_string_label]) > 1))
6487 {
6488 // fprintf(stderr, "XXX1:%s:%s\n", attr_strings[attr_string_label], attr_strings[attr_string_capacity]);
6489 label_new = g_strdup_printf("%s:%s", attr_strings[attr_string_label], attr_strings[attr_string_capacity]);
6490 }
6491 else
6492 {
6493 label_new = g_strdup_printf("%s", attr_strings[attr_string_capacity]);
6494 }
6495 item_bin_add_attr_string(item_bin, attr_label, label_new);
6496 g_free(label_new);
6497 }
6498 else
6499 {
6500 item_bin_add_attr_string(item_bin, item_is_town(*item_bin) ? attr_town_name : attr_label, attr_strings[attr_string_label]);
6501 }
6502 item_bin_add_attr_string(item_bin, attr_house_number, attr_strings[attr_string_house_number]);
6503 item_bin_add_attr_string(item_bin, attr_street_name, attr_strings[attr_string_street_name]);
6504 item_bin_add_attr_string(item_bin, attr_phone, attr_strings[attr_string_phone]);
6505 item_bin_add_attr_string(item_bin, attr_fax, attr_strings[attr_string_fax]);
6506 item_bin_add_attr_string(item_bin, attr_email, attr_strings[attr_string_email]);
6507 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
6508 item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]);
6509
6510 // -- NEW 001 --
6511 item_bin_add_attr_string(item_bin, attr_ref, attr_strings[attr_string_ref]);
6512 item_bin_add_attr_string(item_bin, attr_exit_to, attr_strings[attr_string_exit_to]);
6513 // -- NEW 001 --
6514
6515 if ((types[i] == type_poi_bicycle_parking) || (types[i] == type_poi_bicycle_rental))
6516 {
6517 int capacity = 0;
6518 if (attr_strings[attr_string_capacity] != NULL)
6519 {
6520 if (strlen(attr_strings[attr_string_capacity]) > 0)
6521 {
6522 capacity = atoi(attr_strings[attr_string_capacity]);
6523 item_bin_add_attr_int(item_bin, attr_capacity, attr_strings[attr_string_capacity]);
6524 }
6525 }
6526 }
6527
6528 item_bin_add_attr_longlong(item_bin, attr_osm_nodeid, osmid_attr_value);
6529 item_bin_add_attr_string(item_bin, attr_debug, debug_attr_buffer);
6530
6531 if ((item_is_town(*item_bin)) || (item_is_district(*item_bin)))
6532 {
6533 if (attr_strings[attr_string_label_alt])
6534 {
6535 item_bin_add_attr_string(item_bin, attr_town_name_match, attr_strings[attr_string_label_alt]);
6536 // if (debug_itembin(item_bin))
6537 // {
6538 // fprintf(stderr, "town name : %s\n", attr_strings[attr_string_label]);
6539 // fprintf(stderr, "town name alt: %s\n", attr_strings[attr_string_label_alt]);
6540 // }
6541 }
6542 }
6543
6544 postal = attr_strings[attr_string_postal];
6545 if (postal)
6546 {
6547 char *sep = strchr(postal, ',');
6548 if (sep)
6549 {
6550 *sep = '\0';
6551 }
6552 item_bin_add_attr_string(item_bin, item_is_town(*item_bin) ? attr_town_postal : attr_postal, postal);
6553 }
6554
6555 item_bin_write(item_bin, osm->nodes);
6556
6557
6558
6559
6560 // town waytype = 4
6561 if ((item_is_town(*item_bin)) || (item_is_district(*item_bin)))
6562 {
6563 if (attr_strings[attr_string_label])
6564 {
6565 add_point_as_way_to_db(attr_strings[attr_string_label], osmid_attr_value, 4, save_node_lat, save_node_lon);
6566 }
6567
6568 if (attr_strings[attr_string_label_alt])
6569 {
6570 add_point_as_way_to_db(attr_strings[attr_string_label_alt], osmid_attr_value, 4, save_node_lat, save_node_lon);
6571 }
6572 }
6573
6574 // POI waytype = 40
6575 if (item_is_poi(types[i]))
6576 {
6577
6578 if (!item_not_for_search_index(types[i]))
6579 {
6580 if (attr_strings[attr_string_label])
6581 {
6582 add_point_as_way_to_db(attr_strings[attr_string_label], osmid_attr_value, 40, save_node_lat, save_node_lon);
6583 // fprintf(stderr, "POI name : %s id=%lld\n", attr_strings[attr_string_label], osmid_attr_value);
6584 }
6585
6586 if (attr_strings[attr_string_label_alt])
6587 {
6588 add_point_as_way_to_db(attr_strings[attr_string_label_alt], osmid_attr_value, 40, save_node_lat, save_node_lon);
6589 }
6590 }
6591 }
6592
6593
6594
6595 if (item_is_town(*item_bin) && attr_strings[attr_string_label] && osm->towns)
6596 {
6597 item_bin = init_item(item_bin->type, 0);
6598 item_bin_add_coord(item_bin, &ni->c, 1);
6599 item_bin_add_attr_string(item_bin, attr_osm_is_in, is_in_buffer);
6600 item_bin_add_attr_longlong(item_bin, attr_osm_nodeid, osmid_attr_value);
6601 item_bin_add_attr_string(item_bin, attr_town_postal, postal);
6602 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
6603 item_bin_add_attr_string(item_bin, attr_town_name, attr_strings[attr_string_label]);
6604
6605 //if (attr_strings[attr_string_label_alt])
6606 //{
6607 // item_bin_add_attr_string(item_bin, attr_town_name_match, attr_strings[attr_string_label_alt]);
6608 // if (debug_itembin(item_bin))
6609 // {
6610 // fprintf(stderr, "town name : %s\n", attr_strings[attr_string_label]);
6611 // fprintf(stderr, "town name alt: %s\n", attr_strings[attr_string_label_alt]);
6612 // }
6613 //}
6614
6615 item_bin_write(item_bin, osm->towns);
6616 }
6617
6618 // put POI into DB for index search
6619 // put town into DB for index search
6620 // put district into DB for index search
6621 // ****** retval = sqlite3_prepare_v2(sql_handle, "INSERT INTO way (id, name, town_id, lat, lon, name_fold, ind, name_fold_idx, waytype) VALUES (?,?,?,?,?,?,0,?,?);", -1, &stmt_way, NULL);
6622
6623
6624 //fprintf(stderr,"********DUMP nd1***********\n");
6625 //if (types[i] == type_house_number)
6626 //{
6627 // dump_itembin(item_bin);
6628 //}
6629 //fprintf(stderr,"********DUMP nd1***********\n");
6630
6631 }
6632 processed_nodes_out++;
6633 attr_longest_match_clear();
6634 }
6635
6636 static struct country_table *
6637 osm_process_town_unknown_country(void)
6638 {
6639 static struct country_table *unknown;
6640 unknown = country_from_countryid(999);
6641 return unknown;
6642 }
6643
6644 static struct country_table *
6645 osm_process_item_by_country_id(int id)
6646 {
6647 static struct country_table *ct;
6648 ct = country_from_countryid(id);
6649 return ct;
6650 }
6651
6652 static struct country_table *
6653 osm_process_item_fixed_country(void)
6654 {
6655 static struct country_table *fixed;
6656 fixed = country_from_countryid(global_fixed_country_id);
6657 return fixed;
6658 }
6659
6660 static struct country_table *
6661 osm_process_town_by_is_in(struct item_bin *ib, char *is_in)
6662 {
6663 struct country_table *result = NULL, *lookup;
6664 char *tok, *dup = g_strdup(is_in), *buf = dup;
6665 int conflict;
6666
6667 while ((tok = strtok(buf, ",;")))
6668 {
6669 while (*tok == ' ')
6670 {
6671 tok++;
6672 }
6673 lookup = g_hash_table_lookup(country_table_hash, tok);
6674 if (lookup)
6675 {
6676 if (result && result->countryid != lookup->countryid)
6677 {
6678 //char *label = item_bin_get_attr(ib, attr_town_name, NULL);
6679 //osm_warning("node", item_bin_get_nodeid(ib), 0, "conflict for %s is_in=%s country %d vs %d\n", label, is_in, lookup->countryid, result->countryid);
6680 conflict = 1;
6681 }
6682 result = lookup;
6683 }
6684 buf = NULL;
6685 }
6686 g_free(dup);
6687 return result;
6688 }
6689
6690 static int osm_process_street_by_boundary(GList *bl, long long town_osm_id, long long town_relation_id, struct coord *c)
6691 {
6692 GList *matches = NULL;
6693
6694 matches = boundary_find_matches_single(bl, c);
6695 if (g_list_length(matches) > 0)
6696 {
6697 return 1;
6698 }
6699
6700 return 0;
6701 }
6702
6703
6704 static struct country_table* osm_process_town_by_manual_country_borders(GList *bl_manual, struct coord *c)
6705 {
6706 GList *l = bl_manual;
6707 while (l)
6708 {
6709 if (l)
6710 {
6711 struct boundary_manual *b = l->data;
6712 if (b)
6713 {
6714 if (bbox_contains_coord(&b->r, c) == 1)
6715 {
6716 if (geom_poly_point_inside(b->c, b->coord_count, c) == 1)
6717 {
6718 // ok, now check all innery polys of this country id
6719 if (osm_check_all_inner_polys_of_country_id(b->countryid, c) == 1)
6720 {
6721 return b->country;
6722 }
6723 }
6724 }
6725 }
6726 l = g_list_next(l);
6727 }
6728 }
6729
6730 return NULL;
6731 }
6732
6733 // return: 1 -> coord is not in ANY inner of this country id
6734 // 0 -> coord is at least in 1 inner of this country id
6735 static int osm_check_all_inner_polys_of_country_id(int country_id, struct coord *c)
6736 {
6737 GList *l = boundary_list_inner_manual;
6738
6739 while (l)
6740 {
6741 if (l)
6742 {
6743 struct boundary_manual *b = l->data;
6744 if (b)
6745 {
6746 if (b->countryid == country_id)
6747 {
6748 if (bbox_contains_coord(&b->r, c) == 1)
6749 {
6750 if (geom_poly_point_inside(b->c, b->coord_count, c) == 1)
6751 {
6752 // c is inside 1 inner poly
6753 return 0;
6754 }
6755 }
6756 }
6757 }
6758 l = g_list_next(l);
6759 }
6760 }
6761
6762 return 1;
6763 }
6764
6765
6766 static long long osm_process_street_by_manual_country_borders(GList *bl_manual, struct coord *c)
6767 {
6768 GList *l = bl_manual;
6769
6770 while (l)
6771 {
6772 if (l)
6773 {
6774 struct boundary_manual *b = l->data;
6775 if (b)
6776 {
6777 if (bbox_contains_coord(&b->r, c) == 1)
6778 {
6779 if (geom_poly_point_inside(b->c, b->coord_count, c) == 1)
6780 {
6781 // ok, now check all innery polys of this country id
6782 if (osm_check_all_inner_polys_of_country_id(b->countryid, c) == 1)
6783 {
6784 return b->town_id;
6785 }
6786 }
6787 }
6788 }
6789 l = g_list_next(l);
6790 }
6791 }
6792
6793 return -1;
6794 }
6795
6796 static long long osm_process_town_by_manual_country_id(GList *bl_manual, int country_id)
6797 {
6798 struct country_table4
6799 {
6800 int countryid;
6801 char *names;
6802 char *admin_levels;
6803 FILE *file;
6804 int size;
6805 struct rect r;
6806 };
6807
6808 struct country_table4 *c4;
6809 struct boundary_manual *b;
6810
6811 GList *l = bl_manual;
6812 while (l)
6813 {
6814 if (l)
6815 {
6816 b = l->data;
6817 if (b)
6818 {
6819 c4 = b->country;
6820 if (c4)
6821 {
6822 if (c4->countryid == country_id)
6823 {
6824 return b->town_id;
6825 }
6826 }
6827 }
6828 l = g_list_next(l);
6829 }
6830 }
6831
6832 return -1;
6833 }
6834
6835 static struct country_table *
6836 osm_process_town_by_boundary(GList *bl, struct item_bin *ib, struct coord *c, struct attr *attrs)
6837 {
6838 GList *l, *matches = boundary_find_matches(bl, c);
6839 struct boundary *match = NULL;
6840
6841 //fprintf(stderr,"town_by_boundary:001\n");
6842
6843 l = matches;
6844 while (l)
6845 {
6846 //fprintf(stderr,"town_by_boundary:002\n");
6847 struct boundary *b = l->data;
6848 if (b->country)
6849 {
6850 //fprintf(stderr,"town_by_boundary:003\n");
6851 //if (match)
6852 //{
6853 //osm_warning("node", item_bin_get_nodeid(ib), 0, "node (x=0x%x,y=0x%x) conflict country ", c->x, c->y);
6854 //osm_warning("relation", boundary_relid(match), 1, "country %d vs ", match->country->countryid);
6855 //osm_warning("relation", boundary_relid(b), 1, "country %d\n", b->country->countryid);
6856 //}
6857 match = b;
6858 }
6859 l = g_list_next(l);
6860 }
6861
6862 //fprintf(stderr,"town_by_boundary:004\n");
6863
6864 if (match)
6865 {
6866 //fprintf(stderr,"town_by_boundary:005\n");
6867 if (match && match->country && match->country->admin_levels)
6868 {
6869 //fprintf(stderr,"town_by_boundary:006\n");
6870 l = matches;
6871 while (l)
6872 {
6873 //fprintf(stderr,"town_by_boundary:007\n");
6874
6875 struct boundary *b = l->data;
6876 char *admin_level = osm_tag_value(b->ib, "admin_level");
6877 char *postal = osm_tag_value(b->ib, "postal_code");
6878 if (admin_level)
6879 {
6880 //fprintf(stderr,"town_by_boundary:008\n");
6881
6882 int a = atoi(admin_level);
6883 int end = strlen(match->country->admin_levels) + 3;
6884 char *name;
6885 if (a > 2 && a < end)
6886 {
6887 //fprintf(stderr,"town_by_boundary:009\n");
6888
6889 enum attr_type attr_type = attr_none;
6890 switch (match->country->admin_levels[a - 3])
6891 {
6892 case 's':
6893 attr_type = attr_state_name;
6894 break;
6895 case 'c':
6896 attr_type = attr_county_name;
6897 break;
6898 case 'm':
6899 attr_type = attr_municipality_name;
6900 break;
6901 }
6902 name = osm_tag_value(b->ib, "name");
6903 if (name && attr_type != attr_none)
6904 {
6905 //fprintf(stderr,"town_by_boundary:010\n");
6906 attrs[a - 2].type = attr_type;
6907 attrs[a - 2].u.str = name;
6908 }
6909 }
6910 }
6911 if (postal)
6912 {
6913 //fprintf(stderr,"town_by_boundary:011\n");
6914 attrs[0].type = attr_town_postal;
6915 attrs[0].u.str = postal;
6916 }
6917 l = g_list_next(l);
6918 }
6919 }
6920 g_list_free(matches);
6921 return match->country;
6922 }
6923 else
6924 {
6925 //fprintf(stderr,"town_by_boundary:099\n");
6926 g_list_free(matches);
6927 return NULL;
6928 }
6929 }
6930
6931 int town_size_estimate(int type)
6932 {
6933 int size = 0; // default -> not use this area as a town!
6934
6935 switch (type)
6936 {
6937 case type_town_label_1e7:
6938 case type_town_label_5e6:
6939 case type_town_label_2e6:
6940 case type_town_label_1e6:
6941 case type_town_label_5e5:
6942 case type_town_label_2e5:
6943 size = 10000;
6944 break;
6945 case type_town_label_1e5:
6946 case type_town_label_5e4:
6947 case type_town_label_2e4:
6948 size = 3100;
6949 break;
6950 case type_town_label_1e4:
6951 case type_town_label_5e3:
6952 case type_town_label_2e3:
6953 size = 1460;
6954 break;
6955 case type_town_label_1e3:
6956 case type_town_label_5e2:
6957 case type_town_label_2e2:
6958 case type_town_label_1e2:
6959 case type_town_label_5e1:
6960 case type_town_label_2e1:
6961 case type_town_label_1e1:
6962 case type_town_label_5e0:
6963 case type_town_label_2e0:
6964 case type_town_label_1e0:
6965 case type_town_label_0e0:
6966 size = 660;
6967 break;
6968 default:
6969 break;
6970 }
6971
6972 // size = size * global_search_street_size_factor;
6973
6974 // lat factor 0.072
6975 // lon factor 0.101
6976
6977 // size = 10000
6978 // lat lon size = size / 100000
6979
6980 /*
6981 sel->u.c_rect.lu.x = c->x - size;
6982 sel->u.c_rect.lu.y = c->y + size;
6983 sel->u.c_rect.rl.x = c->x + size;
6984 sel->u.c_rect.rl.y = c->y - size;
6985 */
6986 return size;
6987 }
6988
6989 void assign_town_to_streets_v1();
6990 void assign_town_to_streets_by_boundary(GList *bl, GList *man_borders);
6991 void assign_town_to_country_by_manual_borders(GList *bl, GList *man_borders);
6992 void copy_town_border_data();
6993 void assign_streets_to_towns_by_manual_country_borders(GList *man_borders);
6994
6995 void assign_town_to_streets(GList *bl, GList *man_borders)
6996 {
6997 int skipped = 0;
6998
6999 copy_town_border_data();
7000 assign_town_to_country_by_manual_borders(bl, man_borders);
7001 assign_town_to_streets_by_boundary(bl, man_borders);
7002
7003 // now process all the left over towns
7004 assign_town_to_streets_v1(1);
7005 assign_town_to_streets_v1(2); // make townsize double on second pass
7006
7007 assign_streets_to_towns_by_manual_country_borders(man_borders);
7008 }
7009
7010 void purge_unused_towns()
7011 {
7012 int retval8;
7013 sqlite3_stmt *stmt_mm_1;
7014
7015 sql_counter = 0;
7016 retval8 = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7017 fprintf_(stderr, "prep:%d (error here is ok!)\n", retval8);
7018
7019 retval8 = sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7020 fprintf_(stderr, "prep:%d\n", retval8);
7021
7022 retval8 = sqlite3_prepare_v2(sql_handle, "select count(*) from town where id not in (select town_id from way);", -1, &stmt_mm_1, NULL);
7023 fprintf_(stderr, "prep:%d\n", retval8);
7024 sqlite3_step(stmt_mm_1);
7025 fprintf(stderr, "purge_unused_towns:%lld\n", sqlite3_column_int64(stmt_mm_1, 0));
7026 sqlite3_reset(stmt_mm_1);
7027
7028 // SQL:fin:
7029 retval8 = sqlite3_finalize(stmt_mm_1);
7030 fprintf_(stderr, "fin:%d\n", retval8);
7031
7032 retval8 = sqlite3_exec(sql_handle, "delete from town where id not in (select town_id from way);", 0, 0, 0);
7033 fprintf_(stderr, "prep:%d\n", retval8);
7034
7035 sql_counter = 0;
7036 retval8 = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7037 fprintf_(stderr, "prep:%d\n", retval8);
7038 }
7039
7040 void assign_town_to_country_by_manual_borders(GList *bl, GList *man_borders)
7041 {
7042 long long nd;
7043 long long wid;
7044 long long town_rel_id;
7045 int size;
7046 double lat;
7047 double lon;
7048 double temp;
7049 double lat_min;
7050 double lat_max;
7051 double lon_min;
7052 double lon_max;
7053 int rc;
7054 int rc2;
7055 long long town_count;
7056 long long town_processed_count;
7057 int town_steps = 1;
7058 int result = 0;
7059 struct boundary *bound_temp;
7060 int retval8;
7061 sqlite3_stmt *stmt_mm_2;
7062 struct coord c;
7063 long long dummy_town_id2 = 0;
7064 int commit_after = 20000;
7065 int count = 0;
7066
7067
7068 sql_counter = 0;
7069 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7070
7071 // number of towns in DB
7072 sqlite3_reset(stmt_town_sel002);
7073 sqlite3_step(stmt_town_sel002);
7074 town_count = sqlite3_column_int64(stmt_town_sel002, 0);
7075 town_processed_count = 0;
7076
7077 fprintf(stderr, "towns0a: %lld/%lld\n", town_processed_count, town_count);
7078
7079 if (town_count == 0)
7080 {
7081 sqlite3_reset(stmt_town_sel002);
7082 return;
7083 }
7084
7085 retval8 = sqlite3_prepare_v2(sql_handle, "update town set border_id = ? where id = ?;", -1, &stmt_mm_2, NULL);
7086 fprintf_(stderr, "prep:%d\n", retval8);
7087
7088
7089 sql_counter = 0;
7090 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7091
7092 if (town_count > 1000000)
7093 {
7094 town_steps = 500;
7095 }
7096 else if (town_count > 100000)
7097 {
7098 town_steps = 100;
7099 }
7100 else if (town_count > 10000)
7101 {
7102 town_steps = 10;
7103 }
7104
7105 // DEBUG
7106 // town_steps = 1;
7107 // DEBUG
7108
7109 // loop thru all the towns
7110 do
7111 {
7112 rc = sqlite3_step(stmt_town_sel001);
7113 switch (rc)
7114 {
7115 case SQLITE_DONE:
7116 break;
7117 case SQLITE_ROW:
7118 nd = sqlite3_column_int64(stmt_town_sel001, 0);
7119 town_rel_id = sqlite3_column_int64(stmt_town_sel001, 5);
7120 lat = sqlite3_column_double(stmt_town_sel001, 2);
7121 lon = sqlite3_column_double(stmt_town_sel001, 3);
7122
7123 if (town_rel_id == -1)
7124 {
7125 if ((lat != 999) && (lon != 999))
7126 {
7127 c.x = transform_from_geo_lon(lon);
7128 c.y = transform_from_geo_lat(lat);
7129 // check if c is inside manual country border
7130 dummy_town_id2 = osm_process_street_by_manual_country_borders(man_borders, &c);
7131
7132 if (dummy_town_id2 != -1)
7133 {
7134 //fprintf(stderr, "== town_to_country_by_manual_borders: townid=%lld\n", nd);
7135 sqlite3_bind_int64(stmt_mm_2, 1, dummy_town_id2); // parent country rel id
7136 sqlite3_bind_int64(stmt_mm_2, 2, nd); // town id
7137 sqlite3_step(stmt_mm_2);
7138 sqlite3_reset(stmt_mm_2);
7139 }
7140
7141 if (count == 0)
7142 {
7143 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7144 }
7145 count++;
7146
7147 if (count > commit_after)
7148 {
7149 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7150 count = 0;
7151 }
7152 }
7153 }
7154 break;
7155 default:
7156 fprintf(stderr, "SQL Error: %d\n", rc);
7157 break;
7158 }
7159 }
7160 while (rc == SQLITE_ROW);
7161 sqlite3_reset(stmt_town_sel001);
7162
7163 // SQL:fin:
7164 rc = sqlite3_finalize(stmt_mm_2);
7165 fprintf_(stderr, "fin:%d\n", rc);
7166
7167 sql_counter = 0;
7168 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7169
7170
7171 }
7172
7173
7174 void save_manual_country_borders_to_db(GList *man_borders)
7175 {
7176 GList *l = man_borders;
7177 while (l)
7178 {
7179 struct boundary_manual *b = l->data;
7180
7181 // also save dummy id to border list, to be able to compare it later
7182 b->town_id = dummy_town_id;
7183
7184 sqlite3_bind_int64(stmt_town, 1, dummy_town_id); // town id
7185 sqlite3_bind_int(stmt_town, 2, b->country->countryid); // country id
7186 sqlite3_bind_text(stmt_town, 3, b->country->names, -1, SQLITE_STATIC); // name
7187 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + 2) * TOWN_BY_BOUNDARY_SIZE_FACTOR); // town size
7188 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC); // postal
7189
7190 sqlite3_bind_double(stmt_town, 6, 999); // lat -> 999:exclude
7191 sqlite3_bind_double(stmt_town, 7, 999); // lon -> 999:exclude
7192
7193 sqlite3_bind_int64(stmt_town, 8, -1); // border relation id
7194 sqlite3_step(stmt_town);
7195 sqlite3_reset(stmt_town);
7196 dummy_town_id--;
7197
7198 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7199
7200 l = g_list_next(l);
7201 }
7202 }
7203
7204 void assign_streets_to_towns_by_manual_country_borders(GList *man_borders)
7205 {
7206 long long nd;
7207 int size;
7208 double lat;
7209 double lon;
7210 double lat_min;
7211 double lat_max;
7212 double lon_min;
7213 double lon_max;
7214 double temp;
7215 int rc;
7216 sqlite3_stmt *stmt_mm_1;
7217 sqlite3_stmt *stmt_mm_2;
7218 int retval9;
7219 int retval8;
7220 int street_steps;
7221 long long streets_processed_count;
7222 long long wid;
7223 struct coord c;
7224 char *wname;
7225
7226 sql_counter = 0;
7227 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7228
7229
7230 retval9 = sqlite3_prepare_v2(sql_handle, "select w.lat, w.lon, w.id, w.name from way w where w.town_id='-1';", -1, &stmt_mm_1, NULL);
7231 fprintf_(stderr, "prep:%d\n", retval9);
7232
7233 retval8 = sqlite3_prepare_v2(sql_handle, "update way set town_id = ? where id = ?;", -1, &stmt_mm_2, NULL);
7234 fprintf_(stderr, "prep:%d\n", retval8);
7235
7236 street_steps = 1000; // set to better value!!
7237 streets_processed_count = 0;
7238
7239 sql_counter = 0;
7240 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7241
7242 // loop thru all the ways
7243 do
7244 {
7245 rc = sqlite3_step(stmt_mm_1);
7246 switch (rc)
7247 {
7248 case SQLITE_DONE:
7249 break;
7250 case SQLITE_ROW:
7251 lat = sqlite3_column_double(stmt_mm_1, 0);
7252 lon = sqlite3_column_double(stmt_mm_1, 1);
7253 wid = sqlite3_column_int64(stmt_mm_1, 2);
7254 wname = g_strdup_printf("%s", sqlite3_column_text(stmt_mm_1, 3));
7255
7256 // now update the ways
7257 if (sql_counter == 0)
7258 {
7259 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7260 }
7261
7262 c.x = transform_from_geo_lon(lon);
7263 c.y = transform_from_geo_lat(lat);
7264 nd = osm_process_street_by_manual_country_borders(man_borders, &c);
7265
7266 streets_processed_count++;
7267 if ((streets_processed_count % street_steps) == 0)
7268 {
7269 fprintf_(stderr, "streets0: %lld\n", streets_processed_count);
7270 }
7271
7272 if (nd != -1)
7273 {
7274 // fprintf(stderr, "== streets by manual_country_borders: way=%s townid=%lld\n", wname, nd);
7275
7276 sqlite3_bind_int64(stmt_mm_2, 1, nd);
7277 sqlite3_bind_int64(stmt_mm_2, 2, wid);
7278 sqlite3_step(stmt_mm_2);
7279 sqlite3_reset(stmt_mm_2);
7280
7281 sql_counter++;
7282 if (sql_counter > MAX_ROWS_WO_COMMIT_2a)
7283 {
7284 sql_counter = 0;
7285 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7286 }
7287 }
7288
7289 if (wname)
7290 {
7291 g_free(wname);
7292 }
7293
7294 break;
7295 default:
7296 fprintf(stderr, "SQL Error: %d\n", rc);
7297 break;
7298 }
7299 }
7300 while (rc == SQLITE_ROW);
7301 sqlite3_reset(stmt_mm_1);
7302
7303 // SQL:fin:
7304 rc = sqlite3_finalize(stmt_mm_1);
7305 fprintf_(stderr, "fin:%d\n", rc);
7306
7307 // SQL:fin:
7308 rc = sqlite3_finalize(stmt_mm_2);
7309 fprintf_(stderr, "fin:%d\n", rc);
7310
7311 sql_counter = 0;
7312 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7313
7314 }
7315
7316
7317 void copy_town_border_data()
7318 {
7319 long long bid;
7320 long long tid;
7321 int retval;
7322 int rc = 0;
7323 int commit_after = 20000;
7324 int count = 0;
7325 int admin_l = 99;
7326
7327 sqlite3_stmt *stmt_d_1;
7328 sqlite3_stmt *stmt_d_2;
7329 retval = sqlite3_prepare_v2(sql_handle, "select border_id, id, admin_level from town2;", -1, &stmt_d_1, NULL);
7330 // fprintf(stderr, "prep:%d\n", retval);
7331 retval = sqlite3_prepare_v2(sql_handle, "update town set border_id = ?, size = ? where id = ?;", -1, &stmt_d_2, NULL);
7332 // fprintf(stderr, "prep:%d\n", retval);
7333
7334 // loop thru all the towns
7335 do
7336 {
7337 rc = sqlite3_step(stmt_d_1);
7338 switch (rc)
7339 {
7340 case SQLITE_DONE:
7341 break;
7342 case SQLITE_ROW:
7343 bid = sqlite3_column_int64(stmt_d_1, 0);
7344 tid = sqlite3_column_int64(stmt_d_1, 1);
7345 admin_l = sqlite3_column_int(stmt_d_1, 2);
7346
7347 // fprintf(stderr, "bid=%lld tid=%lld amdin_level=%d\n", bid, tid, admin_l);
7348
7349 if (count == 0)
7350 {
7351 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7352 }
7353 count++;
7354
7355 sqlite3_bind_int64(stmt_d_2, 1, bid);
7356 sqlite3_bind_int64(stmt_d_2, 2, tid);
7357 sqlite3_bind_int(stmt_d_2, 3, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_l) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
7358 sqlite3_step(stmt_d_2);
7359 sqlite3_reset(stmt_d_2);
7360
7361 if (count > commit_after)
7362 {
7363 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7364 count = 0;
7365 }
7366
7367 break;
7368 default:
7369 fprintf(stderr, "SQL Error: %d\n", rc);
7370 break;
7371 }
7372 }
7373 while (rc == SQLITE_ROW);
7374 sqlite3_reset(stmt_d_1);
7375
7376 retval = sqlite3_finalize(stmt_d_2);
7377 // fprintf(stderr, "fin:%d\n", retval);
7378 retval = sqlite3_finalize(stmt_d_1);
7379 // fprintf(stderr, "fin:%d\n", retval);
7380
7381 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7382
7383 }
7384
7385 void assign_town_to_streets_by_boundary(GList *bl, GList *man_borders)
7386 {
7387 long long nd;
7388 long long wid;
7389 long long town_rel_id;
7390 int size;
7391 double lat;
7392 double lon;
7393 double temp;
7394 double lat_min;
7395 double lat_max;
7396 double lon_min;
7397 double lon_max;
7398 int rc;
7399 int rc2;
7400 long long town_count;
7401 long long town_processed_count;
7402 int town_steps = 1;
7403 int result = 0;
7404 struct boundary *bound_temp;
7405
7406 sql_counter = 0;
7407 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7408
7409 // number of towns in DB
7410 sqlite3_reset(stmt_town_sel002);
7411 sqlite3_step(stmt_town_sel002);
7412 town_count = sqlite3_column_int64(stmt_town_sel002, 0);
7413 town_processed_count = 0;
7414
7415 fprintf(stderr, "towns0b: %lld/%lld\n", town_processed_count, town_count);
7416
7417 if (town_count == 0)
7418 {
7419 sqlite3_reset(stmt_town_sel002);
7420 return;
7421 }
7422
7423 sql_counter = 0;
7424 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7425
7426 if (town_count > 1000000)
7427 {
7428 town_steps = 500;
7429 }
7430 else if (town_count > 100000)
7431 {
7432 town_steps = 100;
7433 }
7434 else if (town_count > 10000)
7435 {
7436 town_steps = 10;
7437 }
7438
7439 // DEBUG
7440 // town_steps = 1;
7441 // DEBUG
7442
7443 // loop thru all the towns
7444 do
7445 {
7446 rc = sqlite3_step(stmt_town_sel001);
7447 switch (rc)
7448 {
7449 case SQLITE_DONE:
7450 break;
7451 case SQLITE_ROW:
7452 nd = sqlite3_column_int64(stmt_town_sel001, 0);
7453 town_rel_id = sqlite3_column_int64(stmt_town_sel001, 5);
7454 //size = sqlite3_column_int(stmt_town_sel001, 1);
7455 //lat = sqlite3_column_double(stmt_town_sel001, 2);
7456 //lon = sqlite3_column_double(stmt_town_sel001, 3);
7457
7458
7459 GList *match_town = NULL;
7460 GList *l = bl;
7461 int has_found = 0;
7462
7463 if ((town_rel_id != 0) && (town_rel_id != -1))
7464 {
7465 //fprintf(stderr ,"processing town name:%s id:%lld border_id:%lld\n", sqlite3_column_text(stmt_town_sel001, 4), nd, town_rel_id);
7466
7467 while (l)
7468 {
7469 struct boundary *b = l->data;
7470 //fprintf(stderr, "bid=%lld tid=%lld\n", item_bin_get_relationid(b->ib), town_rel_id);
7471 if (item_bin_get_relationid(b->ib) == town_rel_id)
7472 {
7473 match_town = l;
7474 has_found = 1;
7475 //fprintf(stderr, "bid=%lld tid=%lld\n", item_bin_get_relationid(b->ib), town_rel_id);
7476 //fprintf(stderr, "*** town border found***\n");
7477 break;
7478 }
7479 l = g_list_next(l);
7480 }
7481 }
7482
7483 if (has_found == 1)
7484 {
7485 // now run thru all the ways
7486 do
7487 {
7488 // only select ways that are in the bounding box of boundary!
7489
7490 bound_temp = match_town->data;
7491 lat_min = transform_to_geo_lat(bound_temp->r.l.y);
7492 lat_max = transform_to_geo_lat(bound_temp->r.h.y);
7493 lon_min = transform_to_geo_lon(bound_temp->r.l.x);
7494 lon_max = transform_to_geo_lon(bound_temp->r.h.x);
7495
7496 //fprintf(stderr, "lat min:%f max:%f lon min:%f max:%f \n", lat_min, lat_max, lon_min, lon_max);
7497
7498 sqlite3_bind_double(stmt_way3a, 1, lat_min);
7499 sqlite3_bind_double(stmt_way3a, 2, lat_max);
7500 sqlite3_bind_double(stmt_way3a, 3, lon_min);
7501 sqlite3_bind_double(stmt_way3a, 4, lon_max);
7502 rc2 = sqlite3_step(stmt_way3a);
7503 switch (rc2)
7504 {
7505 case SQLITE_DONE:
7506 break;
7507 case SQLITE_ROW:
7508 wid = sqlite3_column_int64(stmt_way3a, 0);
7509 lat = sqlite3_column_double(stmt_way3a, 1);
7510 lon = sqlite3_column_double(stmt_way3a, 2);
7511
7512 struct coord c;
7513 c.x = transform_from_geo_lon(lon);
7514 c.y = transform_from_geo_lat(lat);
7515
7516 //fprintf(stderr,"== street by boundary == x:%d y:%d name:%s ==\n", c.x, c.y, sqlite3_column_text(stmt_way3a, 3));
7517
7518 if (town_rel_id != 0)
7519 {
7520 //fprintf(stderr, "town:%lld\n", nd);
7521 //fprintf(stderr,"== street by boundary == x:%d y:%d ==\n", c.x, c.y);
7522 result = osm_process_street_by_boundary(match_town, nd, town_rel_id, &c);
7523 if (result == 1)
7524 {
7525 //fprintf(stderr,"== street by boundary == wid:%lld townid:%lld ==\n", wid, nd);
7526 // write "town id" to "way"
7527 if (sql_counter == 0)
7528 {
7529 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7530 }
7531 sql_counter++;
7532 sqlite3_bind_int64(stmt_way3b, 1, nd);
7533 sqlite3_bind_int64(stmt_way3b, 2, wid);
7534 sqlite3_step(stmt_way3b);
7535 sqlite3_reset(stmt_way3b);
7536 if (sql_counter > MAX_ROWS_WO_COMMIT_2b)
7537 {
7538 sql_counter = 0;
7539 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7540 }
7541 }
7542 }
7543
7544 break;
7545 default:
7546 fprintf(stderr, "SQL Error: %d\n", rc);
7547 break;
7548 }
7549 }
7550 while (rc2 == SQLITE_ROW);
7551 sqlite3_reset(stmt_way3a);
7552
7553
7554 // mark town as "done"
7555 if (sql_counter == 0)
7556 {
7557 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7558 }
7559 sql_counter++;
7560 sqlite3_bind_int64(stmt_town_sel007, 1, nd);
7561 sqlite3_step(stmt_town_sel007);
7562 sqlite3_reset(stmt_town_sel007);
7563 if (sql_counter > MAX_ROWS_WO_COMMIT_2b)
7564 {
7565 sql_counter = 0;
7566 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7567 }
7568
7569 }
7570
7571 town_processed_count++;
7572 if ((town_processed_count % town_steps) == 0)
7573 {
7574 fprintf_(stderr, "towns0b: %lld/%lld\n", town_processed_count, town_count);
7575 }
7576
7577 break;
7578 default:
7579 fprintf(stderr, "SQL Error: %d\n", rc);
7580 break;
7581 }
7582 }
7583 while (rc == SQLITE_ROW);
7584 sqlite3_reset(stmt_town_sel001);
7585
7586 sql_counter = 0;
7587 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7588 }
7589
7590
7591 void assign_town_to_streets_v1(int pass_num)
7592 {
7593
7594 long long nd;
7595 int size;
7596 double lat;
7597 double lon;
7598 double lat_min;
7599 double lat_max;
7600 double lon_min;
7601 double lon_max;
7602 double temp;
7603 int rc;
7604 long long town_count;
7605 long long town_processed_count;
7606 int town_steps = 1;
7607
7608 sql_counter = 0;
7609 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7610
7611 // number of towns in DB
7612 sqlite3_reset(stmt_town_sel002);
7613 sqlite3_step(stmt_town_sel002);
7614 town_count = sqlite3_column_int64(stmt_town_sel002, 0);
7615 town_processed_count = 0;
7616
7617 fprintf(stderr, "towns1-%d: %lld/%lld\n", pass_num, town_processed_count, town_count);
7618
7619 if (town_count == 0)
7620 {
7621 sqlite3_reset(stmt_town_sel002);
7622 return;
7623 }
7624
7625 sql_counter = 0;
7626 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7627
7628 if (town_count > 1000000)
7629 {
7630 town_steps = 500;
7631 }
7632 else if (town_count > 100000)
7633 {
7634 town_steps = 100;
7635 }
7636 else if (town_count > 10000)
7637 {
7638 town_steps = 10;
7639 }
7640
7641 // DEBUG
7642 // town_steps = 1;
7643 // DEBUG
7644
7645 // loop thru all the towns
7646 do
7647 {
7648 rc = sqlite3_step(stmt_town_sel001);
7649 switch (rc)
7650 {
7651 case SQLITE_DONE:
7652 break;
7653 case SQLITE_ROW:
7654 nd = sqlite3_column_int64(stmt_town_sel001, 0);
7655 size = sqlite3_column_int(stmt_town_sel001, 1);
7656 lat = sqlite3_column_double(stmt_town_sel001, 2);
7657 lon = sqlite3_column_double(stmt_town_sel001, 3);
7658
7659 if ((lat == 999) && (lon == 999))
7660 {
7661 // this should be excluded!!
7662 }
7663 else
7664 {
7665 //fprintf(stderr ,"processing (size:%d) town name:%s id:%lld\n", size, sqlite3_column_text(stmt_town_sel001, 4), nd);
7666
7667 // now update the ways
7668 if (sql_counter == 0)
7669 {
7670 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7671 }
7672
7673 sqlite3_bind_int64(stmt_way3, 1, nd);
7674
7675 if ((pass_num == 2) && (size > 0))
7676 {
7677 size = size * 3;
7678 }
7679
7680 temp = ((double) size) / 100000;
7681 lat_min = lat - temp;
7682 lat_max = lat + temp;
7683 lon_min = lon - temp;
7684 lon_max = lon + temp;
7685 sqlite3_bind_double(stmt_way3, 2, lat_min);
7686 sqlite3_bind_double(stmt_way3, 3, lat_max);
7687 sqlite3_bind_double(stmt_way3, 4, lon_min);
7688 sqlite3_bind_double(stmt_way3, 5, lon_max);
7689 //fprintf(stderr, "size korr:%f %f %f %f %f\n", temp, lat_min, lat_max, lon_min, lon_max);
7690
7691 sqlite3_step(stmt_way3);
7692 sqlite3_reset(stmt_way3);
7693
7694 sql_counter++;
7695 if (sql_counter > MAX_ROWS_WO_COMMIT_2a)
7696 {
7697 sql_counter = 0;
7698 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7699 }
7700 town_processed_count++;
7701
7702 if ((town_processed_count % town_steps) == 0)
7703 {
7704 fprintf_(stderr, "towns1-%d: %lld/%lld\n", pass_num, town_processed_count, town_count);
7705 }
7706 }
7707
7708 break;
7709 default:
7710 fprintf(stderr, "SQL Error: %d\n", rc);
7711 break;
7712 }
7713 }
7714 while (rc == SQLITE_ROW);
7715 sqlite3_reset(stmt_town_sel001);
7716
7717 sql_counter = 0;
7718 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
7719
7720 }
7721
7722
7723 int lat_lon_inside_rect(struct node_lat_lon *n, struct rect_lat_lon *r)
7724 {
7725 if ((n->lat >= r->lu_lat) && (n->lon >= r->lu_lon) && (n->lat <= r->rl_lat) && (n->lon <= r->rl_lon))
7726 {
7727 return 1;
7728 }
7729
7730 return 0;
7731 }
7732
7733 GList* osm_process_towns(FILE *in, FILE *coords, FILE *boundaries, FILE *ways, GList *bl_manual)
7734 {
7735 struct item_bin *ib = NULL;
7736 GList *bl = NULL;
7737 struct attr attrs[10];
7738 time_t start_tt, end_tt;
7739 double diff_tt;
7740 double diff2_tt;
7741 long long size_in;
7742 long long pos_in;
7743 struct node_lat_lon node_coords;
7744 FILE *file_coords_for_map = NULL;
7745 FILE *file_coords_for_map2 = NULL;
7746
7747 if (debug_itembin(1))
7748 {
7749 fprintf(stderr, "osm_process_towns == START ==\n");
7750 }
7751
7752 if (!global_less_verbose)
7753 {
7754 file_coords_for_map = fopen("towns_no_country.coords.txt", "wb");
7755 fprintf(file_coords_for_map, "-175.0|85.0|-175_85\n");
7756 fprintf(file_coords_for_map, "175|85.0|175_85\n");
7757 fprintf(file_coords_for_map, "-175.0|-85.0|-175_-85\n");
7758 fprintf(file_coords_for_map, "175.0|-85.0|175_-85\n");
7759
7760 file_coords_for_map2 = fopen("towns_no_country.coords_names.txt", "wb");
7761 fprintf(file_coords_for_map2, "-175.0|85.0|-175_85\n");
7762 fprintf(file_coords_for_map2, "175|85.0|175_85\n");
7763 fprintf(file_coords_for_map2, "-175.0|-85.0|-175_-85\n");
7764 fprintf(file_coords_for_map2, "175.0|-85.0|175_-85\n");
7765 }
7766
7767 //fprintf(stderr,"osm_process_towns == PB 001 %p,%p ==\n", boundaries, ways);
7768
7769 time(&start_tt);
7770 bl = process_boundaries(boundaries, coords, ways);
7771 time(&end_tt);
7772 diff_tt = difftime(end_tt, start_tt);
7773 char outstring[200];
7774 char outstring2[200];
7775 char outstring3[200];
7776 convert_to_human_time(diff_tt, outstring);
7777 fprintf(stderr, "-RUNTIME-BOUNDARIES: %s\n", outstring);
7778
7779 int _c = 0;
7780 int _e = 10000;
7781
7782 //fprintf(stderr,"osm_process_towns == PB 002 ==\n");
7783
7784 long long pos_now = ftello(in); // 64bit
7785 fseeko(in, 0, SEEK_END);
7786 size_in = ftello(in); // 64bit
7787 fseeko(in, (off_t)pos_now, SEEK_SET);
7788
7789 // reset timer
7790 diff2_tt = 0;
7791 _c = 0;
7792 time(&start_tt);
7793
7794 while ((ib = read_item(in, 0)))
7795 {
7796 struct coord *c = (struct coord *) (ib + 1);
7797 struct country_table *result = NULL;
7798 char *is_in = item_bin_get_attr(ib, attr_osm_is_in, NULL);
7799 int i;
7800
7801 _c++;
7802
7803 if (debug_itembin(ib))
7804 {
7805 fprintf(stderr, "== item ==\n");
7806 dump_itembin(ib);
7807 }
7808
7809 memset(attrs, 0, sizeof(attrs));
7810 if (debug_itembin(ib))
7811 {
7812 fprintf(stderr, "== osm_process_town_by_boundary ==\n");
7813 }
7814
7815 if (use_global_fixed_country_id == 1)
7816 {
7817 result = osm_process_item_fixed_country();
7818 if (debug_itembin(ib))
7819 {
7820 if (result == NULL)
7821 {
7822 fprintf(stderr, "== osm_process_item_fixed_country == #NULL# ==\n");
7823 }
7824 else
7825 {
7826 fprintf(stderr, "== osm_process_item_fixed_country == %d %s ==\n", result->countryid, result->names);
7827 }
7828 }
7829 }
7830
7831 if (!result)
7832 {
7833 result = osm_process_town_by_boundary(bl, ib, c, attrs);
7834 if (result) // DEBUG
7835 {
7836 if (debug_itembin(ib))
7837 {
7838 char *name=item_bin_get_attr(ib, attr_town_name, NULL);
7839 //fprintf(stderr,"== town by boundary == t:%s ==\n", name);
7840 //fprintf(stderr,"== town by boundary == country_id:%d country_name:%s townname:%s ==\n", result->countryid, result->names, name);
7841 }
7842 }
7843 }
7844
7845 if (!result)
7846 {
7847 if (debug_itembin(ib))
7848 {
7849 fprintf(stderr, "== osm_process_town_by_is_in == %s ==\n", is_in);
7850 }
7851 result = osm_process_town_by_is_in(ib, is_in);
7852 if (result) // DEBUG
7853 {
7854 // fprintf(stderr,"== town by is_in == country_id:%d country_name:%s ==\n", result->countryid, result->names);
7855 }
7856 }
7857
7858
7859 // ok as a last resort check in manual country borders
7860 if (!result)
7861 {
7862 result = osm_process_town_by_manual_country_borders(bl_manual, c);
7863 if (result)
7864 {
7865 if (debug_itembin(ib))
7866 {
7867 char *name=item_bin_get_attr(ib, attr_town_name, NULL);
7868 fprintf(stderr,"== town by manual_country_borders == country_id:%d country_name:%s town_name=%s ==\n", result->countryid, result->names, name);
7869 }
7870 }
7871 }
7872
7873 if (!result && unknown_country)
7874 {
7875 if (debug_itembin(ib))
7876 {
7877 fprintf(stderr, "== osm_process_town_unknown_country ==\n");
7878 }
7879 result = osm_process_town_unknown_country();
7880 }
7881
7882 if (result)
7883 {
7884 if (!result->file)
7885 {
7886 char *name = g_strdup_printf("country_%d.unsorted.tmp", result->countryid);
7887 result->file = fopen(name, "wb");
7888
7889 if (debug_itembin(ib))
7890 {
7891 fprintf(stderr, "== create: country_%d.unsorted.tmp == FILEP: %p ==\n", result->countryid, result->file);
7892 }
7893
7894 g_free(name);
7895 }
7896
7897 // generate file to plot coords on world map -------------------
7898 if (result->countryid == 999)
7899 {
7900 if (!global_less_verbose)
7901 {
7902 // format: lon|lat|[townname]\n
7903 fprintf(file_coords_for_map, "%lf|%lf|\n", transform_to_geo_lon(c->x), transform_to_geo_lat(c->y));
7904
7905 char *town_name_tmp = item_bin_get_attr(ib, attr_town_name, NULL);
7906 fprintf(file_coords_for_map2, "%lf|%lf|%s\n", transform_to_geo_lon(c->x), transform_to_geo_lat(c->y), town_name_tmp);
7907 }
7908 }
7909 // generate file to plot coords on world map -------------------
7910
7911 if (result->file)
7912 {
7913 long long *nodeid;
7914 long long nd_id = item_bin_get_nodeid(ib);
7915 if (is_in)
7916 {
7917 item_bin_remove_attr(ib, is_in);
7918 }
7919 nodeid = item_bin_get_attr(ib, attr_osm_nodeid, NULL);
7920 if (nodeid)
7921 {
7922 item_bin_remove_attr(ib, nodeid);
7923 }
7924
7925 char *postal = NULL;
7926 char *postal2 = NULL;
7927 //if (attrs[0].type != attr_none) // what does this line do??
7928 //{
7929 postal = item_bin_get_attr(ib, attr_town_postal, NULL);
7930 if (postal)
7931 {
7932 postal2 = g_strdup_printf("%s", postal);
7933 // --- item_bin_remove_attr(ib, postal);
7934 // fprintf(stderr, "town postal:%s\n", postal2);
7935 }
7936 //}
7937
7938 for (i = 0; i < 10; i++)
7939 {
7940 if (attrs[i].type != attr_none)
7941 {
7942 item_bin_add_attr(ib, &attrs[i]);
7943 }
7944 }
7945
7946 if (sql_counter == 0)
7947 {
7948 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
7949 }
7950
7951 if (nd_id)
7952 {
7953 // town size (will later be translated into radius)
7954 int twn_size = town_size_estimate(ib->type);
7955
7956 if (twn_size > 0)
7957 {
7958
7959 sqlite3_bind_int64(stmt_town, 1, nd_id);
7960 if (result->countryid)
7961 {
7962 sqlite3_bind_int(stmt_town, 2, result->countryid);
7963 }
7964 else
7965 {
7966 sqlite3_bind_int(stmt_town, 2, 999);
7967 }
7968
7969 sqlite3_bind_int(stmt_town, 4, twn_size);
7970
7971 char *label_t = item_bin_get_attr(ib, attr_town_name, NULL);
7972 //fprintf(stderr, "town: %lld,%d,%s\n", nd_id, result->countryid, label_t);
7973
7974 sqlite3_bind_text(stmt_town, 3, label_t, -1, SQLITE_STATIC);
7975
7976 if (postal2)
7977 {
7978 sqlite3_bind_text(stmt_town, 5, postal2, -1, SQLITE_STATIC);
7979 }
7980 else
7981 {
7982 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
7983 }
7984
7985 get_lat_lon_for_node(nd_id, &node_coords);
7986 if (node_coords.valid == 1)
7987 {
7988 sqlite3_bind_double(stmt_town, 6, node_coords.lat);
7989 sqlite3_bind_double(stmt_town, 7, node_coords.lon);
7990 }
7991
7992 sqlite3_step(stmt_town);
7993 sqlite3_reset(stmt_town);
7994 sql_counter++;
7995
7996 if (postal2)
7997 {
7998 g_free(postal2);
7999 }
8000
8001 }
8002 }
8003
8004 if (sql_counter > MAX_ROWS_WO_COMMIT_5)
8005 {
8006 sql_counter = 0;
8007 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
8008 //fprintf(stderr, "SQL: COMMIT\n");
8009 }
8010
8011 // ** used until 2011-11-06 seems bad ** item_bin_write_match(ib, attr_town_name, attr_town_name_match, result->file);
8012 item_bin_town_write_match(ib, attr_town_name, attr_town_name_match, result->file);
8013 // ** origname ** item_bin_write(ib,result->file);
8014 }
8015 }
8016 else
8017 {
8018 if (debug_itembin(ib))
8019 {
8020 fprintf(stderr, "== no result ==\n");
8021 }
8022 }
8023
8024 if (_c > _e)
8025 {
8026 _c = 0;
8027
8028 pos_in = ftello(in); // 64bit
8029 time(&end_tt);
8030 diff_tt = difftime(end_tt, start_tt);
8031 convert_to_human_time(diff_tt, outstring);
8032 convert_to_human_bytes(pos_in, outstring2);
8033 convert_to_human_bytes(size_in, outstring3);
8034 fprintf_(stderr, "-RUNTIME-LOOP-TOWN: %s elapsed (POS:%s of %s)\n", outstring, outstring2, outstring3);
8035 if (pos_in > 0)
8036 {
8037 double eta_time = ((diff_tt / (pos_in)) * (size_in)) - diff_tt;
8038 convert_to_human_time(eta_time, outstring);
8039 fprintf_(stderr, "-RUNTIME-LOOP-TOWN: %s left\n", outstring);
8040 }
8041 }
8042 }
8043
8044 if (debug_itembin(1))
8045 {
8046 fprintf(stderr, "osm_process_towns == END ==\n");
8047 }
8048
8049 if (!global_less_verbose)
8050 {
8051 fclose(file_coords_for_map);
8052 fclose(file_coords_for_map2);
8053 }
8054
8055 return bl;
8056
8057 }
8058
8059 void sort_countries(int keep_tmpfiles)
8060 {
8061 int i;
8062 struct country_table *co;
8063 char *name_in, *name_out;
8064 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
8065 {
8066 co = &country_table[i];
8067 if (co->file)
8068 {
8069 fclose(co->file);
8070 co->file = NULL;
8071 }
8072 name_in = g_strdup_printf("country_%d.unsorted.tmp", co->countryid);
8073 name_out = g_strdup_printf("country_%d.tmp", co->countryid);
8074
8075 if (debug_itembin(2))
8076 {
8077 fprintf(stderr, "in=country_%d.unsorted.tmp\n", co->countryid);
8078 fprintf(stderr, "out=country_%d.tmp\n", co->countryid);
8079 }
8080
8081 co->r = world_bbox;
8082 item_bin_sort_file(name_in, name_out, &co->r, &co->size);
8083 if (!keep_tmpfiles)
8084 {
8085 unlink(name_in);
8086 }
8087 g_free(name_in);
8088 g_free(name_out);
8089 }
8090 }
8091
8092 static int search_relation_member(struct item_bin *ib, char *role, struct relation_member *memb, int *min_count)
8093 {
8094 char *str = NULL;
8095 int count = 0;
8096 while ((str = item_bin_get_attr(ib, attr_osm_member, str)))
8097 {
8098 if (!get_relation_member(str, memb))
8099 return 0;
8100 count++;
8101 if (!strcmp(memb->role, role) && (!min_count || *min_count < count))
8102 {
8103 if (min_count)
8104 *min_count = count;
8105 return 1;
8106 }
8107 }
8108 return 0;
8109 }
8110
8111 static int load_way_index(FILE *ways_index, int p, long long *idx)
8112 {
8113 int step = sizeof(*idx) * 2;
8114 fseek(ways_index, p * step, SEEK_SET);
8115 if (fread(idx, step, 1, ways_index) != 1)
8116 {
8117 fprintf(stderr, "read failed\n");
8118 return 0;
8119 }
8120 return 1;
8121 }
8122
8123 static int seek_to_way(FILE *way, FILE *ways_index, long long wayid)
8124 {
8125 long offset;
8126 long long idx[2];
8127 int count, interval, p;
8128 void *p_tmp;
8129 int rr;
8130 size_t data_size2 = sizeof(int);
8131
8132 fprintf_(stderr, "seek_to_way ---\n");
8133
8134 if (way_hash_cfu)
8135 {
8136 //if (!(g_hash_table_lookup_extended(way_hash, (gpointer) (long) wayid, NULL, (gpointer) & offset)))
8137 //{
8138 // return 0;
8139 //}
8140
8141 rr = cfuhash_get_data(way_hash_cfu, (long long) wayid, sizeof(long long), &p_tmp, &data_size2);
8142 if (rr == 0)
8143 {
8144 return 0;
8145 }
8146
8147 // printf("got value %ld\n", p_tmp);
8148 offset = p_tmp;
8149
8150 fseek(way, offset, SEEK_SET);
8151 return 1;
8152 }
8153
8154 fprintf(stderr, "**BAD**WAYSTUFF**\n");
8155
8156 fseek(ways_index, 0, SEEK_END);
8157 count = ftell(ways_index) / sizeof(idx);
8158 interval = count / 4;
8159 p = count / 2;
8160 if (interval == 0)
8161 {
8162 // If fewer than 4 nodes defined so far set interval to 1 to
8163 // avoid infinite loop
8164 interval = 1;
8165 }
8166 if (!load_way_index(ways_index, p, idx))
8167 return 0;
8168 for (;;)
8169 {
8170 if (idx[0] == wayid)
8171 {
8172 fseek(way, idx[1], SEEK_SET);
8173 return 1;
8174 }
8175 if (idx[0] < wayid)
8176 {
8177 p += interval;
8178 if (interval == 1)
8179 {
8180 if (p >= count)
8181 return 0;
8182 if (!load_way_index(ways_index, p, idx))
8183 return 0;
8184 if (idx[0] > wayid)
8185 return 0;
8186 }
8187 else
8188 {
8189 if (p >= count)
8190 p = count - 1;
8191 if (!load_way_index(ways_index, p, idx))
8192 return 0;
8193 }
8194 }
8195 else
8196 {
8197 p -= interval;
8198 if (interval == 1)
8199 {
8200 if (p < 0)
8201 return 0;
8202 if (!load_way_index(ways_index, p, idx))
8203 return 0;
8204 if (idx[0] < wayid)
8205 return 0;
8206 }
8207 else
8208 {
8209 if (p < 0)
8210 p = 0;
8211 if (!load_way_index(ways_index, p, idx))
8212 return 0;
8213 }
8214 }
8215 if (interval > 1)
8216 interval /= 2;
8217 }
8218 }
8219
8220 static struct coord *
8221 get_way(FILE *way, FILE *ways_index, struct coord *c, long long wayid, struct item_bin *ret, int debug)
8222 {
8223 long long currid;
8224 int last;
8225 struct coord *ic;
8226 if (!seek_to_way(way, ways_index, wayid))
8227 {
8228 if (debug)
8229 {
8230 fprintf(stderr, "not found in index");
8231 }
8232 return NULL;
8233 }
8234
8235 while (item_bin_read(ret, way))
8236 {
8237 currid = item_bin_get_wayid(ret);
8238 if (debug)
8239 fprintf(stderr,LONGLONG_FMT":",currid);
8240 if (currid != wayid)
8241 return NULL;
8242 ic = (struct coord *) (ret + 1);
8243 last = ret->clen / 2 - 1;
8244 if (debug)
8245 fprintf(stderr, "(0x%x,0x%x)-(0x%x,0x%x)", ic[0].x, ic[0].y, ic[last].x, ic[last].y);
8246 if (!c)
8247 return &ic[0];
8248 if (ic[0].x == c->x && ic[0].y == c->y)
8249 return &ic[last];
8250 if (ic[last].x == c->x && ic[last].y == c->y)
8251 return &ic[0];
8252 }
8253 return NULL;
8254 }
8255
8256 struct turn_restriction
8257 {
8258 osmid relid;
8259 enum item_type type;
8260 struct coord *c[3];
8261 int c_count[3];
8262 };
8263
8264 static void process_turn_restrictions_member(void *func_priv, void *relation_priv, struct item_bin *member, void *member_priv)
8265 {
8266 int count, type = (long) member_priv;
8267 struct turn_restriction *turn_restriction = relation_priv;
8268 struct coord *c = (struct coord *) (member + 1);
8269 int ccount = member->clen / 2;
8270
8271 if (member->type < type_line)
8272 {
8273 count = 1;
8274 }
8275 else
8276 {
8277 count = 2;
8278 }
8279
8280 turn_restriction->c[type]=g_renew(struct coord, turn_restriction->c[type], turn_restriction->c_count[type]+count);
8281 turn_restriction->c[type][turn_restriction->c_count[type]++] = c[0];
8282
8283 if (count > 1)
8284 {
8285 turn_restriction->c[type][turn_restriction->c_count[type]++] = c[ccount - 1];
8286 }
8287 }
8288
8289 static void process_turn_restrictions_fromto(struct turn_restriction *t, int type, struct coord **c)
8290 {
8291 int i, j;
8292 for (i = 0; i < t->c_count[type]; i += 2)
8293 {
8294 for (j = 0; j < t->c_count[1]; j++)
8295 {
8296 if (coord_is_equal(t->c[type][i], t->c[1][j]))
8297 {
8298 c[0] = &t->c[type][i + 1];
8299 c[1] = &t->c[type][i];
8300 return;
8301 }
8302 if (coord_is_equal(t->c[type][i + 1], t->c[1][j]))
8303 {
8304 c[0] = &t->c[type][i];
8305 c[1] = &t->c[type][i + 1];
8306 return;
8307 }
8308 }
8309 }
8310 }
8311
8312 static void process_turn_restrictions_dump_coord(struct coord *c, int count)
8313 {
8314 int i;
8315 for (i = 0; i < count; i++)
8316 {
8317 fprintf(stderr, "(0x%x,0x%x)", c[i].x, c[i].y);
8318 }
8319 }
8320
8321 static void process_turn_restrictions_finish(GList *tr, FILE *out)
8322 {
8323 GList *l = tr;
8324 while (l)
8325 {
8326 struct turn_restriction *t = l->data;
8327 struct coord *c[4];
8328 struct item_bin *ib = item_bin_2;
8329
8330 // init array
8331 c[0] = NULL;
8332 c[1] = NULL;
8333 c[2] = NULL;
8334 c[3] = NULL;
8335
8336 if (!t->c_count[0])
8337 {
8338 //osm_warning("relation",t->relid,0,"turn restriction: from member not found\n");
8339 }
8340 else if (!t->c_count[1])
8341 {
8342 //osm_warning("relation",t->relid,0,"turn restriction: via member not found\n");
8343 }
8344 else if (!t->c_count[2])
8345 {
8346 //osm_warning("relation",t->relid,0,"turn restriction: to member not found\n");
8347 }
8348 else
8349 {
8350 process_turn_restrictions_fromto(t, 0, c);
8351 process_turn_restrictions_fromto(t, 2, c + 2);
8352 if (!c[0] || !c[2])
8353 {
8354 //osm_warning("relation",t->relid,0,"turn restriction: via (");
8355 //process_turn_restrictions_dump_coord(t->c[1], t->c_count[1]);
8356 //fprintf(stderr,")");
8357 if (!c[0])
8358 {
8359 //fprintf(stderr," failed to connect to from (");
8360 //process_turn_restrictions_dump_coord(t->c[0], t->c_count[0]);
8361 //fprintf(stderr,")");
8362 }
8363 if (!c[2])
8364 {
8365 //fprintf(stderr," failed to connect to to (");
8366 //process_turn_restrictions_dump_coord(t->c[2], t->c_count[2]);
8367 //fprintf(stderr,")");
8368 }
8369 //fprintf(stderr,"\n");
8370 }
8371 else
8372 {
8373 if (t->c_count[1] <= 2)
8374 {
8375 item_bin_init(ib, t->type);
8376 item_bin_add_coord(ib, c[0], 1);
8377 item_bin_add_coord(ib, c[1], 1);
8378 if (t->c_count[1] > 1)
8379 {
8380 item_bin_add_coord(ib, c[3], 1);
8381 }
8382 item_bin_add_coord(ib, c[2], 1);
8383 item_bin_write(ib, out);
8384 }
8385
8386 }
8387 }
8388 g_free(t);
8389 l = g_list_next(l);
8390 }
8391 g_list_free(tr);
8392 }
8393
8394 static GList *
8395 process_turn_restrictions_setup(FILE *in, struct relations *relations)
8396 {
8397 struct relation_member fromm, tom, viam, tmpm;
8398 long long relid;
8399 struct item_bin *ib;
8400 struct relations_func *relations_func;
8401 int min_count;
8402 GList *turn_restrictions = NULL;
8403
8404 fseek(in, 0, SEEK_SET);
8405 relations_func = relations_func_new(process_turn_restrictions_member, NULL);
8406 while ((ib = read_item(in, 0)))
8407 {
8408 struct turn_restriction *turn_restriction=g_new0(struct turn_restriction, 1);
8409 relid = item_bin_get_relationid(ib);
8410 turn_restriction->relid = relid;
8411 turn_restriction->type = ib->type;
8412 min_count = 0;
8413 if (!search_relation_member(ib, "from", &fromm, &min_count))
8414 {
8415 osm_warning("relation", relid, 0, "turn restriction: from member missing\n");
8416 continue;
8417 }
8418 if (search_relation_member(ib, "from", &tmpm, &min_count))
8419 {
8420 osm_warning("relation", relid, 0, "turn restriction: multiple from members\n");
8421 continue;
8422 }
8423 min_count = 0;
8424 if (!search_relation_member(ib, "to", &tom, &min_count))
8425 {
8426 osm_warning("relation", relid, 0, "turn restriction: to member missing\n");
8427 continue;
8428 }
8429 if (search_relation_member(ib, "to", &tmpm, &min_count))
8430 {
8431 osm_warning("relation", relid, 0, "turn restriction: multiple to members\n");
8432 continue;
8433 }
8434 min_count = 0;
8435 if (!search_relation_member(ib, "via", &viam, &min_count))
8436 {
8437 osm_warning("relation", relid, 0, "turn restriction: via member missing\n");
8438 continue;
8439 }
8440 if (search_relation_member(ib, "via", &tmpm, &min_count))
8441 {
8442 osm_warning("relation", relid, 0, "turn restriction: multiple via member\n");
8443 continue;
8444 }
8445 if (fromm.type != 2)
8446 {
8447 osm_warning("relation", relid, 0, "turn restriction: wrong type for from member ");
8448 osm_warning(osm_types[fromm.type], fromm.id, 1, "\n");
8449 continue;
8450 }
8451 if (tom.type != 2)
8452 {
8453 osm_warning("relation", relid, 0, "turn restriction: wrong type for to member ");
8454 osm_warning(osm_types[tom.type], tom.id, 1, "\n");
8455 continue;
8456 }
8457 if (viam.type != 1 && viam.type != 2)
8458 {
8459 osm_warning("relation", relid, 0, "turn restriction: wrong type for via member ");
8460 osm_warning(osm_types[viam.type], viam.id, 1, "\n");
8461 continue;
8462 }
8463 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 0, fromm.type, fromm.id);
8464 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 1, viam.type, viam.id);
8465 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 2, tom.type, tom.id);
8466 turn_restrictions = g_list_append(turn_restrictions, turn_restriction);
8467 }
8468 return turn_restrictions;
8469 }
8470
8471 void process_turn_restrictions(FILE *in, FILE *coords, FILE *ways, FILE *ways_index, FILE *out)
8472 {
8473 struct relations *relations = relations_new();
8474 GList *turn_restrictions;
8475 fseek(in, 0, SEEK_SET);
8476 if (verbose_mode)
8477 fprintf(stderr, "process_turn_restrictions A1\n");
8478 turn_restrictions = process_turn_restrictions_setup(in, relations);
8479 if (verbose_mode)
8480 fprintf(stderr, "process_turn_restrictions B2\n");
8481 relations_process(relations, coords, ways, NULL);
8482 if (verbose_mode)
8483 fprintf(stderr, "process_turn_restrictions C3\n");
8484 process_turn_restrictions_finish(turn_restrictions, out);
8485 if (verbose_mode)
8486 fprintf(stderr, "process_turn_restrictions D4\n");
8487 }
8488
8489 static void node_ref_way(osmid node, int local_thread_num)
8490 {
8491 struct node_item *ni;
8492 ni = node_item_get_fast(node, local_thread_num);
8493
8494 if (ni)
8495 {
8496 // fprintf(stderr, "node id=%lld ref=%d\n", node, (ni->ref_way + 1));
8497 ni->ref_way++;
8498 }
8499 }
8500
8501 static void nodes_ref_item_bin(struct item_bin *ib, int local_thread_num, osmid this_way_id)
8502 {
8503 int i;
8504 int *node_num_offset;
8505 int offset = 0;
8506 osmid way_node;
8507 struct coord *c = (struct coord *) (ib + 1); // set pointer to coord struct of this item
8508
8509 for (i = 0; i < ib->clen / 2; i++)
8510 {
8511 // fprintf(stderr, "1 i=%d this_way_id=%lld\n", i, this_way_id);
8512
8513 // offset for split cycleways!
8514 node_num_offset = item_bin_get_attr(ib, attr_debugsplitway, NULL);
8515
8516 if (node_num_offset)
8517 {
8518 offset = (int)*node_num_offset;
8519 }
8520
8521 way_node = get_waynode_num(this_way_id, (i + offset), local_thread_num);
8522 // fprintf(stderr, "2 ref2=%lld this_way_id=%lld offset=%d\n", way_node, this_way_id, offset);
8523 node_ref_way(way_node, local_thread_num);
8524 // fprintf(stderr, "3 ref2=%lld this_way_id=%lld\n", way_node, this_way_id);
8525 }
8526
8527 //fprintf(stderr,"********DUMP rw ***********\n");
8528 // dump_itembin(ib);
8529 //fprintf(stderr,"********DUMP rw ***********\n");
8530 }
8531
8532 osmid get_waynode_num(osmid way_id, int coord_num, int local_thread_num)
8533 {
8534 int rc2;
8535 sqlite3_stmt *st;
8536 osmid nd;
8537 long long seekpos_waynode2;
8538 #define __USE_WAYNODEFILE_CACHE__ 1
8539
8540 //fprintf(stderr, "get_waynode_num:w=%lld cnum=%d\n", way_id, (coord_num + 1));
8541
8542 if (last_seek_wayid[local_thread_num] != way_id)
8543 {
8544
8545 last_seek_wayid[local_thread_num] = way_id;
8546
8547 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
8548 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
8549 {
8550 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
8551 {
8552 #endif
8553 st = stmt_sel0012_tt[local_thread_num];
8554 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
8555 }
8556 else
8557 {
8558 st = stmt_sel0012b_tt[local_thread_num];
8559 }
8560 }
8561 else
8562 {
8563 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
8564 {
8565 st = stmt_sel0012__2_tt[local_thread_num];
8566 }
8567 else
8568 {
8569 st = stmt_sel0012__2b_tt[local_thread_num];
8570 }
8571 }
8572 #endif
8573
8574 sqlite3_bind_int64(st, 1, way_id);
8575 sqlite3_bind_int(st, 2, (int)1);
8576
8577 // execute the statement
8578 rc2 = sqlite3_step(st);
8579
8580 switch (rc2)
8581 {
8582 case SQLITE_DONE:
8583 break;
8584 case SQLITE_ROW:
8585 seekpos_waynode[local_thread_num] = sqlite3_column_int64(st, 1);
8586 break;
8587 default:
8588 fprintf(stderr, "Error: %d\n", rc2);
8589 break;
8590 }
8591 sqlite3_reset(st);
8592 }
8593
8594 seekpos_waynode2 = seekpos_waynode[local_thread_num] + (sizeof(osmid) * coord_num); // seek to coord in way
8595
8596 if ((! __USE_WAYNODEFILE_CACHE__) || (seekpos_waynode2 != last_seekpos_waynode[local_thread_num]))
8597 {
8598 //fprintf(stderr, "w seek3 seekpos_waynode=%lld last_seekpos_waynode=%lld\n", seekpos_waynode[local_thread_num], last_seekpos_waynode[local_thread_num]);
8599 fseeko(ways_ref_file_thread[local_thread_num], (off_t)seekpos_waynode2, SEEK_SET);
8600 }
8601 //else
8602 //{
8603 // fprintf(stderr, "w seek3:NO SEEK\n");
8604 //}
8605
8606 int fret = (int)fread(&nd, sizeof(osmid), 1, ways_ref_file_thread[local_thread_num]);
8607 if (fret == 0)
8608 {
8609 fprintf(stderr, "**ERROR** at fread 002b: wayid:%lld count=%d\n", way_id, coord_num);
8610 }
8611 else if ((osmid)nd > 4994968164L)
8612 {
8613 fprintf(stderr, "**ERROR** at fread 002b: wayid:%lld count=%d w_node=%lld\n", way_id, coord_num, (osmid)nd);
8614 }
8615
8616 last_seekpos_waynode[local_thread_num] = seekpos_waynode2 + sizeof(osmid);
8617 //fprintf(stderr, "get_waynode_num:result node=%lld last_seekpos_waynode=%lld\n", nd, last_seekpos_waynode[local_thread_num]);
8618
8619 return nd;
8620 }
8621
8622
8623 osmid get_waynode_num_have_seekpos(osmid way_id, int coord_num, int local_thread_num, off_t seek_pos)
8624 {
8625 int rc2;
8626 sqlite3_stmt *st;
8627 osmid nd;
8628 long long seekpos_waynode2;
8629 #define __USE_WAYNODEFILE_CACHE__ 1
8630
8631 //fprintf(stderr, "get_waynode_num:w=%lld cnum=%d\n", way_id, (coord_num + 1));
8632
8633 if (last_seek_wayid[local_thread_num] != way_id)
8634 {
8635 last_seek_wayid[local_thread_num] = way_id;
8636 }
8637
8638 seekpos_waynode[local_thread_num] = seek_pos;
8639 seekpos_waynode2 = seekpos_waynode[local_thread_num] + (sizeof(osmid) * coord_num); // seek to coord in way
8640
8641 if ((! __USE_WAYNODEFILE_CACHE__) || (seekpos_waynode2 != last_seekpos_waynode[local_thread_num]))
8642 {
8643 //fprintf(stderr, "w seek3 seekpos_waynode=%lld last_seekpos_waynode=%lld\n", seekpos_waynode[local_thread_num], last_seekpos_waynode[local_thread_num]);
8644 fseeko(ways_ref_file_thread[local_thread_num], (off_t)seekpos_waynode2, SEEK_SET);
8645 }
8646 //else
8647 //{
8648 // fprintf(stderr, "w seek3:NO SEEK\n");
8649 //}
8650
8651 int fret = (int)fread(&nd, sizeof(osmid), 1, ways_ref_file_thread[local_thread_num]);
8652 if (fret == 0)
8653 {
8654 fprintf(stderr, "**ERROR** at fread 002b: wayid:%lld count=%d\n", way_id, coord_num);
8655 }
8656 else if ((osmid)nd > 4994968164L)
8657 {
8658 fprintf(stderr, "**ERROR** at fread 002b: wayid:%lld count=%d w_node=%lld\n", way_id, coord_num, (osmid)nd);
8659 }
8660
8661
8662 last_seekpos_waynode[local_thread_num] = seekpos_waynode2 + sizeof(osmid);
8663 // fprintf(stderr, "get_waynode_num:result node=%lld last_seekpos_waynode=%lld\n", nd, last_seekpos_waynode[local_thread_num]);
8664
8665 return nd;
8666 }
8667
8668
8669 void add_waynode_to_db(osmid ref, int c_count)
8670 {
8671 // ------- save way node to SQL db ------------
8672 #ifdef MAPTOOL_USE_SQL
8673
8674 if (sql_counter4 == 0)
8675 {
8676 sqlite3_exec(sql_handle004, "BEGIN", 0, 0, 0);
8677 sqlite3_exec(sql_handle005, "BEGIN", 0, 0, 0);
8678 sqlite3_exec(sql_handle006, "BEGIN", 0, 0, 0);
8679 sqlite3_exec(sql_handle007, "BEGIN", 0, 0, 0);
8680 //fprintf(stderr, "begin: way nodes\n");
8681 }
8682 sql_counter4++;
8683 //fprintf(stderr, "insert #WN:wid=%lld ccount=%d ref=%lld\n", osmid_attr_value, c_count + 1, ref);
8684
8685 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
8686 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
8687 {
8688 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
8689 {
8690 #endif
8691 sqlite3_bind_int64(stmt_way_node, 1, osmid_attr_value);
8692 sqlite3_bind_int64(stmt_way_node, 2, ref);
8693 sqlite3_bind_int(stmt_way_node, 3, c_count + 1);
8694 sqlite3_bind_int64(stmt_way_node, 4, seekpos1);
8695 sqlite3_step(stmt_way_node);
8696 sqlite3_reset(stmt_way_node);
8697 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
8698 }
8699 else
8700 {
8701 sqlite3_bind_int64(stmt_way_nodeb, 1, osmid_attr_value);
8702 sqlite3_bind_int64(stmt_way_nodeb, 2, ref);
8703 sqlite3_bind_int(stmt_way_nodeb, 3, c_count + 1);
8704 sqlite3_bind_int64(stmt_way_nodeb, 4, seekpos1);
8705 sqlite3_step(stmt_way_nodeb);
8706 sqlite3_reset(stmt_way_nodeb);
8707 }
8708 }
8709 else
8710 {
8711 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
8712 {
8713 sqlite3_bind_int64(stmt_way_node__2, 1, osmid_attr_value);
8714 sqlite3_bind_int64(stmt_way_node__2, 2, ref);
8715 sqlite3_bind_int(stmt_way_node__2, 3, c_count + 1);
8716 sqlite3_bind_int64(stmt_way_node__2, 4, seekpos1);
8717 sqlite3_step(stmt_way_node__2);
8718 sqlite3_reset(stmt_way_node__2);
8719 }
8720 else
8721 {
8722 sqlite3_bind_int64(stmt_way_node__2b, 1, osmid_attr_value);
8723 sqlite3_bind_int64(stmt_way_node__2b, 2, ref);
8724 sqlite3_bind_int(stmt_way_node__2b, 3, c_count + 1);
8725 sqlite3_bind_int64(stmt_way_node__2b, 4, seekpos1);
8726 sqlite3_step(stmt_way_node__2b);
8727 sqlite3_reset(stmt_way_node__2b);
8728 }
8729 }
8730 #endif
8731
8732 if (sql_counter4 > MAX_ROWS_WO_COMMIT_4)
8733 {
8734 sqlite3_exec(sql_handle004, "COMMIT", 0, 0, 0);
8735 sqlite3_exec(sql_handle005, "COMMIT", 0, 0, 0);
8736 sqlite3_exec(sql_handle006, "COMMIT", 0, 0, 0);
8737 sqlite3_exec(sql_handle007, "COMMIT", 0, 0, 0);
8738 //fprintf(stderr, "COMMIT:way nodes: %lld\n", sql_counter4);
8739 sql_counter4 = 0;
8740 }
8741 #endif
8742 // ------- save way node to SQL db ------------
8743
8744
8745 }
8746
8747 void osm_add_nd(osmid ref)
8748 {
8749 // SET_REF(&coord_buffer[coord_count], ref);
8750
8751 //fprintf(stderr, "osm_add_nd:result way=%lld node=%lld\n", osmid_attr_value, ref);
8752
8753 // put lat, lon into &coord_buffer[coord_count] -------- START ----------
8754 struct coord *c = &coord_buffer[coord_count];
8755 c->x = REF_X;
8756 // c->y = REF_Y;
8757 c->y = coord_count; // number of this node in the way (starting at zero)
8758 // put lat, lon into &coord_buffer[coord_count] -------- START ----------
8759
8760 if (coord_count == 0)
8761 {
8762 first_node_of_current_way = ref;
8763 //fprintf(stderr, "w ftell1\n");
8764 seekpos1 = (long long)ftello(ways_ref_file); // 64bit
8765 }
8766
8767 //fprintf(stderr, "write:wid=%lld nd=%lld seekpos1=%lld\n", osmid_attr_value, ref, seekpos1);
8768 fwrite(&ref, sizeof(osmid), 1, ways_ref_file); // write way node to ref file
8769 //ftello(ways_ref_file); // --> sometimes this is needed!!
8770 //fprintf(stderr, "coord_count=%d filepos after write=%lld\n", coord_count, (long long)ff);
8771
8772 coord_count++;
8773 if (coord_count > 65536)
8774 {
8775 fprintf(stderr, "ERROR: Overflow\n");
8776 exit(1);
8777 }
8778 }
8779
8780 static void write_item_part(FILE *out, FILE *out_index, FILE *out_graph, struct item_bin *orig, int first, int last, long long *last_id)
8781 {
8782 struct item_bin new;
8783 struct coord *c = (struct coord *) (orig + 1);
8784 char *attr = (char *) (c + orig->clen / 2);
8785 int attr_len = orig->len - orig->clen - 2;
8786 processed_ways++;
8787 new.type = orig->type;
8788 new.clen = (last - first + 1) * 2;
8789 new.len = new.clen + attr_len + 2;
8790
8791 if (out_index)
8792 {
8793 long long idx[2];
8794 idx[0] = item_bin_get_wayid(orig);
8795 idx[1] = ftello(out);
8796 if (way_hash_cfu)
8797 {
8798 //if (!(g_hash_table_lookup_extended(way_hash, (gpointer) (long) idx[0], NULL, NULL)))
8799 //{
8800 // g_hash_table_insert(way_hash, (gpointer) (long) idx[0], (gpointer) (long) idx[1]);
8801 // // fprintf(stderr,"way_hash size="LONGLONG_FMT"\n", g_hash_table_size(way_hash));
8802 //}
8803
8804 if (!cfuhash_exists_data(way_hash_cfu, (long long) idx[0], sizeof(long long)))
8805 {
8806 cfuhash_put_data(way_hash_cfu, (long long) idx[0], sizeof(long long), (int) idx[1], sizeof(int), NULL);
8807 }
8808 }
8809 else
8810 {
8811 if (!last_id || *last_id != idx[0])
8812 {
8813 fwrite(idx, sizeof(idx), 1, out_index);
8814 }
8815
8816 if (last_id)
8817 {
8818 *last_id = idx[0];
8819 }
8820 }
8821
8822 }
8823
8824 fwrite(&new, sizeof(new), 1, out);
8825 fwrite(c + first, new.clen * 4, 1, out);
8826 fwrite(attr, attr_len * 4, 1, out);
8827 }
8828
8829 void ref_ways(FILE *in, int local_thread_num)
8830 {
8831 struct item_bin *ib;
8832
8833 long long ways_count = 0;
8834 osmid *this_way_id;
8835
8836 // ---------- reset the cached values ----------
8837 seekpos_waynode[local_thread_num] = -1;
8838 last_seekpos_waynode[local_thread_num] = -1;
8839 last_seek_wayid[local_thread_num] = -1;
8840 fseeko(ways_ref_file_thread[local_thread_num], (off_t)0, SEEK_SET);
8841 // ---------- reset the cached values ----------
8842
8843 fseek(in, 0, SEEK_SET);
8844 while ((ib = read_item(in, local_thread_num))) // loop all "ways" from file "in"
8845 {
8846 this_way_id = item_bin_get_attr(ib, attr_osm_wayid, NULL);
8847
8848 if (this_way_id)
8849 {
8850 nodes_ref_item_bin(ib, local_thread_num, *this_way_id);
8851 }
8852 else
8853 {
8854 // no "way id"? so we do nothing
8855 // nodes_ref_item_bin(ib, local_thread_num, 0);
8856 }
8857 ways_count++;
8858
8859 if ((ways_count % 1000000) == 0)
8860 {
8861 fprintf_(stderr, "[THREAD] #%d ways: %lld\n", local_thread_num, ways_count);
8862 }
8863 }
8864 }
8865
8866
8867 /**
8868 * Get POI coordinates from line coordinates.
8869 * @param in *in input file with line coordinates.
8870 * @param in *out output file with POI coordinates
8871 * @param in type input file original contents type: type_line or type_area
8872 * @returns nothing
8873 */
8874 void process_way2poi_housenumber(FILE *in, FILE *out)
8875 {
8876 struct item_bin *ib;
8877 // char *house_number = NULL;
8878 // char *street_name = NULL;
8879
8880 while ((ib = read_item(in, 0)))
8881 {
8882 int count = ib->clen / 2;
8883 if (count > 1 && ib->type == type_poly_building)
8884 {
8885 struct coord *c = (struct coord *) (ib + 1), c1, c2;
8886 int done = 0;
8887 //if (ib->type == type_area) // --> buildings are "lines" now, not polys
8888 //{
8889 if (count < 3)
8890 {
8891 osm_warning("way", item_bin_get_wayid(ib), 0, "Broken polygon, less than 3 points defined\n");
8892 }
8893 else if (!geom_poly_centroid(c, count, &c1))
8894 {
8895 osm_warning("way", item_bin_get_wayid(ib), 0, "Broken polygon, area is 0\n");
8896 }
8897 else
8898 {
8899 if (geom_poly_point_inside(c, count, &c1))
8900 {
8901 //fprintf(stderr, "geom_poly_point_inside\n");
8902 c[0] = c1;
8903 }
8904 else
8905 {
8906 //fprintf(stderr, "geom_poly_closest_point\n");
8907 geom_poly_closest_point(c, count, &c1, &c2);
8908 c[0] = c2;
8909 }
8910 done = 1;
8911 }
8912 //}
8913
8914 if (!done)
8915 {
8916 //fprintf(stderr, "geom_line_middle\n");
8917 geom_line_middle(c, count, &c1);
8918 c[0] = c1;
8919 }
8920
8921 char *street_name = item_bin_get_attr(ib, attr_street_name_dummy, NULL);
8922 char *house_number = item_bin_get_attr(ib, attr_house_number_dummy, NULL);
8923 if ((street_name != NULL) && (house_number != NULL))
8924 {
8925 osm_append_housenumber_node(out, c, house_number, street_name);
8926 }
8927 }
8928 }
8929 }
8930
8931 void fill_hash_waytag(void)
8932 {
8933 if (verbose_mode)
8934 fprintf(stderr, "fill_hash_waytag - START\n");
8935 if (waytag_hash)
8936 {
8937 g_hash_table_destroy(waytag_hash);
8938 waytag_hash = NULL;
8939 }
8940 waytag_hash = g_hash_table_new(NULL, NULL);
8941 waytag_buffer_to_hash();
8942 if (verbose_mode)
8943 fprintf(stderr, "fill_hash_waytag - END\n");
8944 }
8945
8946 void append_pre_resolved_ways(FILE *out, struct maptool_osm *osm2)
8947 {
8948 struct item_bin *ib;
8949
8950 // fseek(out, 0, SEEK_END); // seek to the end of current way-file
8951 rewind(osm2->ways_with_coords); // start with the first "way"
8952
8953 while ((ib = read_item(osm2->ways_with_coords, 0))) // loop thru all "ways" from file "in"
8954 {
8955 // write way to outfile
8956 item_bin_write(ib, out);
8957 }
8958 }
8959
8960 int copy_tags_to_ways(FILE *in, FILE *out, FILE *tags_in)
8961 {
8962 struct item_bin *ib;
8963
8964 long long slice_size2 = sizeof(struct way_tag) * 1024 * 1024; // xx MByte
8965 long long final_slice_size2;
8966 struct way_tag *wt3;
8967 int slices2 = 0;
8968 int i;
8969 struct stat st;
8970 int final_slice = 0;
8971 long long size2;
8972
8973 fseek(tags_in, 0, SEEK_END);
8974 size2 = ftello(tags_in); // 64bit
8975
8976 slices2 = (size2 / slice_size2) + 1;
8977 final_slice_size2 = size2 - ((slices2 - 1) * slice_size2);
8978
8979 if (slices2 == 1)
8980 {
8981 final_slice_size2 = size2;
8982 }
8983
8984 if (verbose_mode) fprintf(stderr, "relationsfile size="LONGLONG_FMT" bytes\n", size2);
8985 if (verbose_mode) fprintf(stderr, "final_slice_size2="LONGLONG_FMT" bytes\n", final_slice_size2);
8986 if (verbose_mode)
8987 fprintf(stderr, "slices=%d\n", slices2);
8988
8989 for (i = 0; i < slices2; i++)
8990 {
8991 if (i == (slices2 - 1))
8992 {
8993 final_slice = 1;
8994 }
8995
8996 if (final_slice == 1)
8997 {
8998 load_buffer_fp(tags_in, &waytag_buffer, i * slice_size2, final_slice_size2); // load in the relations
8999 }
9000 else
9001 {
9002 load_buffer_fp(tags_in, &waytag_buffer, i * slice_size2, slice_size2); // load in the relations
9003 }
9004 fill_hash_waytag();
9005 struct way_tag *wt2 = (struct way_tag *) (waytag_buffer.base);
9006
9007 rewind(in); // start with the first "way"
9008 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9009 {
9010 if (waytag_hash)
9011 {
9012 int tag_start_pointer;
9013 long long wid = item_bin_get_wayid(ib);
9014
9015 //fprintf(stderr,"looking for wid=%lu\n", wid);
9016
9017 long long p_tmp = 0;
9018 int ret = 0;
9019 ret = g_hash_table_lookup_extended(waytag_hash, (gpointer) (long long) wid, NULL, (gpointer) & p_tmp);
9020 //fprintf(stderr,"ret=%d\n", ret);
9021 if (ret == 1)
9022 {
9023 // only if wayid is found in hash!
9024 tag_start_pointer = (int) (long long) (p_tmp);
9025 wt3 = wt2 + tag_start_pointer;
9026
9027 if ((long long) wt3->way_id == (long long) wid)
9028 {
9029 if (wt3->tag_id == 1)
9030 {
9031 // add "type" to "way"
9032 // ## char tag[strlen("waterway") + strlen("riverbank") + 2];
9033 // ## sprintf(tag, "%s=%s", "waterway", "riverbank");
9034 // ## item_bin_add_attr_string(ib, attr_osm_tag, tag);
9035 if (item_bin_is_closed_poly(ib) == 1)
9036 {
9037 item_bin_set_type(ib, type_poly_water_from_relations);
9038 }
9039 else
9040 {
9041 item_bin_set_type(ib, type_water_river);
9042 }
9043 }
9044 else if (wt3->tag_id == 2)
9045 {
9046 if (item_bin_is_closed_poly(ib) == 1)
9047 {
9048 item_bin_set_type(ib, type_poly_water_from_relations);
9049 }
9050 else
9051 {
9052 item_bin_set_type(ib, type_water_river);
9053 }
9054 }
9055 else if (wt3->tag_id == 4)
9056 {
9057 if (item_bin_is_closed_poly(ib) == 1)
9058 {
9059 item_bin_set_type(ib, type_poly_water_from_relations);
9060 }
9061 else
9062 {
9063 item_bin_set_type(ib, type_water_river);
9064 }
9065 }
9066 else if (wt3->tag_id == 5)
9067 {
9068 if (item_bin_is_closed_poly(ib) == 1)
9069 {
9070 item_bin_set_type(ib, type_poly_wood_from_triang);
9071 }
9072 else
9073 {
9074 //fprintf(stderr, "wood_from_relations(5)\n");
9075 item_bin_set_type(ib, type_wood_from_relations);
9076 }
9077 }
9078 else if (wt3->tag_id == 6)
9079 {
9080 if (item_bin_is_closed_poly(ib) == 1)
9081 {
9082 item_bin_set_type(ib, type_poly_wood_from_triang);
9083 }
9084 else
9085 {
9086 //fprintf(stderr, "wood_from_relations(6)\n");
9087 item_bin_set_type(ib, type_wood_from_relations);
9088 }
9089 }
9090 else if (wt3->tag_id == 7)
9091 {
9092 // "inner" way
9093 //fprintf(stderr, "wood_from_relations(7)\n");
9094 item_bin_set_type(ib, type_wood_from_relations);
9095 }
9096 else if (wt3->tag_id == 8)
9097 {
9098 // "inner" way
9099 //fprintf(stderr, "wood_from_relations(8)\n");
9100 item_bin_set_type(ib, type_wood_from_relations);
9101 }
9102 }
9103 }
9104 }
9105 // write way to outfile
9106 item_bin_write(ib, out);
9107 }
9108
9109 }
9110 }
9111
9112 /* filecopy: copy file ifp to file ofp */
9113 void filecopy(FILE *ifp, FILE *ofp)
9114 {
9115 int c;
9116 while ((c = getc(ifp)) != EOF)
9117 {
9118 putc(c, ofp);
9119 }
9120 }
9121
9122
9123
9124 #include "osm_s_index.h"
9125
9126
9127
9128
9129
9130
9131
9132 void remove_attr_str(struct item_bin *ib, int attr_type)
9133 {
9134 char *attr_str = item_bin_get_attr(ib, attr_type, NULL);
9135 if (attr_str)
9136 {
9137 item_bin_remove_attr(ib, attr_str);
9138 }
9139 }
9140
9141 int remove_useless_ways(FILE *in, FILE *out)
9142 {
9143 struct item_bin *ib;
9144 long long *wayid;
9145 int *dup;
9146
9147 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9148 {
9149 if (ib->type != type_street_unkn) // remove unknown streets here
9150 {
9151 // write way to outfile
9152 item_bin_write(ib, out);
9153 }
9154 }
9155 }
9156
9157 int remove_useless_tags_from_ways(FILE *in, FILE *out)
9158 {
9159 struct item_bin *ib;
9160 long long *wayid;
9161 int *dup;
9162
9163 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9164 {
9165 // attr_street_name_dummy
9166 remove_attr_str(ib, attr_street_name_dummy);
9167 // attr_house_number_dummy
9168 remove_attr_str(ib, attr_house_number_dummy);
9169 // attr_debug
9170 remove_attr_str(ib, attr_debug);
9171
9172 // osm wayid
9173 //wayid = item_bin_get_attr(ib, attr_osm_wayid, NULL);
9174 //if (wayid)
9175 //{
9176 // item_bin_remove_attr(ib, wayid);
9177 //}
9178
9179 // duplicate_way
9180 dup = item_bin_get_attr(ib, attr_duplicate_way, NULL);
9181 if (dup)
9182 {
9183 item_bin_remove_attr(ib, dup);
9184 //fprintf(stderr, "attr_duplicate_way:2: dup=true wayid=%lld\n", item_bin_get_wayid(ib));
9185 }
9186
9187 // write way to outfile
9188 item_bin_write(ib, out);
9189 }
9190 }
9191
9192 int remove_useless_tags_from_nodes(FILE *in, FILE *out)
9193 {
9194 struct item_bin *ib;
9195 long long *nodeid;
9196
9197 while ((ib = read_item(in, 0))) // loop thru all "nodes" from file "in"
9198 {
9199 // attr_debug
9200 remove_attr_str(ib, attr_debug);
9201
9202 // osm nodeid
9203 nodeid = item_bin_get_attr(ib, attr_osm_nodeid, NULL);
9204 if (nodeid)
9205 {
9206 item_bin_remove_attr(ib, nodeid);
9207 }
9208
9209 // write node to outfile
9210 item_bin_write(ib, out);
9211 }
9212 }
9213
9214 void map_find_housenumbers_interpolation(FILE *in, FILE *out)
9215 {
9216 int ccount;
9217 struct item_bin *ib;
9218 long long ways_count = 0;
9219 struct coord c;
9220
9221 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9222 {
9223 ways_count++;
9224 if ((ways_count % 10000000) == 0)
9225 {
9226 fprintf_(stderr, "ways: %lld\n", ways_count);
9227 }
9228
9229 // we need at least 2 points in way
9230 if (ccount <= 1)
9231 {
9232 continue;
9233 }
9234
9235 // only type "house_number_interpolate*"
9236 if (
9237 (ib->type != type_house_number_interpolation_even) &&
9238 (ib->type != type_house_number_interpolation_odd) &&
9239 (ib->type != type_house_number_interpolation_all) &&
9240 (ib->type != type_house_number_interpolation_alphabetic)
9241 )
9242 {
9243 continue;
9244 }
9245
9246 ccount = ib->clen / 2;
9247
9248 // set coord of housenumber here
9249 c.x = 0;
9250 c.y = 0;
9251 // set coord of housenumber here
9252 char *street_name = item_bin_get_attr(ib, attr_street_name_dummy, NULL);
9253 char *house_number = item_bin_get_attr(ib, attr_house_number_dummy, NULL);
9254 if ((street_name != NULL) && (house_number != NULL))
9255 {
9256 //fprintf(stderr, "ADDR:Interpolate:%s, %s\n", house_number, street_name);
9257 osm_append_housenumber_node(out, &c, house_number, street_name);
9258 }
9259 }
9260 }
9261
9262 int map_find_intersections(FILE *in, FILE *out, FILE *out_index, FILE *out_graph, FILE *out_coastline, int final)
9263 {
9264 struct coord *c;
9265 int i, ccount, last, remaining;
9266 osmid ndref;
9267 struct item_bin *ib;
9268 struct node_item *ni;
9269 long long last_id = 0;
9270 long long ways_count = 0;
9271 osmid *this_way_id;
9272 osmid this_way_id_real;
9273 int i_real = -1;
9274
9275 processed_nodes = 0;
9276 processed_nodes_out = 0;
9277 processed_ways = 0;
9278 processed_relations = 0;
9279 processed_tiles = 0;
9280
9281
9282 // ---------- reset the cached values ----------
9283 seekpos_waynode[0] = -1;
9284 last_seekpos_waynode[0] = -1;
9285 last_seek_wayid[0] = -1;
9286 fseeko(ways_ref_file_thread[0], (off_t)0, SEEK_SET);
9287 // ---------- reset the cached values ----------
9288
9289
9290 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9291 {
9292
9293 this_way_id = item_bin_get_attr(ib, attr_osm_wayid, NULL);
9294 if (this_way_id)
9295 {
9296 this_way_id_real = *this_way_id;
9297 // fprintf(stderr,"wayid:%lld type:0x%x len:%d clen:%d\n", *this_way_id, ib->type, ib->len, (ib->clen / 2));
9298 }
9299
9300 ccount = ib->clen / 2;
9301
9302 if (ccount <= 1)
9303 {
9304 continue;
9305 }
9306
9307 //fprintf(stderr,"********DUMP ww1***********\n");
9308 //dump_itembin(ib);
9309 //fprintf(stderr,"********DUMP ww1***********\n");
9310
9311
9312 ways_count++;
9313 if ((ways_count % 10000000) == 0)
9314 {
9315 fprintf_(stderr, "ways: %lld\n", ways_count);
9316 }
9317
9318 c = (struct coord *) (ib + 1);
9319 last = 0;
9320 i_real = -1;
9321 for (i = 0; i < ccount; i++) // loop thru all the coordinates (nodes) of this way
9322 {
9323 //if (this_way_id_real != 0)
9324 //{
9325 // fprintf(stderr, "this_way_id_real=%lld this_way_id=%lld i=%d ccount=%d\n", this_way_id_real, *this_way_id, i, ccount);
9326 //}
9327
9328 if (IS_REF(c[i]))
9329 {
9330 //fprintf(stderr, "is ref\n");
9331 if (this_way_id_real != 0)
9332 {
9333 i_real = c[i].y; // number of this node in the way (starting at zero)
9334 ndref = get_waynode_num(this_way_id_real, i_real, 0);
9335 //fprintf(stderr, "wayid:%lld wid(p)=%p i_real=%d i=%d ndref(1)=%lld\n", this_way_id_real, this_way_id, i_real, i, ndref);
9336 ni = node_item_get_fast(ndref, 0);
9337 //fprintf(stderr, "ni(1)=%p\n", ni);
9338 }
9339 else
9340 {
9341 ni = NULL;
9342 }
9343
9344 //fprintf(stderr, "ndref(2)=%lld\n", ndref);
9345
9346 if (ni)
9347 {
9348 //fprintf(stderr, "ni TRUE\n");
9349
9350 c[i] = ni->c; // write "lat,long" from node into way !!
9351 if (ni->ref_way > 1 && i != 0 && i != ccount - 1 && i != last && item_get_default_flags(ib->type))
9352 {
9353 //fprintf(stderr, "wr i 001\n");
9354 // if "ref_way > 1" , means this node belongs to more than 1 way, so it must be an intersection
9355 // *** write_item_part(out, out_index, out_graph, ib, last, i, &last_id);
9356 write_item_part(out, NULL, out_graph, ib, last, i, &last_id);
9357 last = i;
9358 }
9359 }
9360 else if (final)
9361 {
9362 //fprintf(stderr, "wr i 002(f)\n");
9363
9364 //osm_warning("way",item_bin_get_wayid(ib),0,"Non-existing reference to ");
9365 //osm_warning("node",ndref,1,"\n");
9366 remaining = (ib->len + 1) * 4 - sizeof(struct item_bin) - i * sizeof(struct coord);
9367 memmove(&c[i], &c[i + 1], remaining);
9368 ib->clen -= 2;
9369 ib->len -= 2;
9370 i--;
9371 ccount--;
9372 }
9373 }
9374 //else
9375 //{
9376 // fprintf(stderr, "is NOT ref\n");
9377 //}
9378 }
9379
9380 if (ccount)
9381 {
9382 //fprintf(stderr, "wr i 003(ccount)\n");
9383
9384 //fprintf(stderr,"*x*coastline*x*\n");
9385 // ***** write_item_part(out, out_index, out_graph, ib, last, ccount - 1, &last_id);
9386 write_item_part(out, NULL, out_graph, ib, last, ccount - 1, &last_id);
9387 if (final && ib->type == type_water_line && out_coastline)
9388 {
9389 //fprintf(stderr,"write out_coastline\n");
9390 write_item_part(out_coastline, NULL, NULL, ib, last, ccount - 1, NULL);
9391 }
9392 }
9393 }
9394
9395 return 0;
9396 }
9397
9398 int map_find_intersections__quick__for__debug(FILE *in, FILE *out, FILE *out_index, FILE *out_graph, FILE *out_coastline, int final)
9399 {
9400 struct coord *c;
9401 int i, ccount, last, remaining;
9402 osmid ndref;
9403 struct item_bin *ib;
9404 struct node_item *ni;
9405 long long last_id = 0;
9406 long long ways_count = 0;
9407 osmid *this_way_id;
9408 osmid this_way_id_real;
9409 int i_real = -1;
9410
9411 fprintf(stderr, "DEBUG:map_find_intersections__quick__for__debug\n");
9412
9413 processed_nodes = 0;
9414 processed_nodes_out = 0;
9415 processed_ways = 0;
9416 processed_relations = 0;
9417 processed_tiles = 0;
9418
9419
9420 // ---------- reset the cached values ----------
9421 seekpos_waynode[0] = -1;
9422 last_seekpos_waynode[0] = -1;
9423 last_seek_wayid[0] = -1;
9424 fseeko(ways_ref_file_thread[0], (off_t)0, SEEK_SET);
9425 // ---------- reset the cached values ----------
9426
9427
9428 while ((ib = read_item(in, 0))) // loop thru all "ways" from file "in"
9429 {
9430
9431 this_way_id = item_bin_get_attr(ib, attr_osm_wayid, NULL);
9432 if (this_way_id)
9433 {
9434 this_way_id_real = *this_way_id;
9435 // fprintf(stderr,"wayid:%lld type:0x%x len:%d clen:%d\n", *this_way_id, ib->type, ib->len, (ib->clen / 2));
9436 }
9437
9438 ccount = ib->clen / 2;
9439
9440 if (ccount <= 1)
9441 {
9442 continue;
9443 }
9444
9445 //fprintf(stderr,"********DUMP ww1***********\n");
9446 //dump_itembin(ib);
9447 //fprintf(stderr,"********DUMP ww1***********\n");
9448
9449
9450 ways_count++;
9451 if ((ways_count % 10000000) == 0)
9452 {
9453 fprintf_(stderr, "ways: %lld\n", ways_count);
9454 }
9455
9456 c = (struct coord *) (ib + 1);
9457 last = 0;
9458 i_real = -1;
9459 for (i = 0; i < ccount; i++) // loop thru all the coordinates (nodes) of this way
9460 {
9461 //if (this_way_id_real != 0)
9462 //{
9463 // fprintf(stderr, "this_way_id_real=%lld this_way_id=%lld i=%d ccount=%d\n", this_way_id_real, *this_way_id, i, ccount);
9464 //}
9465
9466 if (IS_REF(c[i]))
9467 {
9468 //fprintf(stderr, "is ref\n");
9469 if (this_way_id_real != 0)
9470 {
9471 i_real = c[i].y; // number of this node in the way (starting at zero)
9472 ndref = get_waynode_num(this_way_id_real, i_real, 0);
9473 //fprintf(stderr, "wayid:%lld wid(p)=%p i_real=%d i=%d ndref(1)=%lld\n", this_way_id_real, this_way_id, i_real, i, ndref);
9474 ni = node_item_get_fast(ndref, 0);
9475 //fprintf(stderr, "ni(1)=%p\n", ni);
9476 }
9477 else
9478 {
9479 ni = NULL;
9480 }
9481
9482 //fprintf(stderr, "ndref(2)=%lld\n", ndref);
9483
9484 if (ni)
9485 {
9486 //fprintf(stderr, "ni TRUE\n");
9487
9488 c[i] = ni->c; // write "lat,long" from node into way !!
9489 }
9490 }
9491 }
9492 item_bin_write(ib, out);
9493
9494 }
9495
9496 return 0;
9497 }
9498
9499
9500 static void index_country_add(struct zip_info *info, int country_id, int zipnum)
9501 {
9502 struct item_bin *item_bin = init_item(type_countryindex, 0);
9503 item_bin_add_attr_int(item_bin, attr_country_id, country_id);
9504 item_bin_add_attr_int(item_bin, attr_zipfile_ref, zipnum);
9505 item_bin_write(item_bin, zip_get_index(info));
9506 }
9507
9508 void write_countrydir(struct zip_info *zip_info)
9509 {
9510 int i, zipnum, num;
9511 int max = 11;
9512 char tilename[32];
9513 char filename[32];
9514 char suffix[32];
9515 struct country_table *co;
9516 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
9517 {
9518 co = &country_table[i];
9519 if (co->size)
9520 {
9521 num = 0;
9522 do
9523 {
9524 tilename[0] = '\0';
9525 sprintf(suffix, "s%d", num);
9526 num++;
9527 tile(&co->r, suffix, tilename, max, overlap, NULL);
9528
9529 sprintf(filename, "country_%d.tmp", co->countryid);
9530 if (debug_itembin(4))
9531 {
9532 fprintf_(stderr, "write_countrydir: tilename=%s country_%d.tmp\n", tilename, co->countryid);
9533 }
9534 zipnum = add_aux_tile(zip_info, tilename, filename, co->size);
9535 }
9536 while (zipnum == -1);
9537 index_country_add(zip_info, co->countryid, zipnum);
9538 }
9539 }
9540 }
9541
9542 void load_countries(void)
9543 {
9544 char filename[32];
9545 FILE *f;
9546 int i;
9547 struct country_table *co;
9548
9549 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
9550 {
9551 co = &country_table[i];
9552 sprintf(filename, "country_%d.tmp", co->countryid);
9553 if (debug_itembin(4))
9554 {
9555 fprintf_(stderr, "load_countries: country_%d.tmp\n", co->countryid);
9556 }
9557
9558 f = fopen(filename, "rb");
9559 if (f)
9560 {
9561 int i, first = 1;
9562 struct item_bin *ib;
9563 while ((ib = read_item(f, 0)))
9564 {
9565 struct coord *c = (struct coord *) (ib + 1);
9566 co->size += ib->len * 4 + 4;
9567 for (i = 0; i < ib->clen / 2; i++)
9568 {
9569 if (first)
9570 {
9571 co->r.l = c[i];
9572 co->r.h = c[i];
9573 first = 0;
9574 }
9575 else
9576 {
9577 bbox_extend(&c[i], &co->r);
9578 }
9579 }
9580 }
9581 fseek(f, 0, SEEK_END);
9582 co->size = ftell(f);
9583 fclose(f);
9584 }
9585 }
9586 }
9587
9588 void remove_countryfiles(void)
9589 {
9590 int i;
9591 char filename[32];
9592 struct country_table *co;
9593
9594 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
9595 {
9596 co = &country_table[i];
9597 if (co->size)
9598 {
9599 sprintf(filename, "country_%d.tmp", co->countryid);
9600 unlink(filename);
9601 }
9602 }
9603 }
9604
9605 void osm_init(FILE* rule_file)
9606 {
9607 build_attrmap(rule_file);
9608 build_countrytable();
9609 }
9610

   
Visit the ZANavi Wiki