Ticket #737: WronglyOrderedWays.diff

File WronglyOrderedWays.diff, 3.0 KB (added by anonymous, 18 years ago)

way direction by pollygon area

  • WronglyOrderedWays.java

     
    5959        if( natural == null)
    6060            return;
    6161       
    62         if(!natural.equals("coastline") ){
     62        if(natural.equals("coastline") ){
    6363                errortype = "Clockwise coastline";
    64         }else if(!natural.equals("water") ){
     64        }else if(natural.equals("water") ){
    6565                errortype = "Clockwise water";
    66         }else if(!natural.equals("land") ){
     66        }else if(natural.equals("land") ){
    6767                errortype = "Clockwise land";
    6868        } else {
    6969                return;
     
    7272        /**
    7373         * Test the directionality of the way
    7474         *
    75          * Checks if the node following the northern-most node is further
    76          * west then the node previous
     75         * Assuming a closed non-looping way, compute twice the area
     76         * of the pollygon using the formula 2*a = sum (Xn * Yn+1 - Xn+1 * Yn)
     77         * If the area is negative the way is ordered in a clockwise direction
    7778         *
    78          * Only tests ways that the first and last node is the same currently
    79          *
    8079         */
    8180       
    8281        if(w.nodes.get(0) == w.nodes.get(w.nodes.size()-1)){
    83                 int maxnode = -1;
    84                 double maxlat = -90;
    85                
    86                 for (int node = 0; node < w.nodes.size(); node++){
    87                         double lat = w.nodes.get(node).coor.lat();
    88                         if(lat > maxlat){
    89                                 maxnode = node;
    90                                 maxlat = lat;
    91                         }
    92                 }               
    93                
    94                 int nextnode;
    95                 int prevnode;
    96                
    97                 // Determine the previous and next nodes in the loop
    98                 if(maxnode==0){
    99                         nextnode = 1;
    100                         prevnode = w.nodes.size()-1;
    101                 }else if(maxnode == w.nodes.size()-1){
    102                         nextnode = 0;
    103                         prevnode = maxnode - 1;
    104                 } else {
    105                         nextnode = maxnode + 1;
    106                         prevnode = maxnode - 1;
    107                 }
    108                
    109                 double prevlon = w.nodes.get(prevnode).coor.lon();
    110                 double nextlon = w.nodes.get(nextnode).coor.lon();
    111                
    112                 if(((natural.equals("coastline") || natural.equals("land")) && prevlon < nextlon)
    113                                 || (natural.equals("water") && prevlon > nextlon)){             
     82                double area2 = 0;
     83
     84                for (int node = 1; node < w.nodes.size(); node++){
     85                    area2 += (w.nodes.get(node-1).coor.lon() * w.nodes.get(node).coor.lat()
     86                           - w.nodes.get(node).coor.lon() * w.nodes.get(node-1).coor.lat());
     87                }
     88
     89                if(((natural.equals("coastline") || natural.equals("land")) && area2 < 0.)
     90                                || (natural.equals("water") && area2 >  0.)){           
    11491                        List<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
    11592                        primitives.add(w);
    11693                        errors.add( new TestError(this, Severity.WARNING, tr(errortype), primitives) );