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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki