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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (7 years, 6 months ago) by zoff99
File MIME type: text/plain
File size: 203885 byte(s)
new map version, lots of fixes and experimental 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 int nodeid, wayid;
152 long long 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
158 static char *attr_present;
159 static int attr_present_count;
160
161 static struct item_bin item;
162
163 int maxspeed_attr_value;
164
165 char debug_attr_buffer[BUFFER_SIZE];
166
167 int flags[4];
168
169 int flags_attr_value;
170
171 struct attr_bin osmid_attr;
172 long int osmid_attr_value;
173
174 char is_in_buffer[BUFFER_SIZE];
175
176 char attr_strings_buffer[BUFFER_SIZE * 16];
177 int attr_strings_buffer_len;
178 int alt_name_found = 0;
179
180 struct coord coord_buffer[65536];
181
182 struct attr_mapping
183 {
184 enum item_type type;
185 int attr_present_idx_count;
186 int attr_present_idx[0];
187 };
188
189 static void nodes_ref_item_bin(struct item_bin *ib, int local_thread_num);
190
191 static struct attr_mapping **attr_mapping_node;
192 static int attr_mapping_node_count;
193 static struct attr_mapping **attr_mapping_way;
194 static int attr_mapping_way_count;
195 static struct attr_mapping **attr_mapping_way2poi;
196 static int attr_mapping_way2poi_count;
197
198 static char *attr_present;
199 static int attr_present_count;
200
201 enum attr_strings
202 {
203 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_last,
204 };
205
206 char *attr_strings[attr_string_last];
207
208 char *osm_types[] = { "unknown", "node", "way", "relation" };
209
210 #define IS_REF(c) ((c).x >= (1 << 30))
211 #define REF(c) ((c).y)
212 #define SET_REF(c,ref) do { (c).x = 1 << 30; (c).y = ref ; } while(0)
213
214 struct country_table
215 {
216 int countryid;
217 char *names;
218 char *admin_levels;
219 FILE *file;
220 int size;
221 struct rect r;
222 }
223 country_table[] =
224 { { 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" }, };
225
226 // first char - item type
227 // =w - ways
228 // =? - used both for nodes and ways
229 // otherwise - nodes
230
231
232 // for coastline-only map
233 static char *attrmap_cl = { "n *=* point_unkn\n"
234 "w *=* street_unkn\n"
235 "w natural=coastline water_line\n" };
236
237 // for border-only map
238 static char *attrmap_bo = { "n *=* point_unkn\n"
239 "w *=* street_unkn\n"
240 "w boundary=administrative,admin_level=2 border_country\n"
241 "w boundary=territorial,admin_level=2 border_country\n"
242 "w boundary=maritime,admin_level=2 border_country\n"
243 "w administrative=boundary,admin_level=2 border_country\n"
244 "w boundary=administrative,maritime=yes,admin_level=2 border_country\n"
245 "w boundary=administrative,maritime=yes,admin_level=4 border_country\n"
246 "w boundary=administrative,border_type=state,admin_level=4 border_country\n" };
247
248 // ==========================
249 // for USA
250 // ==========================
251 // admin_level = 4
252 // border_type = state
253 // boundary = administrative
254 // ==========================
255
256
257 // for normal map
258 static char *attrmap_normal = { "n *=* point_unkn\n"
259 //// "n Annehmlichkeit=Hochsitz poi_hunting_stand\n"
260 "n addr:housenumber=* house_number\n"
261 "n aeroway=aerodrome poi_airport\n"
262 "n aeroway=airport poi_airport\n"
263 "n aeroway=helipad poi_heliport\n"
264 "n aeroway=terminal poi_airport\n"
265 "n amenity=atm poi_bank\n"
266 "n amenity=bank poi_bank\n"
267 "n amenity=bar poi_bar\n"
268 //"n amenity=bench poi_bench\n"
269 "n amenity=biergarten poi_biergarten\n"
270 "n amenity=bus_station poi_bus_station\n"
271 "n amenity=cafe poi_cafe\n"
272 "n amenity=car_wash poi_car_wash\n"
273 "n amenity=cinema poi_cinema\n"
274 "n amenity=college poi_school_college\n"
275 "n amenity=courthouse poi_justice\n"
276 //"n amenity=drinking_water poi_potable_water\n"
277 "n amenity=fast_food poi_fastfood\n"
278 "n amenity=fire_station poi_firebrigade\n"
279 //"n amenity=fountain poi_fountain\n"
280 "n amenity=fuel poi_fuel\n"
281 "n amenity=grave_yard poi_cemetery\n"
282 "n amenity=hospital poi_hospital\n"
283 //"n amenity=hunting_stand poi_hunting_stand\n"
284 "n amenity=kindergarten poi_kindergarten\n"
285 "n amenity=library poi_library\n"
286 "n amenity=nightclub poi_nightclub\n"
287 //"n amenity=park_bench poi_bench\n"
288 "n amenity=parking poi_car_parking\n"
289 "n amenity=pharmacy poi_pharmacy\n"
290 "n amenity=place_of_worship,religion=christian poi_church\n"
291 "n amenity=place_of_worship poi_worship\n"
292 "n amenity=police poi_police\n"
293 //"n amenity=post_box poi_post_box\n"
294 "n amenity=post_office poi_post_office\n"
295 "n amenity=prison poi_prison\n"
296 //"n amenity=pub poi_pub\n"
297 "n amenity=public_building poi_public_office\n"
298 //"n amenity=recycling poi_recycling\n"
299 "n amenity=restaurant,cuisine=fine_dining poi_dining\n"
300 "n amenity=restaurant poi_restaurant\n"
301 "n amenity=school poi_school\n"
302 //"n amenity=shelter poi_shelter\n"
303 "n amenity=taxi poi_taxi\n"
304 //"n amenity=tec_common tec_common\n"
305 //"n amenity=telephone poi_telephone\n"
306 "n amenity=theatre poi_theater\n"
307 "n amenity=toilets poi_restroom\n"
308 "n amenity=townhall poi_townhall\n"
309 "n amenity=university poi_school_university\n"
310 //"n amenity=vending_machine poi_vending_machine\n"
311 "n barrier=bollard barrier_bollard\n"
312 "n barrier=cycle_barrier barrier_cycle\n"
313 "n barrier=lift_gate barrier_lift_gate\n"
314 //"n car=car_rental poi_car_rent\n"
315 "n highway=bus_station poi_bus_station\n"
316 "n highway=bus_stop poi_bus_stop\n"
317 "n highway=mini_roundabout mini_roundabout\n"
318 "n highway=motorway_junction highway_exit\n"
319 "n highway=stop traffic_sign_stop\n"
320 "n highway=toll_booth poi_toll_booth\n"
321 "n highway=traffic_signals traffic_signals\n"
322 "n highway=turning_circle turning_circle\n"
323 //"n historic=boundary_stone poi_boundary_stone\n"
324 "n historic=castle poi_castle\n"
325 "n historic=memorial poi_memorial\n"
326 "n historic=monument poi_monument\n"
327 "n historic=ruins poi_ruins\n"
328 //// "n historic=* poi_ruins\n"
329 "n landuse=cemetery poi_cemetery\n"
330 //"n leisure=fishing poi_fish\n"
331 "n leisure=golf_course poi_golf\n"
332 "n leisure=marina poi_marine\n"
333 //"n leisure=playground poi_playground\n"
334 //"n leisure=slipway poi_boat_ramp\n"
335 //"n leisure=sports_centre poi_sport\n"
336 "n leisure=stadium poi_stadium\n"
337 //"n man_made=tower poi_tower\n"
338 "n military=airfield poi_military\n"
339 //"n military=barracks poi_military\n"
340 //"n military=bunker poi_military\n"
341 "n military=danger_area poi_danger_area\n"
342 "n military=range poi_military\n"
343 "n natural=bay poi_bay\n"
344 "n natural=peak,ele=* poi_peak\n" // show only major peaks with elevation
345 //"n natural=tree poi_tree\n"
346 "n place=city town_label_2e5\n"
347 "n place=hamlet town_label_2e2\n"
348 "n place=locality town_label_2e0\n"
349 "n place=suburb district_label\n"
350 "n place=town town_label_2e4\n"
351 "n place=village town_label_2e3\n"
352 //"n power=tower power_tower\n"
353 //"n power=sub_station power_substation\n"
354 "n railway=halt poi_rail_halt\n"
355 "n railway=level_crossing poi_level_crossing\n"
356 "n railway=station poi_rail_station\n"
357 "n railway=tram_stop poi_rail_tram_stop\n"
358 //"n shop=baker poi_shop_baker\n"
359 //"n shop=bakery poi_shop_baker\n"
360 //"n shop=beverages poi_shop_beverages\n"
361 //"n shop=bicycle poi_shop_bicycle\n"
362 //"n shop=butcher poi_shop_butcher\n"
363 "n shop=car poi_car_dealer_parts\n"
364 "n shop=car_repair poi_repair_service\n"
365 //"n shop=clothes poi_shop_apparel\n"
366 //"n shop=convenience poi_shop_grocery\n"
367 //"n shop=drogist poi_shop_drugstore\n"
368 //"n shop=florist poi_shop_florist\n"
369 //"n shop=fruit poi_shop_fruit\n"
370 //"n shop=furniture poi_shop_furniture\n"
371 //"n shop=garden_centre poi_shop_handg\n"
372 //"n shop=hardware poi_shop_handg\n"
373 //"n shop=hairdresser poi_hairdresser\n"
374 //"n shop=kiosk poi_shop_kiosk\n"
375 //"n shop=optician poi_shop_optician\n"
376 //"n shop=parfum poi_shop_parfum\n"
377 //"n shop=photo poi_shop_photo\n"
378 //"n shop=shoes poi_shop_shoes\n"
379 "n shop=supermarket poi_shopping\n"
380 //"n sport=10pin poi_bowling\n"
381 //"n sport=baseball poi_baseball\n"
382 //"n sport=basketball poi_basketball\n"
383 //"n sport=climbing poi_climbing\n"
384 //"n sport=golf poi_golf\n"
385 //"n sport=motor_sports poi_motor_sport\n"
386 //"n sport=skiing poi_skiing\n"
387 //"n sport=soccer poi_soccer\n"
388 "n sport=stadium poi_stadium\n"
389 "n sport=swimming poi_swimming\n"
390 //"n sport=tennis poi_tennis\n"
391 "n tourism=attraction poi_attraction\n"
392 "n tourism=camp_site poi_camp_rv\n"
393 "n tourism=caravan_site poi_camp_rv\n"
394 "n tourism=guest_house poi_guesthouse\n"
395 "n tourism=hostel poi_hostel\n"
396 "n tourism=hotel poi_hotel\n"
397 "n tourism=information poi_information\n"
398 "n tourism=motel poi_motel\n"
399 "n tourism=museum poi_museum_history\n"
400 //"n tourism=picnic_site poi_picnic\n"
401 "n tourism=theme_park poi_resort\n"
402 //"n tourism=viewpoint poi_viewpoint\n"
403 "n tourism=zoo poi_zoo\n"
404 "n traffic_sign=city_limit traffic_sign_city_limit\n"
405 "n highway=speed_camera tec_common\n"
406 "w *=* street_unkn\n"
407 "w addr:interpolation=even house_number_interpolation_even\n"
408 "w addr:interpolation=odd house_number_interpolation_odd\n"
409 "w addr:interpolation=all house_number_interpolation_all\n"
410 "w addr:interpolation=alphabetic house_number_interpolation_alphabetic\n"
411 //"w aerialway=cable_car lift_cable_car\n"
412 //"w aerialway=chair_lift lift_chair\n"
413 //"w aerialway=drag_lift lift_drag\n"
414 "w aeroway=aerodrome poly_airport\n"
415 "w aeroway=apron poly_apron\n"
416 "w aeroway=runway aeroway_runway\n"
417 "w aeroway=taxiway aeroway_taxiway\n"
418 "w aeroway=terminal poly_terminal\n"
419 "w amenity=college poly_college\n"
420 //"w amenity=grave_yard poly_cemetery\n"
421 "w amenity=parking poly_car_parking\n"
422 "w amenity=place_of_worship poly_building\n"
423 "w amenity=university poly_university\n"
424 // "w boundary=administrative,admin_level=2 border_country\n" --> in border map
425 //"w boundary=civil border_civil\n"
426 "w boundary=national_park border_national_park\n"
427 "w boundary=political border_political\n"
428 "w building=* poly_building\n"
429 "w contour_ext=elevation_major height_line_1\n"
430 "w contour_ext=elevation_medium height_line_2\n"
431 "w contour_ext=elevation_minor height_line_3\n"
432 "w highway=bridleway bridleway\n"
433 "w highway=bus_guideway bus_guideway\n"
434 "w highway=construction street_construction\n"
435 "w highway=cyclepath cycleway\n"
436 "w highway=cycleway cycleway\n"
437 "w highway=footway footway\n"
438 "w highway=footway,piste:type=nordic footway_and_piste_nordic\n"
439 "w highway=living_street living_street\n"
440 "w highway=minor street_1_land\n"
441 "w highway=parking_lane street_parking_lane\n"
442 "w highway=path path\n"
443 "w highway=path,bicycle=designated cycleway\n"
444 "w highway=path,bicycle=official cycleway\n"
445 "w highway=path,foot=designated footway\n"
446 "w highway=path,foot=official footway\n"
447 "w highway=path,horse=designated bridleway\n"
448 "w highway=path,horse=official bridleway\n"
449 //"w highway=path,sac_scale=alpine_hiking hiking_alpine\n"
450 //"w highway=path,sac_scale=demanding_alpine_hiking hiking_alpine_demanding\n"
451 //"w highway=path,sac_scale=demanding_mountain_hiking hiking_mountain_demanding\n"
452 //"w highway=path,sac_scale=difficult_alpine_hiking hiking_alpine_difficult\n"
453 //"w highway=path,sac_scale=hiking hiking\n"
454 //"w highway=path,sac_scale=mountain_hiking hiking_mountain\n"
455 "w highway=pedestrian street_pedestrian\n"
456 "w highway=pedestrian,area=1 poly_pedestrian\n"
457 "w highway=plaza poly_plaza\n"
458 "w highway=motorway highway_land\n"
459 "w highway=motorway,rural=0 highway_city\n"
460 "w highway=motorway_link ramp\n"
461 "w highway=trunk street_4_land\n"
462 "w highway=trunk,name=*,rural=1 street_4_land\n"
463 "w highway=trunk,name=* street_4_city\n"
464 "w highway=trunk,rural=0 street_4_city\n"
465 "w highway=trunk_link ramp\n"
466 "w highway=primary street_4_land\n"
467 "w highway=primary,name=*,rural=1 street_4_land\n"
468 "w highway=primary,name=* street_4_city\n"
469 "w highway=primary,rural=0 street_4_city\n"
470 "w highway=primary_link ramp\n"
471 "w highway=secondary street_3_land\n"
472 "w highway=secondary,name=*,rural=1 street_3_land\n"
473 "w highway=secondary,name=* street_3_city\n"
474 "w highway=secondary,rural=0 street_3_city\n"
475 "w highway=secondary,area=1 poly_street_3\n"
476 "w highway=secondary_link ramp\n"
477 "w highway=tertiary street_2_land\n"
478 "w highway=tertiary,name=*,rural=1 street_2_land\n"
479 "w highway=tertiary,name=* street_2_city\n"
480 "w highway=tertiary,rural=0 street_2_city\n"
481 "w highway=tertiary,area=1 poly_street_2\n"
482 "w highway=tertiary_link ramp\n"
483 "w highway=residential street_1_city\n"
484 "w highway=residential,area=1 poly_street_1\n"
485 "w highway=unclassified street_1_city\n"
486 "w highway=unclassified,area=1 poly_street_1\n"
487 "w highway=road street_1_city\n"
488 "w highway=service street_service\n"
489 "w highway=service,area=1 poly_service\n"
490 "w highway=service,service=parking_aisle street_parking_lane\n"
491 "w highway=track track_gravelled\n"
492 "w highway=track,surface=grass track_grass\n"
493 "w highway=track,surface=gravel track_gravelled\n"
494 "w highway=track,surface=ground track_ground\n"
495 "w highway=track,surface=paved track_paved\n"
496 "w highway=track,surface=unpaved track_unpaved\n"
497 "w highway=track,tracktype=grade1 track_paved\n"
498 "w highway=track,tracktype=grade2 track_gravelled\n"
499 "w highway=track,tracktype=grade3 track_unpaved\n"
500 "w highway=track,tracktype=grade4 track_ground\n"
501 "w highway=track,tracktype=grade5 track_grass\n"
502 "w highway=track,surface=paved,tracktype=grade1 track_paved\n"
503 "w highway=track,surface=gravel,tracktype=grade2 track_gravelled\n"
504 "w highway=track,surface=unpaved,tracktype=grade3 track_unpaved\n"
505 "w highway=track,surface=ground,tracktype=grade4 track_ground\n"
506 "w highway=track,surface=grass,tracktype=grade5 track_grass\n"
507 "w highway=unsurfaced track_gravelled\n"
508 "w highway=steps steps\n"
509 "w historic=archaeological_site poly_archaeological_site\n"
510 "w historic=battlefield poly_battlefield\n"
511 "w historic=ruins poly_ruins\n"
512 "w historic=town gate poly_building\n"
513 //"w landuse=allotments poly_allotments\n"
514 //"w landuse=basin poly_basin\n"
515 //"w landuse=brownfield poly_brownfield\n"
516 "w landuse=cemetery poly_cemetery\n"
517 //"w landuse=commercial poly_commercial\n"
518 //"w landuse=construction poly_construction\n"
519 //"w landuse=farm poly_farm\n"
520 //"w landuse=farmland poly_farm\n"
521 //"w landuse=farmyard poly_town\n"
522 "w landuse=forest poly_wood\n"
523 "w landuse=greenfield poly_greenfield\n"
524 //"w landuse=industrial poly_industry\n"
525 //"w landuse=landfill poly_landfill\n"
526 "w landuse=military poly_military\n"
527 "w landuse=plaza poly_plaza\n"
528 //"w landuse=quarry poly_quarry\n"
529 "w landuse=railway poly_railway\n"
530 //"w landuse=recreation_ground poly_recreation_ground\n"
531 "w landuse=reservoir poly_reservoir\n"
532 //"w landuse=residential poly_town\n"
533 //"w landuse=residential,area=1 poly_town\n"
534 //"w landuse=retail poly_retail\n"
535 //"w landuse=village_green poly_village_green\n"
536 //"w landuse=vineyard poly_farm\n"
537 //"w leisure=common poly_common\n"
538 //"w leisure=fishing poly_fishing\n"
539 //"w leisure=garden poly_garden\n"
540 //"w leisure=golf_course poly_golf_course\n"
541 //"w leisure=marina poly_marina\n"
542 //"w leisure=nature_reserve poly_nature_reserve\n"
543 "w leisure=park poly_park\n"
544 //"w leisure=pitch poly_sports_pitch\n"
545 "w leisure=playground poly_playground\n"
546 //"w leisure=sports_centre poly_sport\n"
547 "w leisure=stadium poly_sports_stadium\n"
548 //"w leisure=track poly_sports_track\n"
549 "w leisure=water_park poly_water_park\n"
550 "w military=airfield poly_airfield\n"
551 //"w military=barracks poly_barracks\n"
552 "w military=danger_area poly_danger_area\n"
553 "w military=naval_base poly_naval_base\n"
554 //"w military=range poly_range\n"
555 "w natural=beach poly_beach\n"
556 // "w natural=coastline water_line\n" --> in coastline map
557 "w natural=fell poly_fell\n"
558 "w natural=glacier poly_glacier\n"
559 "w natural=heath poly_heath\n"
560 "w natural=land poly_land\n"
561 "w natural=marsh poly_marsh\n"
562 "w natural=mud poly_mud\n"
563 "w natural=scree poly_scree\n"
564 "w natural=scrub poly_scrub\n"
565 "w natural=water poly_water\n"
566 "w natural=wood poly_wood\n"
567 //"w piste:type=downhill,piste:difficulty=advanced piste_downhill_advanced\n"
568 //"w piste:type=downhill,piste:difficulty=easy piste_downhill_easy\n"
569 //"w piste:type=downhill,piste:difficulty=expert piste_downhill_expert\n"
570 //"w piste:type=downhill,piste:difficulty=freeride piste_downhill_freeride\n"
571 //"w piste:type=downhill,piste:difficulty=intermediate piste_downhill_intermediate\n"
572 //"w piste:type=downhill,piste:difficulty=novice piste_downhill_novice\n"
573 //"w piste:type=nordic piste_nordic\n"
574 "w place=suburb poly_place1\n"
575 "w place=hamlet poly_place2\n"
576 "w place=village poly_place3\n"
577 "w place=municipality poly_place4\n"
578 "w place=town poly_place5\n"
579 "w place=city poly_place6\n"
580 //"w power=line powerline\n"
581 //"w railway=abandoned rail_abandoned\n"
582 //"w railway=disused rail_disused\n"
583 "w railway=light_rail rail_light\n"
584 "w railway=monorail rail_mono\n"
585 "w railway=narrow_gauge rail_narrow_gauge\n"
586 //"w railway=preserved rail_preserved\n"
587 "w railway=rail rail\n"
588 "w railway=subway rail_subway\n"
589 "w railway=tram rail_tram\n"
590 "w route=ferry ferry\n"
591 //"w route=ski piste_nordic\n"
592 //"w sport=* poly_sport\n"
593 "w tourism=artwork poly_artwork\n"
594 "w tourism=attraction poly_attraction\n"
595 "w tourism=camp_site poly_camp_site\n"
596 "w tourism=caravan_site poly_caravan_site\n"
597 "w tourism=picnic_site poly_picnic_site\n"
598 "w tourism=theme_park poly_theme_park\n"
599 "w tourism=zoo poly_zoo\n"
600 "w waterway=canal water_canal\n"
601 "w waterway=drain water_drain\n"
602 "w waterway=river water_river\n"
603 "w waterway=riverbank poly_water\n"
604 "w waterway=stream water_stream\n"
605 "w barrier=ditch ditch\n"
606 "w barrier=hedge hedge\n"
607 "w barrier=fence fence\n"
608 "w barrier=wall wall\n"
609 "w barrier=retaining_wall retaining_wall\n"
610 "w barrier=city_wall city_wall\n" };
611
612 static void build_attrmap_line(char *line)
613 {
614 char *t = NULL, *kvl = NULL, *i = NULL, *p, *kv;
615 struct attr_mapping *attr_mapping = g_malloc0(sizeof(struct attr_mapping));
616 int idx, attr_mapping_count = 0;
617 t = line;
618 p = strchr(t, '\t');
619 if (p)
620 {
621 while (*p == '\t')
622 *p++ = '\0';
623 kvl = p;
624 p = strchr(kvl, '\t');
625 }
626 if (p)
627 {
628 while (*p == '\t')
629 *p++ = '\0';
630 i = p;
631 }
632 if (t[0] == 'w')
633 {
634 if (!i)
635 i = "street_unkn";
636 }
637 else
638 {
639 if (!i)
640 i = "point_unkn";
641 }
642
643 attr_mapping->type = item_from_name(i);
644
645 if (!attr_mapping->type)
646 {
647 printf("no id found for '%s'\n", i);
648 }
649
650 while ((kv = strtok(kvl, ",")))
651 {
652 kvl = NULL;
653 if (!(idx = (int) (long) g_hash_table_lookup(attr_hash, kv)))
654 {
655 idx = attr_present_count++;
656 g_hash_table_insert(attr_hash, kv, (gpointer) (long) idx);
657 }
658 attr_mapping = g_realloc(attr_mapping, sizeof(struct attr_mapping) + (attr_mapping_count + 1) * sizeof(int));
659 attr_mapping->attr_present_idx[attr_mapping_count++] = idx;
660 attr_mapping->attr_present_idx_count = attr_mapping_count;
661 }
662 if (t[0] == 'w')
663 {
664 attr_mapping_way = g_realloc(attr_mapping_way, sizeof(*attr_mapping_way) * (attr_mapping_way_count + 1));
665 attr_mapping_way[attr_mapping_way_count++] = attr_mapping;
666 }
667 if (t[0] == '?' && doway2poi)
668 {
669 attr_mapping_way2poi = g_realloc(attr_mapping_way2poi, sizeof(*attr_mapping_way2poi) * (attr_mapping_way2poi_count + 1));
670 attr_mapping_way2poi[attr_mapping_way2poi_count++] = attr_mapping;
671 }
672 if (t[0] != 'w')
673 {
674 attr_mapping_node = g_realloc(attr_mapping_node, sizeof(*attr_mapping_node) * (attr_mapping_node_count + 1));
675 attr_mapping_node[attr_mapping_node_count++] = attr_mapping;
676 }
677
678 }
679
680 static void build_attrmap(FILE* rule_file)
681 {
682 attr_hash = g_hash_table_new(g_str_hash, g_str_equal);
683 attr_present_count = 1;
684
685 // build attribute map from rule file if given
686 if (rule_file)
687 {
688 char buffer[200], *p;
689 while (fgets(buffer, 200, rule_file))
690 {
691 p = strchr(buffer, '\n');
692 if (p)
693 *p = 0;
694 build_attrmap_line(g_strdup(buffer));
695 }
696 fclose(rule_file);
697 }
698 // use hardcoded default attributes
699 else
700 {
701 char *p, *map;
702 if (border_only_map == 1)
703 {
704 map = g_strdup(attrmap_bo);
705 }
706 else if (coastline_only_map == 1)
707 {
708 map = g_strdup(attrmap_cl);
709 }
710 else
711 {
712 map = g_strdup(attrmap_normal);
713 }
714
715 while (map)
716 {
717 p = strchr(map, '\n');
718 if (p)
719 *p++ = '\0';
720 if (strlen(map))
721 build_attrmap_line(map);
722 map = p;
723 }
724 }
725
726 attr_present = g_malloc0(sizeof(*attr_present) * attr_present_count);
727 }
728
729 static void build_countrytable(void)
730 {
731 int i;
732 char *names, *str, *tok;
733 country_table_hash = g_hash_table_new(g_str_hash, g_str_equal);
734 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
735 {
736 names = g_strdup(country_table[i].names);
737 str = names;
738 while ((tok = strtok(str, ",")))
739 {
740 str = NULL;
741 g_hash_table_insert(country_table_hash, tok, (gpointer) & country_table[i]);
742 }
743 }
744 }
745
746 void osm_warning(char *type, long long id, int cont, char *fmt, ...)
747 {
748 char str[4096];
749 va_list ap;
750 va_start(ap, fmt);
751 vsnprintf(str, sizeof(str), fmt, ap);
752 va_end(ap);
753 if (verbose_mode) fprintf(stderr,"%shttp://www.openstreetmap.org/browse/%s/"LONGLONG_FMT" %s",cont ? "":"OSM Warning:",type,id,str);
754 }
755
756 void osm_info(char *type, long long id, int cont, char *fmt, ...)
757 {
758 char str[4096];
759 va_list ap;
760 va_start(ap, fmt);
761 vsnprintf(str, sizeof(str), fmt, ap);
762 va_end(ap);
763 if (verbose_mode) fprintf(stderr,"%shttp://www.openstreetmap.org/browse/%s/"LONGLONG_FMT" %s",cont ? "":"OSM Info:",type,id,str);
764 }
765
766 static void attr_strings_clear(void)
767 {
768 alt_name_found = 0;
769 attr_strings_buffer_len = 0;
770 memset(attr_strings, 0, sizeof(attr_strings));
771 }
772
773 static void attr_strings_save(enum attr_strings id, char *str)
774 {
775 attr_strings[id] = attr_strings_buffer + attr_strings_buffer_len;
776 strcpy(attr_strings[id], str);
777 attr_strings_buffer_len += strlen(str) + 1;
778 }
779
780 long long item_bin_get_nodeid(struct item_bin *ib)
781 {
782 long long *ret = item_bin_get_attr(ib, attr_osm_nodeid, NULL);
783 if (ret)
784 {
785 return *ret;
786 }
787 return 0;
788 }
789
790 long long item_bin_get_wayid(struct item_bin *ib)
791 {
792 long long *ret = item_bin_get_attr(ib, attr_osm_wayid, NULL);
793 if (ret)
794 {
795 return *ret;
796 }
797 return 0;
798 }
799
800 long long item_bin_get_relationid(struct item_bin *ib)
801 {
802 long long *ret = item_bin_get_attr(ib, attr_osm_relationid, NULL);
803 if (ret)
804 {
805 return *ret;
806 }
807 return 0;
808 }
809
810 long long item_bin_get_id(struct item_bin *ib)
811 {
812 long long ret;
813 if (ib->type < 0x80000000)
814 {
815 return item_bin_get_nodeid(ib);
816 }
817 ret = item_bin_get_wayid(ib);
818 if (!ret)
819 {
820 ret = item_bin_get_relationid(ib);
821 }
822 return ret;
823 }
824
825 static int node_is_tagged;
826 static void relation_add_tag(char *k, char *v);
827
828 static int access_value(char *v)
829 {
830 if (!strcmp(v, "1"))
831 return 1;
832 if (!strcmp(v, "yes"))
833 return 1;
834 if (!strcmp(v, "designated"))
835 return 1;
836 if (!strcmp(v, "official"))
837 return 1;
838 if (!strcmp(v, "permissive"))
839 return 1;
840 if (!strcmp(v, "0"))
841 return 2;
842 if (!strcmp(v, "no"))
843 return 2;
844 if (!strcmp(v, "agricultural"))
845 return 2;
846 if (!strcmp(v, "forestry"))
847 return 2;
848 if (!strcmp(v, "private"))
849 return 2;
850 if (!strcmp(v, "delivery"))
851 return 2;
852 if (!strcmp(v, "destination"))
853 return 2;
854 return 3;
855 }
856
857 void osm_add_tag(char *k, char *v)
858 {
859 int idx, level = 2;
860 char buffer[BUFFER_SIZE * 2 + 2];
861
862 if (in_relation)
863 {
864 relation_add_tag(k, v);
865 return;
866 }
867
868 if (!strcmp(k, "ele"))
869 level = 9;
870 if (!strcmp(k, "time"))
871 level = 9;
872 if (!strcmp(k, "created_by"))
873 level = 9;
874 if (!strncmp(k, "tiger:", 6) || !strcmp(k, "AND_nodes"))
875 level = 9;
876 if (!strcmp(k, "converted_by") || !strcmp(k, "source"))
877 level = 8;
878 if (!strncmp(k, "osmarender:", 11) || !strncmp(k, "svg:", 4))
879 level = 8;
880 if (!strcmp(k, "layer"))
881 level = 7;
882 if (!strcasecmp(v, "true") || !strcasecmp(v, "yes"))
883 v = "1";
884 if (!strcasecmp(v, "false") || !strcasecmp(v, "no"))
885 v = "0";
886 if (!strcmp(k, "oneway"))
887 {
888 if (!strcmp(v, "1"))
889 {
890 flags[0] |= AF_ONEWAY | AF_ROUNDABOUT_VALID;
891 }
892 if (!strcmp(v, "-1"))
893 {
894 flags[0] |= AF_ONEWAYREV | AF_ROUNDABOUT_VALID;
895 }
896 if (!in_way)
897 level = 6;
898 else
899 level = 5;
900 }
901 if (!strcmp(k, "junction"))
902 {
903 if (!strcmp(v, "roundabout"))
904 flags[0] |= AF_ONEWAY | AF_ROUNDABOUT | AF_ROUNDABOUT_VALID;
905 }
906 if (!strcmp(k, "maxspeed"))
907 {
908 if (strstr(v, "mph"))
909 {
910 maxspeed_attr_value = (int) floor(atof(v) * 1.609344);
911 }
912 else
913 {
914 maxspeed_attr_value = atoi(v);
915 }
916 if (maxspeed_attr_value)
917 flags[0] |= AF_SPEED_LIMIT;
918 level = 5;
919 }
920 if (!strcmp(k, "toll"))
921 {
922 if (!strcmp(v, "1"))
923 {
924 flags[0] |= AF_TOLL;
925 }
926 }
927 if (!strcmp(k, "access"))
928 {
929 flags[access_value(v)] |= AF_DANGEROUS_GOODS | AF_EMERGENCY_VEHICLES | AF_TRANSPORT_TRUCK | AF_DELIVERY_TRUCK | AF_PUBLIC_BUS | AF_TAXI | AF_HIGH_OCCUPANCY_CAR | AF_CAR | AF_MOTORCYCLE | AF_MOPED | AF_HORSE | AF_BIKE | AF_PEDESTRIAN;
930 level = 5;
931 }
932 if (!strcmp(k, "vehicle"))
933 {
934 flags[access_value(v)] |= AF_DANGEROUS_GOODS | AF_EMERGENCY_VEHICLES | AF_TRANSPORT_TRUCK | AF_DELIVERY_TRUCK | AF_PUBLIC_BUS | AF_TAXI | AF_HIGH_OCCUPANCY_CAR | AF_CAR | AF_MOTORCYCLE | AF_MOPED | AF_BIKE;
935 level = 5;
936 }
937 if (!strcmp(k, "motorvehicle"))
938 {
939 flags[access_value(v)] |= AF_DANGEROUS_GOODS | AF_EMERGENCY_VEHICLES | AF_TRANSPORT_TRUCK | AF_DELIVERY_TRUCK | AF_PUBLIC_BUS | AF_TAXI | AF_HIGH_OCCUPANCY_CAR | AF_CAR | AF_MOTORCYCLE | AF_MOPED;
940 level = 5;
941 }
942 if (!strcmp(k, "bicycle"))
943 {
944 flags[access_value(v)] |= AF_BIKE;
945 level = 5;
946 }
947 if (!strcmp(k, "foot"))
948 {
949 flags[access_value(v)] |= AF_PEDESTRIAN;
950 level = 5;
951 }
952 if (!strcmp(k, "horse"))
953 {
954 flags[access_value(v)] |= AF_HORSE;
955 level = 5;
956 }
957 if (!strcmp(k, "moped"))
958 {
959 flags[access_value(v)] |= AF_MOPED;
960 level = 5;
961 }
962 if (!strcmp(k, "motorcycle"))
963 {
964 flags[access_value(v)] |= AF_MOTORCYCLE;
965 level = 5;
966 }
967 if (!strcmp(k, "motorcar"))
968 {
969 flags[access_value(v)] |= AF_CAR;
970 level = 5;
971 }
972 if (!strcmp(k, "hov"))
973 {
974 flags[access_value(v)] |= AF_HIGH_OCCUPANCY_CAR;
975 level = 5;
976 }
977 if (!strcmp(k, "bus"))
978 {
979 flags[access_value(v)] |= AF_PUBLIC_BUS;
980 level = 5;
981 }
982 if (!strcmp(k, "taxi"))
983 {
984 flags[access_value(v)] |= AF_TAXI;
985 level = 5;
986 }
987 if (!strcmp(k, "goods"))
988 {
989 flags[access_value(v)] |= AF_DELIVERY_TRUCK;
990 level = 5;
991 }
992 if (!strcmp(k, "hgv"))
993 {
994 flags[access_value(v)] |= AF_TRANSPORT_TRUCK;
995 level = 5;
996 }
997 if (!strcmp(k, "emergency"))
998 {
999 flags[access_value(v)] |= AF_EMERGENCY_VEHICLES;
1000 level = 5;
1001 }
1002 if (!strcmp(k, "hazmat"))
1003 {
1004 flags[access_value(v)] |= AF_DANGEROUS_GOODS;
1005 level = 5;
1006 }
1007 if (!strcmp(k, "tunnel") && !strcmp(v, "1"))
1008 {
1009 flags[0] |= AF_UNDERGROUND;
1010 }
1011 if (!strcmp(k, "bridge") && !strcmp(v, "1"))
1012 {
1013 flags[0] |= AF_BRIDGE;
1014 }
1015 if (!strcmp(k, "note"))
1016 level = 5;
1017 if (!strcmp(k, "name"))
1018 {
1019 attr_strings_save(attr_string_label, v);
1020 level = 5;
1021 }
1022
1023 if ((!strcmp(k, "name:en")) && (alt_name_found == 0))
1024 {
1025 attr_strings_save(attr_string_label_alt, v);
1026 alt_name_found = 1;
1027 level = 5;
1028 }
1029 if ((!strcmp(k, "alt_name:en")) && (alt_name_found < 2))
1030 {
1031 // only use "alt_name:en" if we dont have "alt_name"
1032 attr_strings_save(attr_string_label_alt, v);
1033 alt_name_found = 2;
1034 level = 5;
1035 }
1036 if (!strcmp(k, "alt_name"))
1037 {
1038 attr_strings_save(attr_string_label_alt, v);
1039 alt_name_found = 3;
1040 level = 5;
1041 }
1042 if (!strcmp(k, "addr:email"))
1043 {
1044 attr_strings_save(attr_string_email, v);
1045 level = 5;
1046 }
1047 if (!strcmp(k, "addr:housenumber"))
1048 {
1049 attr_strings_save(attr_string_house_number, v);
1050 level = 5;
1051 }
1052 if (!strcmp(k, "addr:street"))
1053 {
1054 attr_strings_save(attr_string_street_name, v);
1055 level = 5;
1056 }
1057 if (!strcmp(k, "phone"))
1058 {
1059 attr_strings_save(attr_string_phone, v);
1060 level = 5;
1061 }
1062 if (!strcmp(k, "fax"))
1063 {
1064 attr_strings_save(attr_string_fax, v);
1065 level = 5;
1066 }
1067 if (!strcmp(k, "postal_code"))
1068 {
1069 attr_strings_save(attr_string_postal, v);
1070 level = 5;
1071 }
1072 if (!strcmp(k, "openGeoDB:postal_codes") && !attr_strings[attr_string_postal])
1073 {
1074 attr_strings_save(attr_string_postal, v);
1075 level = 5;
1076 }
1077 if (!strcmp(k, "population"))
1078 {
1079 attr_strings_save(attr_string_population, v);
1080 level = 5;
1081 }
1082 if (!strcmp(k, "openGeoDB:population") && !attr_strings[attr_string_population])
1083 {
1084 attr_strings_save(attr_string_population, v);
1085 level = 5;
1086 }
1087 if (!strcmp(k, "ref"))
1088 {
1089 if (in_way)
1090 attr_strings_save(attr_string_street_name_systematic, v);
1091 level = 5;
1092 }
1093 if (!strcmp(k, "openGeoDB:is_in"))
1094 {
1095 if (!is_in_buffer[0])
1096 strcpy(is_in_buffer, v);
1097 level = 5;
1098 }
1099 if (!strcmp(k, "is_in"))
1100 {
1101 if (!is_in_buffer[0])
1102 strcpy(is_in_buffer, v);
1103 level = 5;
1104 }
1105 if (!strcmp(k, "is_in:country"))
1106 {
1107 /**
1108 * Sometimes there is no is_in tag, only is_in:country.
1109 * I put this here so it can be overwritten by the previous if clause if there IS an is_in tag.
1110 */
1111 strcpy(is_in_buffer, v);
1112 level = 5;
1113 }
1114 if (!strcmp(k, "place_county"))
1115 {
1116 /**
1117 * Ireland uses the place_county OSM tag to describe what county a town is in.
1118 * This would be equivalent to is_in: Town; Locality; Country
1119 * A real world example would be Node: Moycullen (52234625)
1120 * The tag is processed as Moycullen; Galway; Ireland
1121 * where Galway is the county
1122 */
1123 strcpy(is_in_buffer, "Ireland");
1124 attr_strings_save(attr_string_county_name, v);
1125 level = 5;
1126 }
1127 if (!strcmp(k, "gnis:ST_alpha"))
1128 {
1129 /* assume a gnis tag means it is part of the USA:
1130 http://en.wikipedia.org/wiki/Geographic_Names_Information_System
1131 many US towns do not have is_in tags
1132 */
1133 strcpy(is_in_buffer, "USA");
1134 level = 5;
1135 }
1136 if (!strcmp(k, "lanes"))
1137 {
1138 level = 5;
1139 }
1140 if (attr_debug_level >= level)
1141 {
1142 int bytes_left = sizeof(debug_attr_buffer) - strlen(debug_attr_buffer) - 1;
1143 if (bytes_left > 0)
1144 {
1145 snprintf(debug_attr_buffer + strlen(debug_attr_buffer), bytes_left, " %s=%s", k, v);
1146 debug_attr_buffer[sizeof(debug_attr_buffer) - 1] = '\0';
1147 node_is_tagged = 1;
1148 }
1149 }
1150
1151 if (level < 6)
1152 {
1153 node_is_tagged = 1;
1154 }
1155
1156 strcpy(buffer, "*=*");
1157 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1158 attr_present[idx] = 1;
1159
1160 sprintf(buffer, "%s=*", k);
1161 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1162 attr_present[idx] = 2;
1163
1164 sprintf(buffer, "*=%s", v);
1165 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1166 attr_present[idx] = 2;
1167
1168 sprintf(buffer, "%s=%s", k, v);
1169 if ((idx = (int) (long) g_hash_table_lookup(attr_hash, buffer)))
1170 attr_present[idx] = 4;
1171 }
1172
1173 int coord_count;
1174
1175 static void extend_buffer(struct buffer *b)
1176 {
1177 b->malloced += b->malloced_step;
1178 b->base = realloc(b->base, (size_t) b->malloced);
1179 if (b->base == NULL)
1180 {
1181 fprintf(stderr, "realloc of "LONGLONG_FMT" bytes failed\n", (size_t) b->malloced);
1182 exit(1);
1183 }
1184 if (verbose_mode) fprintf(stderr, "extend_buffer: "LONGLONG_FMT" bytes\n", (size_t) b->malloced);
1185 }
1186
1187 int nodeid_last;
1188 GHashTable *way_hash, *waytag_hash;
1189 cfuhash_table_t *way_hash_cfu = NULL;
1190
1191 static void node_buffer_to_hash(int local_thread_num)
1192 {
1193 if (verbose_mode)
1194 fprintf(stderr, "node_buffer_to_hash t:%d nb->size:%lu\n", local_thread_num, node_buffer[local_thread_num].size);
1195 if (verbose_mode)
1196 fprintf(stderr, "node_buffer_to_hash t:%d nb->base:%lu\n", local_thread_num, node_buffer[local_thread_num].base);
1197 if (verbose_mode)
1198 fprintf(stderr, "node_buffer_to_hash t:%d nh:%p\n", local_thread_num, node_hash[local_thread_num]);
1199
1200 int i = 0;
1201 int count2 = node_buffer[local_thread_num].size / sizeof(struct node_item);
1202 struct node_item *ni = (struct node_item *) node_buffer[local_thread_num].base;
1203
1204 if (verbose_mode)
1205 fprintf(stderr, "node_buffer_to_hash %d %d %d\n", local_thread_num, i, count2);
1206
1207 for (i = 0; i < count2; i++)
1208 {
1209 /*
1210 if (i % 5000)
1211 {
1212 fprintf(stderr, "thread #%d fill #%d\n", local_thread_num, i);
1213 }
1214 */
1215 // g_hash_table_insert(node_hash[local_thread_num], (gpointer) (long) (ni[i].id), (gpointer) (long) i);
1216 //cfuhash_put(node_hash_cfu[local_thread_num], &(long long)ni[i].id, xxx);
1217
1218 //fprintf(stderr, "node id:%lld i:%d\n", (long long)ni[i].id, i);
1219 cfuhash_put_data(node_hash_cfu[local_thread_num], (long long)ni[i].id, sizeof(long long), i, 1, NULL);
1220 }
1221
1222 if (verbose_mode)
1223 {
1224 fprintf(stderr, "node_buffer_to_hash ready\n");
1225 }
1226 }
1227
1228 static void waytag_buffer_to_hash(void)
1229 {
1230 int i, count = waytag_buffer.size / sizeof(struct way_tag);
1231 struct way_tag *wt = (struct way_tag *) waytag_buffer.base;
1232 for (i = 0; i < count; i++)
1233 {
1234 // fprintf(stderr, "waytag_hash insert:%lu %lu\n", (long) (wt[i].way_id), (long) i);
1235 g_hash_table_insert(waytag_hash, (gpointer) (long) (wt[i].way_id), (gpointer) (long) i);
1236 }
1237 }
1238
1239 static struct node_item *ni;
1240
1241 void init_node_hash(int threads, int clear)
1242 {
1243 int i;
1244 for (i = 0; i < threads; i++)
1245 {
1246 //if (clear)
1247 //{
1248 //*node_hash[i] = NULL;
1249 //}
1250 //else
1251 //{
1252 //*node_hash[i] = g_hash_table_new(NULL, NULL);
1253 //}
1254 node_hash_cfu[i] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_NODES);
1255 cfuhash_set_flag(node_hash_cfu[i], CFUHASH_FROZEN);
1256 }
1257 }
1258
1259 void fill_hash_node(int local_thread_num)
1260 {
1261 //fprintf(stderr, "001t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1262
1263 if (verbose_mode)
1264 fprintf(stderr, "fill_hash_node - START\n");
1265 //if (node_hash[local_thread_num])
1266 if (node_hash_cfu[local_thread_num])
1267 {
1268 //g_hash_table_destroy(node_hash[local_thread_num]);
1269 //node_hash[local_thread_num] = NULL;
1270 cfuhash_clear(node_hash_cfu[local_thread_num]);
1271 cfuhash_destroy(node_hash_cfu[local_thread_num]);
1272 node_hash_cfu[local_thread_num] = NULL;
1273 }
1274
1275 // node_hash[local_thread_num] = g_hash_table_new(NULL, NULL);
1276 node_hash_cfu[local_thread_num] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_NODES);
1277 cfuhash_set_flag(node_hash_cfu[local_thread_num], CFUHASH_FROZEN);
1278
1279 //fprintf(stderr, "002t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1280 node_buffer_to_hash(local_thread_num);
1281 //fprintf(stderr, "003t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1282 if (verbose_mode)
1283 fprintf(stderr, "fill_hash_node - END\n");
1284 }
1285
1286 void flush_nodes(int final, int local_thread_num)
1287 {
1288 save_buffer("coords.tmp", &node_buffer[local_thread_num], slices * slice_size);
1289 //fprintf(stderr, "004t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1290
1291 if (verbose_mode)
1292 fprintf(stderr, "flush_nodes %d\n", final);
1293 if (!final)
1294 {
1295 // ------ this part optional ------
1296 // //free buffer
1297 //fprintf(stderr, "flush_nodes:# freeing buffer #\n");
1298 //free(node_buffer.base);
1299 //node_buffer.base = NULL;
1300 //node_buffer.malloced = 0;
1301 // //malloc a new buffer
1302 //node_buffer.malloced = 10 * 1024 * 1024;
1303 //node_buffer.base = malloc((size_t) node_buffer.malloced); // 10 MByte
1304 // ------ this part optional ------
1305
1306 // ------- need this always --------
1307
1308 //fprintf(stderr, "005t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1309 node_buffer[local_thread_num].size = 0;
1310 //fprintf(stderr, "006t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1311
1312 //if (node_hash[local_thread_num])
1313 if (node_hash_cfu[local_thread_num])
1314 {
1315 if (verbose_mode) fprintf(stderr,"node_hash size="LONGLONG_FMT"\n", g_hash_table_size (node_hash[local_thread_num]));
1316 //g_hash_table_destroy(node_hash[local_thread_num]);
1317 //node_hash[local_thread_num] = NULL;
1318 cfuhash_clear(node_hash_cfu[local_thread_num]);
1319 cfuhash_destroy(node_hash_cfu[local_thread_num]);
1320 node_hash_cfu[local_thread_num] = NULL;
1321 }
1322 }
1323 if (verbose_mode) fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer[local_thread_num].size);
1324 if (verbose_mode) fprintf(stderr,"node: node_buffer malloced="LONGLONG_FMT"\n", node_buffer[local_thread_num].malloced);
1325 //fprintf(stderr, "007t:%d base:%lu\n", local_thread_num, node_buffer[local_thread_num]);
1326 slices++;
1327 }
1328
1329 double transform_to_geo_lat(int y)
1330 {
1331 double lat = navit_atan(exp(y / 6371000.0)) / M_PI * 360 - 90;
1332 return lat;
1333 }
1334
1335 double transform_to_geo_lon(int x)
1336 {
1337 double lon = (x * 0.00000899322);
1338 return lon;
1339 }
1340
1341 int transform_from_geo_lat(double lat)
1342 {
1343 /* slower */
1344 // int ret = log(tan(M_PI_4 + lat * M_PI / 360)) * 6371000.0;
1345 //fprintf(stderr, "y=%d\n", ret);
1346 /* slower */
1347
1348 /* fast */
1349 int ret = log(tan(M_PI_4 + lat * 0.008726646259971647884618)) * 6371000.0; // already calced (M_PI/360)
1350 /* fast */
1351
1352 return ret;
1353 }
1354
1355 int transform_from_geo_lon(double lon)
1356 {
1357 /* slower */
1358 //int ret = lon * 6371000.0 * M_PI / 180;
1359 // fprintf(stderr, "x=%d\n", ret);
1360 /* slower */
1361
1362 /* fast */
1363 int ret = lon * 111194.9266445587373; // already calced (6371000.0*M_PI/180)
1364 /* fast */
1365
1366 return ret;
1367 }
1368
1369 void osm_add_node(osmid id, double lat, double lon)
1370 {
1371 in_node = 1;
1372 if (node_buffer[0].size + sizeof(struct node_item) > node_buffer[0].malloced)
1373 {
1374 extend_buffer(&node_buffer[0]);
1375 }
1376 attr_strings_clear();
1377 node_is_tagged = 0;
1378 nodeid = id;
1379 item.type = type_point_unkn;
1380 debug_attr_buffer[0] = '\0';
1381 is_in_buffer[0] = '\0';
1382 debug_attr_buffer[0] = '\0';
1383 osmid_attr.type = attr_osm_nodeid;
1384 osmid_attr.len = 3;
1385 osmid_attr_value = id;
1386 if (node_buffer[0].size + sizeof(struct node_item) > slice_size)
1387 {
1388 flush_nodes(0, 0);
1389 }
1390 ni = (struct node_item *) (node_buffer[0].base + node_buffer[0].size);
1391 ni->id = id;
1392 ni->ref_node = 0;
1393 ni->ref_way = 0;
1394 ni->ref_ref = 0;
1395 ni->dummy = 0;
1396
1397 /* slower */
1398 // ni->c.x = lon * 6371000.0 * M_PI / 180;
1399 // ni->c.y = log(tan(M_PI_4 + lat * M_PI / 360)) * 6371000.0;
1400 /* slower */
1401
1402 /* fast */
1403 ni->c.x = lon * 111194.9266445587373; // already calced (6371000.0*M_PI/180)
1404 ni->c.y = log(tan(M_PI_4 + lat * 0.008726646259971647884618)) * 6371000.0; // already calced (M_PI/360)
1405 /* fast */
1406
1407 #ifdef MAPTOOL_USE_SQL
1408
1409 if (sql_counter2 == 0)
1410 {
1411 sqlite3_exec(sql_handle002a, "BEGIN", 0, 0, 0);
1412 sqlite3_exec(sql_handle003a, "BEGIN", 0, 0, 0);
1413 sqlite3_exec(sql_handle002b, "BEGIN", 0, 0, 0);
1414 sqlite3_exec(sql_handle003b, "BEGIN", 0, 0, 0);
1415 }
1416 sql_counter2++;
1417
1418 #ifdef MAPTOOL_SPLIT_NODE_DB
1419 if (nodeid % 2)
1420 {
1421 if ((nodeid & MAPTOOL_SPLIT_NODE_BIT) > 0)
1422 {
1423 sqlite3_bind_int64(stmt_node__2a, 1, nodeid);
1424 sqlite3_bind_double(stmt_node__2a, 2, lat);
1425 sqlite3_bind_double(stmt_node__2a, 3, lon);
1426 sqlite3_step(stmt_node__2a);
1427 sqlite3_reset(stmt_node__2a);
1428 }
1429 else
1430 {
1431 sqlite3_bind_int64(stmt_node__2b, 1, nodeid);
1432 sqlite3_bind_double(stmt_node__2b, 2, lat);
1433 sqlite3_bind_double(stmt_node__2b, 3, lon);
1434 sqlite3_step(stmt_node__2b);
1435 sqlite3_reset(stmt_node__2b);
1436 }
1437 }
1438 else
1439 {
1440 if ((nodeid & MAPTOOL_SPLIT_NODE_BIT) > 0)
1441 {
1442 #endif
1443 sqlite3_bind_int64(stmt_nodea, 1, nodeid);
1444 sqlite3_bind_double(stmt_nodea, 2, lat);
1445 sqlite3_bind_double(stmt_nodea, 3, lon);
1446 sqlite3_step(stmt_nodea);
1447 sqlite3_reset(stmt_nodea);
1448 #ifdef MAPTOOL_SPLIT_NODE_DB
1449 }
1450 else
1451 {
1452 sqlite3_bind_int64(stmt_nodeb, 1, nodeid);
1453 sqlite3_bind_double(stmt_nodeb, 2, lat);
1454 sqlite3_bind_double(stmt_nodeb, 3, lon);
1455 sqlite3_step(stmt_nodeb);
1456 sqlite3_reset(stmt_nodeb);
1457 }
1458 }
1459 #endif
1460
1461 if (sql_counter2 > MAX_ROWS_WO_COMMIT)
1462 {
1463 sql_counter2 = 0;
1464 sqlite3_exec(sql_handle002a, "COMMIT", 0, 0, 0);
1465 sqlite3_exec(sql_handle003a, "COMMIT", 0, 0, 0);
1466 sqlite3_exec(sql_handle002b, "COMMIT", 0, 0, 0);
1467 sqlite3_exec(sql_handle003b, "COMMIT", 0, 0, 0);
1468 //fprintf(stderr, "SQL: COMMIT\n");
1469 }
1470
1471 #endif
1472
1473 //fprintf(stderr,"node: %d %d\n", ni->c.x, ni->c.y);
1474 //fprintf(stderr,"node: %f %f\n", lat, lon);
1475
1476
1477 node_buffer[0].size += sizeof(struct node_item);
1478 //fprintf(stderr,"node: inc node_buffer by %d\n", sizeof(struct node_item));
1479 //fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer.size);
1480
1481 #if 0
1482 //if (!node_hash[0])
1483 if (!node_hash_cfu[0])
1484 {
1485 if (ni->id > nodeid_last)
1486 {
1487 nodeid_last = ni->id;
1488 }
1489 else
1490 {
1491 //fprintf(stderr,"INFO: Nodes out of sequence (new %d vs old %d), adding hash\n", ni->id, nodeid_last);
1492 //node_hash[0] = g_hash_table_new(NULL, NULL);
1493
1494 node_hash_cfu[0] = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_OTHER);
1495 cfuhash_set_flag(node_hash_cfu[0], CFUHASH_FROZEN);
1496
1497 node_buffer_to_hash(0);
1498 }
1499 }
1500 //else if (!g_hash_table_lookup(node_hash[0], (gpointer) (long) (ni->id)))
1501 // else if (!cfuhash_exists(node_hash_cfu[0], (long long) ni->id ))
1502
1503 else if (!cfuhash_exists_data(node_hash_cfu[0], (long long) ni->id, sizeof(long long)))
1504 {
1505 //g_hash_table_insert(node_hash[0], (gpointer) (long) (ni->id), (gpointer) (long) (ni - (struct node_item *) node_buffer[0].base));
1506 //cfuhash_put(node_hash_cfu[0], (gpointer)ni->id, (gpointer)(long) (ni - (struct node_item *) node_buffer[0].base) );
1507 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);
1508
1509 }
1510 else
1511 {
1512 node_buffer[0].size -= sizeof(struct node_item);
1513 if (verbose_mode)
1514 fprintf(stderr, "node: Decr. node_buffer by %d\n", sizeof(struct node_item));
1515 if (verbose_mode) fprintf(stderr,"node: node_buffer size="LONGLONG_FMT"\n", node_buffer[0].size);
1516 nodeid = 0;
1517 }
1518 #endif
1519
1520 }
1521
1522 void clear_node_item_buffer(void)
1523 {
1524 int j, count = node_buffer[0].size / sizeof(struct node_item);
1525 struct node_item *ni = (struct node_item *) (node_buffer[0].base);
1526 for (j = 0; j < count; j++)
1527 {
1528 ni[j].ref_way = 0;
1529 }
1530 }
1531
1532 static struct node_item *
1533 node_item_get_fast(int id, int local_thread_num)
1534 {
1535 int i;
1536 void *p_tmp;
1537 int rr;
1538 size_t data_size2 = sizeof(int *);
1539 struct node_item *ni = (struct node_item *) (node_buffer[local_thread_num].base);
1540
1541 //if (node_hash[local_thread_num])
1542 if (node_hash_cfu[local_thread_num])
1543 {
1544 //p_tmp = (gpointer)(g_hash_table_lookup(node_hash[local_thread_num], (gpointer) (long) id));
1545 rr = cfuhash_get_data(node_hash_cfu[local_thread_num], (long long) id, sizeof(long long), &p_tmp, &data_size2);
1546 if (rr == 1)
1547 {
1548 // printf("got value %ld\n", p_tmp);
1549 i = p_tmp;
1550 return ni + i;
1551 }
1552
1553 //if (p_tmp != NULL)
1554 //{
1555 // // only if node is found in hash!
1556 // i = (int) (long) (p_tmp);
1557 // return ni + i;
1558 //}
1559 }
1560 return NULL;
1561 }
1562
1563 static struct node_item *
1564 node_item_get(int id, int local_thread_num)
1565 {
1566 struct node_item *ni = (struct node_item *) (node_buffer[local_thread_num].base);
1567 int count = node_buffer[local_thread_num].size / sizeof(struct node_item);
1568 int interval = count / 4;
1569 int p = count / 2;
1570 if (interval == 0)
1571 {
1572 // If fewer than 4 nodes defined so far set interval to 1 to
1573 // avoid infinite loop
1574 interval = 1;
1575 }
1576
1577 if (node_hash[local_thread_num])
1578 {
1579 int i;
1580 void* p_tmp = (gpointer)(g_hash_table_lookup(node_hash[local_thread_num], (gpointer) (long) id));
1581 if (p_tmp != NULL)
1582 {
1583 // only if node is found in hash!
1584 i = (int) (long) (p_tmp);
1585 return ni + i;
1586 }
1587 }
1588
1589 fprintf(stderr, "[THREAD] #%d **BAD**\n", local_thread_num);
1590 // return NULL;
1591
1592 if (ni[0].id > id)
1593 {
1594 return NULL;
1595 }
1596
1597 if (ni[count - 1].id < id)
1598 {
1599 return NULL;
1600 }
1601
1602 while (ni[p].id != id)
1603 {
1604 #if 0
1605 fprintf(stderr,"p=%d count=%d interval=%d id=%d ni[p].id=%d\n", p, count, interval, id, ni[p].id);
1606 #endif
1607 if (ni[p].id < id)
1608 {
1609 p += interval;
1610 if (interval == 1)
1611 {
1612 if (p >= count)
1613 return NULL;
1614
1615 if (ni[p].id > id)
1616 return NULL;
1617 }
1618 else
1619 {
1620 if (p >= count)
1621 p = count - 1;
1622 }
1623 }
1624 else
1625 {
1626 p -= interval;
1627 if (interval == 1)
1628 {
1629 if (p < 0)
1630 return NULL;
1631 if (ni[p].id < id)
1632 return NULL;
1633 }
1634 else
1635 {
1636 if (p < 0)
1637 p = 0;
1638 }
1639 }
1640
1641 if (interval > 1)
1642 interval /= 2;
1643 }
1644
1645 return &ni[p];
1646 }
1647
1648 static int load_node(FILE *coords, int p, struct node_item *ret)
1649 {
1650 fseek(coords, p * sizeof(struct node_item), SEEK_SET);
1651 if (fread(ret, sizeof(*ret), 1, coords) != 1)
1652 {
1653 fprintf(stderr, "read failed\n");
1654 return 0;
1655 }
1656 return 1;
1657 }
1658
1659 void osm_add_way(osmid id)
1660 {
1661 static int wayid_last;
1662
1663 in_way = 1;
1664 wayid = id;
1665 coord_count = 0;
1666 attr_strings_clear();
1667 item.type = type_street_unkn;
1668 debug_attr_buffer[0] = '\0';
1669 maxspeed_attr_value = 0;
1670 flags_attr_value = 0;
1671 memset(flags, 0, sizeof(flags));
1672 debug_attr_buffer[0] = '\0';
1673 osmid_attr_value = id;
1674 if (wayid < wayid_last && !way_hash_cfu)
1675 {
1676 if (verbose_mode)
1677 {
1678 fprintf(stderr, "INFO: Ways out of sequence (new %d vs old %d), adding hash\n", wayid, wayid_last);
1679 }
1680 // way_hash = g_hash_table_new(NULL, NULL);
1681 way_hash_cfu = cfuhash_new_with_initial_size(CFUHASH_BUCKETS_WAYS);
1682 cfuhash_set_flag(node_hash_cfu, CFUHASH_FROZEN);
1683
1684 }
1685 wayid_last = wayid;
1686 }
1687
1688 char relation_type[BUFFER_SIZE];
1689 char iso_code[BUFFER_SIZE];
1690 int admin_level;
1691 int boundary;
1692
1693 void osm_add_relation(osmid id)
1694 {
1695 current_id = id;
1696 in_relation = 1;
1697 debug_attr_buffer[0] = '\0';
1698 relation_type[0] = '\0';
1699 iso_code[0] = '\0';
1700 admin_level = -1;
1701 boundary = 0;
1702 item_bin_init(item_bin_2, type_none);
1703 item_bin_add_attr_longlong(item_bin_2, attr_osm_relationid, current_id);
1704 }
1705
1706 static int country_id_from_iso2(char *iso)
1707 {
1708 int ret = 0;
1709 if (iso)
1710 {
1711 // make lowercase
1712 int i99;
1713 for (i99 = 0; iso[i99]; i99++)
1714 {
1715 iso[i99] = tolower(iso[i99]);
1716 }
1717 struct country_search *search;
1718 struct attr country_iso2, country_id;
1719 struct item *item;
1720 country_iso2.type = attr_country_iso2;
1721 country_iso2.u.str = iso;
1722 search = country_search_new(&country_iso2, 0);
1723 if ((item = country_search_get_item(search)) && item_attr_get(item, attr_country_id, &country_id))
1724 {
1725 ret = country_id.u.num;
1726 if (debug_itembin(3))
1727 {
1728 fprintf(stderr, "country id(1)=%d\n", ret);
1729 }
1730 }
1731 else
1732 {
1733 if (debug_itembin(3))
1734 {
1735 fprintf(stderr, "country id(2)=%d\n", ret);
1736 }
1737 }
1738 country_search_destroy(search);
1739 }
1740 return ret;
1741 }
1742
1743 static struct country_table *
1744 country_from_countryid(int id)
1745 {
1746 int i;
1747 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
1748 {
1749 //if (debug_itembin(3))
1750 //{
1751 // fprintf(stderr,"== country_from_countryid: %d ==\n", country_table[i].countryid);
1752 //}
1753
1754 if (country_table[i].countryid == id)
1755 {
1756 return &country_table[i];
1757 }
1758 }
1759
1760 //if (debug_itembin(3))
1761 //{
1762 // fprintf(stderr,"== country_from_countryid: return NULL ==\n");
1763 //}
1764
1765 return NULL;
1766 }
1767
1768 struct country_table *
1769 country_from_iso2(char *iso)
1770 {
1771 return country_from_countryid(country_id_from_iso2(iso));
1772 }
1773
1774 static int get_relation_member(char *str, struct relation_member *memb)
1775 {
1776 int len;
1777 sscanf(str,"%d:"LONGLONG_FMT":%n",&memb->type,&memb->id,&len);
1778 memb->role = str + len;
1779 return 1;
1780 }
1781
1782 #if 0
1783 void report(io, markers, reporttriangles, reportneighbors, reportsegments, reportedges, reportnorms)
1784 struct triangulateio *io;int markers;int reporttriangles;int reportneighbors;int reportsegments;int reportedges;int reportnorms;
1785 {
1786 int i, j;
1787
1788 for (i = 0; i < io->numberofpoints; i++)
1789 {
1790 fprintf(stderr, "Point %4d:", i);
1791 for (j = 0; j < 2; j++)
1792 {
1793 fprintf(stderr, " %.6g", io->pointlist[i * 2 + j]);
1794 }
1795 if (io->numberofpointattributes > 0)
1796 {
1797 fprintf(stderr, " attributes");
1798 }
1799 for (j = 0; j < io->numberofpointattributes; j++)
1800 {
1801 fprintf(stderr, " %.6g", io->pointattributelist[i * io->numberofpointattributes + j]);
1802 }
1803 if (markers)
1804 {
1805 fprintf(stderr, " marker %d\n", io->pointmarkerlist[i]);
1806 }
1807 else
1808 {
1809 fprintf(stderr, "\n");
1810 }
1811 }
1812 fprintf(stderr, "\n");
1813
1814 if (reporttriangles || reportneighbors)
1815 {
1816 for (i = 0; i < io->numberoftriangles; i++)
1817 {
1818 if (reporttriangles)
1819 {
1820 fprintf(stderr, "Triangle %4d points:", i);
1821 for (j = 0; j < io->numberofcorners; j++)
1822 {
1823 fprintf(stderr, " %4d", io->trianglelist[i * io->numberofcorners + j]);
1824 }
1825 if (io->numberoftriangleattributes > 0)
1826 {
1827 fprintf(stderr, " attributes");
1828 }
1829 for (j = 0; j < io->numberoftriangleattributes; j++)
1830 {
1831 fprintf(stderr, " %.6g", io->triangleattributelist[i * io->numberoftriangleattributes + j]);
1832 }
1833 fprintf(stderr, "\n");
1834 }
1835 if (reportneighbors)
1836 {
1837 fprintf(stderr, "Triangle %4d neighbors:", i);
1838 for (j = 0; j < 3; j++)
1839 {
1840 fprintf(stderr, " %4d", io->neighborlist[i * 3 + j]);
1841 }
1842 fprintf(stderr, "\n");
1843 }
1844 }
1845 fprintf(stderr, "\n");
1846 }
1847
1848 if (reportsegments)
1849 {
1850 for (i = 0; i < io->numberofsegments; i++)
1851 {
1852 fprintf(stderr, "Segment %4d points:", i);
1853 for (j = 0; j < 2; j++)
1854 {
1855 fprintf(stderr, " %4d", io->segmentlist[i * 2 + j]);
1856 }
1857 if (markers)
1858 {
1859 fprintf(stderr, " marker %d\n", io->segmentmarkerlist[i]);
1860 }
1861 else
1862 {
1863 fprintf(stderr, "\n");
1864 }
1865 }
1866 fprintf(stderr, "\n");
1867 }
1868
1869 if (reportedges)
1870 {
1871 for (i = 0; i < io->numberofedges; i++)
1872 {
1873 fprintf(stderr, "Edge %4d points:", i);
1874 for (j = 0; j < 2; j++)
1875 {
1876 fprintf(stderr, " %4d", io->edgelist[i * 2 + j]);
1877 }
1878 if (reportnorms && (io->edgelist[i * 2 + 1] == -1))
1879 {
1880 for (j = 0; j < 2; j++)
1881 {
1882 fprintf(stderr, " %.6g", io->normlist[i * 2 + j]);
1883 }
1884 }
1885 if (markers)
1886 {
1887 fprintf(stderr, " marker %d\n", io->edgemarkerlist[i]);
1888 }
1889 else
1890 {
1891 fprintf(stderr, "\n");
1892 }
1893 }
1894 fprintf(stderr, "\n");
1895 }
1896 }
1897
1898 #endif
1899
1900 void get_lat_lon_for_node(long long nd, struct node_lat_lon *node);
1901
1902 void get_lat_lon_way_first_node(long long way_id, struct node_lat_lon *node)
1903 {
1904 int rc2;
1905 sqlite3_stmt *st;
1906 long long nd = 0;
1907
1908
1909 node->valid = 0;
1910
1911 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
1912 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
1913 {
1914 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
1915 {
1916 #endif
1917 st = stmt_sel001;
1918 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
1919 }
1920 else
1921 {
1922 st = stmt_sel001b;
1923 }
1924 }
1925 else
1926 {
1927 if ((way_id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
1928 {
1929 st = stmt_sel001__2;
1930 }
1931 else
1932 {
1933 st = stmt_sel001__2b;
1934 }
1935 }
1936 #endif
1937 sqlite3_bind_int64(st, 1, way_id);
1938
1939 // execute the statement
1940 rc2 = sqlite3_step(st);
1941 switch (rc2)
1942 {
1943 case SQLITE_DONE:
1944 break;
1945 case SQLITE_ROW:
1946 nd = sqlite3_column_int64(st, 0);
1947 break;
1948 default:
1949 fprintf(stderr, "Error: %d\n", rc2);
1950 break;
1951 }
1952 sqlite3_reset(st);
1953
1954 if (nd != 0)
1955 {
1956 get_lat_lon_for_node(nd, node);
1957 }
1958 }
1959
1960 void get_lat_lon_for_node(long long nd, struct node_lat_lon *node)
1961 {
1962 int rc2;
1963
1964 // now read the coords of this node
1965 node->valid = 0;
1966
1967 #ifdef MAPTOOL_USE_SQL
1968
1969 #ifdef MAPTOOL_SPLIT_NODE_DB
1970 if (nd % 2)
1971 {
1972 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
1973 {
1974 sqlite3_bind_int64(stmt_sel002__2a, 1, nd);
1975 }
1976 else
1977 {
1978 sqlite3_bind_int64(stmt_sel002__2b, 1, nd);
1979 }
1980 }
1981 else
1982 {
1983 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
1984 {
1985 #endif
1986 sqlite3_bind_int64(stmt_sel002a, 1, nd);
1987 #ifdef MAPTOOL_SPLIT_NODE_DB
1988 }
1989 else
1990 {
1991 sqlite3_bind_int64(stmt_sel002b, 1, nd);
1992 }
1993 }
1994 #endif
1995
1996 rc2 = 0;
1997 //do
1998 //{
1999 #ifdef MAPTOOL_SPLIT_NODE_DB
2000 if (nd % 2)
2001 {
2002 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2003 {
2004 rc2 = sqlite3_step(stmt_sel002__2a);
2005 }
2006 else
2007 {
2008 rc2 = sqlite3_step(stmt_sel002__2b);
2009 }
2010 }
2011 else
2012 {
2013 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2014 {
2015 #endif
2016 rc2 = sqlite3_step(stmt_sel002a);
2017 #ifdef MAPTOOL_SPLIT_NODE_DB
2018 }
2019 else
2020 {
2021 rc2 = sqlite3_step(stmt_sel002b);
2022 }
2023 }
2024 #endif
2025 //cols2 = sqlite3_column_count(stmt_sel002);
2026 switch (rc2)
2027 {
2028 //case SQLITE_DONE:
2029 // break;
2030 case SQLITE_ROW:
2031
2032 #ifdef MAPTOOL_SPLIT_NODE_DB
2033 if (nd % 2)
2034 {
2035 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2036 {
2037 node->lat = (double) sqlite3_column_double(stmt_sel002__2a, 0);
2038 node->lon = (double) sqlite3_column_double(stmt_sel002__2a, 1);
2039 }
2040 else
2041 {
2042 node->lat = (double) sqlite3_column_double(stmt_sel002__2b, 0);
2043 node->lon = (double) sqlite3_column_double(stmt_sel002__2b, 1);
2044 }
2045 }
2046 else
2047 {
2048 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2049 {
2050 #endif
2051 node->lat = (double) sqlite3_column_double(stmt_sel002a, 0);
2052 node->lon = (double) sqlite3_column_double(stmt_sel002a, 1);
2053 #ifdef MAPTOOL_SPLIT_NODE_DB
2054 }
2055 else
2056 {
2057 node->lat = (double) sqlite3_column_double(stmt_sel002b, 0);
2058 node->lon = (double) sqlite3_column_double(stmt_sel002b, 1);
2059 }
2060 }
2061 #endif
2062 node->valid = 1;
2063 break;
2064 default:
2065 fprintf(stderr, "get_lat_lon_for_node:SQL Error: %d nodeid:%lld\n", rc2, nd);
2066 break;
2067 }
2068 //}
2069 //while (rc2 == SQLITE_ROW);
2070 #ifdef MAPTOOL_SPLIT_NODE_DB
2071 if (nd % 2)
2072 {
2073 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2074 {
2075 sqlite3_reset(stmt_sel002__2a);
2076 }
2077 else
2078 {
2079 sqlite3_reset(stmt_sel002__2b);
2080 }
2081 }
2082 else
2083 {
2084 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2085 {
2086 #endif
2087 sqlite3_reset(stmt_sel002a);
2088 #ifdef MAPTOOL_SPLIT_NODE_DB
2089 }
2090 else
2091 {
2092 sqlite3_reset(stmt_sel002b);
2093 }
2094 }
2095 #endif
2096 #endif
2097
2098 }
2099
2100 void catch_signal(int param)
2101 {
2102 // fprintf (stderr, "Terminating program...\n");
2103 fprintf(stderr, "****** got SIGSEGV ******\n");
2104 THROW(MAPTOOL_00002_EXCEPTION);
2105 // exit(1);
2106 }
2107
2108 struct way_and_endnodes
2109 {
2110 unsigned long long way_id;
2111 unsigned long long first_node_id;
2112 unsigned long long last_node_id;
2113 int reverse;
2114 int role;
2115 GList *nodes;
2116 };
2117
2118 // if same value -> 0
2119 // otherwise -> 1
2120 gint comp_func(gconstpointer item1, gconstpointer item2)
2121 {
2122 long long a;
2123 long long b;
2124
2125 a = *((long long *) item1);
2126 b = *((long long *) item2);
2127
2128 //fprintf(stderr," item1=%lld\n", a);
2129 //fprintf(stderr," item2=%lld\n", b);
2130
2131 if (a == b)
2132 {
2133 return 0;
2134 }
2135
2136 return 1;
2137 }
2138
2139 // return 0 if item is not found in GList
2140 int glist_find_already_inside(GList* list, gconstpointer item)
2141 {
2142 if (g_list_find_custom(list, item, (GCompareFunc) comp_func) == NULL)
2143 {
2144 return 0;
2145 }
2146 return 1;
2147 }
2148
2149 void osm_end_relation(struct maptool_osm *osm)
2150 {
2151 //fprintf(stderr, "XXX000\n");
2152
2153 struct item_bin *item_bin_tri;
2154 struct coord coord_tri;
2155 struct node_lat_lon node_coords;
2156 int i9;
2157 int j9;
2158 int tmp9;
2159
2160 int save_relation = 0;
2161 int save_relations_other_method = 0;
2162 int tag_id = 1;
2163 char *str = NULL;
2164 int triangulate_not_done = 1;
2165 int problem = 0;
2166 struct relation_member memb;
2167 struct relation_member memb2;
2168 struct way_and_endnodes *way_nodes1;
2169 struct way_and_endnodes *way_nodes2;
2170 int found_new_way = 0;
2171 gpointer *gptr;
2172 int rc = 0;
2173 int rc2 = 0;
2174 int cols;
2175 int col;
2176 int first_node_found = 0;
2177 long long nd;
2178 long long first_nd;
2179 long long search_nd;
2180 long long *ndptr;
2181
2182 /*
2183 long lat_nd;
2184 long lon_nd;
2185 */
2186
2187 double lat_nd;
2188 double lon_nd;
2189
2190 int number_of_points_in_poly = 0;
2191 int number_of_points_current_poly = 0;
2192 int exception = 0;
2193 int role_ = 0;
2194
2195 double factor = 10000000;
2196
2197 // triangulate vars
2198 #if 0
2199 struct triangulateio in, mid, out, vorout;
2200 #endif
2201
2202 #ifdef MAPTOOL_TRIANGULATE
2203 GPtrArray *current_points;
2204 GPtrArray *current_hole;
2205 P2tCDT *cdt;
2206 P2trCDT *rcdt;
2207 P2trRefiner *refiner;
2208 P2tTrianglePtrArray triangles;
2209 gint refine_max_steps = 1000;
2210 double x;
2211 double y;
2212 int i;
2213 int j;
2214 GHashTable *point_map;
2215 P2trCDT *rmesh;
2216 #endif
2217 GList *all_holes;
2218
2219 int count_of_outer_ways = 0;
2220
2221 GList *unsorted_outer_ways = NULL;
2222 GList *unsorted_inner_ways = NULL;
2223 GList *sorted_outer_ways = NULL;
2224 GList *temp_nodes_list = NULL;
2225 all_holes = NULL;
2226
2227 // -- save relations for riverbanks --
2228 while ((str = item_bin_get_attr(item_bin_2, attr_osm_tag, str)))
2229 {
2230 if (!strcmp(str, "waterway=riverbank"))
2231 {
2232 // fprintf(stderr,"k:v=%s\n", str);
2233 save_relation = 1;
2234 save_relations_other_method = 0;
2235 tag_id = 1;
2236 }
2237 else if (!strcmp(str, "natural=water"))
2238 {
2239 save_relation = 1;
2240 save_relations_other_method = 0;
2241 tag_id = 2;
2242 }
2243 /*
2244 else if (!strcmp(str, "water=river"))
2245 {
2246 save_relation = 1;
2247 tag_id = 3;
2248 }
2249 */
2250 else if (!strcmp(str, "natural=lake"))
2251 {
2252 save_relation = 1;
2253 save_relations_other_method = 0;
2254 tag_id = 4;
2255 }
2256 else if (!strcmp(str, "landuse=forest"))
2257 {
2258 save_relation = 1;
2259 save_relations_other_method = 1;
2260 tag_id = 5;
2261 }
2262 else if (!strcmp(str, "natural=wood"))
2263 {
2264 save_relation = 1;
2265 save_relations_other_method = 1;
2266 tag_id = 6;
2267 }
2268 else if (!strcmp(str, "building=yes"))
2269 {
2270 save_relation = 1;
2271 save_relations_other_method = 0;
2272 tag_id = 9;
2273 //fprintf(stderr,"k:v=%s id=%lld\n", str, current_id);
2274 }
2275
2276 }
2277
2278 if (save_relation == 1)
2279 {
2280
2281 #ifdef MAPTOOL_TRIANGULATE
2282
2283 if (save_relations_other_method == 0)
2284 {
2285
2286 //fprintf(stderr, "START relation id=%lld tag=%d\n", current_id, tag_id);
2287
2288 sqlite3_stmt *st;
2289
2290 triangulate_not_done = 1;
2291 str = NULL;
2292 number_of_points_in_poly = 0;
2293 count_of_outer_ways = 0;
2294 //fprintf(stderr, "\n\n----------START---------\n");
2295 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
2296 {
2297 if (get_relation_member(str, &memb))
2298 {
2299 if (!strcmp(memb.role, "outer"))
2300 {
2301 if (memb.type == 2)
2302 {
2303 count_of_outer_ways++;
2304 }
2305 }
2306 }
2307 }
2308 //fprintf(stderr, "number of outer ways: %d\n", count_of_outer_ways);
2309
2310 str = NULL;
2311 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
2312 {
2313 if (get_relation_member(str, &memb))
2314 {
2315 role_=0;
2316 if (!strcmp(memb.role, "outer"))
2317 {
2318 role_=1;
2319 //fprintf(stderr, "1:role=%s id=%lld\n", memb.role, memb.id);
2320 }
2321 else if (!strcmp(memb.role, "inner"))
2322 {
2323 role_=2;
2324 //fprintf(stderr, "2:role=%s id=%lld\n", memb.role, memb.id);
2325 }
2326
2327 if ((memb.type == 2) && ( (role_ == 1)||(role_ == 2) )) // use "outer" and "inner" ways
2328
2329 {
2330 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2331 way_nodes1->nodes = NULL;
2332 // get id for last and first nodes
2333
2334 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2335 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
2336 {
2337 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2338 {
2339 #endif
2340 st = stmt_sel001;
2341 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
2342 }
2343 else
2344 {
2345 st = stmt_sel001b;
2346 }
2347 }
2348 else
2349 {
2350 if ((memb.id & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
2351 {
2352 st = stmt_sel001__2;
2353 }
2354 else
2355 {
2356 st = stmt_sel001__2b;
2357 }
2358 }
2359 #endif
2360 sqlite3_bind_int64(st, 1, memb.id);
2361
2362 first_node_found = 0;
2363 way_nodes1->first_node_id = 0;
2364 nd = 0;
2365 number_of_points_current_poly = 0;
2366 // execute the statement
2367 do
2368 {
2369 rc = sqlite3_step(st);
2370 // cols = sqlite3_column_count(st);
2371 switch (rc)
2372 {
2373 case SQLITE_DONE:
2374 break;
2375 case SQLITE_ROW:
2376 nd = sqlite3_column_int64(st, 0);
2377 number_of_points_current_poly++;
2378 //fprintf(stderr, "------ %s = %d\n", sqlite3_column_name(st, 0), nd ? nd : "NULL");
2379 if (first_node_found == 0)
2380 {
2381 way_nodes1->first_node_id = nd;
2382 way_nodes1->role = role_;
2383 first_node_found = 1;
2384 //fprintf(stderr, "------ first node found\n");
2385 }
2386 ndptr = malloc(sizeof(long long));
2387 *ndptr = nd;
2388 way_nodes1->nodes = g_list_append(way_nodes1->nodes, ndptr);
2389 break;
2390 default:
2391 fprintf(stderr, "Error: %d\n", rc);
2392 break;
2393 }
2394 }
2395 while (rc == SQLITE_ROW);
2396
2397 // dont count the last node of each way
2398 number_of_points_current_poly--;
2399
2400 sqlite3_reset(st);
2401
2402 way_nodes1->last_node_id = nd;
2403 way_nodes1->way_id = memb.id;
2404
2405 if (way_nodes1->last_node_id == 0)
2406 {
2407 if (way_nodes1->nodes)
2408 {
2409 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2410 while (way_nodes1->nodes)
2411 {
2412 ndptr = way_nodes1->nodes->data;
2413 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
2414 g_free(ndptr);
2415 ndptr = NULL;
2416 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2417 }
2418 }
2419 g_free(way_nodes1);
2420 }
2421 else if (way_nodes1->last_node_id == way_nodes1->first_node_id)
2422 {
2423 //fprintf(stderr, "ln id=%lld, fn id=%lld\n", way_nodes1->last_node_id, way_nodes1->first_node_id);
2424
2425 if (way_nodes1->nodes)
2426 {
2427 if (role_ == 2)
2428 {
2429 //fprintf(stderr, "role=2\n");
2430 unsorted_inner_ways = g_list_append(unsorted_inner_ways, way_nodes1);
2431 }
2432 else if (role_ == 1)
2433 {
2434 if (count_of_outer_ways == 1)
2435 {
2436 // we have only 1 outer way, it must be a closed loop. add it
2437 unsorted_outer_ways = g_list_append(unsorted_outer_ways, way_nodes1);
2438 number_of_points_in_poly = number_of_points_in_poly + number_of_points_current_poly;
2439 //fprintf(stderr, "w,sn,en %lld,%lld,%lld\n", way_nodes1->way_id, way_nodes1->first_node_id, way_nodes1->last_node_id);
2440 //fprintf(stderr, "num points=%d\n", number_of_points_current_poly);
2441 //fprintf(stderr, "SUM points=%d\n", number_of_points_in_poly);
2442 }
2443 else
2444 {
2445 // we have an outer way that is a loop (remove it)
2446 //fprintf(stderr, "remove node ... start\n");
2447 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2448 while (way_nodes1->nodes)
2449 {
2450 //fprintf(stderr, "remove node\n");
2451 ndptr = way_nodes1->nodes->data;
2452 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
2453 g_free(ndptr);
2454 ndptr = NULL;
2455 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2456 }
2457 g_free(way_nodes1);
2458 //fprintf(stderr, "remove node ... ready\n");
2459 }
2460 }
2461 }
2462 }
2463 else
2464 {
2465 if (role_ == 1)
2466 {
2467 unsorted_outer_ways = g_list_append(unsorted_outer_ways, way_nodes1);
2468 number_of_points_in_poly = number_of_points_in_poly + number_of_points_current_poly;
2469 }
2470 //fprintf(stderr, "w,sn,en %lld,%lld,%lld\n", way_nodes1->way_id, way_nodes1->first_node_id, way_nodes1->last_node_id);
2471 //fprintf(stderr, "num points=%d\n", number_of_points_current_poly);
2472 //fprintf(stderr, "SUM points=%d\n", number_of_points_in_poly);
2473 }
2474 }
2475 }
2476 }
2477
2478 // now sort ways to have a linked polygon
2479 if (unsorted_outer_ways)
2480 {
2481 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2482 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2483 way_nodes2 = (struct way_and_endnodes *) unsorted_outer_ways->data;
2484 way_nodes1->way_id = way_nodes2->way_id;
2485 //fprintf(stderr, "(START)way id=%llu\n", way_nodes1->way_id);
2486 way_nodes1->last_node_id = way_nodes2->last_node_id;
2487 way_nodes1->first_node_id = way_nodes2->first_node_id;
2488 // only copy over the pointer to the node list!!
2489 way_nodes1->nodes = way_nodes2->nodes;
2490 // first node is always in the right order
2491 way_nodes1->reverse = 0;
2492
2493 gptr = unsorted_outer_ways->data;
2494 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
2495 // save the very first node of the poly
2496 first_nd = way_nodes1->first_node_id;
2497 // what node we want to find
2498 search_nd = way_nodes1->last_node_id;
2499 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, gptr);
2500 g_free(gptr);
2501 gptr = NULL;
2502
2503 //fprintf(stderr,"remaining=%d sorted=%d\n", g_list_length(unsorted_outer_ways), g_list_length(sorted_outer_ways));
2504 }
2505
2506 if (unsorted_outer_ways)
2507 {
2508
2509 problem = 0;
2510 //fprintf(stderr, "start length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
2511
2512 while ((g_list_length(unsorted_outer_ways) > 0) && (problem == 0))
2513 {
2514 //fprintf(stderr, "loop thru remaining ways 1\n");
2515
2516 // loop thru all the remaining ways
2517 found_new_way = 0;
2518 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2519 while (unsorted_outer_ways)
2520 {
2521 //fprintf(stderr, "loop thru remaining ways 2 search=%lld\n", search_nd);
2522
2523 if (unsorted_outer_ways->data)
2524 {
2525 way_nodes2 = (struct way_and_endnodes *) unsorted_outer_ways->data;
2526 if (way_nodes2->first_node_id == search_nd)
2527 {
2528
2529 if (way_nodes2->last_node_id == first_nd)
2530 {
2531 if (g_list_length(unsorted_outer_ways) > 1)
2532 {
2533 // we have close the poly, but there are still "ways" left
2534 // sounds broken
2535 problem = 1;
2536 break;
2537 }
2538 }
2539
2540 // what node we want to find
2541 search_nd = way_nodes2->last_node_id;
2542
2543 // now move the way from unsorted to sorted list
2544 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2545 way_nodes1->way_id = way_nodes2->way_id;
2546
2547 // ok next way found
2548 //fprintf(stderr, "(F)way id=%llu\n", way_nodes1->way_id);
2549 //fprintf(stderr, "now search id=%llu\n", search_nd);
2550
2551 way_nodes1->last_node_id = way_nodes2->last_node_id;
2552 way_nodes1->first_node_id = way_nodes2->first_node_id;
2553 // only copy over the pointer to the node list!!
2554 way_nodes1->nodes = way_nodes2->nodes;
2555 // first node is always in the right order
2556 way_nodes1->reverse = 0;
2557 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
2558 if (unsorted_outer_ways)
2559 {
2560 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2561 }
2562 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, way_nodes2);
2563 g_free(way_nodes2);
2564 way_nodes2 = NULL;
2565
2566 found_new_way = 1;
2567 //fprintf(stderr, "length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
2568 break;
2569 }
2570 else if (way_nodes2->last_node_id == search_nd)
2571 {
2572 if (way_nodes2->first_node_id == first_nd)
2573 {
2574 if (g_list_length(unsorted_outer_ways) > 1)
2575 {
2576 // we have closed the poly, but there are still "ways" left
2577 // sounds broken
2578 problem = 1;
2579 break;
2580 }
2581 }
2582
2583 // what node we want to find
2584 search_nd = way_nodes2->first_node_id;
2585
2586 // now move the way from unsorted to sorted list
2587 way_nodes1 = (struct way_and_endnodes *) malloc(sizeof(struct way_and_endnodes));
2588 way_nodes1->way_id = way_nodes2->way_id;
2589
2590 // ok next way found
2591 //fprintf(stderr, "(R)way id=%llu\n", way_nodes1->way_id);
2592 //fprintf(stderr, "now search id=%llu\n", search_nd);
2593
2594 way_nodes1->first_node_id = way_nodes2->last_node_id;
2595 way_nodes1->last_node_id = way_nodes2->first_node_id;
2596 // only copy over the pointer to the node list!!
2597 way_nodes1->nodes = way_nodes2->nodes;
2598 way_nodes1->nodes = g_list_reverse(way_nodes1->nodes);
2599 // first node is always in the right order
2600 way_nodes1->reverse = 1;
2601 sorted_outer_ways = g_list_append(sorted_outer_ways, way_nodes1);
2602 if (unsorted_outer_ways)
2603 {
2604 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
2605 }
2606 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, way_nodes2);
2607 g_free(way_nodes2);
2608 way_nodes2 = NULL;
2609
2610 found_new_way = 1;
2611 //fprintf(stderr, "length unsorted_outer_ways=%d\n", g_list_length(unsorted_outer_ways));
2612 break;
2613 }
2614 }
2615 unsorted_outer_ways = g_list_next(unsorted_outer_ways);
2616 }
2617 //fprintf(stderr, "after while loop 2\n");
2618
2619 if (found_new_way == 0)
2620 {
2621 // some problem with this multipolygon
2622 fprintf(stderr, "relation id=%lld\n", current_id);
2623 fprintf(stderr, "some problem with this multipolygon\n");
2624 triangulate_not_done = 1;
2625 problem = 1;
2626 break;
2627 }
2628 }
2629 //fprintf(stderr, "after while loop 1\n");
2630
2631 }
2632
2633 // now at last check if very first node matches the last found node
2634 // and less than xxx 1000 points in polygon
2635 // if ((search_nd == first_nd) && (number_of_points_in_poly > 2) && (number_of_points_in_poly < 1000))
2636 if ((problem == 0) && ((search_nd == first_nd) && (number_of_points_in_poly > 2) && (number_of_points_in_poly < 30000)))
2637 {
2638
2639 //fprintf(stderr, "in relation id=%lld\n", current_id);
2640
2641 point_map = g_hash_table_new(g_direct_hash, g_direct_equal);
2642 current_points = g_ptr_array_new();
2643
2644 number_of_points_in_poly = 0;
2645
2646 sorted_outer_ways = g_list_first(sorted_outer_ways);
2647 while (sorted_outer_ways)
2648 {
2649 if (sorted_outer_ways->data)
2650 {
2651 way_nodes1 = (struct way_and_endnodes *) sorted_outer_ways->data;
2652 // now read all the nodes of this way
2653 //**alread in GList** //sqlite3_bind_int64(stmt_sel001, 1, way_nodes1->way_id);
2654 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2655 first_node_found = 0;
2656 while (way_nodes1->nodes)
2657 {
2658 ndptr = (long long *) way_nodes1->nodes->data;
2659 nd = *ndptr;
2660
2661 get_lat_lon_for_node(nd, &node_coords);
2662 if (node_coords.valid == 1)
2663 {
2664 // lat
2665 lat_nd = node_coords.lat;
2666 // lon
2667 lon_nd = node_coords.lon;
2668
2669 if (first_node_found == 0)
2670 {
2671 first_node_found = 1;
2672 }
2673 else
2674 {
2675 y = lat_nd * factor;
2676 x = lon_nd * factor;
2677
2678 // ------ ok use this node and coords for triangulation ----
2679 //fprintf(stderr, "nd,lat,lon %lld,%f,%f\n", nd,lat_nd,lon_nd);
2680 //fprintf(stderr, "x,y %f,%f\n", x, y);
2681
2682 g_ptr_array_add(current_points, p2t_point_new_dd(x, y));
2683 }
2684 }
2685 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2686 }
2687 }
2688 //sqlite3_reset(st);
2689
2690 sorted_outer_ways = g_list_next(sorted_outer_ways);
2691 }
2692
2693 //fprintf(stderr, "after sorting ways\n");
2694
2695
2696 exception = 0;
2697
2698 TRY
2699 {
2700 // catch SIGSEGV !!
2701 signal(SIGSEGV, catch_signal);
2702
2703 // alternate signal stack ------------
2704 //ss.ss_size = SIGSTKSZ;
2705 //ss.ss_sp = stack;
2706 //sa.sa_handler = catch_signal;
2707 //sa.sa_flags = SA_ONSTACK;
2708 //sigfillset(&sa.sa_mask);
2709
2710 //sigaltstack(&ss, 0);
2711 //sigfillset(&sa.sa_mask);
2712 //sigaction(SIGSEGV, &sa, 0);
2713 // alternate signal stack ------------
2714
2715 // catch SIGSEGV !!
2716
2717 //fprintf(stderr, "In Try Statement (p2t_cdt_new)\n");
2718 cdt = p2t_cdt_new(current_points);
2719 //fprintf(stderr, "all OK (p2t_cdt_new)\n");
2720 }
2721 CATCH( MAPTOOL_00001_EXCEPTION )
2722 {
2723 fprintf(stderr, "relation id=%lld\n", current_id);
2724 fprintf(stderr, "Got Exception 1!\n");
2725 exception = 1;
2726 }
2727 CATCH( MAPTOOL_00002_EXCEPTION )
2728 {
2729 fprintf(stderr, "relation id=%lld\n", current_id);
2730 fprintf(stderr, "Got segv Exception 1!\n");
2731 exception = 1;
2732 }
2733 FINALLY
2734 {
2735 // set default
2736 signal (SIGSEGV, SIG_DFL);
2737 // set default
2738
2739 //fprintf(stderr, "finally\n");
2740 }
2741 ETRY;
2742
2743 //fprintf(stderr, "after cdt_new\n");
2744
2745 GList* save = unsorted_inner_ways;
2746
2747 int no_holes = 0;
2748 if (exception == 0)
2749 {
2750
2751 // first run a check loop
2752 int number_of_nodes_in_this_way;
2753 int cur_node_count_in_way;
2754 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
2755
2756 int number_of_nodes_in_this_way3 = g_list_length(unsorted_inner_ways);
2757 //fprintf(stderr, "uiw 001.0 c=%d\n", number_of_nodes_in_this_way3);
2758 while (unsorted_inner_ways)
2759 {
2760 if ((unsorted_inner_ways->data)&&(no_holes == 0))
2761 {
2762 way_nodes1 = (struct way_and_endnodes *) unsorted_inner_ways->data;
2763 number_of_nodes_in_this_way = g_list_length(way_nodes1->nodes);
2764 // now read all the nodes of this "hole"-way
2765 GList* save0 = way_nodes1->nodes;
2766 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2767 first_node_found = 0;
2768 cur_node_count_in_way = 1;
2769 while (way_nodes1->nodes)
2770 {
2771 ndptr = (long long *) way_nodes1->nodes->data;
2772 // fprintf(stderr, "hole node:%lld\n", (long long)*ndptr);
2773
2774 if (temp_nodes_list != NULL)
2775 {
2776 //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);
2777 // the last node should always be same as first node (for now we only support holes that dont span over multiple ways!)
2778 if (cur_node_count_in_way < number_of_nodes_in_this_way)
2779 {
2780 if (glist_find_already_inside(temp_nodes_list, ndptr) != 0)
2781 {
2782 fprintf(stderr, "relation id=%lld\n", current_id);
2783 fprintf(stderr, "problem with hole!\n");
2784 no_holes = 1;
2785 break;
2786 }
2787 else
2788 {
2789 //fprintf(stderr, "hole check ok\n");
2790 }
2791 }
2792 }
2793
2794 temp_nodes_list = g_list_append(temp_nodes_list, ndptr);
2795
2796 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
2797 cur_node_count_in_way++;
2798 }
2799 way_nodes1->nodes = save0;
2800 }
2801 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
2802 }
2803
2804 }
2805
2806 unsorted_inner_ways = save;
2807
2808 //fprintf(stderr, "ex=%d nohole=%d\n", exception, no_holes);
2809
2810
2811
2812 if ((exception == 0) && (no_holes == 0))
2813 {
2814 //fprintf(stderr, "uiw 000\n");
2815
2816 // now do the real loop
2817 int number_of_added_holes = 0;
2818
2819 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
2820 //int number_of_nodes_in_this_way4 = g_list_length(unsorted_inner_ways);
2821 //fprintf(stderr, "uiw 001.2 c=%d\n", number_of_nodes_in_this_way4);
2822 while (unsorted_inner_ways)
2823 {
2824 //fprintf(stderr, "uiw 001\n");
2825
2826 if (unsorted_inner_ways->data)
2827 {
2828 //fprintf(stderr, "uiw 002\n");
2829
2830 current_hole = g_ptr_array_new();
2831
2832 //fprintf(stderr, "uiw 003\n");
2833
2834 way_nodes1 = (struct way_and_endnodes *) unsorted_inner_ways->data;
2835 // now read all the nodes of this "hole"-way
2836 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
2837 first_node_found = 0;
2838 while (way_nodes1->nodes)
2839 {
2840 //fprintf(stderr, "uiw 004\n");
2841
2842 ndptr = (long long *) way_nodes1->nodes->data;
2843 nd = *ndptr;
2844
2845 // now read the coords of this node
2846
2847 #ifdef MAPTOOL_SPLIT_NODE_DB
2848 if (nd % 2)
2849 {
2850 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2851 {
2852 sqlite3_bind_int64(stmt_sel002__2a, 1, nd);
2853 }
2854 else
2855 {
2856 sqlite3_bind_int64(stmt_sel002__2b, 1, nd);
2857 }
2858 }
2859 else
2860 {
2861 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2862 {
2863 #endif
2864 sqlite3_bind_int64(stmt_sel002a, 1, nd);
2865 #ifdef MAPTOOL_SPLIT_NODE_DB
2866 }
2867 else
2868 {
2869 sqlite3_bind_int64(stmt_sel002b, 1, nd);
2870 }
2871 }
2872 #endif
2873 rc2 = 0;
2874 do
2875 {
2876 #ifdef MAPTOOL_SPLIT_NODE_DB
2877 if (nd % 2)
2878 {
2879 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2880 {
2881 rc2 = sqlite3_step(stmt_sel002__2a);
2882 }
2883 else
2884 {
2885 rc2 = sqlite3_step(stmt_sel002__2b);
2886 }
2887 }
2888 else
2889 {
2890 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2891 {
2892 #endif
2893 rc2 = sqlite3_step(stmt_sel002a);
2894 #ifdef MAPTOOL_SPLIT_NODE_DB
2895 }
2896 else
2897 {
2898 rc2 = sqlite3_step(stmt_sel002b);
2899 }
2900 }
2901 #endif
2902
2903 switch (rc2)
2904 {
2905 case SQLITE_DONE:
2906 break;
2907 case SQLITE_ROW:
2908
2909 #ifdef MAPTOOL_SPLIT_NODE_DB
2910 if (nd % 2)
2911 {
2912 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2913 {
2914 lat_nd = (double) sqlite3_column_double(stmt_sel002__2a, 0);
2915 lon_nd = (double) sqlite3_column_double(stmt_sel002__2a, 1);
2916 }
2917 else
2918 {
2919 lat_nd = (double) sqlite3_column_double(stmt_sel002__2b, 0);
2920 lon_nd = (double) sqlite3_column_double(stmt_sel002__2b, 1);
2921 }
2922 }
2923 else
2924 {
2925 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
2926 {
2927 #endif
2928 lat_nd = (double) sqlite3_column_double(stmt_sel002a, 0);
2929 lon_nd = (double) sqlite3_column_double(stmt_sel002a, 1);
2930 #ifdef MAPTOOL_SPLIT_NODE_DB
2931 }
2932 else
2933 {
2934 lat_nd = (double) sqlite3_column_double(stmt_sel002b, 0);
2935 lon_nd = (double) sqlite3_column_double(stmt_sel002b, 1);
2936 }
2937 }
2938 #endif
2939
2940 if (first_node_found == 0)
2941 {
2942 first_node_found = 1;
2943 }
2944 else
2945 {
2946 // ------ ok use this node and coords for triangulation ----
2947 //fprintf(stderr, "HOLE:nd,lat,lon %lld,%f,%f\n", nd,lat_nd,lon_nd);
2948
2949 //fprintf(stderr, "np=%d\n", number_of_points_in_poly);
2950 //in.pointlist[number_of_points_in_poly] = (REAL) (lat_nd*10);
2951 //number_of_points_in_poly++;
2952 //fprintf(stderr, "np=%d\n", number_of_points_in_poly);
2953 //in.pointlist[number_of_points_in_poly] = (REAL) (lon_nd*10);
2954 //number_of_points_in_poly++;
2955 #if 1
2956 y = lat_nd * factor;
2957 x = lon_nd * factor;
2958
2959 TRY
2960 {
2961 signal(SIGSEGV, catch_signal);
2962
2963 // hole complete, now add it
2964 //fprintf(stderr, "hole new\n");
2965 g_ptr_array_add(current_hole, p2t_point_new_dd(x, y));
2966 //fprintf(stderr, "hole new done\n");
2967 }
2968 CATCH( MAPTOOL_00001_EXCEPTION )
2969 {
2970 fprintf(stderr, "relation id=%lld\n", current_id);
2971 fprintf(stderr, "Got Exception A!\n");
2972 exception = 1;
2973 }
2974 CATCH( MAPTOOL_00002_EXCEPTION )
2975 {
2976 fprintf(stderr, "relation id=%lld\n", current_id);
2977 fprintf(stderr, "Got segv Exception A!\n");
2978 exception = 1;
2979 }
2980 FINALLY
2981 {
2982 // set default
2983 signal (SIGSEGV, SIG_DFL);
2984 // set default
2985
2986 //fprintf(stderr, "finally\n");
2987 }
2988 ETRY;
2989
2990 #endif
2991 }
2992 break;
2993 default:
2994 fprintf(stderr, "Error: %d\n", rc2);
2995 break;
2996 }
2997 }
2998 while (rc2 == SQLITE_ROW);
2999
3000 #ifdef MAPTOOL_SPLIT_NODE_DB
3001 if (nd % 2)
3002 {
3003 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3004 {
3005 sqlite3_reset(stmt_sel002__2a);
3006 }
3007 else
3008 {
3009 sqlite3_reset(stmt_sel002__2b);
3010 }
3011 }
3012 else
3013 {
3014 if ((nd & MAPTOOL_SPLIT_NODE_BIT) > 0)
3015 {
3016 #endif
3017 sqlite3_reset(stmt_sel002a);
3018 #ifdef MAPTOOL_SPLIT_NODE_DB
3019 }
3020 else
3021 {
3022 sqlite3_reset(stmt_sel002b);
3023 }
3024 }
3025 #endif
3026
3027 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3028 }
3029
3030 //fprintf(stderr, "uiw 005\n");
3031
3032 // remember the pointer, to free the memory later
3033 all_holes = g_list_append(all_holes, current_hole);
3034
3035 //fprintf(stderr, "uiw 005.1\n");
3036
3037 TRY
3038 {
3039 signal(SIGSEGV, catch_signal);
3040
3041 // hole complete, now add it
3042 //fprintf(stderr, "adding hole\n");
3043 if (number_of_added_holes < MAX_HOLES_PER_POLY)
3044 {
3045 p2t_cdt_add_hole(cdt, current_hole);
3046 number_of_added_holes++;
3047 }
3048 //fprintf(stderr, "adding done\n");
3049 }
3050 CATCH( MAPTOOL_00001_EXCEPTION )
3051 {
3052 fprintf(stderr, "relation id=%lld\n", current_id);
3053 fprintf(stderr, "Got Exception 8!\n");
3054 exception = 1;
3055 }
3056 CATCH( MAPTOOL_00002_EXCEPTION )
3057 {
3058 fprintf(stderr, "relation id=%lld\n", current_id);
3059 fprintf(stderr, "Got segv Exception 8!\n");
3060 exception = 1;
3061 }
3062 FINALLY
3063 {
3064 // set default
3065 signal (SIGSEGV, SIG_DFL);
3066 // set default
3067
3068 //fprintf(stderr, "finally\n");
3069 }
3070 ETRY;
3071
3072 }
3073
3074 //fprintf(stderr, "uiw 006\n");
3075 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
3076
3077 }
3078 //sqlite3_reset(st);
3079
3080 //fprintf(stderr, "uiw 007\n");
3081 }
3082
3083 //fprintf(stderr, "after adding holes\n");
3084
3085 #if 1
3086
3087 if (exception == 0)
3088 {
3089 exception = 0;
3090
3091 TRY
3092 {
3093 // catch SIGSEGV !!
3094 signal(SIGSEGV, catch_signal);
3095 //signal(SIGILL, catch_signal);
3096
3097 // alternate signal stack ------------
3098 //ss.ss_size = SIGSTKSZ;
3099 //ss.ss_sp = stack;
3100 //sa.sa_handler = catch_signal;
3101 //sa.sa_flags = SA_ONSTACK;
3102 //sigfillset(&sa.sa_mask);
3103
3104 //sigaltstack(&ss, 0);
3105 //sigfillset(&sa.sa_mask);
3106 //sigaction(SIGSEGV, &sa, 0);
3107 // alternate signal stack ------------
3108
3109 // catch SIGSEGV !!
3110
3111 //fprintf(stderr, "triangulate start ...\n");
3112 p2t_cdt_triangulate(cdt);
3113 //fprintf(stderr, "triangulate end\n");
3114 }
3115 CATCH( MAPTOOL_00001_EXCEPTION )
3116 {
3117 fprintf(stderr, "relation id=%lld\n", current_id);
3118 fprintf(stderr, "Got Exception 2! (in triangulate)\n");
3119 exception = 1;
3120 }
3121 CATCH( MAPTOOL_00002_EXCEPTION )
3122 {
3123 fprintf(stderr, "relation id=%lld\n", current_id);
3124 fprintf(stderr, "Got segv Exception 2! (in triangulate)\n");
3125 exception = 1;
3126 }
3127 FINALLY
3128 {
3129 //fprintf(stderr, "finally 2 (in triangulate)\n");
3130
3131 //sa.sa_handler = SIG_DFL;
3132 // sigfillset(&sa.sa_mask);
3133 //sigaction(SIGSEGV, &sa, 0);
3134
3135 // set default
3136 signal (SIGSEGV, SIG_DFL);
3137 // set default
3138 }
3139 ETRY;
3140
3141 if (exception == 0)
3142 {
3143 rmesh = g_slice_new (P2trCDT);
3144 rmesh->mesh = p2tr_mesh_new ();
3145 // rmesh->outline = p2tr_pslg_new ();
3146
3147 /* First iteration over the CDT - create all the points */
3148 //fprintf(stderr, "trinew 006\n");
3149 triangles = p2t_cdt_get_triangles(cdt);
3150 //fprintf(stderr, "trinew 007\n");
3151 for (i = 0; i < triangles->len; i++)
3152 {
3153 P2tTriangle *cdt_tri = triangle_index (triangles, i);
3154 for (j = 0; j < 3; j++)
3155 {
3156 P2tPoint *cdt_pt = p2t_triangle_get_point(cdt_tri, j);
3157 P2trPoint *new_pt = (P2trPoint*) g_hash_table_lookup (point_map, cdt_pt);
3158
3159 if (new_pt == NULL)
3160 {
3161 //fprintf(stderr, "p1 x=%d, y=%d\n",(int)cdt_pt->x, (int)cdt_pt->y);
3162 new_pt = p2tr_mesh_new_point2 (rmesh->mesh, (cdt_pt->x / factor), (cdt_pt->y / factor));
3163 g_hash_table_insert (point_map, cdt_pt, new_pt);
3164 }
3165 }
3166 }
3167
3168 for (i = 0; i < triangles->len; i++)
3169 {
3170 P2tTriangle *cdt_tri = triangle_index(triangles, i);
3171 //fprintf(stderr, "tri# %d\n", i);
3172 gboolean ii = p2t_triangle_is_interior(cdt_tri);
3173 //fprintf(stderr, "is int=%d\n", ii);
3174
3175 P2trPoint *pt1 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 0));
3176 P2trPoint *pt2 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 1));
3177 P2trPoint *pt3 = (P2trPoint*) g_hash_table_lookup (point_map, p2t_triangle_get_point (cdt_tri, 2));
3178
3179 //fprintf(stderr, "0 x=%f, y=%f\n",pt1->c.x, pt1->c.y);
3180 //fprintf(stderr, "1 x=%f, y=%f\n",pt2->c.x, pt2->c.y);
3181 //fprintf(stderr, "2 x=%f, y=%f\n",pt3->c.x, pt3->c.y);
3182
3183 // make item and write it to ways file -------------------
3184 // make item and write it to ways file -------------------
3185 if (tag_id == 1)
3186 {
3187 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3188 //fprintf(stderr, "T:water\n");
3189 }
3190 else if (tag_id == 2)
3191 {
3192 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3193 //fprintf(stderr, "T:water\n");
3194 }
3195 else if (tag_id == 4)
3196 {
3197 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3198 //fprintf(stderr, "T:water\n");
3199 }
3200 else if (tag_id == 5)
3201 {
3202 item_bin_tri = init_item(type_poly_wood_from_triang, 0);
3203 //fprintf(stderr, "T:forest\n");
3204 }
3205 else if (tag_id == 6)
3206 {
3207 item_bin_tri = init_item(type_poly_wood_from_triang, 0);
3208 //fprintf(stderr, "T:forest\n");
3209 }
3210 else if (tag_id == 9)
3211 {
3212 item_bin_tri = init_item(type_poly_building_from_triang, 0);
3213 //fprintf(stderr, "T:building\n");
3214 }
3215 else
3216 {
3217 // default
3218 item_bin_tri = init_item(type_poly_water_from_triang, 0);
3219 //fprintf(stderr, "T:water\n");
3220 }
3221
3222 // add dummy osm wayid
3223 item_bin_add_attr_longlong(item_bin_tri, attr_osm_wayid, current_id);
3224 coord_tri.x = transform_from_geo_lon(pt1->c.x);
3225 coord_tri.y = transform_from_geo_lat(pt1->c.y);
3226 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3227 coord_tri.x = transform_from_geo_lon(pt2->c.x);
3228 coord_tri.y = transform_from_geo_lat(pt2->c.y);
3229 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3230 coord_tri.x = transform_from_geo_lon(pt3->c.x);
3231 coord_tri.y = transform_from_geo_lat(pt3->c.y);
3232 item_bin_add_coord(item_bin_tri, &coord_tri, 1);
3233
3234 item_bin_write(item_bin_tri, osm->ways_with_coords);
3235 // make item and write it to ways file -------------------
3236 // make item and write it to ways file -------------------
3237
3238 }
3239
3240 //fprintf(stderr, "trinew 091\n");
3241
3242 }
3243
3244 //fprintf(stderr, "trinew 092.0\n");
3245
3246
3247 TRY
3248 {
3249 signal(SIGSEGV, catch_signal);
3250
3251 /* Now finally unref the points we added into the map */
3252 if (point_map != NULL)
3253 {
3254 GHashTableIter iter;
3255 P2trPoint *pt_iter = NULL;
3256 g_hash_table_iter_init (&iter, point_map);
3257 while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&pt_iter))
3258 {
3259 p2tr_point_unref(pt_iter);
3260 }
3261 g_hash_table_destroy(point_map);
3262 }
3263 }
3264 CATCH( MAPTOOL_00001_EXCEPTION )
3265 {
3266 fprintf(stderr, "relation id=%lld\n", current_id);
3267 fprintf(stderr, "Got Exception 3!\n");
3268 exception = 1;
3269 }
3270 CATCH( MAPTOOL_00002_EXCEPTION )
3271 {
3272 fprintf(stderr, "relation id=%lld\n", current_id);
3273 fprintf(stderr, "Got segv Exception 3!\n");
3274 exception = 1;
3275 }
3276 FINALLY
3277 {
3278 signal (SIGSEGV, SIG_DFL);
3279 }
3280 ETRY;
3281
3282 //fprintf(stderr, "trinew 092.1\n");
3283
3284
3285 TRY
3286 {
3287 signal(SIGSEGV, catch_signal);
3288 // p2tr_mesh_clear(rmesh->mesh);
3289 // --- maybe need this?? --- p2tr_mesh_unref(rmesh->mesh);
3290 }
3291 CATCH( MAPTOOL_00001_EXCEPTION )
3292 {
3293 fprintf(stderr, "relation id=%lld\n", current_id);
3294 fprintf(stderr, "Got Exception 4!\n");
3295 exception = 1;
3296 }
3297 CATCH( MAPTOOL_00002_EXCEPTION )
3298 {
3299 fprintf(stderr, "relation id=%lld\n", current_id);
3300 fprintf(stderr, "Got segv Exception 4!\n");
3301 exception = 1;
3302 }
3303 FINALLY
3304 {
3305 signal (SIGSEGV, SIG_DFL);
3306 }
3307 ETRY;
3308
3309 //fprintf(stderr, "trinew 092.2\n");
3310
3311 TRY
3312 {
3313 signal(SIGSEGV, catch_signal);
3314 p2t_cdt_free(cdt);
3315 }
3316 CATCH( MAPTOOL_00001_EXCEPTION )
3317 {
3318 fprintf(stderr, "relation id=%lld\n", current_id);
3319 fprintf(stderr, "Got Exception 5!\n");
3320 exception = 1;
3321 }
3322 CATCH( MAPTOOL_00002_EXCEPTION )
3323 {
3324 fprintf(stderr, "relation id=%lld\n", current_id);
3325 fprintf(stderr, "Got segv Exception 5!\n");
3326 exception = 1;
3327 }
3328 FINALLY
3329 {
3330 signal (SIGSEGV, SIG_DFL);
3331 }
3332 ETRY;
3333
3334 //fprintf(stderr, "trinew 092.3\n");
3335
3336 } // exception == 0 ? -- END
3337
3338 // rcdt = p2tr_cdt_new(cdt);
3339 /*
3340 if (refine_max_steps > 0)
3341 {
3342 fprintf(stderr, "Refining the mesh!\n");
3343 refiner = p2tr_refiner_new(G_PI / 6, p2tr_refiner_false_too_big, rcdt);
3344 p2tr_refiner_refine(refiner, refine_max_steps, NULL);
3345 p2tr_refiner_free(refiner);
3346
3347 p2tr_cdt_validate_edges(rcdt);
3348 }
3349 */
3350
3351 //fprintf(stderr, "free holes\n");
3352
3353 if (all_holes)
3354 {
3355 all_holes = g_list_first(all_holes);
3356 while (all_holes)
3357 {
3358 current_hole = all_holes->data;
3359 for (i = 0; i < current_hole->len; ++i)
3360 {
3361 p2t_point_free(g_ptr_array_index(current_hole, i));
3362 }
3363 g_ptr_array_free(current_hole, TRUE);
3364
3365 all_holes = g_list_next(all_holes);
3366 }
3367 g_list_free(all_holes);
3368 all_holes = NULL;
3369 }
3370
3371 //fprintf(stderr, "free points\n");
3372
3373 for (i = 0; i < current_points->len; ++i)
3374 {
3375 p2t_point_free(g_ptr_array_index(current_points, i));
3376 }
3377 g_ptr_array_free(current_points, TRUE);
3378
3379 // p2tr_cdt_free(rcdt);
3380
3381 #endif
3382
3383 #if 0
3384 item_bin_write(item_bin_tri, osm->ways_with_coords);
3385 #endif
3386 triangulate_not_done = 0;
3387 }
3388
3389 // free everything!! ----------------
3390 if (unsorted_outer_ways)
3391 {
3392 unsorted_outer_ways = g_list_first(unsorted_outer_ways);
3393 while (unsorted_outer_ways)
3394 {
3395 gptr = unsorted_outer_ways->data;
3396 way_nodes1 = unsorted_outer_ways->data;
3397 if (way_nodes1->nodes)
3398 {
3399 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3400 while (way_nodes1->nodes)
3401 {
3402 ndptr = way_nodes1->nodes->data;
3403 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3404 g_free(ndptr);
3405 ndptr = NULL;
3406 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3407 }
3408 }
3409 unsorted_outer_ways = g_list_remove(unsorted_outer_ways, gptr);
3410 g_free(gptr);
3411 gptr = NULL;
3412 unsorted_outer_ways = g_list_next(unsorted_outer_ways);
3413 }
3414 }
3415
3416 if (unsorted_inner_ways)
3417 {
3418 unsorted_inner_ways = g_list_first(unsorted_inner_ways);
3419 while (unsorted_inner_ways)
3420 {
3421 gptr = unsorted_inner_ways->data;
3422 way_nodes1 = unsorted_inner_ways->data;
3423 if (way_nodes1->nodes)
3424 {
3425 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3426 while (way_nodes1->nodes)
3427 {
3428 ndptr = way_nodes1->nodes->data;
3429 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3430 g_free(ndptr);
3431 ndptr = NULL;
3432 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3433 }
3434 }
3435 unsorted_inner_ways = g_list_remove(unsorted_inner_ways, gptr);
3436 g_free(gptr);
3437 gptr = NULL;
3438 unsorted_inner_ways = g_list_next(unsorted_inner_ways);
3439 }
3440 }
3441
3442 if (sorted_outer_ways)
3443 {
3444 sorted_outer_ways = g_list_first(sorted_outer_ways);
3445 while (sorted_outer_ways)
3446 {
3447 gptr = sorted_outer_ways->data;
3448 way_nodes1 = sorted_outer_ways->data;
3449 if (way_nodes1->nodes)
3450 {
3451 way_nodes1->nodes = g_list_first(way_nodes1->nodes);
3452 while (way_nodes1->nodes)
3453 {
3454 ndptr = way_nodes1->nodes->data;
3455 way_nodes1->nodes = g_list_remove(way_nodes1->nodes, ndptr);
3456 g_free(ndptr);
3457 ndptr = NULL;
3458 way_nodes1->nodes = g_list_next(way_nodes1->nodes);
3459 }
3460 }
3461 sorted_outer_ways = g_list_remove(sorted_outer_ways, gptr);
3462 g_free(gptr);
3463 gptr = NULL;
3464 sorted_outer_ways = g_list_next(sorted_outer_ways);
3465 }
3466 }
3467 // free everything!! ----------------
3468 }
3469 // save relations other method --------------
3470
3471 #endif
3472
3473 if (triangulate_not_done == 1)
3474 {
3475 str = NULL;
3476 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3477 {
3478 if (get_relation_member(str, &memb))
3479 {
3480 if ((memb.type == 2) && (!strcmp(memb.role, "outer"))) // use only "outer" "ways" here!!
3481 {
3482 struct way_tag wt;
3483 wt.way_id = memb.id;
3484 wt.tag_id = tag_id;
3485 // only use "water" related ways here
3486 if ((tag_id == 1) || (tag_id == 2) || (tag_id == 4) || (tag_id == 5) || (tag_id == 6))
3487 {
3488 //fprintf(stderr,"rel way=%llu\n", memb.id);
3489 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3490 }
3491 }
3492 else if ((memb.type == 2) && (!strcmp(memb.role, "inner"))) // use only "outer" "ways" here!!
3493 {
3494 struct way_tag wt;
3495 wt.way_id = memb.id;
3496 wt.tag_id = tag_id;
3497 // only use "water" related ways here
3498 if (tag_id == 6)
3499 {
3500 wt.tag_id = 8;
3501 //fprintf(stderr,"rel way(8)=%llu\n", memb.id);
3502 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3503 }
3504 else if (tag_id == 5)
3505 {
3506 wt.tag_id = 7;
3507 //fprintf(stderr,"rel way(7)=%llu\n", memb.id);
3508 fwrite(&wt, sizeof(struct way_tag), 1, osm->relations_riverbank);
3509 }
3510 }
3511
3512 }
3513 }
3514 }
3515 }
3516 // -- save relations for riverbanks --
3517
3518
3519 in_relation = 0;
3520
3521 if ((!strcmp(relation_type, "multipolygon") || !strcmp(relation_type, "boundary")) && boundary)
3522 {
3523 int found_town_rel = 0;
3524
3525 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
3526 {
3527 // save relation id for towns into DB
3528 str = NULL;
3529 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3530 {
3531 if (get_relation_member(str, &memb))
3532 {
3533 if (memb.type == 1)
3534 {
3535 //fprintf(stderr, "role=%s\n", memb.role);
3536
3537 if (!strcmp(memb.role, "label"))
3538 {
3539 fprintf(stderr, "town to relation: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
3540 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3541 // fprintf(stderr, "ret=%d\n", rr);
3542 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
3543 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
3544 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
3545 sqlite3_step(stmt_town_sel008);
3546 //fprintf(stderr, "ret=%d\n", rr);
3547 sqlite3_reset(stmt_town_sel008);
3548 //fprintf(stderr, "ret=%d\n", rr);
3549
3550 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
3551
3552 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3553 //fprintf(stderr, "ret=%d\n", rr);
3554
3555 found_town_rel = 1;
3556 }
3557 }
3558 }
3559 }
3560 }
3561
3562
3563
3564 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
3565 {
3566 // save relation id for towns into DB
3567 str = NULL;
3568 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3569 {
3570 if (get_relation_member(str, &memb))
3571 {
3572 if (memb.type == 1)
3573 {
3574 //fprintf(stderr, "role=%s\n", memb.role);
3575
3576 if (!strcmp(memb.role, "admin_centre"))
3577 {
3578 fprintf(stderr, "town to relation: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
3579 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3580 // fprintf(stderr, "ret=%d\n", rr);
3581 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
3582 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
3583 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
3584 sqlite3_step(stmt_town_sel008);
3585 //fprintf(stderr, "ret=%d\n", rr);
3586 sqlite3_reset(stmt_town_sel008);
3587 //fprintf(stderr, "ret=%d\n", rr);
3588
3589 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
3590
3591 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3592 //fprintf(stderr, "ret=%d\n", rr);
3593
3594 found_town_rel = 1;
3595 }
3596 }
3597 }
3598 }
3599 }
3600
3601 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
3602 {
3603 // save relation id for towns into DB
3604 str = NULL;
3605 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3606 {
3607 if (get_relation_member(str, &memb))
3608 {
3609 if (memb.type == 1)
3610 {
3611 //fprintf(stderr, "role=%s\n", memb.role);
3612
3613 if (!strcmp(memb.role, "admin_center"))
3614 {
3615 fprintf(stderr, "town to relation: rel-name:%s r:%lld t:%lld\n",osm_tag_value(item_bin_2, "name"), current_id, memb.id);
3616 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3617 // fprintf(stderr, "ret=%d\n", rr);
3618 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
3619 sqlite3_bind_int64(stmt_town_sel008, 2, memb.id);
3620 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
3621 sqlite3_step(stmt_town_sel008);
3622 //fprintf(stderr, "ret=%d\n", rr);
3623 sqlite3_reset(stmt_town_sel008);
3624 //fprintf(stderr, "ret=%d\n", rr);
3625
3626 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
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
3639
3640 if ((found_town_rel == 0) && (admin_level >= TOWN_ADMIN_LEVEL_START))
3641 {
3642 // save relation id for towns into DB (use dummy town data)
3643 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);
3644 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3645 // fprintf(stderr, "ret=%d\n", rr);
3646 sqlite3_bind_int64(stmt_town_sel008, 1, current_id);
3647 sqlite3_bind_int64(stmt_town_sel008, 2, dummy_town_id);
3648 sqlite3_bind_int(stmt_town_sel008, 3, admin_level);
3649 sqlite3_step(stmt_town_sel008);
3650 //fprintf(stderr, "ret=%d\n", rr);
3651 sqlite3_reset(stmt_town_sel008);
3652 //fprintf(stderr, "ret=%d\n", rr);
3653
3654
3655 str = NULL;
3656 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3657 {
3658 if (get_relation_member(str, &memb))
3659 {
3660 if ((memb.type == 2) && (!strcmp(memb.role, "outer")))
3661 {
3662 // way-id is "memb.id"
3663 // use the first nodes coords
3664 get_lat_lon_way_first_node(memb.id, &node_coords);
3665 if (node_coords.valid == 1)
3666 {
3667 sqlite3_bind_double(stmt_town, 6, node_coords.lat);
3668 sqlite3_bind_double(stmt_town, 7, node_coords.lon);
3669 }
3670
3671 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
3672
3673 break;
3674 }
3675 }
3676 }
3677
3678 sqlite3_bind_int64(stmt_town, 1, dummy_town_id);
3679 sqlite3_bind_int(stmt_town, 2, 999);
3680 sqlite3_bind_int(stmt_town, 4, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_level) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
3681 sqlite3_bind_text(stmt_town, 3, osm_tag_value(item_bin_2, "name"), -1, SQLITE_STATIC);
3682 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
3683 sqlite3_bind_int64(stmt_town, 8, current_id);
3684 sqlite3_step(stmt_town);
3685 sqlite3_reset(stmt_town);
3686
3687 found_town_rel = 1;
3688
3689 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3690 //fprintf(stderr, "ret=%d\n", rr);
3691
3692 dummy_town_id--;
3693 }
3694
3695 // rest of the boundaries!!!
3696 if (found_town_rel == 0)
3697 {
3698 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3699
3700 str = NULL;
3701 while ((str = item_bin_get_attr(item_bin_2, attr_osm_member, str)))
3702 {
3703 if (get_relation_member(str, &memb))
3704 {
3705 if ((memb.type == 2) && (!strcmp(memb.role, "outer")))
3706 {
3707 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);
3708
3709 add_boundary_to_db(current_id, admin_level, item_bin_2, &memb);
3710 break;
3711 }
3712 }
3713 }
3714 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3715
3716 found_town_rel = 0;
3717 }
3718
3719
3720 #if 0
3721 if (admin_level == 2)
3722 {
3723 FILE *f;
3724 fprintf(stderr,"Multipolygon for %s\n", iso_code);
3725 char *name=g_strdup_printf("country_%s.tmp",iso_code);
3726 f=fopen(name,"w");
3727 item_bin_write(item_bin_2, f);
3728 fclose(f);
3729 }
3730 #endif
3731 item_bin_write(item_bin_2, osm->boundaries);
3732 }
3733
3734 if (!strcmp(relation_type, "restriction") && (item_bin_2->type == type_street_turn_restriction_no || item_bin_2->type == type_street_turn_restriction_only))
3735 {
3736 item_bin_write(item_bin_2, osm->turn_restrictions);
3737 }
3738 }
3739
3740
3741 void correct_boundary_ref_point(GList *bl)
3742 {
3743 long long rid;
3744 int retval;
3745 int rc = 0;
3746 int commit_after = 20000;
3747 int count = 0;
3748 int admin_l = 99;
3749 double lat, lon;
3750 struct coord c;
3751 GList *l = NULL;
3752 GList *l2 = NULL;
3753 GList *match = NULL;
3754 struct boundary *b;
3755 int admin_l_boundary;
3756 int max_admin_l;
3757
3758 // loop thru all the boundaries
3759 do
3760 {
3761 rc = sqlite3_step(stmt_bd_002);
3762 switch (rc)
3763 {
3764 case SQLITE_DONE:
3765 break;
3766 case SQLITE_ROW:
3767
3768 // select rel_id, admin_level, lat, lon, name from boundary where done = 0;
3769 rid = sqlite3_column_int64(stmt_bd_002, 0);
3770 admin_l = sqlite3_column_int(stmt_bd_002, 1);
3771 lat = sqlite3_column_double(stmt_bd_002, 2);
3772 lon = sqlite3_column_double(stmt_bd_002, 3);
3773 c.x = transform_from_geo_lon(lon);
3774 c.y = transform_from_geo_lat(lat);
3775
3776 //fprintf(stderr, "correct_boundary_ref_point: relid:%lld(%d) x=%d y=%d\n", rid, admin_l, c.x, c.y);
3777
3778 // find this boundary in list
3779 l = bl;
3780 match = NULL;
3781 while (l)
3782 {
3783 b = l->data;
3784 //fprintf(stderr, "correct_boundary_ref_point: %lld %lld\n", item_bin_get_relationid(b->ib), rid);
3785 if (item_bin_get_relationid(b->ib) == rid)
3786 {
3787 match = l;
3788 break;
3789 }
3790 l = g_list_next(l);
3791 }
3792
3793 if (match)
3794 {
3795 b = match->data;
3796 //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);
3797
3798 // -------------------------------------------
3799 /*
3800 GList *sl;
3801 sl = b->sorted_segments;
3802 if (sl)
3803 {
3804 struct geom_poly_segment *gs = sl->data;
3805 struct coord *c2 = gs->first;
3806 fprintf(stderr, "1a c=%d y=%d\n", c2->x, c2->y);
3807 }
3808 */
3809 // -------------------------------------------
3810
3811 if (find_correct_point_in_boundary(match, &lat, &lon) == 1)
3812 {
3813 sqlite3_bind_double(stmt_bd_004, 1, lat);
3814 sqlite3_bind_double(stmt_bd_004, 2, lon);
3815 sqlite3_bind_int64(stmt_bd_004, 3, rid);
3816 sqlite3_step(stmt_bd_004);
3817 sqlite3_reset(stmt_bd_004);
3818 }
3819 }
3820
3821
3822 if (count == 0)
3823 {
3824 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
3825 }
3826 count++;
3827
3828 if (count > commit_after)
3829 {
3830 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3831 count = 0;
3832 }
3833
3834 break;
3835 default:
3836 fprintf(stderr, "SQL Error: %d\n", rc);
3837 break;
3838 }
3839 }
3840 while (rc == SQLITE_ROW);
3841 sqlite3_reset(stmt_bd_002);
3842
3843 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
3844
3845 }
3846
3847
3848 int find_correct_point_in_boundary(GList *bl, double* lat, double* lon)
3849 {
3850 double lat2, lon2;
3851 struct coord c;
3852 struct boundary *b;
3853 GList *matches;
3854 double lat_d = 0.00006;
3855 double lon_d = 0.00006;
3856
3857 lat2 = *lat;
3858 lon2 = *lon;
3859
3860 c.x = transform_from_geo_lon(lon2 + lon_d);
3861 c.y = transform_from_geo_lat(lat2 + lat_d);
3862 //fprintf(stderr, "2 c=%d y=%d\n", c.x, c.y);
3863 matches = NULL;
3864 matches = boundary_find_matches_single(bl, &c);
3865 if (g_list_length(matches) > 0)
3866 {
3867 *lon = lon2 + lon_d;
3868 *lat = lat2 + lat_d;
3869 return 1;
3870 }
3871
3872 c.x = transform_from_geo_lon(lon2 + lon_d);
3873 c.y = transform_from_geo_lat(lat2 - lat_d);
3874 //fprintf(stderr, "3 c=%d y=%d\n", c.x, c.y);
3875 matches = NULL;
3876 matches = boundary_find_matches_single(bl, &c);
3877 if (g_list_length(matches) > 0)
3878 {
3879 *lon = lon2 + lon_d;
3880 *lat = lat2 - lat_d;
3881 return 1;
3882 }
3883
3884 c.x = transform_from_geo_lon(lon2 - lon_d);
3885 c.y = transform_from_geo_lat(lat2 + lat_d);
3886 //fprintf(stderr, "4 c=%d y=%d\n", c.x, c.y);
3887 matches = NULL;
3888 matches = boundary_find_matches_single(bl, &c);
3889 if (g_list_length(matches) > 0)
3890 {
3891 *lon = lon2 - lon_d;
3892 *lat = lat2 + lat_d;
3893 return 1;
3894 }
3895
3896 c.x = transform_from_geo_lon(lon2 - lon_d);
3897 c.y = transform_from_geo_lat(lat2 - lat_d);
3898 //fprintf(stderr, "5 c=%d y=%d\n", c.x, c.y);
3899 matches = NULL;
3900 matches = boundary_find_matches_single(bl, &c);
3901 if (g_list_length(matches) > 0)
3902 {
3903 *lon = lon2 - lon_d;
3904 *lat = lat2 - lat_d;
3905 return 1;
3906 }
3907
3908
3909 c.x = transform_from_geo_lon(lon2 + lon_d);
3910 c.y = transform_from_geo_lat(lat2);
3911 //fprintf(stderr, "6 c=%d y=%d\n", c.x, c.y);
3912 matches = NULL;
3913 matches = boundary_find_matches_single(bl, &c);
3914 if (g_list_length(matches) > 0)
3915 {
3916 *lon = lon2 - lon_d;
3917 *lat = lat2;
3918 return 1;
3919 }
3920
3921 c.x = transform_from_geo_lon(lon2 - lon_d);
3922 c.y = transform_from_geo_lat(lat2);
3923 //fprintf(stderr, "7 c=%d y=%d\n", c.x, c.y);
3924 matches = NULL;
3925 matches = boundary_find_matches_single(bl, &c);
3926 if (g_list_length(matches) > 0)
3927 {
3928 *lon = lon2 - lon_d;
3929 *lat = lat2;
3930 return 1;
3931 }
3932
3933 c.x = transform_from_geo_lon(lon2);
3934 c.y = transform_from_geo_lat(lat2 + lat_d);
3935 //fprintf(stderr, "8 c=%d y=%d\n", c.x, c.y);
3936 matches = NULL;
3937 matches = boundary_find_matches_single(bl, &c);
3938 if (g_list_length(matches) > 0)
3939 {
3940 *lon = lon2;
3941 *lat = lat2 + lat_d;
3942 return 1;
3943 }
3944
3945
3946 c.x = transform_from_geo_lon(lon2);
3947 c.y = transform_from_geo_lat(lat2 - lat_d);
3948 //fprintf(stderr, "9 c=%d y=%d\n", c.x, c.y);
3949 matches = NULL;
3950 matches = boundary_find_matches_single(bl, &c);
3951 if (g_list_length(matches) > 0)
3952 {
3953 *lon = lon2;
3954 *lat = lat2 - lat_d;
3955 return 1;
3956 }
3957
3958 return 0;
3959
3960 }
3961
3962 void build_boundary_tree(GList *bl)
3963 {
3964 long long rid;
3965 int retval;
3966 int rc = 0;
3967 int commit_after = 20000;
3968 int count = 0;
3969 int admin_l = 99;
3970 double lat, lon;
3971 struct coord c;
3972 GList *l = NULL;
3973 GList *l2 = NULL;
3974 GList *match = NULL;
3975 struct boundary *b;
3976 int admin_l_boundary;
3977 int max_admin_l;
3978
3979 // loop thru all the boundaries
3980 do
3981 {
3982 rc = sqlite3_step(stmt_bd_002);
3983 switch (rc)
3984 {
3985 case SQLITE_DONE:
3986 break;
3987 case SQLITE_ROW:
3988
3989 // select rel_id, admin_level, lat, lon, name from boundary where done = 0;
3990 rid = sqlite3_column_int64(stmt_bd_002, 0);
3991 admin_l = sqlite3_column_int(stmt_bd_002, 1);
3992 lat = sqlite3_column_double(stmt_bd_002, 2);
3993 lon = sqlite3_column_double(stmt_bd_002, 3);
3994 c.x = transform_from_geo_lon(lon);
3995 c.y = transform_from_geo_lat(lat);
3996
3997 //fprintf(stderr, "relid:%lld(%d) x=%d y=%d\n", rid, admin_l, c.x, c.y);
3998
3999 max_admin_l = 0;
4000 l2 = boundary_find_matches_level(bl, &c, 0, (admin_l - 1));
4001 l = l2;
4002 match = NULL;
4003 while (l)
4004 {
4005 b = l->data;
4006 char *admin_l_boundary_str = osm_tag_value(b->ib, "admin_level");
4007 if (!admin_l_boundary_str)
4008 {
4009 admin_l_boundary = 9999;
4010 }
4011 else
4012 {
4013 admin_l_boundary = atoi(admin_l_boundary_str);
4014 }
4015
4016 if (admin_l_boundary < 2)
4017 {
4018 admin_l_boundary = 9999;
4019 }
4020
4021 //fprintf(stderr, "b=%d\n", admin_l_boundary);
4022 if (admin_l_boundary > max_admin_l)
4023 {
4024 max_admin_l = admin_l_boundary;
4025 match = l;
4026 //fprintf(stderr, "b2=%d\n", max_admin_l);
4027 }
4028 l = g_list_next(l);
4029 }
4030
4031 if (match)
4032 {
4033 b = match->data;
4034 //fprintf(stderr, "relid:%lld(%d) parentid:%lld(%d)\n", rid, admin_l, item_bin_get_relationid(b->ib), max_admin_l);
4035
4036 sqlite3_bind_int64(stmt_bd_003, 1, item_bin_get_relationid(b->ib));
4037 sqlite3_bind_int64(stmt_bd_003, 2, rid);
4038 sqlite3_step(stmt_bd_003);
4039 sqlite3_reset(stmt_bd_003);
4040 }
4041
4042
4043 if (count == 0)
4044 {
4045 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4046 }
4047 count++;
4048
4049 if (count > commit_after)
4050 {
4051 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4052 count = 0;
4053 }
4054
4055 break;
4056 default:
4057 fprintf(stderr, "SQL Error: %d\n", rc);
4058 break;
4059 }
4060 }
4061 while (rc == SQLITE_ROW);
4062 sqlite3_reset(stmt_bd_002);
4063
4064 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4065 }
4066
4067 void add_boundary_to_db(long long current_id, int admin_level, struct item_bin *item_bin_3, struct relation_member *memb)
4068 {
4069 char *str = NULL;
4070 struct node_lat_lon node_coords;
4071
4072 sqlite3_bind_int64(stmt_bd_001, 1, current_id);
4073
4074 if (admin_level < 2)
4075 {
4076 sqlite3_bind_int(stmt_bd_001, 2, 9999);
4077 }
4078 else
4079 {
4080 sqlite3_bind_int(stmt_bd_001, 2, admin_level);
4081 }
4082 while ((str = item_bin_get_attr(item_bin_3, attr_osm_member, str)))
4083 {
4084 if (get_relation_member(str, memb))
4085 {
4086 if (memb->type == 2)
4087 {
4088 // way-id is "memb.id"
4089 // use the first nodes coords
4090 get_lat_lon_way_first_node(memb->id, &node_coords);
4091 if (node_coords.valid == 1)
4092 {
4093 sqlite3_bind_double(stmt_bd_001, 3, node_coords.lat);
4094 sqlite3_bind_double(stmt_bd_001, 4, node_coords.lon);
4095 }
4096 break;
4097 }
4098 }
4099 }
4100 sqlite3_bind_text(stmt_bd_001, 5, osm_tag_value(item_bin_3, "name"), -1, SQLITE_STATIC);
4101 sqlite3_step(stmt_bd_001);
4102 sqlite3_reset(stmt_bd_001);
4103 }
4104
4105
4106 void osm_add_member(int type, osmid ref, char *role)
4107 {
4108 char member_buffer[BUFFER_SIZE * 3 + 3];
4109 struct attr memberattr = { attr_osm_member };
4110
4111 sprintf(member_buffer,"%d:"LONGLONG_FMT":%s", type, (long long) ref, role);
4112 memberattr.u.str = member_buffer;
4113 item_bin_add_attr(item_bin_2, &memberattr);
4114 }
4115
4116 static void relation_add_tag(char *k, char *v)
4117 {
4118 int add_tag = 1;
4119 #if 0
4120 fprintf(stderr,"add tag %s %s\n",k,v);
4121 #endif
4122 if (!strcmp(k, "type"))
4123 {
4124 strcpy(relation_type, v);
4125 add_tag = 0;
4126 }
4127 else if (!strcmp(k, "restriction"))
4128 {
4129 if (!strncmp(v, "no_", 3))
4130 {
4131 item_bin_2->type = type_street_turn_restriction_no;
4132 add_tag = 0;
4133 }
4134 else if (!strncmp(v, "only_", 5))
4135 {
4136 item_bin_2->type = type_street_turn_restriction_only;
4137 add_tag = 0;
4138 }
4139 else
4140 {
4141 item_bin_2->type = type_none;
4142 //osm_warning("relation", current_id, 0, "Unknown restriction %s\n", v);
4143 }
4144 }
4145 else if (!strcmp(k, "admin_level"))
4146 {
4147 admin_level = atoi(v);
4148 }
4149 else if (!strcmp(k, "boundary"))
4150 {
4151 if (!strcmp(v, "administrative") || (experimental && !strcmp(v, "postal_code")))
4152 {
4153 boundary = 1;
4154 }
4155 }
4156 else if (!strcmp(k, "ISO3166-1"))
4157 {
4158 strcpy(iso_code, v);
4159 }
4160
4161 if (add_tag)
4162 {
4163 //fprintf(stderr,"*TAG*%s=%s\n", k, v);
4164 char tag[strlen(k) + strlen(v) + 2];
4165 sprintf(tag, "%s=%s", k, v);
4166 item_bin_add_attr_string(item_bin_2, attr_osm_tag, tag);
4167 }
4168 }
4169
4170 static int attr_longest_match(struct attr_mapping **mapping, int mapping_count, enum item_type *types, int types_count)
4171 {
4172 int i, j, longest = 0, ret = 0, sum, val;
4173 struct attr_mapping *curr;
4174 for (i = 0; i < mapping_count; i++)
4175 {
4176 sum = 0;
4177 curr = mapping[i];
4178 for (j = 0; j < curr->attr_present_idx_count; j++)
4179 {
4180 val = attr_present[curr->attr_present_idx[j]];
4181 if (val)
4182 sum += val;
4183 else
4184 {
4185 sum = -1;
4186 break;
4187 }
4188 }
4189 if (sum > longest)
4190 {
4191 longest = sum;
4192 ret = 0;
4193 }
4194 if (sum > 0 && sum == longest && ret < types_count)
4195 types[ret++] = curr->type;
4196 }
4197 return ret;
4198 }
4199
4200 static void attr_longest_match_clear(void)
4201 {
4202 memset(attr_present, 0, sizeof(*attr_present) * attr_present_count);
4203 }
4204
4205 char* remove_all_spaces_non_utf8(char *str)
4206 {
4207 char *write = str, *read = str;
4208
4209 if (str == NULL)
4210 {
4211 return NULL;
4212 }
4213
4214 do
4215 {
4216 if (!g_ascii_isspace(*read))
4217 {
4218 *write++ = *read;
4219 }
4220 } while (*read++);
4221
4222 return str;
4223 }
4224
4225 void osm_end_way(struct maptool_osm *osm)
4226 {
4227 int i, count;
4228 int *def_flags, add_flags;
4229 enum item_type types[10];
4230 struct item_bin *item_bin;
4231 int count_lines = 0, count_areas = 0;
4232 int dont_save_to_db = 1;
4233 int first = 1;
4234 int retval;
4235 char *name_folded = NULL;
4236 char *name_folded2 = NULL;
4237 struct node_lat_lon node_coords;
4238
4239 in_way = 0;
4240
4241 if (!osm->ways)
4242 {
4243 return;
4244 }
4245
4246 /*
4247 if (dedupe_ways_hash)
4248 {
4249 if (g_hash_table_lookup(dedupe_ways_hash, (gpointer) (long) wayid))
4250 {
4251 return;
4252 }
4253 g_hash_table_insert(dedupe_ways_hash, (gpointer) (long) wayid, (gpointer) 1);
4254 }
4255 */
4256
4257 ways_processed_count++;
4258
4259 count = attr_longest_match(attr_mapping_way, attr_mapping_way_count, types, sizeof(types) / sizeof(enum item_type));
4260 if (!count)
4261 {
4262 count = 1;
4263 types[0] = type_street_unkn;
4264 }
4265
4266 if (count >= 10)
4267 {
4268 fprintf(stderr, "way id %ld\n", osmid_attr_value);
4269 dbg_assert(count < 10);
4270 }
4271
4272 if (attr_strings[attr_string_label] != NULL)
4273 {
4274 if (strlen(attr_strings[attr_string_label]) > 0)
4275 {
4276 dont_save_to_db = 0;
4277 }
4278 }
4279
4280 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!!)
4281 {
4282 add_flags = 0;
4283 if (types[i] == type_none)
4284 {
4285 continue;
4286 }
4287
4288 //if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn))
4289 // continue;
4290
4291 // DO NOT exclude unknown streets here, or city-to-countrypolygon matching won't work!!
4292 if (ignore_unkown && (types[i] == type_point_unkn))
4293 {
4294 continue;
4295 }
4296
4297 if (types[i] != type_street_unkn)
4298 {
4299 if (types[i] < type_area)
4300 {
4301 count_lines++;
4302 }
4303 else
4304 {
4305 count_areas++;
4306 }
4307 }
4308 item_bin = init_item(types[i], 0);
4309 item_bin_add_coord(item_bin, coord_buffer, coord_count);
4310 nodes_ref_item_bin(item_bin, 0);
4311 def_flags = item_get_default_flags(types[i]);
4312
4313 // save nodes of way -----------------
4314 // save nodes of way -----------------
4315 // * now save ALL way_nodes to DB !! could be a very big DB !!
4316 if (first == 1)
4317 {
4318 first = 0;
4319 int i788;
4320 struct coord *c788 = (struct coord *) (item_bin + 1); // set pointer to coord struct of this item
4321 for (i788 = 0; i788 < item_bin->clen / 2; i788++)
4322 {
4323 if (i788 == 0)
4324 {
4325 // save lat,lon of first node of this way
4326 if (dont_save_to_db == 0)
4327 {
4328
4329 #ifdef MAPTOOL_USE_SQL
4330
4331 //fprintf(stderr, "insert -w\n");
4332
4333 get_lat_lon_for_node(REF(c788[i788]), &node_coords);
4334
4335 if (sql_counter == 0)
4336 {
4337 retval = sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4338 if ((retval > 0) && (retval < 100))
4339 {
4340 fprintf(stderr, "begin: ways code:%d\n", retval);
4341 }
4342 else
4343 {
4344 //fprintf(stderr, "begin: ways\n");
4345 }
4346 }
4347
4348 sql_counter++;
4349
4350 sqlite3_bind_int64(stmt_way, 1, osmid_attr_value);
4351 sqlite3_bind_text(stmt_way, 2, attr_strings[attr_string_label], -1, SQLITE_STATIC);
4352
4353 // streetname folded for later sort/search
4354 name_folded = linguistics_casefold(attr_strings[attr_string_label]);
4355 if (name_folded)
4356 {
4357 name_folded2 = linguistics_remove_all_specials(name_folded);
4358 if (name_folded2)
4359 {
4360 g_free(name_folded);
4361 name_folded = name_folded2;
4362 }
4363
4364 name_folded2 = linguistics_expand_special(name_folded, 1);
4365 if (name_folded2)
4366 {
4367 sqlite3_bind_text(stmt_way, 6, name_folded2, -1, SQLITE_STATIC);
4368 }
4369 else
4370 {
4371 sqlite3_bind_text(stmt_way, 6, name_folded, -1, SQLITE_STATIC);
4372 }
4373 }
4374 else
4375 {
4376 // use original string
4377 sqlite3_bind_text(stmt_way, 6, attr_strings[attr_string_label], -1, SQLITE_STATIC);
4378 }
4379
4380 // town id
4381 sqlite3_bind_int(stmt_way, 3, -1);
4382
4383 if (node_coords.valid == 1)
4384 {
4385 // lat
4386 sqlite3_bind_double(stmt_way, 4, node_coords.lat);
4387 // lon
4388 sqlite3_bind_double(stmt_way, 5, node_coords.lon);
4389 }
4390
4391 // retval =
4392 sqlite3_step(stmt_way);
4393 //if ((retval > 0) && (retval < 100))
4394 //{
4395 // fprintf(stderr, "ways step: code:%d wid:%lld\n", retval, osmid_attr_value);
4396 //}
4397 sqlite3_reset(stmt_way);
4398
4399 if (name_folded)
4400 {
4401 g_free(name_folded);
4402 }
4403
4404 if (name_folded2)
4405 {
4406 g_free(name_folded2);
4407 }
4408
4409 if (sql_counter > MAX_ROWS_WO_COMMIT_3)
4410 {
4411 sql_counter = 0;
4412 retval = sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
4413 if ((retval > 0) && (retval < 100))
4414 {
4415 fprintf(stderr, "ways: %lld code:%d\n", ways_processed_count, retval);
4416 }
4417 else
4418 {
4419 //fprintf(stderr, "ways: %lld\n", ways_processed_count);
4420 }
4421 }
4422
4423 #endif
4424
4425 }
4426
4427 }
4428
4429 #ifdef MAPTOOL_USE_SQL
4430
4431 if (sql_counter4 == 0)
4432 {
4433 sqlite3_exec(sql_handle004, "BEGIN", 0, 0, 0);
4434 sqlite3_exec(sql_handle005, "BEGIN", 0, 0, 0);
4435 sqlite3_exec(sql_handle006, "BEGIN", 0, 0, 0);
4436 sqlite3_exec(sql_handle007, "BEGIN", 0, 0, 0);
4437 //fprintf(stderr, "begin: way nodes\n");
4438 }
4439 sql_counter4++;
4440 // fprintf(stderr, "insert #WN\n");
4441
4442 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
4443 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT) > 0)
4444 {
4445 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
4446 {
4447 #endif
4448 sqlite3_bind_int64(stmt_way_node, 1, osmid_attr_value);
4449 sqlite3_bind_int64(stmt_way_node, 2, REF(c788[i788]));
4450 sqlite3_bind_int64(stmt_way_node, 3, (i788 + 1));
4451 sqlite3_step(stmt_way_node);
4452 sqlite3_reset(stmt_way_node);
4453 #ifdef MAPTOOL_SPLIT_WAYNODE_DB
4454 }
4455 else
4456 {
4457 sqlite3_bind_int64(stmt_way_nodeb, 1, osmid_attr_value);
4458 sqlite3_bind_int64(stmt_way_nodeb, 2, REF(c788[i788]));
4459 sqlite3_bind_int64(stmt_way_nodeb, 3, (i788 + 1));
4460 sqlite3_step(stmt_way_nodeb);
4461 sqlite3_reset(stmt_way_nodeb);
4462 }
4463 }
4464 else
4465 {
4466 if ((osmid_attr_value & MAPTOOL_SPLIT_WAYNODE_BIT2) > 0)
4467 {
4468 sqlite3_bind_int64(stmt_way_node__2, 1, osmid_attr_value);
4469 sqlite3_bind_int64(stmt_way_node__2, 2, REF(c788[i788]));
4470 sqlite3_bind_int64(stmt_way_node__2, 3, (i788 + 1));
4471 sqlite3_step(stmt_way_node__2);
4472 sqlite3_reset(stmt_way_node__2);
4473 }
4474 else
4475 {
4476 sqlite3_bind_int64(stmt_way_node__2b, 1, osmid_attr_value);
4477 sqlite3_bind_int64(stmt_way_node__2b, 2, REF(c788[i788]));
4478 sqlite3_bind_int64(stmt_way_node__2b, 3, (i788 + 1));
4479 sqlite3_step(stmt_way_node__2b);
4480 sqlite3_reset(stmt_way_node__2b);
4481 }
4482 }
4483 #endif
4484
4485 if (sql_counter4 > MAX_ROWS_WO_COMMIT_4)
4486 {
4487 sqlite3_exec(sql_handle004, "COMMIT", 0, 0, 0);
4488 sqlite3_exec(sql_handle005, "COMMIT", 0, 0, 0);
4489 sqlite3_exec(sql_handle006, "COMMIT", 0, 0, 0);
4490 sqlite3_exec(sql_handle007, "COMMIT", 0, 0, 0);
4491 //fprintf(stderr, "way nodes: %lld\n", sql_counter4);
4492 sql_counter4 = 0;
4493 }
4494 #endif
4495 }
4496 }
4497 // save nodes of way -----------------
4498 // save nodes of way -----------------
4499
4500
4501 if (def_flags)
4502 {
4503 flags_attr_value = (*def_flags | flags[0] | flags[1]) & ~flags[2];
4504 // only add flags if they differ from the default flags!!
4505 if (flags_attr_value != *def_flags)
4506 {
4507 add_flags = 1;
4508 }
4509 }
4510 else // maybe should explicitly specify which "types" to consider? now its all "types"
4511 {
4512 def_flags = 0;
4513 flags_attr_value = (flags[0] | flags[1]) & ~flags[2];
4514
4515 // only add flags if we really have a value
4516 if (flags_attr_value != 0)
4517 {
4518 add_flags = 1;
4519 }
4520 }
4521 item_bin_add_attr_string(item_bin, def_flags ? attr_street_name : attr_label, attr_strings[attr_string_label]);
4522
4523 // housenumber from buildings --------------------
4524 if (types[i] == type_poly_building)
4525 {
4526 // addr:street
4527 item_bin_add_attr_string(item_bin, attr_street_name_dummy, attr_strings[attr_string_street_name]);
4528 // fprintf(stderr, "addr:street :%s\n", attr_strings[attr_string_street_name]);
4529 // addr:housenumber
4530 item_bin_add_attr_string(item_bin, attr_house_number_dummy, attr_strings[attr_string_house_number]);
4531 // fprintf(stderr, "addr:housenumber:%s\n", attr_strings[attr_string_house_number]);
4532 }
4533 // housenumber from buildings --------------------
4534
4535
4536 if (attr_strings[attr_string_label_alt])
4537 {
4538 item_bin_add_attr_string(item_bin, attr_street_name_match, attr_strings[attr_string_label_alt]);
4539 if (debug_itembin(ib))
4540 {
4541 fprintf(stderr, "street name : %s\n", attr_strings[attr_string_label]);
4542 fprintf(stderr, "street name sys: %s\n", attr_strings[attr_string_street_name_systematic]);
4543 fprintf(stderr, "street name alt: %s\n", attr_strings[attr_string_label_alt]);
4544 }
4545 }
4546 item_bin_add_attr_string(item_bin, attr_street_name_systematic, attr_strings[attr_string_street_name_systematic]);
4547
4548 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value);
4549 if (debug_attr_buffer[0])
4550 item_bin_add_attr_string(item_bin, attr_debug, debug_attr_buffer);
4551 if (add_flags)
4552 item_bin_add_attr_int(item_bin, attr_flags, flags_attr_value);
4553 if (maxspeed_attr_value)
4554 item_bin_add_attr_int(item_bin, attr_maxspeed, maxspeed_attr_value);
4555
4556
4557 // if we duplicated this way here (because of multiple attributes), then set "duplicate_way" attr
4558 if (i > 0)
4559 {
4560 item_bin_add_attr_int(item_bin, attr_duplicate_way, 1);
4561 }
4562
4563 item_bin_write(item_bin, osm->ways);
4564
4565 if (border_only_map_as_xml == 1)
4566 {
4567 item_bin_write_xml(item_bin, "borders.xml");
4568 }
4569 }
4570
4571 /*
4572 if(osm->line2poi) {
4573 count=attr_longest_match(attr_mapping_way2poi, attr_mapping_way2poi_count, types, sizeof(types)/sizeof(enum item_type));
4574 dbg_assert(count < 10);
4575 for (i = 0 ; i < count ; i++) {
4576 if (types[i] == type_none || types[i] == type_point_unkn)
4577 continue;
4578 item_bin=init_item(types[i], 0);
4579 item_bin_add_coord(item_bin, coord_buffer, coord_count);
4580 item_bin_add_attr_string(item_bin, attr_label, attr_strings[attr_string_label]);
4581 item_bin_add_attr_string(item_bin, attr_house_number, attr_strings[attr_string_house_number]);
4582 item_bin_add_attr_string(item_bin, attr_street_name, attr_strings[attr_string_street_name]);
4583 item_bin_add_attr_string(item_bin, attr_phone, attr_strings[attr_string_phone]);
4584 item_bin_add_attr_string(item_bin, attr_fax, attr_strings[attr_string_fax]);
4585 item_bin_add_attr_string(item_bin, attr_email, attr_strings[attr_string_email]);
4586 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
4587 item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]);
4588 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value);
4589 item_bin_write(item_bin, count_areas<count_lines?osm->line2poi:osm->poly2poi);
4590 }
4591 }
4592 */
4593
4594 attr_longest_match_clear();
4595 }
4596
4597 void osm_append_housenumber_node(FILE *out, struct coord *c, char *house_number, char *street_name)
4598 {
4599 struct item_bin *item_bin;
4600 item_bin = init_item(type_house_number, 0);
4601 // item_bin->type = type_house_number;
4602
4603 item_bin_add_coord(item_bin, c, 1);
4604 item_bin_add_attr_string(item_bin, attr_house_number, house_number);
4605 item_bin_add_attr_string(item_bin, attr_street_name, street_name);
4606
4607 // DEBUG -- DEBUG -- DEBUG --
4608 // DEBUG -- DEBUG -- DEBUG --
4609 // dump_itembin(item_bin);
4610 // DEBUG -- DEBUG -- DEBUG --
4611 // DEBUG -- DEBUG -- DEBUG --
4612
4613 item_bin_write(item_bin, out);
4614 }
4615
4616 void osm_end_node(struct maptool_osm *osm)
4617 {
4618 int count, i;
4619 char *postal;
4620 enum item_type types[10];
4621 struct item_bin *item_bin;
4622 in_node = 0;
4623
4624 if (!osm->nodes || !node_is_tagged || !nodeid)
4625 return;
4626
4627 count = attr_longest_match(attr_mapping_node, attr_mapping_node_count, types, sizeof(types) / sizeof(enum item_type));
4628
4629 if (!count)
4630 {
4631 types[0] = type_point_unkn;
4632 count = 1;
4633 }
4634
4635 dbg_assert(count < 10);
4636
4637 for (i = 0; i < count; i++)
4638 {
4639 if (types[i] == type_none)
4640 {
4641 continue;
4642 }
4643
4644 //if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn))
4645 // continue;
4646
4647 if (ignore_unkown && (types[i] == type_point_unkn))
4648 {
4649 continue;
4650 }
4651
4652 item_bin = init_item(types[i], 0);
4653
4654 if (item_is_town(*item_bin) && attr_strings[attr_string_label])
4655 {
4656 if (debug_itembin(ib))
4657 {
4658 fprintf(stderr, "osm_end_node: have town: %s\n", attr_strings[attr_string_label]);
4659 }
4660 }
4661
4662 if (item_is_town(*item_bin) && attr_strings[attr_string_population])
4663 {
4664 item_bin_set_type_by_population(item_bin, atoi(attr_strings[attr_string_population]));
4665 }
4666
4667 item_bin_add_coord(item_bin, &ni->c, 1);
4668 item_bin_add_attr_string(item_bin, item_is_town(*item_bin) ? attr_town_name : attr_label, attr_strings[attr_string_label]);
4669 item_bin_add_attr_string(item_bin, attr_house_number, attr_strings[attr_string_house_number]);
4670 item_bin_add_attr_string(item_bin, attr_street_name, attr_strings[attr_string_street_name]);
4671 item_bin_add_attr_string(item_bin, attr_phone, attr_strings[attr_string_phone]);
4672 item_bin_add_attr_string(item_bin, attr_fax, attr_strings[attr_string_fax]);
4673 item_bin_add_attr_string(item_bin, attr_email, attr_strings[attr_string_email]);
4674 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
4675 item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]);
4676 item_bin_add_attr_longlong(item_bin, attr_osm_nodeid, osmid_attr_value);
4677 item_bin_add_attr_string(item_bin, attr_debug, debug_attr_buffer);
4678
4679 postal = attr_strings[attr_string_postal];
4680 if (postal)
4681 {
4682 char *sep = strchr(postal, ',');
4683 if (sep)
4684 {
4685 *sep = '\0';
4686 }
4687 item_bin_add_attr_string(item_bin, item_is_town(*item_bin) ? attr_town_postal : attr_postal, postal);
4688 }
4689
4690 item_bin_write(item_bin, osm->nodes);
4691
4692 if (item_is_town(*item_bin) && attr_strings[attr_string_label] && osm->towns)
4693 {
4694 item_bin = init_item(item_bin->type, 0);
4695 item_bin_add_coord(item_bin, &ni->c, 1);
4696 item_bin_add_attr_string(item_bin, attr_osm_is_in, is_in_buffer);
4697 item_bin_add_attr_longlong(item_bin, attr_osm_nodeid, osmid_attr_value);
4698 item_bin_add_attr_string(item_bin, attr_town_postal, postal);
4699 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);
4700 item_bin_add_attr_string(item_bin, attr_town_name, attr_strings[attr_string_label]);
4701 if (attr_strings[attr_string_label_alt])
4702 {
4703 item_bin_add_attr_string(item_bin, attr_town_name_match, attr_strings[attr_string_label_alt]);
4704 if (debug_itembin(item_bin))
4705 {
4706 fprintf(stderr, "town name : %s\n", attr_strings[attr_string_label]);
4707 fprintf(stderr, "town name alt: %s\n", attr_strings[attr_string_label_alt]);
4708 }
4709 }
4710
4711 item_bin_write(item_bin, osm->towns);
4712 }
4713
4714 //fprintf(stderr,"********DUMP nd1***********\n");
4715 //if (types[i] == type_house_number)
4716 //{
4717 // dump_itembin(item_bin);
4718 //}
4719 //fprintf(stderr,"********DUMP nd1***********\n");
4720
4721 }
4722 processed_nodes_out++;
4723 attr_longest_match_clear();
4724 }
4725
4726 static struct country_table *
4727 osm_process_town_unknown_country(void)
4728 {
4729 static struct country_table *unknown;
4730 unknown = country_from_countryid(999);
4731 return unknown;
4732 }
4733
4734 static struct country_table *
4735 osm_process_item_fixed_country(void)
4736 {
4737 static struct country_table *fixed;
4738 fixed = country_from_countryid(global_fixed_country_id);
4739 return fixed;
4740 }
4741
4742 static struct country_table *
4743 osm_process_town_by_is_in(struct item_bin *ib, char *is_in)
4744 {
4745 struct country_table *result = NULL, *lookup;
4746 char *tok, *dup = g_strdup(is_in), *buf = dup;
4747 int conflict;
4748
4749 while ((tok = strtok(buf, ",;")))
4750 {
4751 while (*tok == ' ')
4752 {
4753 tok++;
4754 }
4755 lookup = g_hash_table_lookup(country_table_hash, tok);
4756 if (lookup)
4757 {
4758 if (result && result->countryid != lookup->countryid)
4759 {
4760 //char *label = item_bin_get_attr(ib, attr_town_name, NULL);
4761 //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);
4762 conflict = 1;
4763 }
4764 result = lookup;
4765 }
4766 buf = NULL;
4767 }
4768 g_free(dup);
4769 return result;
4770 }
4771
4772 static int osm_process_street_by_boundary(GList *bl, long long town_osm_id, long long town_relation_id, struct coord *c)
4773 {
4774 GList *matches = NULL;
4775
4776 matches = boundary_find_matches_single(bl, c);
4777 if (g_list_length(matches) > 0)
4778 {
4779 return 1;
4780 }
4781
4782 return 0;
4783 }
4784
4785
4786 static struct country_table *
4787 osm_process_town_by_boundary(GList *bl, struct item_bin *ib, struct coord *c, struct attr *attrs)
4788 {
4789 GList *l, *matches = boundary_find_matches(bl, c);
4790 struct boundary *match = NULL;
4791
4792 //fprintf(stderr,"town_by_boundary:001\n");
4793
4794 l = matches;
4795 while (l)
4796 {
4797 //fprintf(stderr,"town_by_boundary:002\n");
4798 struct boundary *b = l->data;
4799 if (b->country)
4800 {
4801 //fprintf(stderr,"town_by_boundary:003\n");
4802 //if (match)
4803 //{
4804 //osm_warning("node", item_bin_get_nodeid(ib), 0, "node (x=0x%x,y=0x%x) conflict country ", c->x, c->y);
4805 //osm_warning("relation", boundary_relid(match), 1, "country %d vs ", match->country->countryid);
4806 //osm_warning("relation", boundary_relid(b), 1, "country %d\n", b->country->countryid);
4807 //}
4808 match = b;
4809 }
4810 l = g_list_next(l);
4811 }
4812
4813 //fprintf(stderr,"town_by_boundary:004\n");
4814
4815 if (match)
4816 {
4817 //fprintf(stderr,"town_by_boundary:005\n");
4818 if (match && match->country && match->country->admin_levels)
4819 {
4820 //fprintf(stderr,"town_by_boundary:006\n");
4821 l = matches;
4822 while (l)
4823 {
4824 //fprintf(stderr,"town_by_boundary:007\n");
4825
4826 struct boundary *b = l->data;
4827 char *admin_level = osm_tag_value(b->ib, "admin_level");
4828 char *postal = osm_tag_value(b->ib, "postal_code");
4829 if (admin_level)
4830 {
4831 //fprintf(stderr,"town_by_boundary:008\n");
4832
4833 int a = atoi(admin_level);
4834 int end = strlen(match->country->admin_levels) + 3;
4835 char *name;
4836 if (a > 2 && a < end)
4837 {
4838 //fprintf(stderr,"town_by_boundary:009\n");
4839
4840 enum attr_type attr_type = attr_none;
4841 switch (match->country->admin_levels[a - 3])
4842 {
4843 case 's':
4844 attr_type = attr_state_name;
4845 break;
4846 case 'c':
4847 attr_type = attr_county_name;
4848 break;
4849 case 'm':
4850 attr_type = attr_municipality_name;
4851 break;
4852 }
4853 name = osm_tag_value(b->ib, "name");
4854 if (name && attr_type != attr_none)
4855 {
4856 //fprintf(stderr,"town_by_boundary:010\n");
4857 attrs[a - 2].type = attr_type;
4858 attrs[a - 2].u.str = name;
4859 }
4860 }
4861 }
4862 if (postal)
4863 {
4864 //fprintf(stderr,"town_by_boundary:011\n");
4865 attrs[0].type = attr_town_postal;
4866 attrs[0].u.str = postal;
4867 }
4868 l = g_list_next(l);
4869 }
4870 }
4871 return match->country;
4872 }
4873 else
4874 {
4875 //fprintf(stderr,"town_by_boundary:099\n");
4876 return NULL;
4877 }
4878 }
4879
4880 int town_size_estimate(int type)
4881 {
4882 int size = 0; // default -> not use this area as a town!
4883
4884 switch (type)
4885 {
4886 case type_town_label_1e7:
4887 case type_town_label_5e6:
4888 case type_town_label_2e6:
4889 case type_town_label_1e6:
4890 case type_town_label_5e5:
4891 case type_town_label_2e5:
4892 size = 10000;
4893 break;
4894 case type_town_label_1e5:
4895 case type_town_label_5e4:
4896 case type_town_label_2e4:
4897 size = 3100;
4898 break;
4899 case type_town_label_1e4:
4900 case type_town_label_5e3:
4901 case type_town_label_2e3:
4902 size = 1460;
4903 break;
4904 case type_town_label_1e3:
4905 case type_town_label_5e2:
4906 // case type_town_label_2e2:
4907 case type_town_label_1e2:
4908 case type_town_label_5e1:
4909 case type_town_label_2e1:
4910 case type_town_label_1e1:
4911 case type_town_label_5e0:
4912 // case type_town_label_2e0:
4913 case type_town_label_1e0:
4914 case type_town_label_0e0:
4915 size = 660;
4916 break;
4917 default:
4918 break;
4919 }
4920
4921 // size = size * global_search_street_size_factor;
4922
4923 // lat factor 0.072
4924 // lon factor 0.101
4925
4926 // size = 10000
4927 // lat lon size = size / 100000
4928
4929 /*
4930 sel->u.c_rect.lu.x = c->x - size;
4931 sel->u.c_rect.lu.y = c->y + size;
4932 sel->u.c_rect.rl.x = c->x + size;
4933 sel->u.c_rect.rl.y = c->y - size;
4934 */
4935 return size;
4936 }
4937
4938 void assign_town_to_streets_v1();
4939 void assign_town_to_streets_by_boundary(GList *bl);
4940 void copy_town_border_data();
4941
4942 void assign_town_to_streets(GList *bl)
4943 {
4944 int skipped = 0;
4945
4946 copy_town_border_data();
4947 assign_town_to_streets_by_boundary(bl);
4948
4949 // now process all the left over towns
4950 assign_town_to_streets_v1(1);
4951 assign_town_to_streets_v1(2); // make townsize double on second pass
4952 }
4953
4954
4955 void copy_town_border_data()
4956 {
4957 long long bid;
4958 long long tid;
4959 int retval;
4960 int rc = 0;
4961 int commit_after = 20000;
4962 int count = 0;
4963 int admin_l = 99;
4964
4965 sqlite3_stmt *stmt_d_1;
4966 sqlite3_stmt *stmt_d_2;
4967 retval = sqlite3_prepare_v2(sql_handle, "select border_id, id, admin_level from town2;", -1, &stmt_d_1, NULL);
4968 //fprintf(stderr, "prep:%d\n", retval);
4969 retval = sqlite3_prepare_v2(sql_handle, "update town set border_id = ?, size = ? where id = ?;", -1, &stmt_d_2, NULL);
4970 //fprintf(stderr, "prep:%d\n", retval);
4971
4972 // loop thru all the towns
4973 do
4974 {
4975 rc = sqlite3_step(stmt_d_1);
4976 switch (rc)
4977 {
4978 case SQLITE_DONE:
4979 break;
4980 case SQLITE_ROW:
4981 bid = sqlite3_column_int64(stmt_d_1, 0);
4982 tid = sqlite3_column_int64(stmt_d_1, 1);
4983 admin_l = sqlite3_column_int(stmt_d_1, 2);
4984
4985 //fprintf(stderr, "bid=%lld tid=%lld amdin_level=%d\n", bid, tid, admin_l);
4986
4987 if (count == 0)
4988 {
4989 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
4990 }
4991 count++;
4992
4993 sqlite3_bind_int64(stmt_d_2, 1, bid);
4994 sqlite3_bind_int64(stmt_d_2, 2, tid);
4995 sqlite3_bind_int(stmt_d_2, 3, (TOWN_ADMIN_LEVEL_CORR_BASE + admin_l) * TOWN_BY_BOUNDARY_SIZE_FACTOR);
4996 sqlite3_step(stmt_d_2);
4997 sqlite3_reset(stmt_d_2);
4998
4999 if (count > commit_after)
5000 {
5001 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5002 count = 0;
5003 }
5004
5005 break;
5006 default:
5007 fprintf(stderr, "SQL Error: %d\n", rc);
5008 break;
5009 }
5010 }
5011 while (rc == SQLITE_ROW);
5012 sqlite3_reset(stmt_d_1);
5013
5014 retval = sqlite3_finalize(stmt_d_2);
5015 //fprintf(stderr, "fin:%d\n", retval);
5016 retval = sqlite3_finalize(stmt_d_1);
5017 //fprintf(stderr, "fin:%d\n", retval);
5018
5019 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5020
5021 }
5022
5023 void assign_town_to_streets_by_boundary(GList *bl)
5024 {
5025 long long nd;
5026 long long wid;
5027 long long town_rel_id;
5028 int size;
5029 double lat;
5030 double lon;
5031 double temp;
5032 double lat_min;
5033 double lat_max;
5034 double lon_min;
5035 double lon_max;
5036 int rc;
5037 int rc2;
5038 long long town_count;
5039 long long town_processed_count;
5040 int town_steps = 1;
5041 int result = 0;
5042 struct boundary *bound_temp;
5043
5044 sql_counter = 0;
5045 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5046
5047 // number of towns in DB
5048 sqlite3_reset(stmt_town_sel002);
5049 sqlite3_step(stmt_town_sel002);
5050 town_count = sqlite3_column_int64(stmt_town_sel002, 0);
5051 town_processed_count = 0;
5052
5053 fprintf(stderr, "towns0: %lld/%lld\n", town_processed_count, town_count);
5054
5055 if (town_count == 0)
5056 {
5057 sqlite3_reset(stmt_town_sel002);
5058 return;
5059 }
5060
5061 sql_counter = 0;
5062 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5063
5064 if (town_count > 1000000)
5065 {
5066 town_steps = 500;
5067 }
5068 else if (town_count > 100000)
5069 {
5070 town_steps = 100;
5071 }
5072 else if (town_count > 10000)
5073 {
5074 town_steps = 10;
5075 }
5076
5077 // DEBUG
5078 // town_steps = 1;
5079 // DEBUG
5080
5081 // loop thru all the towns
5082 do
5083 {
5084 rc = sqlite3_step(stmt_town_sel001);
5085 switch (rc)
5086 {
5087 case SQLITE_DONE:
5088 break;
5089 case SQLITE_ROW:
5090 nd = sqlite3_column_int64(stmt_town_sel001, 0);
5091 town_rel_id = sqlite3_column_int64(stmt_town_sel001, 5);
5092 //size = sqlite3_column_int(stmt_town_sel001, 1);
5093 //lat = sqlite3_column_double(stmt_town_sel001, 2);
5094 //lon = sqlite3_column_double(stmt_town_sel001, 3);
5095
5096
5097 GList *match_town = NULL;
5098 GList *l = bl;
5099 int has_found = 0;
5100
5101 if (town_rel_id != 0)
5102 {
5103 // fprintf(stderr ,"processing town name:%s id:%lld border_id:%lld\n", sqlite3_column_text(stmt_town_sel001, 4), nd, town_rel_id);
5104
5105 while (l)
5106 {
5107 struct boundary *b = l->data;
5108 //fprintf(stderr, "bid=%lld tid=%lld\n", item_bin_get_relationid(b->ib), town_rel_id);
5109 if (item_bin_get_relationid(b->ib) == town_rel_id)
5110 {
5111 match_town = l;
5112 has_found = 1;
5113 //fprintf(stderr, "bid=%lld tid=%lld\n", item_bin_get_relationid(b->ib), town_rel_id);
5114 //fprintf(stderr, "*** town border found***\n");
5115 break;
5116 }
5117 l = g_list_next(l);
5118 }
5119 }
5120
5121 if (has_found == 1)
5122 {
5123 // now run thru all the ways
5124 do
5125 {
5126 // only select ways that are in the bounding box of boundary!
5127
5128 bound_temp = match_town->data;
5129 lat_min = transform_to_geo_lat(bound_temp->r.l.y);
5130 lat_max = transform_to_geo_lat(bound_temp->r.h.y);
5131 lon_min = transform_to_geo_lon(bound_temp->r.l.x);
5132 lon_max = transform_to_geo_lon(bound_temp->r.h.x);
5133
5134 //fprintf(stderr, "lat min:%f max:%f lon min:%f max:%f \n", lat_min, lat_max, lon_min, lon_max);
5135
5136 sqlite3_bind_double(stmt_way3a, 1, lat_min);
5137 sqlite3_bind_double(stmt_way3a, 2, lat_max);
5138 sqlite3_bind_double(stmt_way3a, 3, lon_min);
5139 sqlite3_bind_double(stmt_way3a, 4, lon_max);
5140 rc2 = sqlite3_step(stmt_way3a);
5141 switch (rc2)
5142 {
5143 case SQLITE_DONE:
5144 break;
5145 case SQLITE_ROW:
5146 wid = sqlite3_column_int64(stmt_way3a, 0);
5147 lat = sqlite3_column_double(stmt_way3a, 1);
5148 lon = sqlite3_column_double(stmt_way3a, 2);
5149
5150 struct coord c;
5151 c.x = transform_from_geo_lon(lon);
5152 c.y = transform_from_geo_lat(lat);
5153
5154 //fprintf(stderr,"== street by boundary == x:%d y:%d ==\n", c.x, c.y);
5155
5156 if (town_rel_id != 0)
5157 {
5158 // fprintf(stderr, "town:%lld\n", nd);
5159 // fprintf(stderr,"== street by boundary == x:%d y:%d ==\n", c.x, c.y);
5160 result = osm_process_street_by_boundary(match_town, nd, town_rel_id, &c);
5161 if (result == 1)
5162 {
5163 //fprintf(stderr,"== street by boundary == wid:%lld townid:%lld ==\n", wid, nd);
5164 // write "town id" to "way"
5165 if (sql_counter == 0)
5166 {
5167 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5168 }
5169 sql_counter++;
5170 sqlite3_bind_int64(stmt_way3b, 1, nd);
5171 sqlite3_bind_int64(stmt_way3b, 2, wid);
5172 sqlite3_step(stmt_way3b);
5173 sqlite3_reset(stmt_way3b);
5174 if (sql_counter > MAX_ROWS_WO_COMMIT_2b)
5175 {
5176 sql_counter = 0;
5177 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5178 }
5179 }
5180 }
5181
5182 break;
5183 default:
5184 fprintf(stderr, "SQL Error: %d\n", rc);
5185 break;
5186 }
5187 }
5188 while (rc2 == SQLITE_ROW);
5189 sqlite3_reset(stmt_way3a);
5190
5191
5192 // mark town as "done"
5193 if (sql_counter == 0)
5194 {
5195 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5196 }
5197 sql_counter++;
5198 sqlite3_bind_int64(stmt_town_sel007, 1, nd);
5199 sqlite3_step(stmt_town_sel007);
5200 sqlite3_reset(stmt_town_sel007);
5201 if (sql_counter > MAX_ROWS_WO_COMMIT_2b)
5202 {
5203 sql_counter = 0;
5204 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5205 }
5206
5207 }
5208
5209 town_processed_count++;
5210 if ((town_processed_count % town_steps) == 0)
5211 {
5212 fprintf(stderr, "towns0: %lld/%lld\n", town_processed_count, town_count);
5213 }
5214
5215 break;
5216 default:
5217 fprintf(stderr, "SQL Error: %d\n", rc);
5218 break;
5219 }
5220 }
5221 while (rc == SQLITE_ROW);
5222 sqlite3_reset(stmt_town_sel001);
5223
5224 sql_counter = 0;
5225 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5226 }
5227
5228
5229 void assign_town_to_streets_v1(int pass_num)
5230 {
5231
5232 long long nd;
5233 int size;
5234 double lat;
5235 double lon;
5236 double lat_min;
5237 double lat_max;
5238 double lon_min;
5239 double lon_max;
5240 double temp;
5241 int rc;
5242 long long town_count;
5243 long long town_processed_count;
5244 int town_steps = 1;
5245
5246 sql_counter = 0;
5247 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5248
5249 // number of towns in DB
5250 sqlite3_reset(stmt_town_sel002);
5251 sqlite3_step(stmt_town_sel002);
5252 town_count = sqlite3_column_int64(stmt_town_sel002, 0);
5253 town_processed_count = 0;
5254
5255 fprintf(stderr, "towns1: %lld/%lld\n", town_processed_count, town_count);
5256
5257 if (town_count == 0)
5258 {
5259 sqlite3_reset(stmt_town_sel002);
5260 return;
5261 }
5262
5263 sql_counter = 0;
5264 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5265
5266 if (town_count > 1000000)
5267 {
5268 town_steps = 500;
5269 }
5270 else if (town_count > 100000)
5271 {
5272 town_steps = 100;
5273 }
5274 else if (town_count > 10000)
5275 {
5276 town_steps = 10;
5277 }
5278
5279 // DEBUG
5280 // town_steps = 1;
5281 // DEBUG
5282
5283 // loop thru all the towns
5284 do
5285 {
5286 rc = sqlite3_step(stmt_town_sel001);
5287 switch (rc)
5288 {
5289 case SQLITE_DONE:
5290 break;
5291 case SQLITE_ROW:
5292 nd = sqlite3_column_int64(stmt_town_sel001, 0);
5293 size = sqlite3_column_int(stmt_town_sel001, 1);
5294 lat = sqlite3_column_double(stmt_town_sel001, 2);
5295 lon = sqlite3_column_double(stmt_town_sel001, 3);
5296 //fprintf(stderr ,"processing (size:%d) town name:%s id:%lld\n", size, sqlite3_column_text(stmt_town_sel001, 4), nd);
5297
5298 // now update the ways
5299 if (sql_counter == 0)
5300 {
5301 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5302 }
5303
5304 sqlite3_bind_int64(stmt_way3, 1, nd);
5305
5306 if ((pass_num == 2) && (size > 0))
5307 {
5308 size = size * 3;
5309 }
5310
5311 temp = ((double) size) / 100000;
5312 lat_min = lat - temp;
5313 lat_max = lat + temp;
5314 lon_min = lon - temp;
5315 lon_max = lon + temp;
5316 sqlite3_bind_double(stmt_way3, 2, lat_min);
5317 sqlite3_bind_double(stmt_way3, 3, lat_max);
5318 sqlite3_bind_double(stmt_way3, 4, lon_min);
5319 sqlite3_bind_double(stmt_way3, 5, lon_max);
5320 //fprintf(stderr, "size korr:%f %f %f %f %f\n", temp, lat_min, lat_max, lon_min, lon_max);
5321
5322 sqlite3_step(stmt_way3);
5323 sqlite3_reset(stmt_way3);
5324
5325 sql_counter++;
5326 if (sql_counter > MAX_ROWS_WO_COMMIT_2a)
5327 {
5328 sql_counter = 0;
5329 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5330 }
5331 town_processed_count++;
5332
5333 if ((town_processed_count % town_steps) == 0)
5334 {
5335 fprintf(stderr, "towns1: %lld/%lld\n", town_processed_count, town_count);
5336 }
5337
5338 break;
5339 default:
5340 fprintf(stderr, "SQL Error: %d\n", rc);
5341 break;
5342 }
5343 }
5344 while (rc == SQLITE_ROW);
5345 sqlite3_reset(stmt_town_sel001);
5346
5347 sql_counter = 0;
5348 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5349
5350 }
5351
5352
5353 int lat_lon_inside_rect(struct node_lat_lon *n, struct rect_lat_lon *r)
5354 {
5355 if ((n->lat >= r->lu_lat) && (n->lon >= r->lu_lon) && (n->lat <= r->rl_lat) && (n->lon <= r->rl_lon))
5356 {
5357 return 1;
5358 }
5359
5360 return 0;
5361 }
5362
5363 GList* osm_process_towns(FILE *in, FILE *coords, FILE *boundaries, FILE *ways)
5364 {
5365 struct item_bin *ib;
5366 GList *bl = NULL;
5367 struct attr attrs[10];
5368 time_t start_tt, end_tt;
5369 double diff_tt;
5370 double diff2_tt;
5371 long long size_in;
5372 long long pos_in;
5373 struct node_lat_lon node_coords;
5374
5375 if (debug_itembin(1))
5376 {
5377 fprintf(stderr, "osm_process_towns == START ==\n");
5378 }
5379
5380 //fprintf(stderr,"osm_process_towns == PB 001 %p,%p ==\n", boundaries, ways);
5381
5382 time(&start_tt);
5383 bl = process_boundaries(boundaries, coords, ways);
5384 time(&end_tt);
5385 diff_tt = difftime(end_tt, start_tt);
5386 char outstring[200];
5387 char outstring2[200];
5388 char outstring3[200];
5389 convert_to_human_time(diff_tt, outstring);
5390 fprintf(stderr, "-RUNTIME-BOUNDARIES: %s\n", outstring);
5391
5392 int _c = 0;
5393 int _e = 10000;
5394
5395 //fprintf(stderr,"osm_process_towns == PB 002 ==\n");
5396
5397 long long pos_now = ftello(in); // 64bit
5398 fseeko(in, 0, SEEK_END);
5399 size_in = ftello(in); // 64bit
5400 fseeko(in, pos_now, SEEK_SET);
5401
5402 // reset timer
5403 diff2_tt = 0;
5404 _c = 0;
5405 time(&start_tt);
5406
5407 while ((ib = read_item(in, 0)))
5408 {
5409 struct coord *c = (struct coord *) (ib + 1);
5410 struct country_table *result = NULL;
5411 char *is_in = item_bin_get_attr(ib, attr_osm_is_in, NULL);
5412 int i;
5413
5414 _c++;
5415
5416 if (debug_itembin(ib))
5417 {
5418 fprintf(stderr, "== item ==\n");
5419 dump_itembin(ib);
5420 }
5421
5422 memset(attrs, 0, sizeof(attrs));
5423 if (debug_itembin(ib))
5424 {
5425 fprintf(stderr, "== osm_process_town_by_boundary ==\n");
5426 }
5427
5428 if (use_global_fixed_country_id == 1)
5429 {
5430 result = osm_process_item_fixed_country();
5431 if (debug_itembin(ib))
5432 {
5433 if (result == NULL)
5434 {
5435 fprintf(stderr, "== osm_process_item_fixed_country == #NULL# ==\n");
5436 }
5437 else
5438 {
5439 fprintf(stderr, "== osm_process_item_fixed_country == %d %s ==\n", result->countryid, result->names);
5440 }
5441 }
5442 }
5443
5444 if (!result)
5445 {
5446 //char *name=item_bin_get_attr(ib, attr_town_name, NULL);
5447 //fprintf(stderr,"== town by boundary == t:%s ==\n", name);
5448
5449 result = osm_process_town_by_boundary(bl, ib, c, attrs);
5450 if (result) // DEBUG
5451 {
5452 //fprintf(stderr,"== town by boundary == country_id:%d country_name:%s ==\n", result->countryid, result->names);
5453 }
5454 }
5455
5456 if (!result)
5457 {
5458 if (debug_itembin(ib))
5459 {
5460 fprintf(stderr, "== osm_process_town_by_is_in == %s ==\n", is_in);
5461 }
5462 result = osm_process_town_by_is_in(ib, is_in);
5463 if (result) // DEBUG
5464 {
5465 // fprintf(stderr,"== town by is_in == country_id:%d country_name:%s ==\n", result->countryid, result->names);
5466 }
5467 }
5468
5469 if (!result && unknown_country)
5470 {
5471 if (debug_itembin(ib))
5472 {
5473 fprintf(stderr, "== osm_process_town_unknown_country ==\n");
5474 }
5475 result = osm_process_town_unknown_country();
5476 }
5477
5478 if (result)
5479 {
5480 if (!result->file)
5481 {
5482 char *name = g_strdup_printf("country_%d.unsorted.tmp", result->countryid);
5483 result->file = fopen(name, "wb");
5484
5485 if (debug_itembin(ib))
5486 {
5487 fprintf(stderr, "== create: country_%d.unsorted.tmp == FILEP: %p ==\n", result->countryid, result->file);
5488 }
5489
5490 g_free(name);
5491 }
5492
5493 if (result->file)
5494 {
5495 long long *nodeid;
5496 long long nd_id = item_bin_get_nodeid(ib);
5497 if (is_in)
5498 {
5499 item_bin_remove_attr(ib, is_in);
5500 }
5501 nodeid = item_bin_get_attr(ib, attr_osm_nodeid, NULL);
5502 if (nodeid)
5503 {
5504 item_bin_remove_attr(ib, nodeid);
5505 }
5506
5507 char *postal = NULL;
5508 char *postal2 = NULL;
5509 //if (attrs[0].type != attr_none) // what does this line do??
5510 //{
5511 postal = item_bin_get_attr(ib, attr_town_postal, NULL);
5512 if (postal)
5513 {
5514 postal2 = g_strdup_printf("%s", postal);
5515 // --- item_bin_remove_attr(ib, postal);
5516 // fprintf(stderr, "town postal:%s\n", postal2);
5517 }
5518 //}
5519
5520 for (i = 0; i < 10; i++)
5521 {
5522 if (attrs[i].type != attr_none)
5523 {
5524 item_bin_add_attr(ib, &attrs[i]);
5525 }
5526 }
5527
5528 if (sql_counter == 0)
5529 {
5530 sqlite3_exec(sql_handle, "BEGIN", 0, 0, 0);
5531 }
5532
5533 if (nd_id)
5534 {
5535 // town size (will later be translated into radius)
5536 int twn_size = town_size_estimate(ib->type);
5537
5538 if (twn_size > 0)
5539 {
5540
5541 sqlite3_bind_int64(stmt_town, 1, nd_id);
5542 if (result->countryid)
5543 {
5544 sqlite3_bind_int(stmt_town, 2, result->countryid);
5545 }
5546 else
5547 {
5548 sqlite3_bind_int(stmt_town, 2, 999);
5549 }
5550
5551 sqlite3_bind_int(stmt_town, 4, twn_size);
5552
5553 char *label_t = item_bin_get_attr(ib, attr_town_name, NULL);
5554 //fprintf(stderr, "town: %lld,%d,%s\n", nd_id, result->countryid, label_t);
5555
5556 sqlite3_bind_text(stmt_town, 3, label_t, -1, SQLITE_STATIC);
5557
5558 if (postal2)
5559 {
5560 sqlite3_bind_text(stmt_town, 5, postal2, -1, SQLITE_STATIC);
5561 }
5562 else
5563 {
5564 sqlite3_bind_text(stmt_town, 5, NULL, -1, SQLITE_STATIC);
5565 }
5566
5567 get_lat_lon_for_node(nd_id, &node_coords);
5568 if (node_coords.valid == 1)
5569 {
5570 sqlite3_bind_double(stmt_town, 6, node_coords.lat);
5571 sqlite3_bind_double(stmt_town, 7, node_coords.lon);
5572 }
5573
5574 sqlite3_step(stmt_town);
5575 sqlite3_reset(stmt_town);
5576 sql_counter++;
5577
5578 if (postal2)
5579 {
5580 g_free(postal2);
5581 }
5582
5583 }
5584 }
5585
5586 if (sql_counter > MAX_ROWS_WO_COMMIT_5)
5587 {
5588 sql_counter = 0;
5589 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
5590 //fprintf(stderr, "SQL: COMMIT\n");
5591 }
5592
5593 // ** used until 2011-11-06 seems bad ** item_bin_write_match(ib, attr_town_name, attr_town_name_match, result->file);
5594 item_bin_town_write_match(ib, attr_town_name, attr_town_name_match, result->file);
5595 // ** origname ** item_bin_write(ib,result->file);
5596 }
5597 }
5598 else
5599 {
5600 if (debug_itembin(ib))
5601 {
5602 fprintf(stderr, "== no result ==\n");
5603 }
5604 }
5605
5606 if (_c > _e)
5607 {
5608 _c = 0;
5609
5610 pos_in = ftello(in); // 64bit
5611 time(&end_tt);
5612 diff_tt = difftime(end_tt, start_tt);
5613 convert_to_human_time(diff_tt, outstring);
5614 convert_to_human_bytes(pos_in, outstring2);
5615 convert_to_human_bytes(size_in, outstring3);
5616 fprintf(stderr, "-RUNTIME-LOOP-TOWN: %s elapsed (POS:%s of %s)\n", outstring, outstring2, outstring3);
5617 if (pos_in > 0)
5618 {
5619 double eta_time = (diff_tt / (pos_in)) * (size_in);
5620 convert_to_human_time(eta_time, outstring);
5621 fprintf(stderr, "-RUNTIME-LOOP-TOWN: %s left\n", outstring);
5622 }
5623 }
5624 }
5625
5626 if (debug_itembin(1))
5627 {
5628 fprintf(stderr, "osm_process_towns == END ==\n");
5629 }
5630
5631 return bl;
5632
5633 }
5634
5635 void sort_countries(int keep_tmpfiles)
5636 {
5637 int i;
5638 struct country_table *co;
5639 char *name_in, *name_out;
5640 for (i = 0; i < sizeof(country_table) / sizeof(struct country_table); i++)
5641 {
5642 co = &country_table[i];
5643 if (co->file)
5644 {
5645 fclose(co->file);
5646 co->file = NULL;
5647 }
5648 name_in = g_strdup_printf("country_%d.unsorted.tmp", co->countryid);
5649 name_out = g_strdup_printf("country_%d.tmp", co->countryid);
5650
5651 if (debug_itembin(2))
5652 {
5653 fprintf(stderr, "in=country_%d.unsorted.tmp\n", co->countryid);
5654 fprintf(stderr, "out=country_%d.tmp\n", co->countryid);
5655 }
5656
5657 co->r = world_bbox;
5658 item_bin_sort_file(name_in, name_out, &co->r, &co->size);
5659 if (!keep_tmpfiles)
5660 {
5661 unlink(name_in);
5662 }
5663 g_free(name_in);
5664 g_free(name_out);
5665 }
5666 }
5667
5668 static int search_relation_member(struct item_bin *ib, char *role, struct relation_member *memb, int *min_count)
5669 {
5670 char *str = NULL;
5671 int count = 0;
5672 while ((str = item_bin_get_attr(ib, attr_osm_member, str)))
5673 {
5674 if (!get_relation_member(str, memb))
5675 return 0;
5676 count++;
5677 if (!strcmp(memb->role, role) && (!min_count || *min_count < count))
5678 {
5679 if (min_count)
5680 *min_count = count;
5681 return 1;
5682 }
5683 }
5684 return 0;
5685 }
5686
5687 static int load_way_index(FILE *ways_index, int p, long long *idx)
5688 {
5689 int step = sizeof(*idx) * 2;
5690 fseek(ways_index, p * step, SEEK_SET);
5691 if (fread(idx, step, 1, ways_index) != 1)
5692 {
5693 fprintf(stderr, "read failed\n");
5694 return 0;
5695 }
5696 return 1;
5697 }
5698
5699 static int seek_to_way(FILE *way, FILE *ways_index, long long wayid)
5700 {
5701 long offset;
5702 long long idx[2];
5703 int count, interval, p;
5704 void *p_tmp;
5705 int rr;
5706 size_t data_size2 = sizeof(int *);
5707
5708 fprintf(stderr, "seek_to_way ---\n");
5709
5710 if (way_hash_cfu)
5711 {
5712 //if (!(g_hash_table_lookup_extended(way_hash, (gpointer) (long) wayid, NULL, (gpointer) & offset)))
5713 //{
5714 // return 0;
5715 //}
5716
5717 rr = cfuhash_get_data(way_hash_cfu, (long long) wayid, sizeof(long long), &p_tmp, &data_size2);
5718 if (rr == 0)
5719 {
5720 return 0;
5721 }
5722
5723 // printf("got value %ld\n", p_tmp);
5724 offset = p_tmp;
5725
5726 fseek(way, offset, SEEK_SET);
5727 return 1;
5728 }
5729
5730 fprintf(stderr, "**BAD**WAYSTUFF**\n");
5731
5732 fseek(ways_index, 0, SEEK_END);
5733 count = ftell(ways_index) / sizeof(idx);
5734 interval = count / 4;
5735 p = count / 2;
5736 if (interval == 0)
5737 {
5738 // If fewer than 4 nodes defined so far set interval to 1 to
5739 // avoid infinite loop
5740 interval = 1;
5741 }
5742 if (!load_way_index(ways_index, p, idx))
5743 return 0;
5744 for (;;)
5745 {
5746 if (idx[0] == wayid)
5747 {
5748 fseek(way, idx[1], SEEK_SET);
5749 return 1;
5750 }
5751 if (idx[0] < wayid)
5752 {
5753 p += interval;
5754 if (interval == 1)
5755 {
5756 if (p >= count)
5757 return 0;
5758 if (!load_way_index(ways_index, p, idx))
5759 return 0;
5760 if (idx[0] > wayid)
5761 return 0;
5762 }
5763 else
5764 {
5765 if (p >= count)
5766 p = count - 1;
5767 if (!load_way_index(ways_index, p, idx))
5768 return 0;
5769 }
5770 }
5771 else
5772 {
5773 p -= interval;
5774 if (interval == 1)
5775 {
5776 if (p < 0)
5777 return 0;
5778 if (!load_way_index(ways_index, p, idx))
5779 return 0;
5780 if (idx[0] < wayid)
5781 return 0;
5782 }
5783 else
5784 {
5785 if (p < 0)
5786 p = 0;
5787 if (!load_way_index(ways_index, p, idx))
5788 return 0;
5789 }
5790 }
5791 if (interval > 1)
5792 interval /= 2;
5793 }
5794 }
5795
5796 static struct coord *
5797 get_way(FILE *way, FILE *ways_index, struct coord *c, long long wayid, struct item_bin *ret, int debug)
5798 {
5799 long long currid;
5800 int last;
5801 struct coord *ic;
5802 if (!seek_to_way(way, ways_index, wayid))
5803 {
5804 if (debug)
5805 {
5806 fprintf(stderr, "not found in index");
5807 }
5808 return NULL;
5809 }
5810
5811 while (item_bin_read(ret, way))
5812 {
5813 currid = item_bin_get_wayid(ret);
5814 if (debug)
5815 fprintf(stderr,LONGLONG_FMT":",currid);
5816 if (currid != wayid)
5817 return NULL;
5818 ic = (struct coord *) (ret + 1);
5819 last = ret->clen / 2 - 1;
5820 if (debug)
5821 fprintf(stderr, "(0x%x,0x%x)-(0x%x,0x%x)", ic[0].x, ic[0].y, ic[last].x, ic[last].y);
5822 if (!c)
5823 return &ic[0];
5824 if (ic[0].x == c->x && ic[0].y == c->y)
5825 return &ic[last];
5826 if (ic[last].x == c->x && ic[last].y == c->y)
5827 return &ic[0];
5828 }
5829 return NULL;
5830 }
5831
5832 struct turn_restriction
5833 {
5834 osmid relid;
5835 enum item_type type;
5836 struct coord *c[3];
5837 int c_count[3];
5838 };
5839
5840 static void process_turn_restrictions_member(void *func_priv, void *relation_priv, struct item_bin *member, void *member_priv)
5841 {
5842 int count, type = (long) member_priv;
5843 struct turn_restriction *turn_restriction = relation_priv;
5844 struct coord *c = (struct coord *) (member + 1);
5845 int ccount = member->clen / 2;
5846
5847 if (member->type < type_line)
5848 {
5849 count = 1;
5850 }
5851 else
5852 {
5853 count = 2;
5854 }turn_restriction->c[type]=g_renew(struct coord, turn_restriction->c[type], turn_restriction->c_count[type]+count);
5855 turn_restriction->c[type][turn_restriction->c_count[type]++] = c[0];
5856 if (count > 1)
5857 {
5858 turn_restriction->c[type][turn_restriction->c_count[type]++] = c[ccount - 1];
5859 }
5860 }
5861
5862 static void process_turn_restrictions_fromto(struct turn_restriction *t, int type, struct coord **c)
5863 {
5864 int i, j;
5865 for (i = 0; i < t->c_count[type]; i += 2)
5866 {
5867 for (j = 0; j < t->c_count[1]; j++)
5868 {
5869 if (coord_is_equal(t->c[type][i], t->c[1][j]))
5870 {
5871 c[0] = &t->c[type][i + 1];
5872 c[1] = &t->c[type][i];
5873 return;
5874 }
5875 if (coord_is_equal(t->c[type][i + 1], t->c[1][j]))
5876 {
5877 c[0] = &t->c[type][i];
5878 c[1] = &t->c[type][i + 1];
5879 return;
5880 }
5881 }
5882 }
5883 }
5884
5885 static void process_turn_restrictions_dump_coord(struct coord *c, int count)
5886 {
5887 int i;
5888 for (i = 0; i < count; i++)
5889 {
5890 fprintf(stderr, "(0x%x,0x%x)", c[i].x, c[i].y);
5891 }
5892 }
5893
5894 static void process_turn_restrictions_finish(GList *tr, FILE *out)
5895 {
5896 GList *l = tr;
5897 while (l)
5898 {
5899 struct turn_restriction *t = l->data;
5900 struct coord *c[4];
5901 struct item_bin *ib = item_bin_2;
5902
5903 // init array
5904 c[0] = NULL;
5905 c[1] = NULL;
5906 c[2] = NULL;
5907 c[3] = NULL;
5908
5909 if (!t->c_count[0])
5910 {
5911 //osm_warning("relation",t->relid,0,"turn restriction: from member not found\n");
5912 }
5913 else if (!t->c_count[1])
5914 {
5915 //osm_warning("relation",t->relid,0,"turn restriction: via member not found\n");
5916 }
5917 else if (!t->c_count[2])
5918 {
5919 //osm_warning("relation",t->relid,0,"turn restriction: to member not found\n");
5920 }
5921 else
5922 {
5923 process_turn_restrictions_fromto(t, 0, c);
5924 process_turn_restrictions_fromto(t, 2, c + 2);
5925 if (!c[0] || !c[2])
5926 {
5927 //osm_warning("relation",t->relid,0,"turn restriction: via (");
5928 //process_turn_restrictions_dump_coord(t->c[1], t->c_count[1]);
5929 //fprintf(stderr,")");
5930 if (!c[0])
5931 {
5932 //fprintf(stderr," failed to connect to from (");
5933 //process_turn_restrictions_dump_coord(t->c[0], t->c_count[0]);
5934 //fprintf(stderr,")");
5935 }
5936 if (!c[2])
5937 {
5938 //fprintf(stderr," failed to connect to to (");
5939 //process_turn_restrictions_dump_coord(t->c[2], t->c_count[2]);
5940 //fprintf(stderr,")");
5941 }
5942 //fprintf(stderr,"\n");
5943 }
5944 else
5945 {
5946 if (t->c_count[1] <= 2)
5947 {
5948 item_bin_init(ib, t->type);
5949 item_bin_add_coord(ib, c[0], 1);
5950 item_bin_add_coord(ib, c[1], 1);
5951 if (t->c_count[1] > 1)
5952 {
5953 item_bin_add_coord(ib, c[3], 1);
5954 }
5955 item_bin_add_coord(ib, c[2], 1);
5956 item_bin_write(ib, out);
5957 }
5958
5959 }
5960 }
5961 g_free(t);
5962 l = g_list_next(l);
5963 }
5964 g_list_free(tr);
5965 }
5966
5967 static GList *
5968 process_turn_restrictions_setup(FILE *in, struct relations *relations)
5969 {
5970 struct relation_member fromm, tom, viam, tmpm;
5971 long long relid;
5972 struct item_bin *ib;
5973 struct relations_func *relations_func;
5974 int min_count;
5975 GList *turn_restrictions = NULL;
5976
5977 fseek(in, 0, SEEK_SET);
5978 relations_func = relations_func_new(process_turn_restrictions_member, NULL);
5979 while ((ib = read_item(in, 0)))
5980 {
5981 struct turn_restriction *turn_restriction=g_new0(struct turn_restriction, 1);
5982 relid = item_bin_get_relationid(ib);
5983 turn_restriction->relid = relid;
5984 turn_restriction->type = ib->type;
5985 min_count = 0;
5986 if (!search_relation_member(ib, "from", &fromm, &min_count))
5987 {
5988 osm_warning("relation", relid, 0, "turn restriction: from member missing\n");
5989 continue;
5990 }
5991 if (search_relation_member(ib, "from", &tmpm, &min_count))
5992 {
5993 osm_warning("relation", relid, 0, "turn restriction: multiple from members\n");
5994 continue;
5995 }
5996 min_count = 0;
5997 if (!search_relation_member(ib, "to", &tom, &min_count))
5998 {
5999 osm_warning("relation", relid, 0, "turn restriction: to member missing\n");
6000 continue;
6001 }
6002 if (search_relation_member(ib, "to", &tmpm, &min_count))
6003 {
6004 osm_warning("relation", relid, 0, "turn restriction: multiple to members\n");
6005 continue;
6006 }
6007 min_count = 0;
6008 if (!search_relation_member(ib, "via", &viam, &min_count))
6009 {
6010 osm_warning("relation", relid, 0, "turn restriction: via member missing\n");
6011 continue;
6012 }
6013 if (search_relation_member(ib, "via", &tmpm, &min_count))
6014 {
6015 osm_warning("relation", relid, 0, "turn restriction: multiple via member\n");
6016 continue;
6017 }
6018 if (fromm.type != 2)
6019 {
6020 osm_warning("relation", relid, 0, "turn restriction: wrong type for from member ");
6021 osm_warning(osm_types[fromm.type], fromm.id, 1, "\n");
6022 continue;
6023 }
6024 if (tom.type != 2)
6025 {
6026 osm_warning("relation", relid, 0, "turn restriction: wrong type for to member ");
6027 osm_warning(osm_types[tom.type], tom.id, 1, "\n");
6028 continue;
6029 }
6030 if (viam.type != 1 && viam.type != 2)
6031 {
6032 osm_warning("relation", relid, 0, "turn restriction: wrong type for via member ");
6033 osm_warning(osm_types[viam.type], viam.id, 1, "\n");
6034 continue;
6035 }
6036 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 0, fromm.type, fromm.id);
6037 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 1, viam.type, viam.id);
6038 relations_add_func(relations, relations_func, turn_restriction, (gpointer) 2, tom.type, tom.id);
6039 turn_restrictions = g_list_append(turn_restrictions, turn_restriction);
6040 }