2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
21 #define _FILE_OFFSET_BITS 64
22 #define _LARGEFILE_SOURCE
23 #define _LARGEFILE64_SOURCE
39 #ifdef HAVE_POSTGRESQL
43 #define BUFFER_SIZE 1280
45 #define GENERATE_INDEX
48 #define debug_tile(x) 0
50 #define debug_tile(x) (!strcmp(x,"bcdbd") || !strcmp(x,"bcdbd") || !strcmp(x,"bcdbda") || !strcmp(x,"bcdbdb") || !strcmp(x,"bcdbdba") || !strcmp(x,"bcdbdbb") || !strcmp(x,"bcdbdbba") || !strcmp(x,"bcdbdbaa") || !strcmp(x,"bcdbdbacaa") || !strcmp(x,"bcdbdbacab") || !strcmp(x,"bcdbdbacaba") || !strcmp(x,"bcdbdbacabaa") || !strcmp(x,"bcdbdbacabab") || !strcmp(x,"bcdbdbacababb") || !strcmp(x,"bcdbdbacababba") || !strcmp(x,"bcdbdbacababbb") || !strcmp(x,"bcdbdbacababbd") || !strcmp(x,"bcdbdbacababaa") || !strcmp(x,"bcdbdbacababab") || !strcmp(x,"bcdbdbacababac") || !strcmp(x,"bcdbdbacababad") || !strcmp(x,"bcdbdbacabaaa") || !strcmp(x,"bcdbdbacabaaba") || !strcmp(x,"bcdbdbacabaabb") || !strcmp(x,"bcdbdbacabaabc") || !strcmp(x,"bcdbdbacabaabd") || !strcmp(x,"bcdbdbacabaaaa") || !strcmp(x,"bcdbdbacabaaab") || !strcmp(x,"bcdbdbacabaaac") || !strcmp(x,"bcdbdbacabaaad") || 0)
54 static GHashTable *dedupe_ways_hash;
56 static int attr_debug_level=1;
57 static int nodeid,wayid;
58 static int report,phase;
59 static int ignore_unkown = 0, coverage=0;
61 static char *attrmap={
63 "n amenity=hospital poi_hospital\n"
64 "n amenity=atm poi_bank\n"
65 "n amenity=bank poi_bank\n"
66 "n amenity=pub poi_bar\n"
67 "n amenity=cafe poi_cafe\n"
68 "n amenity=bus_station poi_bus_station\n"
69 "n amenity=parking poi_car_parking\n"
70 "n amenity=cinema poi_cinema\n"
71 "n amenity=fire_station poi_firebrigade\n"
72 "n amenity=fuel poi_fuel\n"
73 "n amenity=courthouse poi_justice\n"
74 "n amenity=library poi_library\n"
75 "n amenity=pharmacy poi_pharmacy\n"
76 "n amenity=place_of_worship,religion=christian poi_church\n"
77 "n amenity=police poi_police\n"
78 "n amenity=post_office poi_post\n"
79 "n amenity=post_box poi_post\n"
80 "n amenity=public_building poi_public_office\n"
81 "n amenity=restaurant poi_restaurant\n"
82 "n amenity=fast_food poi_fastfood\n"
83 "n amenity=toilets poi_restroom\n"
84 "n amenity=school poi_school\n"
85 "n amenity=university poi_school\n"
86 "n amenity=college poi_school\n"
87 "n amenity=telephone poi_telephone\n"
88 "n amenity=theatre poi_theater\n"
89 "n amenity=townhall poi_townhall\n"
90 "n highway=bus_stop poi_bus_stop\n"
91 "n highway=mini_roundabout mini_roundabout\n"
92 "n highway=motorway_junction highway_exit\n"
93 "n highway=traffic_signals traffic_signals\n"
94 "n highway=turning_circle turning_circle\n"
95 "n leisure=slipway poi_boat_ramp\n"
96 "n leisure=fishing poi_fish\n"
97 "n sport=golf poi_golf\n"
98 "n leisure=golf_course poi_golf\n"
99 "n leisure=marina poi_marine\n"
100 "n leisure=sports_centre poi_sport\n"
101 "n leisure=stadium poi_stadium\n"
102 "n shop=supermarket poi_shopping\n"
103 "n shop=convenience poi_shop_grocery\n"
104 "n tourism=attraction poi_attraction\n"
105 "n tourism=camp_site poi_camp_rv\n"
106 "n tourism=caravan_site poi_camp_rv\n"
107 "n tourism=hotel poi_hotel\n"
108 "n tourism=motel poi_hotel\n"
109 "n tourism=guest_house poi_hotel\n"
110 "n tourism=hostel poi_hotel\n"
111 "n tourism=information poi_information\n"
112 "n tourism=museum poi_museum_history\n"
113 "n tourism=picnic_site poi_picnic\n"
114 "n tourism=theme_park poi_resort\n"
115 "n tourism=zoo poi_zoo\n"
116 "n amenity=grave_yard poi_cemetery\n"
117 "n landuse=cemetery poi_cemetery\n"
118 "n military=airfield poi_military\n"
119 "n military=bunker poi_military\n"
120 "n military=barracks poi_military\n"
121 "n military=range poi_military\n"
122 "n military=danger_area poi_danger_area\n"
123 "n sport=swimming poi_swimming\n"
124 "n sport=skiing poi_skiing\n"
125 "n aeroway=aerodrome poi_airport\n"
126 "n aeroway=airport poi_airport\n"
127 "n aeroway=terminal poi_airport\n"
128 "n aeroway=helipad poi_heliport\n"
129 "n man_made=tower poi_tower\n"
130 "n natural=bay poi_bay\n"
131 "n natural=peak poi_peak\n"
132 "n place=suburb district_label\n"
133 "n place=city town_label_2e5\n"
134 "n place=town town_label_2e4\n"
135 "n place=village town_label_2e3\n"
136 "n place=hamlet town_label_2e2\n"
137 "n place=locality town_label_2e0\n"
138 "n railway=halt poi_rail_halt\n"
139 "n railway=level_crossing poi_level_crossing\n"
140 "n railway=station poi_rail_station\n"
141 "n railway=tram_stop poi_rail_tram_stop\n"
142 "w *=* street_unkn\n"
143 "w amenity=place_of_worship poly_building\n"
144 "w building=* poly_building\n"
145 "w aeroway=aerodrome poly_airport\n"
146 "w aeroway=apron poly_apron\n"
147 "w aeroway=runway aeroway_runway\n"
148 "w aeroway=taxiway aeroway_taxiway\n"
149 "w aeroway=terminal poly_terminal\n"
150 "w highway=bridleway bridleway\n"
151 "w highway=path,horse=designated bridleway\n"
152 "w highway=cycleway cycleway\n"
153 "w highway=cyclepath cycleway\n"
154 "w highway=path,bicycle=designated cycleway\n"
155 "w highway=footway footway\n"
156 "w piste:type=nordic piste_nordic\n"
157 "w route=ski piste_nordic\n"
158 "w piste:type=downhill,piste:difficulty=novice piste_downhill_novice\n"
159 "w piste:type=downhill,piste:difficulty=easy piste_downhill_easy\n"
160 "w piste:type=downhill,piste:difficulty=intermediate piste_downhill_intermediate\n"
161 "w piste:type=downhill,piste:difficulty=advanced piste_downhill_advanced\n"
162 "w piste:type=downhill,piste:difficulty=expert piste_downhill_expert\n"
163 "w piste:type=downhill,piste:difficulty=freeride piste_downhill_freeride\n"
164 "w highway=path,foot=designated footway\n"
165 "w highway=steps steps\n"
166 "w highway=path path\n"
167 "w highway=unsurfaced track_gravelled\n"
168 "w highway=track track_gravelled\n"
169 "w highway=track,tracktype=grade1 track_paved\n"
170 "w highway=track,surface=paved track_paved\n"
171 "w highway=track,tracktype=grade2 track_gravelled\n"
172 "w highway=track,surface=gravel track_gravelled\n"
173 "w highway=track,tracktype=grade3 track_unpaved\n"
174 "w highway=track,surface=unpaved track_unpaved\n"
175 "w highway=track,tracktype=grade4 track_ground\n"
176 "w highway=track,surface=ground track_ground\n"
177 "w highway=track,tracktype=grade5 track_grass\n"
178 "w highway=track,surface=grass track_grass\n"
179 "w highway=parking_lane street_service\n"
180 "w highway=service street_service\n"
181 "w highway=construction street_construction\n"
182 "w highway=pedestrian street_pedestrian\n"
183 "w highway=pedestrian,area=1 poly_pedestrian\n"
184 "w highway=plaza poly_plaza\n"
185 "w landuse=plaza poly_plaza\n"
186 "w highway=residential street_1_city\n"
187 "w highway=living_street living_street\n"
188 "w highway=unclassified street_1_city\n"
189 "w highway=road street_1_city\n"
190 "w highway=minor street_1_land\n"
191 "w highway=tertiary street_2_city\n"
192 "w highway=tertiary_link ramp\n"
193 "w highway=secondary street_3_city\n"
194 "w highway=secondary_link ramp\n"
195 "w highway=primary street_4_city\n"
196 "w highway=primary_link ramp\n"
197 "w highway=trunk street_4_city\n"
198 "w highway=trunk_link ramp\n"
199 "w highway=motorway highway_city\n"
200 "w highway=motorway_link ramp\n"
201 "w historic=town gate poly_building\n"
202 "w cycleway=track cycleway\n"
203 "w amenity=parking poly_car_parking\n"
204 "w landuse=allotments poly_allotments\n"
205 "w landuse=cemetery poly_cemetery\n"
206 "w amenity=grave_yard poly_cemetery\n"
207 "w landuse=forest poly_wood\n"
208 "w landuse=industrial poly_industry\n"
209 "w landuse=residential poly_town\n"
210 "w landuse=residential,area=1 poly_town\n"
211 "w landuse=farmyard poly_town\n"
212 "w amenity=college poly_college\n"
213 "w amenity=university poly_university\n"
214 "w tourism=camp_site poly_camp_site\n"
215 "w tourism=caravan_site poly_caravan_site\n"
216 "w tourism=picnic_site poly_picnic_site\n"
217 "w tourism=theme_park poly_theme_park\n"
218 "w tourism=attraction poly_attraction\n"
219 "w tourism=zoo poly_zoo\n"
220 "w tourism=artwork poly_artwork\n"
221 "w historic=archaeological_site poly_archaeological_site\n"
222 "w historic=ruins poly_ruins\n"
223 "w historic=battlefield poly_battlefield\n"
224 "w landuse=quarry poly_quarry\n"
225 "w landuse=landfill poly_landfill\n"
226 "w landuse=retail poly_retail\n"
227 "w landuse=commercial poly_commercial\n"
228 "w landuse=brownfield poly_brownfield\n"
229 "w landuse=greenfield poly_greenfield\n"
230 "w landuse=construction poly_construction\n"
231 "w landuse=railway poly_railway\n"
232 "w natural=glacier poly_glacier\n"
233 "w natural=scree poly_scree\n"
234 "w natural=scrub poly_scrub\n"
235 "w natural=fell poly_fell\n"
236 "w natural=heath poly_heath\n"
237 "w natural=marsh poly_marsh\n"
238 "w natural=mud poly_mud\n"
239 "w natural=beach poly_beach\n"
240 "w natural=land poly_land\n"
241 "w landuse=basin poly_basin\n"
242 "w landuse=reservoir poly_reservoir\n"
243 "w landuse=farm poly_farm\n"
244 "w landuse=farmland poly_farm\n"
245 "w landuse=vineyard poly_farm\n"
246 "w leisure=park poly_park\n"
247 "w landuse=village_green poly_village_green\n"
248 "w landuse=recreation_ground poly_recreation_ground\n"
249 "w natural=wood poly_wood\n"
250 "w natural=water poly_water\n"
251 "w natural=coastline water_line\n"
252 "w place=suburb poly_town\n"
253 "w place=town poly_town\n"
254 "w landuse=military poly_military\n"
255 "w military=airfield poly_airfield\n"
256 "w military=barracks poly_barracks\n"
257 "w military=danger_area poly_danger_area\n"
258 "w military=range poly_range\n"
259 "w military=naval_base poly_naval_base\n"
260 "w power=line powerline\n"
261 "w railway=rail rail\n"
262 "w railway=narrow_gauge rail_narrow_gauge\n"
263 "w railway=light_rail rail_light\n"
264 "w railway=subway rail_subway\n"
265 "w railway=tram rail_tram\n"
266 "w railway=monorail rail_mono\n"
267 "w railway=preserved rail_preserved\n"
268 "w railway=disused rail_disused\n"
269 "w railway=abandoned rail_abandoned\n"
270 "w aerialway=cable_car lift_cable_car\n"
271 "w aerialway=chair_lift lift_chair\n"
272 "w aerialway=drag_lift lift_drag\n"
273 "w leisure=golf_course poly_golf_course\n"
274 "w sport=* poly_sport\n"
275 "w leisure=sports_centre poly_sport\n"
276 "w leisure=stadium poly_sports_stadium\n"
277 "w leisure=track poly_sports_track\n"
278 "w leisure=pitch poly_sports_pitch\n"
279 "w leisure=water_park poly_water_park\n"
280 "w leisure=marina poly_marina\n"
281 "w leisure=fishing poly_fishing\n"
282 "w leisure=garden poly_garden\n"
283 "w leisure=common poly_common\n"
284 "w leisure=playground poly_playground\n"
285 "w leisure=nature_reserve poly_nature_reserve\n"
286 "w waterway=canal water_canal\n"
287 "w waterway=river water_river\n"
288 "w waterway=stream water_stream\n"
289 "w waterway=drain water_drain\n"
290 "w waterway=riverbank poly_water\n"
291 "w boundary=administrative border_country\n"
292 "w boundary=civil border_civil\n"
293 "w boundary=political border_political\n"
294 "w boundary=national_park border_national_park\n"
295 "w route=ferry ferry\n"
296 "w highway=bus_guideway bus_guideway\n"
302 } coord_buffer[65536];
304 #define IS_REF(c) ((c).x >= (1 << 30))
305 #define REF(c) ((c).y)
306 #define SET_REF(c,ref) do { (c).x = 1 << 30; (c).y = ref ; } while(0)
312 static void bbox_extend(struct coord *c, struct rect *r);
314 #ifdef GENERATE_INDEX
316 static GHashTable *aux_tile_hash;
317 GList *aux_tile_list;
319 struct country_table {
326 } country_table[] = {
327 { 40,"Austria,Österreich,AUT"},
329 {276,"Germany,Deutschland,Bundesrepublik Deutschland"},
330 {528,"Nederland,The Netherlands,Niederlande,NL"},
335 static GHashTable *country_table_hash;
338 struct attr_mapping {
340 int attr_present_idx_count;
341 int attr_present_idx[0];
344 static struct attr_mapping **attr_mapping_node;
345 static int attr_mapping_node_count;
346 static struct attr_mapping **attr_mapping_way;
347 static int attr_mapping_way_count;
349 static char *attr_present;
350 static int attr_present_count;
351 static GHashTable *attr_hash;
354 static GHashTable *strings_hash = NULL;
357 static char* string_hash_lookup( const char* key )
359 char* key_ptr = NULL;
361 if ( strings_hash == NULL ) {
362 strings_hash = g_hash_table_new(g_str_hash, g_str_equal);
365 if ( ( key_ptr = g_hash_table_lookup(strings_hash, key )) == NULL ) {
366 key_ptr = g_strdup( key );
367 g_hash_table_insert(strings_hash, key_ptr, (gpointer)key_ptr );
374 build_attrmap_line(char *line)
376 char *t=NULL,*kvl=NULL,*i=NULL,*p,*kv;
377 struct attr_mapping ***attr_mapping_curr,*attr_mapping=g_malloc0(sizeof(struct attr_mapping));
378 int idx,attr_mapping_count=0,*attr_mapping_curr_count;
395 attr_mapping_curr=&attr_mapping_way;
396 attr_mapping_curr_count=&attr_mapping_way_count;
400 attr_mapping_curr=&attr_mapping_node;
401 attr_mapping_curr_count=&attr_mapping_node_count;
403 attr_mapping->type=item_from_name(i);
404 while ((kv=strtok(kvl, ","))) {
406 if (!(idx=(int)g_hash_table_lookup(attr_hash, kv))) {
407 idx=attr_present_count++;
408 g_hash_table_insert(attr_hash, kv, (gpointer) idx);
410 attr_mapping=g_realloc(attr_mapping, sizeof(struct attr_mapping)+(attr_mapping_count+1)*sizeof(int));
411 attr_mapping->attr_present_idx[attr_mapping_count++]=idx;
412 attr_mapping->attr_present_idx_count=attr_mapping_count;
414 *attr_mapping_curr=g_realloc(*attr_mapping_curr, sizeof(**attr_mapping_curr)*(*attr_mapping_curr_count+1));
415 (*attr_mapping_curr)[(*attr_mapping_curr_count)++]=attr_mapping;
419 build_attrmap(char *map)
422 attr_hash=g_hash_table_new(g_str_hash, g_str_equal);
423 attr_present_count=1;
429 build_attrmap_line(map);
432 attr_present=g_malloc0(sizeof(*attr_present)*attr_present_count);
435 #ifdef GENERATE_INDEX
437 build_countrytable(void)
440 char *names,*str,*tok;
441 country_table_hash=g_hash_table_new(g_str_hash, g_str_equal);
442 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
443 names=g_strdup(country_table[i].names);
445 while ((tok=strtok(str, ","))) {
447 g_hash_table_insert(country_table_hash, tok, (gpointer)&country_table[i]);
455 static int processed_nodes, processed_nodes_out, processed_ways, processed_relations, processed_tiles;
456 static int in_way, in_node, in_relation;
462 signal(SIGALRM, sig_alrm);
465 fprintf(stderr,"PROGRESS%d: Processed %d nodes (%d out) %d ways %d relations %d tiles\n", phase, processed_nodes, processed_nodes_out, processed_ways, processed_relations, processed_tiles);
479 struct attr_bin label_attr = {
482 char label_attr_buffer[BUFFER_SIZE];
484 struct attr_bin town_name_attr = {
488 struct attr_bin street_name_attr = {
492 struct attr_bin street_name_systematic_attr = {
493 0, attr_street_name_systematic
495 char street_name_systematic_attr_buffer[BUFFER_SIZE];
497 struct attr_bin debug_attr = {
500 char debug_attr_buffer[BUFFER_SIZE];
502 struct attr_bin flags_attr = {
505 int flags_attr_value;
507 char is_in_buffer[BUFFER_SIZE];
510 static void write_zipmember(FILE *out, FILE *dir_out, char *name, int filelen, char *data, int data_size, int compression_level);
513 pad_text_attr(struct attr_bin *a, char *buffer)
516 if (buffer && buffer[0]) {
526 xml_get_attribute(char *xml, char *attribute, char *buffer, int buffer_size)
528 int len=strlen(attribute);
529 char *pos,*i,s,attr[len+2];
530 strcpy(attr, attribute);
531 strcpy(attr+len, "=");
532 pos=strstr(xml, attr);
542 if (i - pos > buffer_size) {
543 fprintf(stderr,"Buffer overflow %d vs %d\n", i-pos, buffer_size);
546 strncpy(buffer, pos, i-pos);
551 static int node_is_tagged;
554 add_tag(char *k, char *v)
556 GHashTable *value_hash;
559 char buffer[BUFFER_SIZE*2+2];
560 if (! strcmp(k,"ele"))
562 if (! strcmp(k,"time"))
564 if (! strcmp(k,"created_by"))
566 if (! strncmp(k,"tiger:",6) || !strcmp(k,"AND_nodes"))
568 if (! strcmp(k,"converted_by") || ! strcmp(k,"source"))
570 if (! strncmp(k,"osmarender:",11) || !strncmp(k,"svg:",4))
572 if (! strcmp(k,"layer"))
574 if (! strcasecmp(v,"true") || ! strcasecmp(v,"yes"))
576 if (! strcmp(k,"oneway")) {
577 if (!strcmp(v,"1")) {
578 flags_attr_value=AF_ONEWAY;
581 if (! strcmp(v,"-1")) {
582 flags_attr_value=AF_ONEWAYREV;
590 if (! strcmp(k,"junction")) {
591 if (! strcmp(v,"roundabout")) {
592 flags_attr_value=AF_ONEWAY;
596 if (! strcmp(k,"maxspeed")) {
599 if (! strcmp(k,"bicycle")) {
602 if (! strcmp(k,"foot")) {
605 if (! strcmp(k,"note"))
607 if (! strcmp(k,"name")) {
608 strcpy(label_attr_buffer, v);
609 pad_text_attr(&label_attr, label_attr_buffer);
612 if (! strcmp(k,"ref")) {
614 strcpy(street_name_systematic_attr_buffer, v);
615 pad_text_attr(&street_name_systematic_attr, street_name_systematic_attr_buffer);
619 if (! strcmp(k,"is_in")) {
620 strcpy(is_in_buffer, v);
623 if (! strcmp(k,"gnis:ST_alpha")) {
624 /* assume a gnis tag means it is part of the USA:
625 http://en.wikipedia.org/wiki/Geographic_Names_Information_System
626 many US towns do not have is_in tags
628 strcpy(is_in_buffer, "USA");
631 if (! strcmp(k,"lanes")) {
634 if (attr_debug_level >= level) {
635 int bytes_left = sizeof( debug_attr_buffer ) - strlen(debug_attr_buffer) - 1;
636 if ( bytes_left > 0 )
638 snprintf(debug_attr_buffer+strlen(debug_attr_buffer), bytes_left, " %s=%s", k, v);
639 debug_attr_buffer[ sizeof( debug_attr_buffer ) - 1 ] = '\0';
648 strcpy(buffer,"*=*");
649 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
652 sprintf(buffer,"%s=*", k);
653 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
656 sprintf(buffer,"*=%s", v);
657 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
660 sprintf(buffer,"%s=%s", k, v);
661 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
677 decode_entities(char *buffer)
682 while ((pos=strchr(pos, '&'))) {
684 for (i = 0 ; i < sizeof(entities)/sizeof(struct entity); i++) {
685 len=strlen(entities[i].entity);
686 if (!strncmp(pos, entities[i].entity, len)) {
688 memmove(pos+1, pos+len, strlen(pos+len)+1);
700 char k_buffer[BUFFER_SIZE];
701 char v_buffer[BUFFER_SIZE];
702 if (!xml_get_attribute(p, "k", k_buffer, BUFFER_SIZE))
704 if (!xml_get_attribute(p, "v", v_buffer, BUFFER_SIZE))
706 decode_entities(v_buffer);
707 add_tag(k_buffer, v_buffer);
719 static struct tile_head {
727 struct tile_head *next;
743 static struct buffer node_buffer = {
748 static char** th_get_subtile( const struct tile_head* th, int idx )
750 char* subtile_ptr = NULL;
751 subtile_ptr = (char*)th + sizeof( struct tile_head ) + idx * sizeof( char *);
752 return (char**)subtile_ptr;
756 extend_buffer(struct buffer *b)
758 b->malloced+=b->malloced_step;
759 b->base=realloc(b->base, b->malloced);
760 if (b->base == NULL) {
761 fprintf(stderr,"realloc of %d bytes failed\n",b->malloced);
768 GHashTable *node_hash;
771 node_buffer_to_hash(void)
773 int i,count=node_buffer.size/sizeof(struct node_item);
774 struct node_item *ni=(struct node_item *)node_buffer.base;
775 for (i = 0 ; i < count ; i++)
776 g_hash_table_insert(node_hash, (gpointer)(ni[i].id), (gpointer)i);
779 static struct node_item *ni;
782 add_node(int id, double lat, double lon)
784 if (node_buffer.size + sizeof(struct node_item) > node_buffer.malloced)
785 extend_buffer(&node_buffer);
788 item.type=type_point_unkn;
790 town_name_attr.len=0;
792 is_in_buffer[0]='\0';
793 sprintf(debug_attr_buffer,"nodeid=%d", nodeid);
794 ni=(struct node_item *)(node_buffer.base+node_buffer.size);
800 ni->c.x=lon*6371000.0*M_PI/180;
801 ni->c.y=log(tan(M_PI_4+lat*M_PI/360))*6371000.0;
802 node_buffer.size+=sizeof(struct node_item);
804 if (ni->id > nodeid_last) {
807 fprintf(stderr,"INFO: Nodes out of sequence (new %d vs old %d), adding hash\n", ni->id, nodeid_last);
808 node_hash=g_hash_table_new(NULL, NULL);
809 node_buffer_to_hash();
812 if (!g_hash_table_lookup(node_hash, (gpointer)(ni->id)))
813 g_hash_table_insert(node_hash, (gpointer)(ni->id), (gpointer)(ni-(struct node_item *)node_buffer.base));
815 node_buffer.size-=sizeof(struct node_item);
824 char id_buffer[BUFFER_SIZE];
825 char lat_buffer[BUFFER_SIZE];
826 char lon_buffer[BUFFER_SIZE];
827 if (!xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
829 if (!xml_get_attribute(p, "lat", lat_buffer, BUFFER_SIZE))
831 if (!xml_get_attribute(p, "lon", lon_buffer, BUFFER_SIZE))
833 add_node(atoi(id_buffer), atof(lat_buffer), atof(lon_buffer));
838 static struct node_item *
839 node_item_get(int id)
841 struct node_item *ni=(struct node_item *)(node_buffer.base);
842 int count=node_buffer.size/sizeof(struct node_item);
843 int interval=count/4;
847 i=(int)(g_hash_table_lookup(node_hash, (gpointer)id));
850 while (ni[p].id != id) {
852 fprintf(stderr,"p=%d count=%d interval=%d id=%d ni[p].id=%d\n", p, count, interval, id, ni[p].id);
887 struct node_item *ni=node_item_get(id);
889 fprintf(stderr,"WARNING: node id %d not found\n", id);
901 item.type=type_street_unkn;
903 street_name_attr.len=0;
904 street_name_systematic_attr.len=0;
907 sprintf(debug_attr_buffer,"wayid=%d", wayid);
913 char id_buffer[BUFFER_SIZE];
914 if (!xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
916 add_way(atoi(id_buffer));
921 parse_relation(char *p)
923 debug_attr_buffer[0]='\0';
928 write_attr(FILE *out, struct attr_bin *attr, void *buffer)
931 fwrite(attr, sizeof(*attr), 1, out);
932 fwrite(buffer, (attr->len-1)*4, 1, out);
937 attr_longest_match(struct attr_mapping **mapping, int mapping_count, enum item_type *types, int types_count)
939 int i,j,longest=0,ret=0,sum,val;
940 struct attr_mapping *curr;
941 for (i = 0 ; i < mapping_count ; i++) {
944 for (j = 0 ; j < curr->attr_present_idx_count ; j++) {
945 val=attr_present[curr->attr_present_idx[j]];
957 if (sum > 0 && sum == longest && ret < types_count)
958 types[ret++]=curr->type;
960 memset(attr_present, 0, sizeof(*attr_present)*attr_present_count);
968 enum item_type types[5];
972 if (dedupe_ways_hash) {
973 if (g_hash_table_lookup(dedupe_ways_hash, (gpointer)wayid))
975 g_hash_table_insert(dedupe_ways_hash, (gpointer)wayid, (gpointer)1);
977 count=attr_longest_match(attr_mapping_way, attr_mapping_way_count, types, sizeof(types)/sizeof(enum item_type));
978 pad_text_attr(&debug_attr, debug_attr_buffer);
980 alen+=label_attr.len+1;
981 if (street_name_systematic_attr.len)
982 alen+=street_name_systematic_attr.len+1;
984 alen+=debug_attr.len+1;
986 alen+=flags_attr.len+1;
990 item.type=type_street_unkn;
991 if (coverage && item_is_street(item))
992 item.type=type_coverage;
993 item.clen=coord_count*2;
994 item.len=item.clen+2+alen;
995 fwrite(&item, sizeof(item), 1, out);
996 fwrite(coord_buffer, coord_count*sizeof(struct coord), 1, out);
997 if (item_is_street(item)) {
998 street_name_attr.len=label_attr.len;
999 write_attr(out, &street_name_attr, label_attr_buffer);
1001 write_attr(out, &label_attr, label_attr_buffer);
1002 write_attr(out, &street_name_systematic_attr, street_name_systematic_attr_buffer);
1003 write_attr(out, &debug_attr, debug_attr_buffer);
1004 write_attr(out, &flags_attr, &flags_attr_value);
1010 int alen=0,conflict=0,count;
1011 enum item_type types[5];
1012 struct country_table *result=NULL, *lookup;
1013 if (!out || ! node_is_tagged || ! nodeid)
1015 count=attr_longest_match(attr_mapping_node, attr_mapping_node_count, types, sizeof(types)/sizeof(enum item_type));
1016 pad_text_attr(&debug_attr, debug_attr_buffer);
1018 alen+=label_attr.len+1;
1020 alen+=debug_attr.len+1;
1024 item.type=type_point_unkn;
1026 item.len=item.clen+2+alen;
1027 fwrite(&item, sizeof(item), 1, out);
1028 fwrite(&ni->c, 1*sizeof(struct coord), 1, out);
1029 if (item_is_town(item)) {
1030 town_name_attr.len=label_attr.len;
1031 write_attr(out, &town_name_attr, label_attr_buffer);
1033 write_attr(out, &label_attr, label_attr_buffer);
1034 write_attr(out, &debug_attr, debug_attr_buffer);
1035 #ifdef GENERATE_INDEX
1036 if (item_is_town(item) && town_name_attr.len) {
1037 char *tok,*buf=is_in_buffer;
1038 while ((tok=strtok(buf, ","))) {
1041 lookup=g_hash_table_lookup(country_table_hash,tok);
1043 if (result && result->countryid != lookup->countryid) {
1044 fprintf(stderr,"conflict for %s %s country %d vs %d\n", label_attr_buffer, debug_attr_buffer, lookup->countryid, result->countryid);
1051 if (result && !conflict) {
1052 if (!result->file) {
1053 char *name=g_strdup_printf("country_%d.bin.unsorted", result->countryid);
1054 result->file=fopen(name,"w");
1059 item.len=item.clen+2+label_attr.len+1;
1060 fwrite(&item, sizeof(item), 1, result->file);
1061 fwrite(&ni->c, 1*sizeof(struct coord), 1, result->file);
1062 write_attr(result->file, &town_name_attr, label_attr_buffer);
1064 result->size+=(item.clen+3+label_attr.len+1)*4;
1070 processed_nodes_out++;
1074 sort_countries_compare(const void *p1, const void *p2)
1076 struct item_bin *ib1=*((struct item_bin **)p1),*ib2=*((struct item_bin **)p2);
1077 struct attr_bin *attr1,*attr2;
1079 assert(ib1->clen==2);
1080 assert(ib2->clen==2);
1081 attr1=(struct attr_bin *)((int *)(ib1+1)+ib1->clen);
1082 attr2=(struct attr_bin *)((int *)(ib2+1)+ib1->clen);
1083 assert(attr1->type == attr_town_name);
1084 assert(attr2->type == attr_town_name);
1085 s1=(char *)(attr1+1);
1086 s2=(char *)(attr2+1);
1087 return strcmp(s1, s2);
1089 fprintf(stderr,"sort_countries_compare p1=%p p2=%p %s %s\n",p1,p2,s1,s2);
1094 #ifdef GENERATE_INDEX
1096 sort_countries(void)
1099 struct country_table *co;
1101 struct item_bin *ib;
1103 char *p,*buffer,**idx,*name;
1104 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
1105 co=&country_table[i];
1111 buffer=malloc(co->size);
1112 assert(buffer != NULL);
1113 idx=malloc(co->count*sizeof(void *));
1114 assert(idx != NULL);
1115 name=g_strdup_printf("country_%d.bin.unsorted", co->countryid);
1118 fread(buffer, co->size, 1, f);
1123 for (j = 0 ; j < co->count ; j++) {
1125 p+=(*((int *)p)+1)*4;
1127 qsort(idx, co->count, sizeof(void *), sort_countries_compare);
1128 name=g_strdup_printf("country_%d.bin", co->countryid);
1130 for (j = 0 ; j < co->count ; j++) {
1131 ib=(struct item_bin *)(idx[j]);
1132 c=(struct coord *)(ib+1);
1133 fwrite(ib, (ib->len+1)*4, 1, f);
1135 bbox_extend(c, &co->r);
1147 add_nd(char *p, int ref)
1150 struct node_item *ni;
1151 ni=node_item_get(ref);
1154 coord_buffer[coord_count++]=ni->c;
1156 SET_REF(coord_buffer[coord_count], ref);
1162 if (len > 0 && p[len-1]=='\n')
1164 fprintf(stderr,"WARNING: way %d: node %d not found (%s)\n",wayid,ref,p);
1166 if (coord_count > 65536) {
1167 fprintf(stderr,"ERROR: Overflow\n");
1175 char ref_buffer[BUFFER_SIZE];
1176 if (!xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
1178 add_nd(p, atoi(ref_buffer));
1184 save_buffer(char *filename, struct buffer *b)
1187 f=fopen(filename,"wb+");
1188 fwrite(b->base, b->size, 1, f);
1193 load_buffer(char *filename, struct buffer *b)
1199 f=fopen(filename,"rb");
1200 fseek(f, 0, SEEK_END);
1201 b->size=b->malloced=ftell(f);
1202 fprintf(stderr,"reading %d bytes from %s\n", b->size, filename);
1203 fseek(f, 0, SEEK_SET);
1204 b->base=malloc(b->size);
1205 assert(b->base != NULL);
1206 fread(b->base, b->size, 1, f);
1211 phase1(FILE *in, FILE *out_ways, FILE *out_nodes)
1217 while (fgets(buffer, size, in)) {
1218 p=strchr(buffer,'<');
1220 fprintf(stderr,"WARNING: wrong line %s\n", buffer);
1223 if (!strncmp(p, "<?xml ",6)) {
1224 } else if (!strncmp(p, "<osm ",5)) {
1225 } else if (!strncmp(p, "<bound ",7)) {
1226 } else if (!strncmp(p, "<node ",6)) {
1228 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1231 } else if (!strncmp(p, "<tag ",5)) {
1233 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1234 } else if (!strncmp(p, "<way ",5)) {
1237 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1239 } else if (!strncmp(p, "<nd ",4)) {
1241 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1242 } else if (!strncmp(p, "<relation ",10)) {
1244 if (!parse_relation(p))
1245 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1246 processed_relations++;
1247 } else if (!strncmp(p, "<member ",8)) {
1248 } else if (!strncmp(p, "</node>",7)) {
1250 end_node(out_nodes);
1251 } else if (!strncmp(p, "</way>",6)) {
1254 } else if (!strncmp(p, "</relation>",11)) {
1256 } else if (!strncmp(p, "</osm>",6)) {
1258 fprintf(stderr,"WARNING: unknown tag in %s\n", buffer);
1266 #ifdef HAVE_POSTGRESQL
1268 phase1_db(char *dbstr, FILE *out_ways, FILE *out_nodes)
1271 PGresult *res,*node,*way,*tag;
1272 int count,tagged,i,j,k;
1273 long min, max, id, tag_id, node_id;
1277 conn=PQconnectdb(dbstr);
1279 fprintf(stderr,"Failed to connect to database with '%s'\n",dbstr);
1282 res=PQexec(conn, "begin");
1284 fprintf(stderr, "Cannot begin transaction: %s\n", PQerrorMessage(conn));
1288 res=PQexec(conn, "set transaction isolation level serializable");
1290 fprintf(stderr, "Cannot set isolation level: %s\n", PQerrorMessage(conn));
1294 res=PQexec(conn, "declare node cursor for select id,x(coordinate),y(coordinate) from node order by id");
1296 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
1300 res=PQexec(conn, "declare way cursor for select id from way order by id");
1302 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
1306 res=PQexec(conn, "declare relation cursor for select id from relation order by id");
1308 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
1313 node=PQexec(conn, "fetch 100000 from node");
1315 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
1319 count=PQntuples(node);
1322 min=atol(PQgetvalue(node, 0, 0));
1323 max=atol(PQgetvalue(node, count-1, 0));
1324 sprintf(query,"select node_id,name,value from node_tag where node_id >= %ld and node_id <= %ld order by node_id", min, max);
1325 tag=PQexec(conn, query);
1327 fprintf(stderr, "Cannot query node_tag: %s\n", PQerrorMessage(conn));
1331 for (i = 0 ; i < count ; i++) {
1332 id=atol(PQgetvalue(node, i, 0));
1333 add_node(id, atof(PQgetvalue(node, i, 1)), atof(PQgetvalue(node, i, 2)));
1337 while (j < PQntuples(tag)) {
1338 tag_id=atol(PQgetvalue(tag, j, 0));
1340 add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
1350 end_node(out_nodes);
1357 way=PQexec(conn, "fetch 100000 from way");
1359 fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn));
1363 count=PQntuples(way);
1366 min=atol(PQgetvalue(way, 0, 0));
1367 max=atol(PQgetvalue(way, count-1, 0));
1368 sprintf(query,"select way_id,node_id from way_node where way_id >= %ld and way_id <= %ld order by way_id,sequence_id", min, max);
1369 node=PQexec(conn, query);
1371 fprintf(stderr, "Cannot query way_node: %s\n", PQerrorMessage(conn));
1374 sprintf(query,"select way_id,name,value from way_tag where way_id >= %ld and way_id <= %ld order by way_id", min, max);
1375 tag=PQexec(conn, query);
1377 fprintf(stderr, "Cannot query way_tag: %s\n", PQerrorMessage(conn));
1382 for (i = 0 ; i < count ; i++) {
1383 id=atol(PQgetvalue(way, i, 0));
1388 while (k < PQntuples(node)) {
1389 node_id=atol(PQgetvalue(node, k, 0));
1390 if (node_id == id) {
1391 add_nd("",atol(PQgetvalue(node, k, 1)));
1400 while (j < PQntuples(tag)) {
1401 tag_id=atol(PQgetvalue(tag, j, 0));
1403 add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
1421 res=PQexec(conn, "commit");
1423 fprintf(stderr, "Cannot commit transaction: %s\n", PQerrorMessage(conn));
1433 static char buffer[150000];
1437 static struct item_bin *
1440 struct item_bin *ib=(struct item_bin *) buffer;
1442 r=fread(ib, sizeof(*ib), 1, in);
1446 assert((ib->len+1) < sizeof(buffer));
1447 s=(ib->len+1)*4-sizeof(*ib);
1448 r=fread(ib+1, s, 1, in);
1456 bbox_extend(struct coord *c, struct rect *r)
1469 bbox(struct coord *c, int count, struct rect *r)
1482 contains_bbox(int xl, int yl, int xh, int yh, struct rect *r)
1484 if (r->h.x < xl || r->h.x > xh) {
1487 if (r->l.x > xh || r->l.x < xl) {
1490 if (r->h.y < yl || r->h.y > yh) {
1493 if (r->l.y > yh || r->l.y < yl) {
1499 struct rect world_bbox = {
1500 { -20000000, -20000000},
1501 { 20000000, 20000000},
1505 tile(struct rect *r, char *ret, int max)
1514 for (i = 0 ; i < max ; i++) {
1521 if ( contains_bbox(x0,y0,x2,y2,r)) {
1525 } else if (contains_bbox(x2,y0,x4,y2,r)) {
1529 } else if (contains_bbox(x0,y2,x2,y4,r)) {
1533 } else if (contains_bbox(x2,y2,x4,y4,r)) {
1543 tile_bbox(char *tile, struct rect *r)
1548 c.x=(r->l.x+r->h.x)/2;
1549 c.y=(r->l.y+r->h.y)/2;
1572 GHashTable *tile_hash;
1573 GHashTable *tile_hash2;
1576 tile_extend(char *tile, struct item_bin *ib, GList **tiles_list)
1578 struct tile_head *th=NULL;
1579 if (debug_tile(tile))
1580 fprintf(stderr,"Tile:Writing %d bytes to '%s' (%p,%p)\n", (ib->len+1)*4, tile, g_hash_table_lookup(tile_hash, tile), tile_hash2 ? g_hash_table_lookup(tile_hash2, tile) : NULL);
1582 th=g_hash_table_lookup(tile_hash2, tile);
1584 th=g_hash_table_lookup(tile_hash, tile);
1586 th=malloc(sizeof(struct tile_head)+ sizeof( char* ) );
1588 // strcpy(th->subtiles, tile);
1591 th->total_size_used=0;
1594 th->name=string_hash_lookup(tile);
1595 *th_get_subtile( th, 0 ) = th->name;
1598 g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
1600 *tiles_list=g_list_append(*tiles_list, string_hash_lookup( th->name ) );
1602 if (debug_tile(tile))
1603 fprintf(stderr,"new '%s'\n", tile);
1605 th->total_size+=ib->len*4+4;
1606 if (debug_tile(tile))
1607 fprintf(stderr,"New total size of %s(%p):%d\n", th->name, th, th->total_size);
1608 g_hash_table_insert(tile_hash, string_hash_lookup( th->name ), th);
1612 tile_data_size(char *tile)
1614 struct tile_head *th;
1615 th=g_hash_table_lookup(tile_hash, tile);
1618 return th->total_size;
1622 merge_tile(char *base, char *sub)
1624 struct tile_head *thb, *ths;
1625 thb=g_hash_table_lookup(tile_hash, base);
1626 ths=g_hash_table_lookup(tile_hash, sub);
1629 if (debug_tile(base) || debug_tile(sub))
1630 fprintf(stderr,"merging '%s'(%p) (%d) with '%s'(%p) (%d)\n", base, thb, thb ? thb->total_size : 0, sub, ths, ths->total_size);
1633 g_hash_table_remove(tile_hash, sub);
1634 thb->name=string_hash_lookup(base);
1635 g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
1638 thb=realloc(thb, sizeof(struct tile_head)+( ths->num_subtiles+thb->num_subtiles ) * sizeof( char*) );
1639 assert(thb != NULL);
1640 memcpy( th_get_subtile( thb, thb->num_subtiles ), th_get_subtile( ths, 0 ), ths->num_subtiles * sizeof( char*) );
1641 thb->num_subtiles+=ths->num_subtiles;
1642 thb->total_size+=ths->total_size;
1643 g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
1644 g_hash_table_remove(tile_hash, sub);
1652 get_tiles_list_func(char *key, struct tile_head *th, GList **list)
1654 *list=g_list_prepend(*list, key);
1658 get_tiles_list(void)
1661 g_hash_table_foreach(tile_hash, (GHFunc)get_tiles_list_func, &ret);
1667 write_tile(char *key, struct tile_head *th, gpointer dummy)
1671 fprintf(stderr,"DEBUG: Writing %s\n", key);
1672 strcpy(buffer,"tiles/");
1675 strcat(buffer,".bin");
1677 f=fopen(buffer, "wb+");
1679 fwrite(th->data, th->size, 1, f);
1687 write_item(char *tile, struct item_bin *ib)
1689 struct tile_head *th;
1692 th=g_hash_table_lookup(tile_hash2, tile);
1694 th=g_hash_table_lookup(tile_hash, tile);
1696 if (th->process != 0 && th->process != 1) {
1697 fprintf(stderr,"error with tile '%s' of length %d\n", tile, strlen(tile));
1702 if (debug_tile(tile))
1703 fprintf(stderr,"Data:Writing %d bytes to '%s' (%p,%p)\n", (ib->len+1)*4, tile, g_hash_table_lookup(tile_hash, tile), tile_hash2 ? g_hash_table_lookup(tile_hash2, tile) : NULL);
1705 if (th->total_size_used+size > th->total_size) {
1706 fprintf(stderr,"Overflow in tile %s (used %d max %d item %d)\n", tile, th->total_size_used, th->total_size, size);
1710 memcpy(th->zip_data+th->total_size_used, ib, size);
1711 th->total_size_used+=size;
1713 fprintf(stderr,"no tile hash found for %s\n", tile);
1720 write_item_part(FILE *out, struct item_bin *orig, int first, int last)
1722 struct item_bin new;
1723 struct coord *c=(struct coord *)(orig+1);
1724 char *attr=(char *)(c+orig->clen/2);
1725 int attr_len=orig->len-orig->clen-2;
1727 new.type=orig->type;
1728 new.clen=(last-first+1)*2;
1729 new.len=new.clen+attr_len+2;
1731 fprintf(stderr,"first %d last %d type 0x%x len %d clen %d attr_len %d\n", first, last, new.type, new.len, new.clen, attr_len);
1733 fwrite(&new, sizeof(new), 1, out);
1734 fwrite(c+first, new.clen*4, 1, out);
1735 fwrite(attr, attr_len*4, 1, out);
1739 phase2(FILE *in, FILE *out)
1742 int i,ccount,last,ndref;
1743 struct item_bin *ib;
1744 struct node_item *ni;
1746 processed_nodes=processed_nodes_out=processed_ways=processed_relations=processed_tiles=0;
1748 while ((ib=read_item(in))) {
1750 fprintf(stderr,"type 0x%x len %d clen %d\n", ib->type, ib->len, ib->clen);
1753 c=(struct coord *)(ib+1);
1755 for (i = 0 ; i < ccount ; i++) {
1758 ni=node_item_get(ndref);
1760 fprintf(stderr,"ni=%p\n", ni);
1763 if (ni->ref_way > 1 && i != 0 && i != ccount-1 && item_is_street(*ib)) {
1764 write_item_part(out, ib, last, i);
1769 write_item_part(out, ib, last, ccount-1);
1777 phase34_process_file(int phase, FILE *in)
1779 struct item_bin *ib;
1784 while ((ib=read_item(in))) {
1785 if (ib->type < 0x80000000)
1789 bbox((struct coord *)(ib+1), ib->clen/2, &r);
1792 if (ib->type == type_street_n_lanes || ib->type == type_highway_city || ib->type == type_highway_land || ib->type == type_ramp)
1794 if (ib->type == type_street_3_city || ib->type == type_street_4_city || ib->type == type_street_3_land || ib->type == type_street_4_land)
1797 tile(&r, buffer, max);
1799 fprintf(stderr,"%s\n", buffer);
1802 tile_extend(buffer, ib, NULL);
1804 write_item(buffer, ib);
1809 struct item_bin item;
1811 struct attr_bin attr_order;
1814 struct attr_bin attr_zipfile_ref;
1819 index_submap_add(int phase, struct tile_head *th, GList **tiles_list)
1821 struct index_item ii;
1822 int len=strlen(th->name);
1823 char index_tile[len+1];
1825 ii.min=(len > 4) ? len-4 : 0;
1827 strcpy(index_tile, th->name);
1833 tile_bbox(th->name, &ii.r);
1835 ii.item.len=sizeof(ii)/4-1;
1836 ii.item.type=type_submap;
1839 ii.attr_order.len=2;
1840 ii.attr_order.type=attr_order;
1842 ii.attr_zipfile_ref.len=2;
1843 ii.attr_zipfile_ref.type=attr_zipfile_ref;
1844 ii.zipfile_ref=th->zipnum;
1847 tile_extend(index_tile, (struct item_bin *)&ii, tiles_list);
1849 write_item(index_tile, (struct item_bin *)&ii);
1851 unsigned int *c=(unsigned int *)ⅈ
1853 for (i = 0 ; i < sizeof(ii)/4 ; i++) {
1854 fprintf(stderr,"%08x ", c[i]);
1856 fprintf(stderr,"\n");
1861 add_tile_hash(struct tile_head *th)
1863 int idx,len,maxnamelen=0;
1867 g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
1869 for( idx = 0; idx < th->num_subtiles; idx++ ) {
1871 data = th_get_subtile( th, idx );
1873 if (debug_tile(data) || debug_tile(th->name)) {
1874 fprintf(stderr,"Parent for '%s' is '%s'\n", *data, th->name);
1877 g_hash_table_insert(tile_hash2, *data, th);
1879 len = strlen( *data );
1881 if (len > maxnamelen) {
1890 create_tile_hash(void)
1892 struct tile_head *th;
1893 int len,maxnamelen=0;
1895 tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
1898 len=add_tile_hash(th);
1899 if (len > maxnamelen)
1907 create_tile_hash_list(GList *list)
1910 struct tile_head *th;
1912 tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
1914 fprintf(stderr,"list=%p\n", list);
1915 next=g_list_first(list);
1917 th=g_hash_table_lookup(tile_hash, next->data);
1919 fprintf(stderr,"No tile found for '%s'\n", (char *)(next->data));
1922 next=g_list_next(next);
1927 destroy_tile_hash(void)
1929 g_hash_table_destroy(tile_hash2);
1935 static void write_countrydir(int phase, int maxnamelen);
1938 write_tilesdir(int phase, int maxlen, FILE *out)
1941 GList *tiles_list,*next;
1943 struct tile_head *th,**last=NULL;
1946 tiles_list=get_tiles_list();
1948 create_tile_hash_list(tiles_list);
1949 next=g_list_first(tiles_list);
1950 last=&tile_head_root;
1953 if (strlen(next->data) > maxlen)
1954 maxlen=strlen(next->data);
1955 next=g_list_next(next);
1961 fprintf(stderr,"PROGRESS: collecting tiles with len=%d\n", len);
1963 #ifdef GENERATE_INDEX
1965 write_countrydir(phase, maxlen);
1967 next=g_list_first(tiles_list);
1969 if (strlen(next->data) == len) {
1970 th=g_hash_table_lookup(tile_hash, next->data);
1976 fprintf(out,"%s:%d",(char *)next->data,th->total_size);
1978 for ( idx = 0; idx< th->num_subtiles; idx++ ){
1979 data= th_get_subtile( th, idx );
1980 fprintf(out,":%s", *data);
1986 index_submap_add(phase, th, &tiles_list);
1990 next=g_list_next(next);
1999 struct tile_head *th;
2000 char basetile[1024];
2002 GList *tiles_list_sorted,*last;
2003 int i,i_min,len,size_all,size[5],size_min,work_done;
2007 tiles_list_sorted=get_tiles_list();
2008 fprintf(stderr,"PROGRESS: sorting %d tiles\n", g_list_length(tiles_list_sorted));
2009 tiles_list_sorted=g_list_sort(tiles_list_sorted, (GCompareFunc)strcmp);
2010 fprintf(stderr,"PROGRESS: sorting %d tiles done\n", g_list_length(tiles_list_sorted));
2011 last=g_list_last(tiles_list_sorted);
2014 th=g_hash_table_lookup(tile_hash, last->data);
2015 zip_size+=th->total_size;
2016 last=g_list_previous(last);
2018 fprintf(stderr,"DEBUG: size=%Ld\n", zip_size);
2019 last=g_list_last(tiles_list_sorted);
2023 len=strlen(last->data);
2025 strcpy(basetile,last->data);
2026 basetile[len-1]='\0';
2027 strcpy(subtile,last->data);
2028 for (i = 0 ; i < 4 ; i++) {
2029 subtile[len-1]='a'+i;
2030 size[i]=tile_data_size(subtile);
2032 size[4]=tile_data_size(basetile);
2033 size_all=size[0]+size[1]+size[2]+size[3]+size[4];
2034 if (size_all < 65536 && size_all > 0 && size_all != size[4]) {
2035 for (i = 0 ; i < 4 ; i++) {
2036 subtile[len-1]='a'+i;
2037 work_done+=merge_tile(basetile, subtile);
2043 for (i = 0 ; i < 4 ; i++) {
2044 if (size[i] && size[i] < size_min) {
2051 if (size[4]+size_min >= 65536)
2053 subtile[len-1]='a'+i_min;
2054 work_done+=merge_tile(basetile, subtile);
2055 size[4]+=size[i_min];
2060 last=g_list_previous(last);
2062 g_list_free(tiles_list_sorted);
2063 fprintf(stderr,"PROGRESS: merged %d tiles\n", work_done);
2064 } while (work_done);
2067 struct country_index_item {
2068 struct item_bin item;
2069 struct attr_bin attr_country_id;
2071 struct attr_bin attr_zipfile_ref;
2076 index_country_add(int phase, int country_id, int zipnum)
2078 struct country_index_item ii;
2079 char *index_tile="";
2081 ii.item.len=sizeof(ii)/4-1;
2082 ii.item.type=type_countryindex;
2085 ii.attr_country_id.len=2;
2086 ii.attr_country_id.type=attr_country_id;
2087 ii.country_id=country_id;
2089 ii.attr_zipfile_ref.len=2;
2090 ii.attr_zipfile_ref.type=attr_zipfile_ref;
2091 ii.zipfile_ref=zipnum;
2094 tile_extend(index_tile, (struct item_bin *)&ii, NULL);
2096 write_item(index_tile, (struct item_bin *)&ii);
2099 #ifdef GENERATE_INDEX
2107 add_aux_tile(int phase, char *name, char *filename, int size)
2109 struct aux_tile *at;
2111 at=g_new0(struct aux_tile, 1);
2112 at->name=g_strdup(name);
2113 at->filename=g_strdup(filename);
2115 aux_tile_list=g_list_append(aux_tile_list, at);
2121 write_aux_tiles(FILE *out, FILE *dir_out, int compression_level, int namelen)
2123 GList *l=aux_tile_list;
2124 struct aux_tile *at;
2131 buffer=malloc(at->size);
2132 assert(buffer != NULL);
2133 f=fopen(at->filename,"r");
2135 fread(buffer, at->size, 1, f);
2137 write_zipmember(out, dir_out, at->name, namelen, buffer, at->size, compression_level);
2145 write_countrydir(int phase, int maxnamelen)
2150 char searchtile[32];
2152 struct country_table *co;
2153 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
2154 co=&country_table[i];
2157 tile(&co->r, tilename, max);
2158 sprintf(searchtile,"%ss%d", tilename, 0);
2159 sprintf(filename,"country_%d.bin", co->countryid);
2160 zipnum=add_aux_tile(phase, searchtile, filename, co->size);
2161 index_country_add(phase,co->countryid,zipnum);
2167 remove_countryfiles(void)
2171 struct country_table *co;
2173 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
2174 co=&country_table[i];
2176 sprintf(filename,"country_%d.bin", co->countryid);
2184 phase34(int phase, int maxnamelen, FILE *ways_in, FILE *nodes_in, FILE *tilesdir_out)
2187 processed_nodes=processed_nodes_out=processed_ways=processed_relations=processed_tiles=0;
2191 tile_hash=g_hash_table_new(g_str_hash, g_str_equal);
2193 phase34_process_file(phase, ways_in);
2195 phase34_process_file(phase, nodes_in);
2196 fprintf(stderr,"read %d bytes\n", bytes_read);
2201 write_tilesdir(phase, maxnamelen, tilesdir_out);
2208 dump_coord(struct coord *c)
2210 printf("0x%x 0x%x",c->x, c->y);
2216 struct item_bin *ib;
2224 while ((ib=read_item(in))) {
2225 c=(struct coord *)(ib+1);
2226 if (ib->type < type_line) {
2230 attr_start=(int *)(ib+1)+ib->clen;
2231 attr_end=(int *)ib+ib->len+1;
2232 printf("type=%s", item_to_name(ib->type));
2233 while (attr_start < attr_end) {
2234 a=(struct attr_bin *)(attr_start);
2235 attr_start+=a->len+1;
2237 attr_data_set(&attr, (a+1));
2238 str=attr_to_text(&attr, NULL, 1);
2239 printf(" %s=\"%s\"", attr_to_name(a->type), str);
2242 printf(" debug=\"length=%d\"", ib->len);
2244 if (ib->type >= type_line) {
2245 for (i = 0 ; i < ib->clen/2 ; i++) {
2255 phase3(FILE *ways_in, FILE *nodes_in, FILE *tilesdir_out)
2257 return phase34(3, 0, ways_in, nodes_in, tilesdir_out);
2260 static long long zipoffset;
2261 static int zipdir_size;
2264 compress2_int(Byte *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
2269 stream.next_in = (Bytef*)source;
2270 stream.avail_in = (uInt)sourceLen;
2271 stream.next_out = dest;
2272 stream.avail_out = (uInt)*destLen;
2273 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
2275 stream.zalloc = (alloc_func)0;
2276 stream.zfree = (free_func)0;
2277 stream.opaque = (voidpf)0;
2279 err = deflateInit2(&stream, level, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
2280 if (err != Z_OK) return err;
2282 err = deflate(&stream, Z_FINISH);
2283 if (err != Z_STREAM_END) {
2284 deflateEnd(&stream);
2285 return err == Z_OK ? Z_BUF_ERROR : err;
2287 *destLen = stream.total_out;
2289 err = deflateEnd(&stream);
2294 write_zipmember(FILE *out, FILE *dir_out, char *name, int filelen, char *data, int data_size, int compression_level)
2296 struct zip_lfh lfh = {
2309 struct zip_cd cd = {
2330 char filename[filelen+1];
2331 int error,crc,len,comp_size=data_size;
2332 uLongf destlen=data_size+data_size/500+12;
2333 char compbuffer[destlen];
2335 crc=crc32(0, NULL, 0);
2336 crc=crc32(crc, (unsigned char *)data, data_size);
2337 if (compression_level) {
2338 error=compress2_int((Byte *)compbuffer, &destlen, (Bytef *)data, data_size, compression_level);
2339 if (error == Z_OK) {
2340 if (destlen < data_size) {
2345 fprintf(stderr,"compress2 returned %d\n", error);
2349 lfh.zipsize=comp_size;
2350 lfh.zipuncmp=data_size;
2351 lfh.zipmthd=compression_level ? 8:0;
2353 cd.zipcsiz=comp_size;
2354 cd.zipcunc=data_size;
2355 cd.zipcmthd=compression_level ? 8:0;
2356 strcpy(filename, name);
2357 len=strlen(filename);
2358 while (len < filelen) {
2359 filename[len++]='_';
2361 filename[filelen]='\0';
2362 fwrite(&lfh, sizeof(lfh), 1, out);
2363 fwrite(filename, filelen, 1, out);
2364 fwrite(data, comp_size, 1, out);
2365 zipoffset+=sizeof(lfh)+filelen+comp_size;
2366 fwrite(&cd, sizeof(cd), 1, dir_out);
2367 fwrite(filename, filelen, 1, dir_out);
2368 zipdir_size+=sizeof(cd)+filelen;
2372 process_slice(FILE *ways_in, FILE *nodes_in, int size, int maxnamelen, FILE *out, FILE *dir_out, int compression_level)
2374 struct tile_head *th;
2375 char *slice_data,*zip_data;
2378 slice_data=malloc(size);
2379 assert(slice_data != NULL);
2380 zip_data=slice_data;
2384 th->zip_data=zip_data;
2385 zip_data+=th->total_size;
2390 fseek(ways_in, 0, SEEK_SET);
2392 fseek(nodes_in, 0, SEEK_SET);
2393 phase34(4, maxnamelen, ways_in, nodes_in, NULL);
2398 #ifdef GENERATE_INDEX
2399 if (! strlen(th->name))
2400 zipfiles+=write_aux_tiles(out, dir_out, compression_level, maxnamelen);
2402 if (th->total_size != th->total_size_used) {
2403 fprintf(stderr,"Size error '%s': %d vs %d\n", th->name, th->total_size, th->total_size_used);
2406 if (strlen(th->name))
2407 write_zipmember(out, dir_out, th->name, maxnamelen, th->zip_data, th->total_size, compression_level);
2409 write_zipmember(out, dir_out, "index", sizeof("index")-1, th->zip_data, th->total_size, compression_level);
2422 cat(FILE *in, FILE *out)
2426 while ((size=fread(buffer, 1, 4096, in)))
2427 fwrite(buffer, 1, size, out);
2431 phase4(FILE *ways_in, FILE *nodes_in, FILE *out, FILE *dir_out, int compression_level)
2433 int slice_size=1024*1024*1024;
2434 int maxnamelen,size,slices;
2436 struct tile_head *th,*th2;
2437 struct zip_eoc eoc = {
2448 maxnamelen=create_tile_hash();
2453 fprintf(stderr, "Maximum slice size %d\n", slice_size);
2455 if (size + th->total_size > slice_size) {
2456 fprintf(stderr,"Slice %d is of size %d\n", slices, size);
2460 size+=th->total_size;
2464 fprintf(stderr,"Slice %d is of size %d\n", slices, size);
2475 while (th && size+th->total_size < slice_size) {
2476 size+=th->total_size;
2480 zipfiles+=process_slice(ways_in, nodes_in, size, maxnamelen, out, dir_out, compression_level);
2483 fseek(dir_out, 0, SEEK_SET);
2485 eoc.zipenum=zipfiles;
2486 eoc.zipecenn=zipfiles;
2487 eoc.zipecsz=zipdir_size;
2488 eoc.zipeofst=zipoffset;
2489 fwrite(&eoc, sizeof(eoc), 1, out);
2499 fprintf(f,"osm2navit - parse osm textfile and converts to NavIt binfile format\n\n");
2500 fprintf(f,"Usage :\n");
2501 fprintf(f,"bzcat planet.osm.bz2 | osm2navit mymap.bin\n");
2502 fprintf(f,"Available switches:\n");
2503 fprintf(f,"-h (--help) : this screen\n");
2504 fprintf(f,"-N (--nodes-only) : process only nodes\n");
2505 fprintf(f,"-W (--ways-only) : process only ways\n");
2506 fprintf(f,"-a (--attr-debug-level) : control which data is included in the debug attribute\n");
2507 fprintf(f,"-c (--dump-coordinates) : dump coordinates after phase 1\n");
2508 #ifdef HAVE_POSTGRESQL
2509 fprintf(f,"-d (--db) : get osm data out of a postgresql database with osm simple scheme and given connect string\n");
2511 fprintf(f,"-e (--end) : end at specified phase\n");
2512 fprintf(f,"-k (--keep-tmpfiles) : do not delete tmp files after processing. useful to reuse them\n\n");
2513 fprintf(f,"-o (--coverage) : map every street to item overage\n");
2514 fprintf(f,"-s (--start) : start at specified phase\n");
2515 fprintf(f,"-i (--input-file) : specify the input file name (OSM), overrules default stdin\n");
2516 fprintf(f,"-w (--dedupe-ways) : ensure no duplicate ways or nodes. useful when using several input files\n");
2517 fprintf(f,"-z (--compression-level) : set the compression level\n");
2522 process_binfile(FILE *in, FILE *out)
2524 struct item_bin *ib;
2525 while ((ib=read_item(in))) {
2526 fwrite(ib, (ib->len+1)*4, 1, out);
2530 int main(int argc, char **argv)
2532 FILE *ways=NULL,*ways_split=NULL,*nodes=NULL,*tilesdir,*zipdir,*res;
2533 char *map=g_strdup(attrmap);
2534 int i,c,start=1,end=4,dump_coordinates=0;
2535 int keep_tmpfiles=0;
2536 int process_nodes=1, process_ways=1;
2537 int compression_level=9;
2540 char *result,*dbstr=NULL;
2541 FILE* input_file = stdin;
2542 struct plugins *plugins=NULL;
2543 struct attr **attrs;
2547 int this_option_optind = optind ? optind : 1;
2549 int option_index = 0;
2550 static struct option long_options[] = {
2551 {"attr-debug-level", 1, 0, 'a'},
2552 {"binfile", 0, 0, 'b'},
2553 {"compression-level", 1, 0, 'z'},
2554 {"coverage", 0, 0, 'o'},
2555 #ifdef HAVE_POSTGRESQL
2558 {"dedupe-ways", 0, 0, 'w'},
2559 {"dump", 0, 0, 'D'},
2561 {"help", 0, 0, 'h'},
2562 {"keep-tmpfiles", 0, 0, 'k'},
2563 {"nodes-only", 0, 0, 'N'},
2565 {"plugin", 1, 0, 'p'},
2566 {"start", 1, 0, 's'},
2567 {"input-file", 1, 0, 'i'},
2568 {"ignore-unknown", 0, 0, 'n'},
2569 {"ways-only", 0, 0, 'W'},
2572 c = getopt_long (argc, argv, "DNWa:bc"
2573 #ifdef HAVE_POSTGRESQL
2576 "e:hi:knm:ps:w", long_options, &option_index);
2590 attr_debug_level=atoi(optarg);
2598 #ifdef HAVE_POSTGRESQL
2610 attrs=(struct attr*[]){
2611 &(struct attr){attr_type,{"textfile"}},
2612 &(struct attr){attr_data,{"bookmark.txt"}},
2615 fprintf(stderr,"optarg=%s\n", optarg);
2618 fprintf(stderr,"I will IGNORE unknown types\n");
2622 fprintf(stderr,"I will KEEP tmp files\n");
2630 plugins=plugins_new();
2631 attrs=(struct attr*[]){&(struct attr){attr_path,{optarg}},NULL};
2632 plugins_add_path(plugins, attrs);
2638 dedupe_ways_hash=g_hash_table_new(NULL, NULL);
2641 input_file = fopen( optarg, "r" );
2642 if ( input_file == NULL )
2644 fprintf( stderr, "\nInput file (%s) not found\n", optarg );
2649 compression_level=atoi(optarg);
2655 fprintf(stderr,"c=%d\n", c);
2659 if (optind != argc-(output == 1 ? 0:1))
2662 plugins_init(plugins);
2663 result=argv[optind];
2665 #ifdef GENERATE_INDEX
2666 build_countrytable();
2673 ways=fopen("ways.tmp","wb+");
2675 nodes=fopen("nodes.tmp","wb+");
2677 fprintf(stderr,"PROGRESS: Phase 1: collecting data\n");
2678 #ifdef HAVE_POSTGRESQL
2680 phase1_db(dbstr,ways,nodes);
2683 phase1(input_file,ways,nodes);
2688 #ifdef GENERATE_INDEX
2689 fprintf(stderr,"PROGRESS: Phase 1: sorting countries\n");
2693 if (end == 1 || dump_coordinates)
2694 save_buffer("coords.tmp",&node_buffer);
2698 load_buffer("coords.tmp",&node_buffer);
2701 ways=fopen("ways.tmp","rb");
2702 ways_split=fopen("ways_split.tmp","wb+");
2704 fprintf(stderr,"PROGRESS: Phase 2: finding intersections\n");
2705 phase2(ways,ways_split);
2711 fprintf(stderr,"PROGRESS: Skipping Phase 2\n");
2713 free(node_buffer.base);
2714 node_buffer.base=NULL;
2715 node_buffer.malloced=0;
2720 ways_split=fopen("ways_split.tmp","wb+");
2721 process_binfile(stdin, ways_split);
2725 fprintf(stderr,"PROGRESS: Phase 3: dumping\n");
2726 if (process_nodes) {
2727 nodes=fopen("nodes.tmp","rb");
2734 ways_split=fopen("ways_split.tmp","rb");
2744 fprintf(stderr,"PROGRESS: Phase 3: generating tiles\n");
2746 ways_split=fopen("ways_split.tmp","rb");
2748 nodes=fopen("nodes.tmp","rb");
2749 tilesdir=fopen("tilesdir.tmp","wb+");
2750 phase3(ways_split,nodes,tilesdir);
2761 fprintf(stderr,"PROGRESS: Phase 4: assembling map\n");
2763 ways_split=fopen("ways_split.tmp","rb");
2765 nodes=fopen("nodes.tmp","rb");
2766 res=fopen(result,"wb+");
2767 zipdir=fopen("zipdir.tmp","wb+");
2768 phase4(ways_split,nodes,res,zipdir,compression_level);
2775 if(!keep_tmpfiles) {
2776 remove("nodes.tmp");
2777 remove("ways_split.tmp");
2778 remove("tilesdir.tmp");
2779 remove("zipdir.tmp");
2780 #ifdef GENERATE_INDEX
2781 remove_countryfiles();