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
40 #define BUFFER_SIZE 1280
42 #define GENERATE_INDEX
45 #define debug_tile(x) 0
47 #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)
50 #define IS_TOWN(item) ((item).type >= type_town_label && (item).type <= type_town_label_1e7)
51 #define IS_STREET(item) ((item).type >= type_street_nopass && (item).type <= type_ferry)
53 static GHashTable *dedupe_ways_hash;
55 static int attr_debug_level=1;
56 static int nodeid,wayid;
57 static int report,phase;
58 static int ignore_unkown = 0, coverage=0;
60 static char *attrmap={
62 "n amenity=hospital poi_hospital\n"
63 "n amenity=atm poi_bank\n"
64 "n amenity=bank poi_bank\n"
65 "n amenity=pub poi_bar\n"
66 "n amenity=cafe poi_cafe\n"
67 "n amenity=bus_station poi_bus_station\n"
68 "n amenity=parking poi_car_parking\n"
69 "n amenity=cinema poi_cinema\n"
70 "n amenity=fire_station poi_firebrigade\n"
71 "n amenity=fuel poi_fuel\n"
72 "n amenity=courthouse poi_justice\n"
73 "n amenity=library poi_library\n"
74 "n amenity=pharmacy poi_pharmacy\n"
75 "n amenity=place_of_worship,religion=christian poi_church\n"
76 "n amenity=police poi_police\n"
77 "n amenity=post_office poi_post\n"
78 "n amenity=post_box poi_post\n"
79 "n amenity=public_building poi_public_office\n"
80 "n amenity=restaurant poi_restaurant\n"
81 "n amenity=fast_food poi_fastfood\n"
82 "n amenity=toilets poi_restroom\n"
83 "n amenity=school poi_school\n"
84 "n amenity=university poi_school\n"
85 "n amenity=college poi_school\n"
86 "n amenity=telephone poi_telephone\n"
87 "n amenity=theatre poi_theater\n"
88 "n amenity=townhall poi_townhall\n"
89 "n highway=bus_stop poi_bus_stop\n"
90 "n highway=mini_roundabout mini_roundabout\n"
91 "n highway=motorway_junction highway_exit\n"
92 "n highway=traffic_signals traffic_signals\n"
93 "n highway=turning_circle turning_circle\n"
94 "n leisure=slipway poi_boat_ramp\n"
95 "n leisure=fishing poi_fish\n"
96 "n sport=golf poi_golf\n"
97 "n leisure=golf_course poi_golf\n"
98 "n leisure=marina poi_marine\n"
99 "n leisure=sports_centre poi_sport\n"
100 "n leisure=stadium poi_stadium\n"
101 "n shop=supermarket poi_shopping\n"
102 "n shop=convenience poi_shop_grocery\n"
103 "n tourism=attraction poi_attraction\n"
104 "n tourism=camp_site poi_camp_rv\n"
105 "n tourism=caravan_site poi_camp_rv\n"
106 "n tourism=hotel poi_hotel\n"
107 "n tourism=motel poi_hotel\n"
108 "n tourism=guest_house poi_hotel\n"
109 "n tourism=hostel poi_hotel\n"
110 "n tourism=information poi_information\n"
111 "n tourism=museum poi_museum_history\n"
112 "n tourism=picnic_site poi_picnic\n"
113 "n tourism=theme_park poi_resort\n"
114 "n tourism=zoo poi_zoo\n"
115 "n amenity=grave_yard poi_cemetery\n"
116 "n landuse=cemetery poi_cemetery\n"
117 "n military=airfield poi_military\n"
118 "n military=bunker poi_military\n"
119 "n military=barracks poi_military\n"
120 "n military=range poi_military\n"
121 "n military=danger_area poi_danger_area\n"
122 "n sport=swimming poi_swimming\n"
123 "n sport=skiing poi_skiing\n"
124 "n aeroway=aerodrome poi_airport\n"
125 "n aeroway=airport poi_airport\n"
126 "n aeroway=terminal poi_airport\n"
127 "n aeroway=helipad poi_heliport\n"
128 "n man_made=tower poi_tower\n"
129 "n natural=bay poi_bay\n"
130 "n natural=peak poi_peak\n"
131 "n place=suburb district_label\n"
132 "n place=city town_label_2e5\n"
133 "n place=town town_label_2e4\n"
134 "n place=village town_label_2e3\n"
135 "n place=hamlet town_label_2e2\n"
136 "n place=locality town_label_2e0\n"
137 "n railway=halt poi_rail_halt\n"
138 "n railway=level_crossing poi_level_crossing\n"
139 "n railway=station poi_rail_station\n"
140 "n railway=tram_stop poi_rail_tram_stop\n"
141 "w *=* street_unkn\n"
142 "w amenity=place_of_worship poly_building\n"
143 "w building=* poly_building\n"
144 "w aeroway=aerodrome poly_airport\n"
145 "w aeroway=apron poly_apron\n"
146 "w aeroway=runway aeroway_runway\n"
147 "w aeroway=taxiway aeroway_taxiway\n"
148 "w aeroway=terminal poly_terminal\n"
149 "w highway=bridleway bridleway\n"
150 "w highway=cycleway cycleway\n"
151 "w highway=cyclepath cycleway\n"
152 "w highway=footway footway\n"
153 "w highway=steps steps\n"
154 "w highway=unsurfaced track_gravelled\n"
155 "w highway=track track_gravelled\n"
156 "w highway=track,tracktype=grade1 track_paved\n"
157 "w highway=track,surface=paved track_paved\n"
158 "w highway=track,tracktype=grade2 track_gravelled\n"
159 "w highway=track,surface=gravel track_gravelled\n"
160 "w highway=track,tracktype=grade3 track_unpaved\n"
161 "w highway=track,surface=unpaved track_unpaved\n"
162 "w highway=track,tracktype=grade4 track_ground\n"
163 "w highway=track,surface=ground track_ground\n"
164 "w highway=track,tracktype=grade5 track_grass\n"
165 "w highway=track,surface=grass track_grass\n"
166 "w highway=service street_service\n"
167 "w highway=construction street_construction\n"
168 "w highway=pedestrian street_pedestrian\n"
169 "w highway=pedestrian,area=1 poly_pedestrian\n"
170 "w highway=plaza poly_plaza\n"
171 "w landuse=plaza poly_plaza\n"
172 "w highway=residential street_1_city\n"
173 "w highway=living_street living_street\n"
174 "w highway=unclassified street_1_city\n"
175 "w highway=road street_1_city\n"
176 "w highway=minor street_1_land\n"
177 "w highway=tertiary street_2_city\n"
178 "w highway=secondary street_3_city\n"
179 "w highway=primary street_4_city\n"
180 "w highway=primary_link ramp\n"
181 "w highway=trunk street_4_city\n"
182 "w highway=trunk_link ramp\n"
183 "w highway=motorway highway_city\n"
184 "w highway=motorway_link ramp\n"
185 "w historic=town gate poly_building\n"
186 "w cycleway=track cycleway\n"
187 "w amenity=parking poly_car_parking\n"
188 "w landuse=allotments poly_allotments\n"
189 "w landuse=cemetery poly_cemetery\n"
190 "w amenity=grave_yard poly_cemetery\n"
191 "w landuse=forest poly_wood\n"
192 "w landuse=industrial poly_industry\n"
193 "w landuse=residential poly_town\n"
194 "w landuse=residential,area=1 poly_town\n"
195 "w amenity=college poly_college\n"
196 "w amenity=university poly_university\n"
197 "w tourism=camp_site poly_camp_site\n"
198 "w tourism=caravan_site poly_caravan_site\n"
199 "w tourism=picnic_site poly_picnic_site\n"
200 "w tourism=theme_park poly_theme_park\n"
201 "w tourism=attraction poly_attraction\n"
202 "w tourism=zoo poly_zoo\n"
203 "w tourism=artwork poly_artwork\n"
204 "w historic=archaeological_site poly_archaeological_site\n"
205 "w historic=ruins poly_ruins\n"
206 "w historic=battlefield poly_battlefield\n"
207 "w landuse=quarry poly_quarry\n"
208 "w landuse=landfill poly_landfill\n"
209 "w landuse=retail poly_retail\n"
210 "w landuse=commercial poly_commercial\n"
211 "w landuse=brownfield poly_brownfield\n"
212 "w landuse=greenfield poly_greenfield\n"
213 "w landuse=construction poly_construction\n"
214 "w landuse=railway poly_railway\n"
215 "w natural=glacier poly_glacier\n"
216 "w natural=scree poly_scree\n"
217 "w natural=scrub poly_scrub\n"
218 "w natural=fell poly_fell\n"
219 "w natural=heath poly_heath\n"
220 "w natural=marsh poly_marsh\n"
221 "w natural=mud poly_mud\n"
222 "w natural=beach poly_beach\n"
223 "w natural=land poly_land\n"
224 "w landuse=basin poly_basin\n"
225 "w landuse=reservoir poly_reservoir\n"
226 "w landuse=farm poly_farm\n"
227 "w landuse=vineyard poly_farm\n"
228 "w leisure=park poly_park\n"
229 "w landuse=village_green poly_village_green\n"
230 "w landuse=recreation_ground poly_recreation_ground\n"
231 "w natural=wood poly_wood\n"
232 "w natural=water poly_water\n"
233 "w natural=coastline water_line\n"
234 "w place=suburb poly_town\n"
235 "w place=town poly_town\n"
236 "w landuse=military poly_military\n"
237 "w military=airfield poly_airfield\n"
238 "w military=barracks poly_barracks\n"
239 "w military=danger_area poly_danger_area\n"
240 "w military=range poly_range\n"
241 "w military=naval_base poly_naval_base\n"
242 "w power=line powerline\n"
243 "w railway=rail rail\n"
244 "w railway=narrow_gauge rail_narrow_gauge\n"
245 "w railway=light_rail rail_light\n"
246 "w railway=subway rail_subway\n"
247 "w railway=tram rail_tram\n"
248 "w railway=monorail rail_mono\n"
249 "w railway=preserved rail_preserved\n"
250 "w railway=disused rail_disused\n"
251 "w railway=abandoned rail_abandoned\n"
252 "w aerialway=cable_car lift_cable_car\n"
253 "w aerialway=chair_lift lift_chair\n"
254 "w aerialway=drag_lift lift_drag\n"
255 "w leisure=golf_course poly_golf_course\n"
256 "w sport=* poly_sport\n"
257 "w leisure=sports_centre poly_sport\n"
258 "w leisure=stadium poly_sports_stadium\n"
259 "w leisure=track poly_sports_track\n"
260 "w leisure=pitch poly_sports_pitch\n"
261 "w leisure=water_park poly_water_park\n"
262 "w leisure=marina poly_marina\n"
263 "w leisure=fishing poly_fishing\n"
264 "w leisure=garden poly_garden\n"
265 "w leisure=common poly_common\n"
266 "w leisure=playground poly_playground\n"
267 "w leisure=nature_reserve poly_nature_reserve\n"
268 "w waterway=canal water_canal\n"
269 "w waterway=river water_river\n"
270 "w waterway=stream water_stream\n"
271 "w waterway=drain water_drain\n"
272 "w waterway=riverbank poly_water\n"
273 "w boundary=administrative border_country\n"
274 "w boundary=civil border_civil\n"
275 "w boundary=political border_political\n"
276 "w boundary=national_park border_national_park\n"
277 "w route=ferry ferry\n"
278 "w highway=bus_guideway bus_guideway\n"
284 } coord_buffer[65536];
286 #define IS_REF(c) ((c).x >= (1 << 30))
287 #define REF(c) ((c).y)
288 #define SET_REF(c,ref) do { (c).x = 1 << 30; (c).y = ref ; } while(0)
294 static void bbox_extend(struct coord *c, struct rect *r);
296 #ifdef GENERATE_INDEX
298 static GHashTable *aux_tile_hash;
299 GList *aux_tile_list;
301 struct country_table {
308 } country_table[] = {
309 { 40,"Austria,Österreich,AUT"},
311 {276,"Germany,Deutschland,Bundesrepublik Deutschland"},
312 {528,"Nederland,The Netherlands,Niederlande,NL"},
316 static GHashTable *country_table_hash;
319 struct attr_mapping {
321 int attr_present_idx_count;
322 int attr_present_idx[0];
325 static struct attr_mapping **attr_mapping_node;
326 static int attr_mapping_node_count;
327 static struct attr_mapping **attr_mapping_way;
328 static int attr_mapping_way_count;
330 static char *attr_present;
331 static int attr_present_count;
332 static GHashTable *attr_hash;
335 static GHashTable *strings_hash = NULL;
338 static char* string_hash_lookup( const char* key )
340 char* key_ptr = NULL;
342 if ( strings_hash == NULL ) {
343 strings_hash = g_hash_table_new(g_str_hash, g_str_equal);
346 if ( ( key_ptr = g_hash_table_lookup(strings_hash, key )) == NULL ) {
347 key_ptr = g_strdup( key );
348 g_hash_table_insert(strings_hash, key_ptr, (gpointer)key_ptr );
355 build_attrmap_line(char *line)
357 char *t=NULL,*kvl=NULL,*i=NULL,*p,*kv;
358 struct attr_mapping ***attr_mapping_curr,*attr_mapping=g_malloc0(sizeof(struct attr_mapping));
359 int idx,attr_mapping_count=0,*attr_mapping_curr_count;
376 attr_mapping_curr=&attr_mapping_way;
377 attr_mapping_curr_count=&attr_mapping_way_count;
381 attr_mapping_curr=&attr_mapping_node;
382 attr_mapping_curr_count=&attr_mapping_node_count;
384 attr_mapping->type=item_from_name(i);
385 while ((kv=strtok(kvl, ","))) {
387 if (!(idx=(int)g_hash_table_lookup(attr_hash, kv))) {
388 idx=attr_present_count++;
389 g_hash_table_insert(attr_hash, kv, (gpointer) idx);
391 attr_mapping=g_realloc(attr_mapping, sizeof(struct attr_mapping)+(attr_mapping_count+1)*sizeof(int));
392 attr_mapping->attr_present_idx[attr_mapping_count++]=idx;
393 attr_mapping->attr_present_idx_count=attr_mapping_count;
395 *attr_mapping_curr=g_realloc(*attr_mapping_curr, sizeof(**attr_mapping_curr)*(*attr_mapping_curr_count+1));
396 (*attr_mapping_curr)[(*attr_mapping_curr_count)++]=attr_mapping;
400 build_attrmap(char *map)
403 attr_hash=g_hash_table_new(g_str_hash, g_str_equal);
404 attr_present_count=1;
410 build_attrmap_line(map);
413 attr_present=g_malloc0(sizeof(*attr_present)*attr_present_count);
416 #ifdef GENERATE_INDEX
418 build_countrytable(void)
421 char *names,*str,*tok;
422 country_table_hash=g_hash_table_new(g_str_hash, g_str_equal);
423 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
424 names=g_strdup(country_table[i].names);
426 while ((tok=strtok(str, ","))) {
428 g_hash_table_insert(country_table_hash, tok, (gpointer)&country_table[i]);
436 static int processed_nodes, processed_nodes_out, processed_ways, processed_relations, processed_tiles;
437 static int in_way, in_node, in_relation;
443 signal(SIGALRM, sig_alrm);
446 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);
460 struct attr_bin label_attr = {
463 char label_attr_buffer[BUFFER_SIZE];
465 struct attr_bin town_name_attr = {
469 struct attr_bin street_name_attr = {
473 struct attr_bin street_name_systematic_attr = {
474 0, attr_street_name_systematic
476 char street_name_systematic_attr_buffer[BUFFER_SIZE];
478 struct attr_bin debug_attr = {
481 char debug_attr_buffer[BUFFER_SIZE];
483 struct attr_bin flags_attr = {
486 int flags_attr_value;
488 char is_in_buffer[BUFFER_SIZE];
491 static void write_zipmember(FILE *out, FILE *dir_out, char *name, int filelen, char *data, int data_size, int compression_level);
494 pad_text_attr(struct attr_bin *a, char *buffer)
497 if (buffer && buffer[0]) {
507 xml_get_attribute(char *xml, char *attribute, char *buffer, int buffer_size)
509 int len=strlen(attribute);
510 char *pos,*i,s,attr[len+2];
511 strcpy(attr, attribute);
512 strcpy(attr+len, "=");
513 pos=strstr(xml, attr);
523 if (i - pos > buffer_size) {
524 fprintf(stderr,"Buffer overflow %d vs %d\n", i-pos, buffer_size);
527 strncpy(buffer, pos, i-pos);
532 static int node_is_tagged;
535 add_tag(char *k, char *v)
537 GHashTable *value_hash;
540 char buffer[BUFFER_SIZE*2+2];
541 if (! strcmp(k,"ele"))
543 if (! strcmp(k,"time"))
545 if (! strcmp(k,"created_by"))
547 if (! strncmp(k,"tiger:",6) || !strcmp(k,"AND_nodes"))
549 if (! strcmp(k,"converted_by") || ! strcmp(k,"source"))
551 if (! strncmp(k,"osmarender:",11) || !strncmp(k,"svg:",4))
553 if (! strcmp(k,"layer"))
555 if (! strcasecmp(v,"true") || ! strcasecmp(v,"yes"))
557 if (! strcmp(k,"oneway")) {
558 if (!strcmp(v,"1")) {
559 flags_attr_value=AF_ONEWAY;
562 if (! strcmp(v,"-1")) {
563 flags_attr_value=AF_ONEWAYREV;
571 if (! strcmp(k,"junction")) {
572 if (! strcmp(v,"roundabout")) {
573 flags_attr_value=AF_ONEWAY;
577 if (! strcmp(k,"maxspeed")) {
580 if (! strcmp(k,"bicycle")) {
583 if (! strcmp(k,"foot")) {
586 if (! strcmp(k,"note"))
588 if (! strcmp(k,"name")) {
589 strcpy(label_attr_buffer, v);
590 pad_text_attr(&label_attr, label_attr_buffer);
593 if (! strcmp(k,"ref")) {
595 strcpy(street_name_systematic_attr_buffer, v);
596 pad_text_attr(&street_name_systematic_attr, street_name_systematic_attr_buffer);
600 if (! strcmp(k,"is_in")) {
601 strcpy(is_in_buffer, v);
604 if (! strcmp(k,"lanes")) {
607 if (attr_debug_level >= level) {
608 int bytes_left = sizeof( debug_attr_buffer ) - strlen(debug_attr_buffer) - 1;
609 if ( bytes_left > 0 )
611 snprintf(debug_attr_buffer+strlen(debug_attr_buffer), bytes_left, " %s=%s", k, v);
612 debug_attr_buffer[ sizeof( debug_attr_buffer ) - 1 ] = '\0';
621 strcpy(buffer,"*=*");
622 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
625 sprintf(buffer,"%s=*", k);
626 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
629 sprintf(buffer,"*=%s", v);
630 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
633 sprintf(buffer,"%s=%s", k, v);
634 if ((idx=(int)g_hash_table_lookup(attr_hash, buffer)))
650 decode_entities(char *buffer)
655 while ((pos=strchr(pos, '&'))) {
657 for (i = 0 ; i < sizeof(entities)/sizeof(struct entity); i++) {
658 len=strlen(entities[i].entity);
659 if (!strncmp(pos, entities[i].entity, len)) {
661 memmove(pos+1, pos+len, strlen(pos+len)+1);
673 char k_buffer[BUFFER_SIZE];
674 char v_buffer[BUFFER_SIZE];
675 if (!xml_get_attribute(p, "k", k_buffer, BUFFER_SIZE))
677 if (!xml_get_attribute(p, "v", v_buffer, BUFFER_SIZE))
679 decode_entities(v_buffer);
680 add_tag(k_buffer, v_buffer);
692 static struct tile_head {
700 struct tile_head *next;
716 static struct buffer node_buffer = {
721 static char** th_get_subtile( const struct tile_head* th, int idx )
723 char* subtile_ptr = NULL;
724 subtile_ptr = (char*)th + sizeof( struct tile_head ) + idx * sizeof( char *);
725 return (char**)subtile_ptr;
729 extend_buffer(struct buffer *b)
731 b->malloced+=b->malloced_step;
732 b->base=realloc(b->base, b->malloced);
733 if (b->base == NULL) {
734 fprintf(stderr,"realloc of %d bytes failed\n",b->malloced);
741 GHashTable *node_hash;
744 node_buffer_to_hash(void)
746 int i,count=node_buffer.size/sizeof(struct node_item);
747 struct node_item *ni=(struct node_item *)node_buffer.base;
748 for (i = 0 ; i < count ; i++)
749 g_hash_table_insert(node_hash, (gpointer)(ni[i].id), (gpointer)i);
752 static struct node_item *ni;
755 add_node(int id, double lat, double lon)
757 if (node_buffer.size + sizeof(struct node_item) > node_buffer.malloced)
758 extend_buffer(&node_buffer);
761 item.type=type_point_unkn;
763 town_name_attr.len=0;
765 is_in_buffer[0]='\0';
766 sprintf(debug_attr_buffer,"nodeid=%d", nodeid);
767 ni=(struct node_item *)(node_buffer.base+node_buffer.size);
773 ni->c.x=lon*6371000.0*M_PI/180;
774 ni->c.y=log(tan(M_PI_4+lat*M_PI/360))*6371000.0;
775 node_buffer.size+=sizeof(struct node_item);
777 if (ni->id > nodeid_last) {
780 fprintf(stderr,"INFO: Nodes out of sequence (new %d vs old %d), adding hash\n", ni->id, nodeid_last);
781 node_hash=g_hash_table_new(NULL, NULL);
782 node_buffer_to_hash();
785 if (!g_hash_table_lookup(node_hash, (gpointer)(ni->id)))
786 g_hash_table_insert(node_hash, (gpointer)(ni->id), (gpointer)(ni-(struct node_item *)node_buffer.base));
788 node_buffer.size-=sizeof(struct node_item);
797 char id_buffer[BUFFER_SIZE];
798 char lat_buffer[BUFFER_SIZE];
799 char lon_buffer[BUFFER_SIZE];
800 if (!xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
802 if (!xml_get_attribute(p, "lat", lat_buffer, BUFFER_SIZE))
804 if (!xml_get_attribute(p, "lon", lon_buffer, BUFFER_SIZE))
806 add_node(atoi(id_buffer), atof(lat_buffer), atof(lon_buffer));
811 static struct node_item *
812 node_item_get(int id)
814 struct node_item *ni=(struct node_item *)(node_buffer.base);
815 int count=node_buffer.size/sizeof(struct node_item);
816 int interval=count/4;
820 i=(int)(g_hash_table_lookup(node_hash, (gpointer)id));
823 while (ni[p].id != id) {
825 fprintf(stderr,"p=%d count=%d interval=%d id=%d ni[p].id=%d\n", p, count, interval, id, ni[p].id);
860 struct node_item *ni=node_item_get(id);
862 fprintf(stderr,"WARNING: node id %d not found\n", id);
874 item.type=type_street_unkn;
876 street_name_attr.len=0;
877 street_name_systematic_attr.len=0;
880 sprintf(debug_attr_buffer,"wayid=%d", wayid);
886 char id_buffer[BUFFER_SIZE];
887 if (!xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
889 add_way(atoi(id_buffer));
894 parse_relation(char *p)
896 debug_attr_buffer[0]='\0';
901 write_attr(FILE *out, struct attr_bin *attr, void *buffer)
904 fwrite(attr, sizeof(*attr), 1, out);
905 fwrite(buffer, (attr->len-1)*4, 1, out);
910 attr_longest_match(struct attr_mapping **mapping, int mapping_count, enum item_type *types, int types_count)
912 int i,j,longest=0,ret=0,sum,val;
913 struct attr_mapping *curr;
914 for (i = 0 ; i < mapping_count ; i++) {
917 for (j = 0 ; j < curr->attr_present_idx_count ; j++) {
918 val=attr_present[curr->attr_present_idx[j]];
930 if (sum > 0 && sum == longest && ret < types_count)
931 types[ret++]=curr->type;
933 memset(attr_present, 0, sizeof(*attr_present)*attr_present_count);
941 enum item_type types[5];
945 if (dedupe_ways_hash) {
946 if (g_hash_table_lookup(dedupe_ways_hash, (gpointer)wayid))
948 g_hash_table_insert(dedupe_ways_hash, (gpointer)wayid, (gpointer)1);
950 count=attr_longest_match(attr_mapping_way, attr_mapping_way_count, types, sizeof(types)/sizeof(enum item_type));
951 pad_text_attr(&debug_attr, debug_attr_buffer);
953 alen+=label_attr.len+1;
954 if (street_name_systematic_attr.len)
955 alen+=street_name_systematic_attr.len+1;
957 alen+=debug_attr.len+1;
959 alen+=flags_attr.len+1;
963 item.type=type_street_unkn;
964 if (coverage && IS_STREET(item))
965 item.type=type_coverage;
966 item.clen=coord_count*2;
967 item.len=item.clen+2+alen;
968 fwrite(&item, sizeof(item), 1, out);
969 fwrite(coord_buffer, coord_count*sizeof(struct coord), 1, out);
970 if (IS_STREET(item)) {
971 street_name_attr.len=label_attr.len;
972 write_attr(out, &street_name_attr, label_attr_buffer);
974 write_attr(out, &label_attr, label_attr_buffer);
975 write_attr(out, &street_name_systematic_attr, street_name_systematic_attr_buffer);
976 write_attr(out, &debug_attr, debug_attr_buffer);
977 write_attr(out, &flags_attr, &flags_attr_value);
983 int alen=0,conflict=0,count;
984 enum item_type types[5];
985 struct country_table *result=NULL, *lookup;
986 if (!out || ! node_is_tagged || ! nodeid)
988 count=attr_longest_match(attr_mapping_node, attr_mapping_node_count, types, sizeof(types)/sizeof(enum item_type));
989 pad_text_attr(&debug_attr, debug_attr_buffer);
991 alen+=label_attr.len+1;
993 alen+=debug_attr.len+1;
997 item.type=type_point_unkn;
999 item.len=item.clen+2+alen;
1000 fwrite(&item, sizeof(item), 1, out);
1001 fwrite(&ni->c, 1*sizeof(struct coord), 1, out);
1002 if (IS_TOWN(item)) {
1003 town_name_attr.len=label_attr.len;
1004 write_attr(out, &town_name_attr, label_attr_buffer);
1006 write_attr(out, &label_attr, label_attr_buffer);
1007 write_attr(out, &debug_attr, debug_attr_buffer);
1008 #ifdef GENERATE_INDEX
1009 if (IS_TOWN(item) && town_name_attr.len) {
1010 char *tok,*buf=is_in_buffer;
1011 while ((tok=strtok(buf, ","))) {
1014 lookup=g_hash_table_lookup(country_table_hash,tok);
1016 if (result && result->countryid != lookup->countryid) {
1017 fprintf(stderr,"conflict for %s %s country %d vs %d\n", label_attr_buffer, debug_attr_buffer, lookup->countryid, result->countryid);
1024 if (result && !conflict) {
1025 if (!result->file) {
1026 char *name=g_strdup_printf("country_%d.bin.unsorted", result->countryid);
1027 result->file=fopen(name,"w");
1032 item.len=item.clen+2+label_attr.len+1;
1033 fwrite(&item, sizeof(item), 1, result->file);
1034 fwrite(&ni->c, 1*sizeof(struct coord), 1, result->file);
1035 write_attr(result->file, &town_name_attr, label_attr_buffer);
1037 result->size+=(item.clen+3+label_attr.len+1)*4;
1043 processed_nodes_out++;
1047 sort_countries_compare(const void *p1, const void *p2)
1049 struct item_bin *ib1=*((struct item_bin **)p1),*ib2=*((struct item_bin **)p2);
1050 struct attr_bin *attr1,*attr2;
1052 assert(ib1->clen==2);
1053 assert(ib2->clen==2);
1054 attr1=(struct attr_bin *)((int *)(ib1+1)+ib1->clen);
1055 attr2=(struct attr_bin *)((int *)(ib2+1)+ib1->clen);
1056 assert(attr1->type == attr_town_name);
1057 assert(attr2->type == attr_town_name);
1058 s1=(char *)(attr1+1);
1059 s2=(char *)(attr2+1);
1060 return strcmp(s1, s2);
1062 fprintf(stderr,"sort_countries_compare p1=%p p2=%p %s %s\n",p1,p2,s1,s2);
1067 #ifdef GENERATE_INDEX
1069 sort_countries(void)
1072 struct country_table *co;
1074 struct item_bin *ib;
1076 char *p,*buffer,**idx,*name;
1077 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
1078 co=&country_table[i];
1084 buffer=malloc(co->size);
1085 assert(buffer != NULL);
1086 idx=malloc(co->count*sizeof(void *));
1087 assert(idx != NULL);
1088 name=g_strdup_printf("country_%d.bin.unsorted", co->countryid);
1091 fread(buffer, co->size, 1, f);
1096 for (j = 0 ; j < co->count ; j++) {
1098 p+=(*((int *)p)+1)*4;
1100 qsort(idx, co->count, sizeof(void *), sort_countries_compare);
1101 name=g_strdup_printf("country_%d.bin", co->countryid);
1103 for (j = 0 ; j < co->count ; j++) {
1104 ib=(struct item_bin *)(idx[j]);
1105 c=(struct coord *)(ib+1);
1106 fwrite(ib, (ib->len+1)*4, 1, f);
1108 bbox_extend(c, &co->r);
1120 add_nd(char *p, int ref)
1123 struct node_item *ni;
1124 ni=node_item_get(ref);
1127 coord_buffer[coord_count++]=ni->c;
1129 SET_REF(coord_buffer[coord_count], ref);
1135 if (len > 0 && p[len-1]=='\n')
1137 fprintf(stderr,"WARNING: way %d: node %d not found (%s)\n",wayid,ref,p);
1139 if (coord_count > 65536) {
1140 fprintf(stderr,"ERROR: Overflow\n");
1148 char ref_buffer[BUFFER_SIZE];
1149 if (!xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
1151 add_nd(p, atoi(ref_buffer));
1157 save_buffer(char *filename, struct buffer *b)
1160 f=fopen(filename,"wb+");
1161 fwrite(b->base, b->size, 1, f);
1166 load_buffer(char *filename, struct buffer *b)
1172 f=fopen(filename,"rb");
1173 fseek(f, 0, SEEK_END);
1174 b->size=b->malloced=ftell(f);
1175 fprintf(stderr,"reading %d bytes from %s\n", b->size, filename);
1176 fseek(f, 0, SEEK_SET);
1177 b->base=malloc(b->size);
1178 assert(b->base != NULL);
1179 fread(b->base, b->size, 1, f);
1184 phase1(FILE *in, FILE *out_ways, FILE *out_nodes)
1190 while (fgets(buffer, size, in)) {
1191 p=strchr(buffer,'<');
1193 fprintf(stderr,"WARNING: wrong line %s\n", buffer);
1196 if (!strncmp(p, "<?xml ",6)) {
1197 } else if (!strncmp(p, "<osm ",5)) {
1198 } else if (!strncmp(p, "<bound ",7)) {
1199 } else if (!strncmp(p, "<node ",6)) {
1201 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1204 } else if (!strncmp(p, "<tag ",5)) {
1206 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1207 } else if (!strncmp(p, "<way ",5)) {
1210 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1212 } else if (!strncmp(p, "<nd ",4)) {
1214 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1215 } else if (!strncmp(p, "<relation ",10)) {
1217 if (!parse_relation(p))
1218 fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
1219 processed_relations++;
1220 } else if (!strncmp(p, "<member ",8)) {
1221 } else if (!strncmp(p, "</node>",7)) {
1223 end_node(out_nodes);
1224 } else if (!strncmp(p, "</way>",6)) {
1227 } else if (!strncmp(p, "</relation>",11)) {
1229 } else if (!strncmp(p, "</osm>",6)) {
1231 fprintf(stderr,"WARNING: unknown tag in %s\n", buffer);
1239 static char buffer[150000];
1243 static struct item_bin *
1246 struct item_bin *ib=(struct item_bin *) buffer;
1248 r=fread(ib, sizeof(*ib), 1, in);
1252 assert((ib->len+1) < sizeof(buffer));
1253 s=(ib->len+1)*4-sizeof(*ib);
1254 r=fread(ib+1, s, 1, in);
1262 bbox_extend(struct coord *c, struct rect *r)
1275 bbox(struct coord *c, int count, struct rect *r)
1288 contains_bbox(int xl, int yl, int xh, int yh, struct rect *r)
1290 if (r->h.x < xl || r->h.x > xh) {
1293 if (r->l.x > xh || r->l.x < xl) {
1296 if (r->h.y < yl || r->h.y > yh) {
1299 if (r->l.y > yh || r->l.y < yl) {
1305 struct rect world_bbox = {
1306 { -20000000, -20000000},
1307 { 20000000, 20000000},
1311 tile(struct rect *r, char *ret, int max)
1320 for (i = 0 ; i < max ; i++) {
1327 if ( contains_bbox(x0,y0,x2,y2,r)) {
1331 } else if (contains_bbox(x2,y0,x4,y2,r)) {
1335 } else if (contains_bbox(x0,y2,x2,y4,r)) {
1339 } else if (contains_bbox(x2,y2,x4,y4,r)) {
1349 tile_bbox(char *tile, struct rect *r)
1354 c.x=(r->l.x+r->h.x)/2;
1355 c.y=(r->l.y+r->h.y)/2;
1378 GHashTable *tile_hash;
1379 GHashTable *tile_hash2;
1382 tile_extend(char *tile, struct item_bin *ib, GList **tiles_list)
1384 struct tile_head *th=NULL;
1385 if (debug_tile(tile))
1386 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);
1388 th=g_hash_table_lookup(tile_hash2, tile);
1390 th=g_hash_table_lookup(tile_hash, tile);
1392 th=malloc(sizeof(struct tile_head)+ sizeof( char* ) );
1394 // strcpy(th->subtiles, tile);
1397 th->total_size_used=0;
1400 th->name=string_hash_lookup(tile);
1401 *th_get_subtile( th, 0 ) = th->name;
1404 g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
1406 *tiles_list=g_list_append(*tiles_list, string_hash_lookup( th->name ) );
1408 if (debug_tile(tile))
1409 fprintf(stderr,"new '%s'\n", tile);
1411 th->total_size+=ib->len*4+4;
1412 if (debug_tile(tile))
1413 fprintf(stderr,"New total size of %s(%p):%d\n", th->name, th, th->total_size);
1414 g_hash_table_insert(tile_hash, string_hash_lookup( th->name ), th);
1418 tile_data_size(char *tile)
1420 struct tile_head *th;
1421 th=g_hash_table_lookup(tile_hash, tile);
1424 return th->total_size;
1428 merge_tile(char *base, char *sub)
1430 struct tile_head *thb, *ths;
1431 thb=g_hash_table_lookup(tile_hash, base);
1432 ths=g_hash_table_lookup(tile_hash, sub);
1435 if (debug_tile(base) || debug_tile(sub))
1436 fprintf(stderr,"merging '%s'(%p) (%d) with '%s'(%p) (%d)\n", base, thb, thb ? thb->total_size : 0, sub, ths, ths->total_size);
1439 g_hash_table_remove(tile_hash, sub);
1440 thb->name=string_hash_lookup(base);
1441 g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
1444 thb=realloc(thb, sizeof(struct tile_head)+( ths->num_subtiles+thb->num_subtiles ) * sizeof( char*) );
1445 assert(thb != NULL);
1446 memcpy( th_get_subtile( thb, thb->num_subtiles ), th_get_subtile( ths, 0 ), ths->num_subtiles * sizeof( char*) );
1447 thb->num_subtiles+=ths->num_subtiles;
1448 thb->total_size+=ths->total_size;
1449 g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
1450 g_hash_table_remove(tile_hash, sub);
1458 get_tiles_list_func(char *key, struct tile_head *th, GList **list)
1460 *list=g_list_prepend(*list, key);
1464 get_tiles_list(void)
1467 g_hash_table_foreach(tile_hash, (GHFunc)get_tiles_list_func, &ret);
1473 write_tile(char *key, struct tile_head *th, gpointer dummy)
1477 fprintf(stderr,"DEBUG: Writing %s\n", key);
1478 strcpy(buffer,"tiles/");
1481 strcat(buffer,".bin");
1483 f=fopen(buffer, "wb+");
1485 fwrite(th->data, th->size, 1, f);
1493 write_item(char *tile, struct item_bin *ib)
1495 struct tile_head *th;
1498 th=g_hash_table_lookup(tile_hash2, tile);
1500 th=g_hash_table_lookup(tile_hash, tile);
1502 if (th->process != 0 && th->process != 1) {
1503 fprintf(stderr,"error with tile '%s' of length %d\n", tile, strlen(tile));
1508 if (debug_tile(tile))
1509 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);
1511 if (th->total_size_used+size > th->total_size) {
1512 fprintf(stderr,"Overflow in tile %s (used %d max %d item %d)\n", tile, th->total_size_used, th->total_size, size);
1516 memcpy(th->zip_data+th->total_size_used, ib, size);
1517 th->total_size_used+=size;
1519 fprintf(stderr,"no tile hash found for %s\n", tile);
1526 write_item_part(FILE *out, struct item_bin *orig, int first, int last)
1528 struct item_bin new;
1529 struct coord *c=(struct coord *)(orig+1);
1530 char *attr=(char *)(c+orig->clen/2);
1531 int attr_len=orig->len-orig->clen-2;
1533 new.type=orig->type;
1534 new.clen=(last-first+1)*2;
1535 new.len=new.clen+attr_len+2;
1537 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);
1539 fwrite(&new, sizeof(new), 1, out);
1540 fwrite(c+first, new.clen*4, 1, out);
1541 fwrite(attr, attr_len*4, 1, out);
1545 phase2(FILE *in, FILE *out)
1548 int i,ccount,last,ndref;
1549 struct item_bin *ib;
1550 struct node_item *ni;
1552 processed_nodes=processed_nodes_out=processed_ways=processed_relations=processed_tiles=0;
1554 while ((ib=read_item(in))) {
1556 fprintf(stderr,"type 0x%x len %d clen %d\n", ib->type, ib->len, ib->clen);
1559 c=(struct coord *)(ib+1);
1561 for (i = 0 ; i < ccount ; i++) {
1564 ni=node_item_get(ndref);
1566 fprintf(stderr,"ni=%p\n", ni);
1569 if (ni->ref_way > 1 && i != 0 && i != ccount-1 && IS_STREET(*ib)) {
1570 write_item_part(out, ib, last, i);
1575 write_item_part(out, ib, last, ccount-1);
1583 phase34_process_file(int phase, FILE *in)
1585 struct item_bin *ib;
1590 while ((ib=read_item(in))) {
1591 if (ib->type < 0x80000000)
1595 bbox((struct coord *)(ib+1), ib->clen/2, &r);
1598 if (ib->type == type_street_n_lanes || ib->type == type_highway_city || ib->type == type_highway_land || ib->type == type_ramp)
1600 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)
1603 tile(&r, buffer, max);
1605 fprintf(stderr,"%s\n", buffer);
1608 tile_extend(buffer, ib, NULL);
1610 write_item(buffer, ib);
1615 struct item_bin item;
1617 struct attr_bin attr_order_limit;
1620 struct attr_bin attr_zipfile_ref;
1625 index_submap_add(int phase, struct tile_head *th, GList **tiles_list)
1627 struct index_item ii;
1628 int len=strlen(th->name);
1629 char index_tile[len+1];
1631 ii.min=(len > 4) ? len-4 : 0;
1633 strcpy(index_tile, th->name);
1639 tile_bbox(th->name, &ii.r);
1641 ii.item.len=sizeof(ii)/4-1;
1642 ii.item.type=type_submap;
1645 ii.attr_order_limit.len=2;
1646 ii.attr_order_limit.type=attr_order_limit;
1648 ii.attr_zipfile_ref.len=2;
1649 ii.attr_zipfile_ref.type=attr_zipfile_ref;
1650 ii.zipfile_ref=th->zipnum;
1653 tile_extend(index_tile, (struct item_bin *)&ii, tiles_list);
1655 write_item(index_tile, (struct item_bin *)&ii);
1657 unsigned int *c=(unsigned int *)ⅈ
1659 for (i = 0 ; i < sizeof(ii)/4 ; i++) {
1660 fprintf(stderr,"%08x ", c[i]);
1662 fprintf(stderr,"\n");
1667 add_tile_hash(struct tile_head *th)
1669 int idx,len,maxnamelen=0;
1673 g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
1675 for( idx = 0; idx < th->num_subtiles; idx++ ) {
1677 data = th_get_subtile( th, idx );
1679 if (debug_tile(data) || debug_tile(th->name)) {
1680 fprintf(stderr,"Parent for '%s' is '%s'\n", *data, th->name);
1683 g_hash_table_insert(tile_hash2, *data, th);
1685 len = strlen( *data );
1687 if (len > maxnamelen) {
1696 create_tile_hash(void)
1698 struct tile_head *th;
1699 int len,maxnamelen=0;
1701 tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
1704 len=add_tile_hash(th);
1705 if (len > maxnamelen)
1713 create_tile_hash_list(GList *list)
1716 struct tile_head *th;
1718 tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
1720 fprintf(stderr,"list=%p\n", list);
1721 next=g_list_first(list);
1723 th=g_hash_table_lookup(tile_hash, next->data);
1725 fprintf(stderr,"No tile found for '%s'\n", (char *)(next->data));
1728 next=g_list_next(next);
1733 destroy_tile_hash(void)
1735 g_hash_table_destroy(tile_hash2);
1741 static void write_countrydir(int phase, int maxnamelen);
1744 write_tilesdir(int phase, int maxlen, FILE *out)
1747 GList *tiles_list,*next;
1749 struct tile_head *th,**last=NULL;
1752 tiles_list=get_tiles_list();
1754 create_tile_hash_list(tiles_list);
1755 next=g_list_first(tiles_list);
1756 last=&tile_head_root;
1759 if (strlen(next->data) > maxlen)
1760 maxlen=strlen(next->data);
1761 next=g_list_next(next);
1767 fprintf(stderr,"PROGRESS: collecting tiles with len=%d\n", len);
1769 #ifdef GENERATE_INDEX
1771 write_countrydir(phase, maxlen);
1773 next=g_list_first(tiles_list);
1775 if (strlen(next->data) == len) {
1776 th=g_hash_table_lookup(tile_hash, next->data);
1782 fprintf(out,"%s:%d",(char *)next->data,th->total_size);
1784 for ( idx = 0; idx< th->num_subtiles; idx++ ){
1785 data= th_get_subtile( th, idx );
1786 fprintf(out,":%s", *data);
1792 index_submap_add(phase, th, &tiles_list);
1796 next=g_list_next(next);
1805 struct tile_head *th;
1806 char basetile[1024];
1808 GList *tiles_list_sorted,*last;
1809 int i,i_min,len,size_all,size[5],size_min,work_done;
1813 tiles_list_sorted=get_tiles_list();
1814 fprintf(stderr,"PROGRESS: sorting %d tiles\n", g_list_length(tiles_list_sorted));
1815 tiles_list_sorted=g_list_sort(tiles_list_sorted, (GCompareFunc)strcmp);
1816 fprintf(stderr,"PROGRESS: sorting %d tiles done\n", g_list_length(tiles_list_sorted));
1817 last=g_list_last(tiles_list_sorted);
1820 th=g_hash_table_lookup(tile_hash, last->data);
1821 zip_size+=th->total_size;
1822 last=g_list_previous(last);
1824 fprintf(stderr,"DEBUG: size=%Ld\n", zip_size);
1825 last=g_list_last(tiles_list_sorted);
1829 len=strlen(last->data);
1831 strcpy(basetile,last->data);
1832 basetile[len-1]='\0';
1833 strcpy(subtile,last->data);
1834 for (i = 0 ; i < 4 ; i++) {
1835 subtile[len-1]='a'+i;
1836 size[i]=tile_data_size(subtile);
1838 size[4]=tile_data_size(basetile);
1839 size_all=size[0]+size[1]+size[2]+size[3]+size[4];
1840 if (size_all < 65536 && size_all > 0 && size_all != size[4]) {
1841 for (i = 0 ; i < 4 ; i++) {
1842 subtile[len-1]='a'+i;
1843 work_done+=merge_tile(basetile, subtile);
1849 for (i = 0 ; i < 4 ; i++) {
1850 if (size[i] && size[i] < size_min) {
1857 if (size[4]+size_min >= 65536)
1859 subtile[len-1]='a'+i_min;
1860 work_done+=merge_tile(basetile, subtile);
1861 size[4]+=size[i_min];
1866 last=g_list_previous(last);
1868 g_list_free(tiles_list_sorted);
1869 fprintf(stderr,"PROGRESS: merged %d tiles\n", work_done);
1870 } while (work_done);
1873 struct country_index_item {
1874 struct item_bin item;
1875 struct attr_bin attr_country_id;
1877 struct attr_bin attr_zipfile_ref;
1882 index_country_add(int phase, int country_id, int zipnum)
1884 struct country_index_item ii;
1885 char *index_tile="";
1887 ii.item.len=sizeof(ii)/4-1;
1888 ii.item.type=type_countryindex;
1891 ii.attr_country_id.len=2;
1892 ii.attr_country_id.type=attr_country_id;
1893 ii.country_id=country_id;
1895 ii.attr_zipfile_ref.len=2;
1896 ii.attr_zipfile_ref.type=attr_zipfile_ref;
1897 ii.zipfile_ref=zipnum;
1900 tile_extend(index_tile, (struct item_bin *)&ii, NULL);
1902 write_item(index_tile, (struct item_bin *)&ii);
1905 #ifdef GENERATE_INDEX
1913 add_aux_tile(int phase, char *name, char *filename, int size)
1915 struct aux_tile *at;
1917 at=g_new0(struct aux_tile, 1);
1918 at->name=g_strdup(name);
1919 at->filename=g_strdup(filename);
1921 aux_tile_list=g_list_append(aux_tile_list, at);
1927 write_aux_tiles(FILE *out, FILE *dir_out, int compression_level, int namelen)
1929 GList *l=aux_tile_list;
1930 struct aux_tile *at;
1937 buffer=malloc(at->size);
1938 assert(buffer != NULL);
1939 f=fopen(at->filename,"r");
1941 fread(buffer, at->size, 1, f);
1943 write_zipmember(out, dir_out, at->name, namelen, buffer, at->size, compression_level);
1951 write_countrydir(int phase, int maxnamelen)
1956 char searchtile[32];
1958 struct country_table *co;
1959 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
1960 co=&country_table[i];
1963 tile(&co->r, tilename, max);
1964 sprintf(searchtile,"%ss%d", tilename, 0);
1965 sprintf(filename,"country_%d.bin", co->countryid);
1966 zipnum=add_aux_tile(phase, searchtile, filename, co->size);
1967 index_country_add(phase,co->countryid,zipnum);
1973 remove_countryfiles(void)
1977 struct country_table *co;
1979 for (i = 0 ; i < sizeof(country_table)/sizeof(struct country_table) ; i++) {
1980 co=&country_table[i];
1982 sprintf(filename,"country_%d.bin", co->countryid);
1990 phase34(int phase, int maxnamelen, FILE *ways_in, FILE *nodes_in, FILE *tilesdir_out)
1993 processed_nodes=processed_nodes_out=processed_ways=processed_relations=processed_tiles=0;
1997 tile_hash=g_hash_table_new(g_str_hash, g_str_equal);
1999 phase34_process_file(phase, ways_in);
2001 phase34_process_file(phase, nodes_in);
2002 fprintf(stderr,"read %d bytes\n", bytes_read);
2007 write_tilesdir(phase, maxnamelen, tilesdir_out);
2014 phase3(FILE *ways_in, FILE *nodes_in, FILE *tilesdir_out)
2016 return phase34(3, 0, ways_in, nodes_in, tilesdir_out);
2019 static long long zipoffset;
2020 static int zipdir_size;
2023 compress2_int(Byte *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
2028 stream.next_in = (Bytef*)source;
2029 stream.avail_in = (uInt)sourceLen;
2030 stream.next_out = dest;
2031 stream.avail_out = (uInt)*destLen;
2032 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
2034 stream.zalloc = (alloc_func)0;
2035 stream.zfree = (free_func)0;
2036 stream.opaque = (voidpf)0;
2038 err = deflateInit2(&stream, level, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
2039 if (err != Z_OK) return err;
2041 err = deflate(&stream, Z_FINISH);
2042 if (err != Z_STREAM_END) {
2043 deflateEnd(&stream);
2044 return err == Z_OK ? Z_BUF_ERROR : err;
2046 *destLen = stream.total_out;
2048 err = deflateEnd(&stream);
2053 write_zipmember(FILE *out, FILE *dir_out, char *name, int filelen, char *data, int data_size, int compression_level)
2055 struct zip_lfh lfh = {
2068 struct zip_cd cd = {
2089 char filename[filelen+1];
2090 int error,crc,len,comp_size=data_size;
2091 uLongf destlen=data_size+data_size/500+12;
2092 char compbuffer[destlen];
2094 crc=crc32(0, NULL, 0);
2095 crc=crc32(crc, (unsigned char *)data, data_size);
2096 if (compression_level) {
2097 error=compress2_int((Byte *)compbuffer, &destlen, (Bytef *)data, data_size, compression_level);
2098 if (error == Z_OK) {
2099 if (destlen < data_size) {
2104 fprintf(stderr,"compress2 returned %d\n", error);
2108 lfh.zipsize=comp_size;
2109 lfh.zipuncmp=data_size;
2110 lfh.zipmthd=compression_level ? 8:0;
2112 cd.zipcsiz=comp_size;
2113 cd.zipcunc=data_size;
2114 cd.zipcmthd=compression_level ? 8:0;
2115 strcpy(filename, name);
2116 len=strlen(filename);
2117 while (len < filelen) {
2118 filename[len++]='_';
2120 filename[filelen]='\0';
2121 fwrite(&lfh, sizeof(lfh), 1, out);
2122 fwrite(filename, filelen, 1, out);
2123 fwrite(data, comp_size, 1, out);
2124 zipoffset+=sizeof(lfh)+filelen+comp_size;
2125 fwrite(&cd, sizeof(cd), 1, dir_out);
2126 fwrite(filename, filelen, 1, dir_out);
2127 zipdir_size+=sizeof(cd)+filelen;
2131 process_slice(FILE *ways_in, FILE *nodes_in, int size, int maxnamelen, FILE *out, FILE *dir_out, int compression_level)
2133 struct tile_head *th;
2134 char *slice_data,*zip_data;
2137 slice_data=malloc(size);
2138 assert(slice_data != NULL);
2139 zip_data=slice_data;
2143 th->zip_data=zip_data;
2144 zip_data+=th->total_size;
2149 fseek(ways_in, 0, SEEK_SET);
2151 fseek(nodes_in, 0, SEEK_SET);
2152 phase34(4, maxnamelen, ways_in, nodes_in, NULL);
2157 #ifdef GENERATE_INDEX
2158 if (! strlen(th->name))
2159 zipfiles+=write_aux_tiles(out, dir_out, compression_level, maxnamelen);
2161 if (th->total_size != th->total_size_used) {
2162 fprintf(stderr,"Size error '%s': %d vs %d\n", th->name, th->total_size, th->total_size_used);
2165 if (strlen(th->name))
2166 write_zipmember(out, dir_out, th->name, maxnamelen, th->zip_data, th->total_size, compression_level);
2168 write_zipmember(out, dir_out, "index", sizeof("index")-1, th->zip_data, th->total_size, compression_level);
2181 cat(FILE *in, FILE *out)
2185 while ((size=fread(buffer, 1, 4096, in)))
2186 fwrite(buffer, 1, size, out);
2190 phase4(FILE *ways_in, FILE *nodes_in, FILE *out, FILE *dir_out, int compression_level)
2192 int slice_size=1024*1024*1024;
2193 int maxnamelen,size,slices;
2195 struct tile_head *th,*th2;
2196 struct zip_eoc eoc = {
2207 maxnamelen=create_tile_hash();
2212 fprintf(stderr, "Maximum slice size %d\n", slice_size);
2214 if (size + th->total_size > slice_size) {
2215 fprintf(stderr,"Slice %d is of size %d\n", slices, size);
2219 size+=th->total_size;
2223 fprintf(stderr,"Slice %d is of size %d\n", slices, size);
2234 while (th && size+th->total_size < slice_size) {
2235 size+=th->total_size;
2239 zipfiles+=process_slice(ways_in, nodes_in, size, maxnamelen, out, dir_out, compression_level);
2242 fseek(dir_out, 0, SEEK_SET);
2244 eoc.zipenum=zipfiles;
2245 eoc.zipecenn=zipfiles;
2246 eoc.zipecsz=zipdir_size;
2247 eoc.zipeofst=zipoffset;
2248 fwrite(&eoc, sizeof(eoc), 1, out);
2258 fprintf(f,"osm2navit - parse osm textfile and converts to NavIt binfile format\n\n");
2259 fprintf(f,"Usage :\n");
2260 fprintf(f,"bzcat planet.osm.bz2 | osm2navit mymap.bin\n");
2261 fprintf(f,"Available switches:\n");
2262 fprintf(f,"-h (--help) : this screen\n");
2263 fprintf(f,"-N (--nodes-only) : process only nodes\n");
2264 fprintf(f,"-W (--ways-only) : process only ways\n");
2265 fprintf(f,"-a (--attr-debug-level) : control which data is included in the debug attribute\n");
2266 fprintf(f,"-c (--dump-coordinates) : dump coordinates after phase 1\n");
2267 fprintf(f,"-e (--end) : end at specified phase\n");
2268 fprintf(f,"-k (--keep-tmpfiles) : do not delete tmp files after processing. useful to reuse them\n\n");
2269 fprintf(f,"-o (--coverage) : map every street to item overage\n");
2270 fprintf(f,"-s (--start) : start at specified phase\n");
2271 fprintf(f,"-i (--input-file) : specify the input file name (OSM), overrules default stdin\n");
2272 fprintf(f,"-w (--dedupe-ways) : ensure no duplicate ways or nodes. useful when using several input files\n");
2273 fprintf(f,"-z (--compression-level) : set the compression level\n");
2277 int main(int argc, char **argv)
2279 FILE *ways=NULL,*ways_split=NULL,*nodes=NULL,*tilesdir,*zipdir,*res;
2280 char *map=g_strdup(attrmap);
2281 int c,start=1,end=4,dump_coordinates=0;
2282 int keep_tmpfiles=0;
2283 int process_nodes=1, process_ways=1;
2284 int compression_level=9;
2286 FILE* input_file = stdin;
2291 int this_option_optind = optind ? optind : 1;
2293 int option_index = 0;
2294 static struct option long_options[] = {
2295 {"attr-debug-level", 1, 0, 'a'},
2296 {"compression-level", 1, 0, 'z'},
2297 {"coverage", 0, 0, 'o'},
2298 {"dedupe-ways", 0, 0, 'w'},
2300 {"help", 0, 0, 'h'},
2301 {"keep-tmpfiles", 0, 0, 'k'},
2302 {"nodes-only", 0, 0, 'N'},
2303 {"start", 1, 0, 's'},
2304 {"input-file", 1, 0, 'i'},
2305 {"ignore-unknown", 0, 0, 'n'},
2306 {"ways-only", 0, 0, 'W'},
2309 c = getopt_long (argc, argv, "Nni:Wa:ce:hks:w", long_options, &option_index);
2320 attr_debug_level=atoi(optarg);
2332 fprintf(stderr,"I will IGNORE unknown types\n");
2336 fprintf(stderr,"I will KEEP tmp files\n");
2346 dedupe_ways_hash=g_hash_table_new(NULL, NULL);
2349 input_file = fopen( optarg, "r" );
2350 if ( input_file == NULL )
2352 fprintf( stderr, "\nInput file (%s) not found\n", optarg );
2357 compression_level=atoi(optarg);
2363 fprintf(stderr,"c=%d\n", c);
2367 if (optind != argc-1)
2369 result=argv[optind];
2371 #ifdef GENERATE_INDEX
2372 build_countrytable();
2378 ways=fopen("ways.tmp","wb+");
2380 nodes=fopen("nodes.tmp","wb+");
2382 fprintf(stderr,"PROGRESS: Phase 1: collecting data\n");
2383 phase1(input_file,ways,nodes);
2388 #ifdef GENERATE_INDEX
2389 fprintf(stderr,"PROGRESS: Phase 1: sorting countries\n");
2393 if (end == 1 || dump_coordinates)
2394 save_buffer("coords.tmp",&node_buffer);
2398 load_buffer("coords.tmp",&node_buffer);
2401 ways=fopen("ways.tmp","rb");
2402 ways_split=fopen("ways_split.tmp","wb+");
2404 fprintf(stderr,"PROGRESS: Phase 2: finding intersections\n");
2405 phase2(ways,ways_split);
2411 fprintf(stderr,"PROGRESS: Skipping Phase 2\n");
2413 free(node_buffer.base);
2414 node_buffer.base=NULL;
2415 node_buffer.malloced=0;
2421 fprintf(stderr,"PROGRESS: Phase 3: generating tiles\n");
2423 ways_split=fopen("ways_split.tmp","rb");
2425 nodes=fopen("nodes.tmp","rb");
2426 tilesdir=fopen("tilesdir.tmp","wb+");
2427 phase3(ways_split,nodes,tilesdir);
2438 fprintf(stderr,"PROGRESS: Phase 4: assembling map\n");
2440 ways_split=fopen("ways_split.tmp","rb");
2442 nodes=fopen("nodes.tmp","rb");
2443 res=fopen(result,"wb+");
2444 zipdir=fopen("zipdir.tmp","wb+");
2445 phase4(ways_split,nodes,res,zipdir,compression_level);
2452 if(!keep_tmpfiles) {
2453 remove("nodes.tmp");
2454 remove("ways_split.tmp");
2455 remove("tilesdir.tmp");
2456 remove("zipdir.tmp");
2457 #ifdef GENERATE_INDEX
2458 remove_countryfiles();