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

     
    15401540        int *def_flags,add_flags; 
    15411541        enum item_type types[10]; 
    15421542        struct item_bin *item_bin; 
     1543        int count_lines=0, count_areas=0; 
    15431544 
    15441545        in_way=0; 
    15451546 
     
    15671568                        continue; 
    15681569                if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn)) 
    15691570                        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++; 
    15721575                item_bin=init_item(types[i]); 
    15731576                item_bin_add_coord(item_bin, coord_buffer, coord_count); 
    15741577                nodes_ref_item_bin(item_bin); 
     
    15891592                        item_bin_add_attr_int(item_bin, attr_maxspeed, maxspeed_attr_value); 
    15901593                item_bin_write(item_bin,osm->ways); 
    15911594        } 
    1592         if(osm->way2poi) { 
     1595        if(osm->line2poi) { 
    15931596                count=attr_longest_match(attr_mapping_way2poi, attr_mapping_way2poi_count, types, sizeof(types)/sizeof(enum item_type)); 
    15941597                dbg_assert(count < 10); 
    15951598                for (i = 0 ; i < count ; i++) { 
     
    16061609                        item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]);  
    16071610                        item_bin_add_attr_string(item_bin, attr_url, attr_strings[attr_string_url]); 
    16081611                        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); 
    16111613                } 
    16121614        } 
    16131615        attr_longest_match_clear(); 
     
    21332135        } 
    21342136} 
    21352137 
     2138FILE * 
     2139resolve_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  */ 
    21362158void 
    2137 process_way2poi(FILE *in, FILE *out) 
     2159process_way2poi(FILE *in, FILE *out, int type) 
    21382160{ 
    21392161        struct item_bin *ib; 
    21402162        while ((ib=read_item(in))) { 
    21412163                int count=ib->clen/2; 
    21422164                if(count>1 && ib->type<type_line) { 
    21432165                        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)) { 
    21482171                                        osm_warning("way",item_bin_get_wayid(ib),0,"Broken polygon, area is 0\n"); 
    21492172                                } else { 
    21502173                                        if(geom_poly_point_inside(c, count, &c1)) { 
     
    21532176                                                geom_poly_closest_point(c, count, &c1, &c2); 
    21542177                                                c[0]=c2; 
    21552178                                        } 
     2179                                        done=1; 
    21562180                                } 
    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; 
    21612181                        } 
     2182                        if(!done) { 
     2183                                geom_line_middle(c,count,&c1); 
     2184                                c[0]=c1; 
     2185                        } 
    21622186                        write_item_part(out, NULL, NULL, ib, 0, 0, NULL); 
    21632187                } 
    21642188        } 
  • navit/maptool/maptool.c

     
    346346 
    347347int main(int argc, char **argv) 
    348348{ 
    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; 
    350350        FILE *files[10]; 
    351351        FILE *references[10]; 
    352352        struct maptool_params p; 
     
    427427                        nodes=tempfile(suffix,"nodes",1); 
    428428                if (p.process_ways && p.process_nodes) { 
    429429                        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                        } 
    432434                } 
    433435                if (p.process_relations) 
    434436                        boundaries=tempfile(suffix,"boundaries",1); 
    435437                phase=1; 
    436438                fprintf(stderr,"PROGRESS: Phase 1: collecting data\n"); 
    437439                osm.ways=ways; 
    438                 osm.way2poi=way2poi; 
     440                osm.line2poi=line2poi; 
     441                osm.poly2poi=poly2poi; 
    439442                osm.nodes=nodes; 
    440443                osm.turn_restrictions=turn_restrictions; 
    441444                osm.boundaries=boundaries; 
     
    470473                                        ref_ways(ways); 
    471474                                        save_buffer("coords.tmp",&node_buffer, i*slice_size); 
    472475                                } 
    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"); 
    480478                                        if (first && !p.keep_tmpfiles)  
    481                                                 tempfile_unlink(suffix,"way2poi"); 
     479                                                tempfile_unlink(suffix,"line2poi"); 
    482480                                } 
     481                                if(poly2poi) { 
     482                                        poly2poi=resolve_ways_file(poly2poi, suffix, "poly2poi_resolved"); 
     483                                        if (first && !p.keep_tmpfiles)  
     484                                                tempfile_unlink(suffix,"poly2poi"); 
     485                                } 
    483486                                first=0; 
    484487                        } 
    485488                } 
    486489                if (ways) 
    487490                        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); 
    491494                } 
     495                if (poly2poi) { 
     496                        process_way2poi(poly2poi, nodes, type_area); 
     497                        fclose(poly2poi); 
     498                } 
    492499                if (nodes) 
    493500                        fclose(nodes); 
    494501                if (turn_restrictions) 
  • navit/maptool/maptool.h

     
    3232#define LONGLONG_FMT "%lld" 
    3333#endif 
    3434 
     35#define sq(x) ((double)(x)*(x)) 
     36 
    3537#define BUFFER_SIZE 1280 
    3638 
    3739#define debug_tile(x) 0 
     
    143145 
    144146void geom_coord_copy(struct coord *from, struct coord *to, int count, int reverse); 
    145147void geom_coord_revert(struct coord *c, int count); 
     148int geom_line_middle(struct coord *p, int count, struct coord *c); 
    146149long long geom_poly_area(struct coord *c, int count); 
    147150int geom_poly_centroid(struct coord *c, int count, struct coord *r); 
    148151int geom_poly_point_inside(struct coord *cp, int count, struct coord *c); 
     
    236239        FILE *turn_restrictions; 
    237240        FILE *nodes; 
    238241        FILE *ways; 
    239         FILE *way2poi; 
     242        FILE *line2poi; 
     243        FILE *poly2poi; 
    240244}; 
    241245 
    242246void osm_add_tag(char *k, char *v); 
     
    254258void process_turn_restrictions(FILE *in, FILE *coords, FILE *ways, FILE *ways_index, FILE *out); 
    255259void ref_ways(FILE *in); 
    256260void resolve_ways(FILE *in, FILE *out); 
    257 void process_way2poi(FILE *in, FILE *out); 
     261FILE *resolve_ways_file(FILE *in, char *suffix, char *filename); 
     262void process_way2poi(FILE *in, FILE *out, int type); 
    258263int map_find_intersections(FILE *in, FILE *out, FILE *out_index, FILE *out_graph, FILE *out_coastline, int final); 
    259264void write_countrydir(struct zip_info *zip_info); 
    260265void remove_countryfiles(void); 
  • navit/maptool/geom.c

     
    1717 * Boston, MA  02110-1301, USA. 
    1818 */ 
    1919#include <string.h> 
     20#include <math.h> 
    2021#include "maptool.h" 
    2122 
    2223void 
     
    3233                *to++=*--from;   
    3334} 
    3435 
     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  */ 
     43int 
     44geom_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 
    3571void 
    3672geom_coord_revert(struct coord *c, int count) 
    3773{ 
     
    78114int 
    79115geom_poly_centroid(struct coord *p, int count, struct coord *c) 
    80116{ 
    81         long long area=0/*geom_poly_area(p, count)*/; 
     117        long long area=0; 
    82118        long long sx=0,sy=0,tmp; 
    83119        int i,j; 
    84120        long long x0=p[0].x, y0=p[0].y, xi, yi, xj, yj; 
  • navit/maptool/ch.c

     
    142142        g_slice_free(struct item_id, data); 
    143143} 
    144144 
    145 #define sq(x) ((double)(x)*(x)) 
    146  
    147145static void 
    148146add_node_to_hash(FILE *idx, GHashTable *hash, struct coord *c, int *nodes) 
    149147{