Changeset 8904 in osm


Ignore:
Timestamp:
2008-07-10T10:54:26+02:00 (16 years ago)
Author:
stoecker
Message:

cleanup wrongly ordered ways - close josm bug #737

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/WronglyOrderedWays.java

    r6374 r8904  
    1616/**
    1717 * Check cyclic ways for errors
    18  * 
     18 *
    1919 * @author jrreid
    2020 */
    2121public class WronglyOrderedWays extends Test  {
    22     /** All ways, grouped by cells */
    23     Map<Point2D,List<Way>> _cellWays;
    24     /** The already detected errors */
    25     Bag<Way, Way> _errorWays;
     22        /** The already detected errors */
     23        Bag<Way, Way> _errorWays;
    2624
    2725        /**
    2826         * Constructor
    2927         */
    30         public WronglyOrderedWays() 
     28        public WronglyOrderedWays()
    3129        {
    3230                super(tr("Wrongly Ordered Ways."),
     
    3432        }
    3533
    36     @Override
    37     public void startTest()
    38     {
    39         _cellWays = new HashMap<Point2D,List<Way>>(1000);
    40         _errorWays = new Bag<Way, Way>();
    41     }
     34        @Override
     35        public void startTest()
     36        {
     37                _errorWays = new Bag<Way, Way>();
     38        }
    4239
    43     @Override
    44     public void endTest()
    45     {
    46         _cellWays = null;
    47         _errorWays = null;
    48     }
    49    
    5040        @Override
    51         public void visit(Way w)
     41        public void endTest()
     42        {
     43                _errorWays = null;
     44        }
     45       
     46        @Override
     47        public void visit(Way w)
    5248        {
    5349                String errortype = "";
    5450               
    55         if( w.deleted || w.incomplete )
    56             return;
    57        
    58         String natural = w.get("natural");
    59         if( natural == null)
    60             return;
    61        
    62         if(!natural.equals("coastline") ){
    63                 errortype = "Clockwise coastline";
    64         }else if(!natural.equals("water") ){
    65                 errortype = "Clockwise water";
    66         }else if(!natural.equals("land") ){
    67                 errortype = "Clockwise land";
    68         } else {
    69                 return;
    70         }
    71        
    72         /**
    73          * Test the directionality of the way
    74          *
    75          * Checks if the node following the northern-most node is further
    76          * west then the node previous
    77          *
    78          * Only tests ways that the first and last node is the same currently
    79          *
    80          */
    81        
    82         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)){             
    114                         List<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
    115                         primitives.add(w);
    116                         errors.add( new TestError(this, Severity.WARNING, tr(errortype), primitives) );
    117                         _errorWays.add(w,w);           
    118                 }       
    119         }
     51                if( w.deleted || w.incomplete )
     52                        return;
     53               
     54                String natural = w.get("natural");
     55                if( natural == null)
     56                        return;
     57               
     58                if( natural.equals("coastline") )
     59                        errortype = tr("Reversed coastline: land not on left side");
     60                else if(natural.equals("water") )
     61                        errortype = tr("Reversed water: land not on left side");
     62                else if( natural.equals("land") )
     63                        errortype = tr("Reversed land: land not on left side");
     64                else
     65                        return;
     66
     67
     68                /**
     69                 * Test the directionality of the way
     70                 *
     71                 * Assuming a closed non-looping way, compute twice the area
     72                 * of the polygon using the formula 2*a = sum (Xn * Yn+1 - Xn+1 * Yn)
     73                 * If the area is negative the way is ordered in a clockwise direction
     74                 *
     75                 */
     76
     77                if(w.nodes.get(0) == w.nodes.get(w.nodes.size()-1))
     78                {
     79                        double area2 = 0;
     80
     81                        for (int node = 1; node < w.nodes.size(); node++)
     82                        {
     83                                area2 += (w.nodes.get(node-1).coor.lon() * w.nodes.get(node).coor.lat()
     84                                - w.nodes.get(node).coor.lon() * w.nodes.get(node-1).coor.lat());
     85                        }
     86
     87                        if(((natural.equals("coastline") || natural.equals("land")) && area2 < 0.)
     88                        || (natural.equals("water") && area2 > 0.))
     89                        {
     90                                List<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
     91                                primitives.add(w);
     92                                errors.add( new TestError(this, Severity.WARNING, errortype, primitives) );
     93                                _errorWays.add(w,w);
     94                        }
     95                }
    12096        }
    12197}
Note: See TracChangeset for help on using the changeset viewer.