Ticket #859: maptool-line-poly-2poi.diff
| File maptool-line-poly-2poi.diff, 9.1 KB (added by https://wiki.navit-project.org/index.php/user:tryagain, 10 months ago) |
|---|
-
navit/maptool/osm.c
1540 1540 int *def_flags,add_flags; 1541 1541 enum item_type types[10]; 1542 1542 struct item_bin *item_bin; 1543 int count_lines=0, count_areas=0; 1543 1544 1544 1545 in_way=0; 1545 1546 … … 1567 1568 continue; 1568 1569 if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn)) 1569 1570 continue; 1570 if(!osm->way2poi && types[i]<type_line) 1571 continue; 1571 if(types[i]<type_area) 1572 count_lines++; 1573 else 1574 count_areas++; 1572 1575 item_bin=init_item(types[i]); 1573 1576 item_bin_add_coord(item_bin, coord_buffer, coord_count); 1574 1577 nodes_ref_item_bin(item_bin); … … 1589 1592 item_bin_add_attr_int(item_bin, attr_maxspeed, maxspeed_attr_value); 1590 1593 item_bin_write(item_bin,osm->ways); 1591 1594 } 1592 if(osm-> way2poi) {1595 if(osm->line2poi) { 1593 1596 count=attr_longest_match(attr_mapping_way2poi, attr_mapping_way2poi_count, types, sizeof(types)/sizeof(enum item_type)); 1594 1597 dbg_assert(count < 10); 1595 1598 for (i = 0 ; i < count ; i++) { … … 1606 1609 item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]); 1607 1610 item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]); 1608 1611 item_bin_add_attr_longlong(item_bin, attr_osm_wayid, osmid_attr_value); 1609 1610 item_bin_write(item_bin,osm->way2poi); 1612 item_bin_write(item_bin, count_areas<count_lines?osm->line2poi:osm->poly2poi); 1611 1613 } 1612 1614 } 1613 1615 attr_longest_match_clear(); … … 2133 2135 } 2134 2136 } 2135 2137 2138 FILE * 2139 resolve_ways_file(FILE *in, char *suffix, char *filename) 2140 { 2141 char *newfilename=g_strdup_printf("%s_new",filename); 2142 FILE *new=tempfile(suffix,newfilename,1); 2143 resolve_ways(in, new); 2144 fclose(in); 2145 fclose(new); 2146 tempfile_rename(suffix,newfilename,filename); 2147 g_free(newfilename); 2148 return tempfile(suffix,filename,0); 2149 } 2150 2151 /** 2152 * Get POI coordinates from area/line coordinates. 2153 * @param in *in input file with area/line coordinates. 2154 * @param in *out output file with POI coordinates 2155 * @param in type input file original contents type: type_line or type_area 2156 * @returns nothing 2157 */ 2136 2158 void 2137 process_way2poi(FILE *in, FILE *out )2159 process_way2poi(FILE *in, FILE *out, int type) 2138 2160 { 2139 2161 struct item_bin *ib; 2140 2162 while ((ib=read_item(in))) { 2141 2163 int count=ib->clen/2; 2142 2164 if(count>1 && ib->type<type_line) { 2143 2165 struct coord *c=(struct coord *)(ib+1), c1, c2; 2144 if(count>2) { 2145 if(!geom_poly_centroid(c, count, &c1)) { 2146 // we have poly with zero area 2147 // Falling back to coordinates of its first vertex... 2166 int done=0; 2167 if(type==type_area) { 2168 if(count<3) { 2169 osm_warning("way",item_bin_get_wayid(ib),0,"Broken polygon, less than 3 points defined\n"); 2170 } else if(!geom_poly_centroid(c, count, &c1)) { 2148 2171 osm_warning("way",item_bin_get_wayid(ib),0,"Broken polygon, area is 0\n"); 2149 2172 } else { 2150 2173 if(geom_poly_point_inside(c, count, &c1)) { … … 2153 2176 geom_poly_closest_point(c, count, &c1, &c2); 2154 2177 c[0]=c2; 2155 2178 } 2179 done=1; 2156 2180 } 2157 } else if (count==2) {2158 osm_warning("way",item_bin_get_wayid(ib),0, "Expected polygon, but only two points defined\n");2159 c[0].x=(c[0].x+c[1].x)/2;2160 c[0].y=(c[0].y+c[1].y)/2;2161 2181 } 2182 if(!done) { 2183 geom_line_middle(c,count,&c1); 2184 c[0]=c1; 2185 } 2162 2186 write_item_part(out, NULL, NULL, ib, 0, 0, NULL); 2163 2187 } 2164 2188 } -
navit/maptool/maptool.c
346 346 347 347 int main(int argc, char **argv) 348 348 { 349 FILE *ways=NULL, * way2poi=NULL, *ways_split=NULL,*ways_split_index=NULL,*nodes=NULL,*turn_restrictions=NULL,*graph=NULL,*coastline=NULL,*tilesdir,*coords,*relations=NULL,*boundaries=NULL;349 FILE *ways=NULL, *line2poi=NULL, *poly2poi=NULL, *ways_split=NULL,*ways_split_index=NULL,*nodes=NULL,*turn_restrictions=NULL,*graph=NULL,*coastline=NULL,*tilesdir,*coords,*relations=NULL,*boundaries=NULL; 350 350 FILE *files[10]; 351 351 FILE *references[10]; 352 352 struct maptool_params p; … … 427 427 nodes=tempfile(suffix,"nodes",1); 428 428 if (p.process_ways && p.process_nodes) { 429 429 turn_restrictions=tempfile(suffix,"turn_restrictions",1); 430 if(doway2poi) 431 way2poi=tempfile(suffix,"way2poi",1); 430 if(doway2poi) { 431 line2poi=tempfile(suffix,"line2poi",1); 432 poly2poi=tempfile(suffix,"poly2poi",1); 433 } 432 434 } 433 435 if (p.process_relations) 434 436 boundaries=tempfile(suffix,"boundaries",1); 435 437 phase=1; 436 438 fprintf(stderr,"PROGRESS: Phase 1: collecting data\n"); 437 439 osm.ways=ways; 438 osm.way2poi=way2poi; 440 osm.line2poi=line2poi; 441 osm.poly2poi=poly2poi; 439 442 osm.nodes=nodes; 440 443 osm.turn_restrictions=turn_restrictions; 441 444 osm.boundaries=boundaries; … … 470 473 ref_ways(ways); 471 474 save_buffer("coords.tmp",&node_buffer, i*slice_size); 472 475 } 473 if(way2poi) { 474 FILE *way2poinew=tempfile(suffix,"way2poi_resolved_new",1); 475 resolve_ways(way2poi, way2poinew); 476 fclose(way2poi); 477 fclose(way2poinew); 478 tempfile_rename(suffix,"way2poi_resolved_new","way2poi_resolved"); 479 way2poi=tempfile(suffix,"way2poi_resolved",0); 476 if(line2poi) { 477 line2poi=resolve_ways_file(line2poi, suffix, "line2poi_resolved"); 480 478 if (first && !p.keep_tmpfiles) 481 tempfile_unlink(suffix," way2poi");479 tempfile_unlink(suffix,"line2poi"); 482 480 } 481 if(poly2poi) { 482 poly2poi=resolve_ways_file(poly2poi, suffix, "poly2poi_resolved"); 483 if (first && !p.keep_tmpfiles) 484 tempfile_unlink(suffix,"poly2poi"); 485 } 483 486 first=0; 484 487 } 485 488 } 486 489 if (ways) 487 490 fclose(ways); 488 if ( way2poi) {489 process_way2poi( way2poi, nodes);490 fclose( way2poi);491 if (line2poi) { 492 process_way2poi(line2poi, nodes, type_line); 493 fclose(line2poi); 491 494 } 495 if (poly2poi) { 496 process_way2poi(poly2poi, nodes, type_area); 497 fclose(poly2poi); 498 } 492 499 if (nodes) 493 500 fclose(nodes); 494 501 if (turn_restrictions) -
navit/maptool/maptool.h
32 32 #define LONGLONG_FMT "%lld" 33 33 #endif 34 34 35 #define sq(x) ((double)(x)*(x)) 36 35 37 #define BUFFER_SIZE 1280 36 38 37 39 #define debug_tile(x) 0 … … 143 145 144 146 void geom_coord_copy(struct coord *from, struct coord *to, int count, int reverse); 145 147 void geom_coord_revert(struct coord *c, int count); 148 int geom_line_middle(struct coord *p, int count, struct coord *c); 146 149 long long geom_poly_area(struct coord *c, int count); 147 150 int geom_poly_centroid(struct coord *c, int count, struct coord *r); 148 151 int geom_poly_point_inside(struct coord *cp, int count, struct coord *c); … … 236 239 FILE *turn_restrictions; 237 240 FILE *nodes; 238 241 FILE *ways; 239 FILE *way2poi; 242 FILE *line2poi; 243 FILE *poly2poi; 240 244 }; 241 245 242 246 void osm_add_tag(char *k, char *v); … … 254 258 void process_turn_restrictions(FILE *in, FILE *coords, FILE *ways, FILE *ways_index, FILE *out); 255 259 void ref_ways(FILE *in); 256 260 void resolve_ways(FILE *in, FILE *out); 257 void process_way2poi(FILE *in, FILE *out); 261 FILE *resolve_ways_file(FILE *in, char *suffix, char *filename); 262 void process_way2poi(FILE *in, FILE *out, int type); 258 263 int map_find_intersections(FILE *in, FILE *out, FILE *out_index, FILE *out_graph, FILE *out_coastline, int final); 259 264 void write_countrydir(struct zip_info *zip_info); 260 265 void remove_countryfiles(void); -
navit/maptool/geom.c
17 17 * Boston, MA 02110-1301, USA. 18 18 */ 19 19 #include <string.h> 20 #include <math.h> 20 21 #include "maptool.h" 21 22 22 23 void … … 32 33 *to++=*--from; 33 34 } 34 35 36 /** 37 * Get coordinates of polyline middle point. 38 * @param in *p array of poly vertex coordinates 39 * @param in count count of poly vertexes 40 * @param out *c coordinates of middle point 41 * @returns number of first vertex of segment containing middle point 42 */ 43 int 44 geom_line_middle(struct coord *p, int count, struct coord *c) 45 { 46 double length=0,half=0,len=0; 47 int i; 48 49 if(count==1) { 50 *c=*p; 51 return 0; 52 } 53 54 for (i=0; i<count-1; i++) { 55 length+=sqrt(sq(p[i].x-p[i+1].x)+sq(p[i].y-p[i+1].y)); 56 } 57 58 length/=2; 59 for (i=0; (i<count-1) && (half<length); i++) { 60 len=sqrt(sq(p[i].x-p[i+1].x)+sq(p[i].y-p[i+1].y)); 61 half+=len; 62 } 63 i--; 64 half-=length; 65 c->x=(p[i].x*half+p[i+1].x*(len-half))/len; 66 c->y=(p[i].y*half+p[i+1].y*(len-half))/len; 67 return i; 68 } 69 70 35 71 void 36 72 geom_coord_revert(struct coord *c, int count) 37 73 { … … 78 114 int 79 115 geom_poly_centroid(struct coord *p, int count, struct coord *c) 80 116 { 81 long long area=0 /*geom_poly_area(p, count)*/;117 long long area=0; 82 118 long long sx=0,sy=0,tmp; 83 119 int i,j; 84 120 long long x0=p[0].x, y0=p[0].y, xi, yi, xj, yj; -
navit/maptool/ch.c
142 142 g_slice_free(struct item_id, data); 143 143 } 144 144 145 #define sq(x) ((double)(x)*(x))146 147 145 static void 148 146 add_node_to_hash(FILE *idx, GHashTable *hash, struct coord *c, int *nodes) 149 147 {
