Ignore:
Timestamp:
2010-03-21T18:16:45+01:00 (14 years ago)
Author:
pieren
Message:

Improve dialogs and buildings

Location:
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/Buildings.java

    r20548 r20585  
    7373   
    7474    private static final int cMaxnode = 10000;
     75    private static final double cDistanceForOptimization = 0.7;
    7576    private int[] dirsX = new int[] {1,1,0,-1,-1,-1,0,1};
    7677    private int[] dirsY = new int[] {0,1,1,1,0,-1,-1,-1};
     
    121122            return;
    122123        selectedImage = null;
    123         // boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
     124        // ctrl = do not merge the new polygon with adjacent elements
     125        boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
     126        // shift = do not use the parcel as a separator
     127        boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
    124128        // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
    125         boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
    126129        for (Layer layer : Main.map.mapView.getAllLayers()) {
    127130            if (layer.isVisible() && layer instanceof WMSLayer && ((WMSLayer)layer).isBuildingsOnly() ) {
     
    158161                    way2.addNode(way2.getNode(0));
    159162                    new SimplifyWay().simplifyWay(way2, 0.2);
     163                    simplifyAngles(way2);
    160164                    Way wayToAdd = new Way();
    161165                    Collection<Command> cmds = new LinkedList<Command>();
    162                     for (int i = 0; i < way2.getNodesCount()-1; i++) {
    163                         Node nearestNode = getNearestNode(way2.getNode(i));
    164                         if (nearestNode == null) {
    165                             // check if we can join new node to existing ways
    166                             List<WaySegment> wss = getNearestWaySegments(way2.getNode(i));
     166                    if (ctrl) {
     167                        for (int i = 0; i < way2.getNodesCount()-1; i++) {
    167168                            wayToAdd.addNode(way2.getNode(i));
    168                             cmds.add(new AddCommand(way2.getNode(i)));
    169                             if (wss.size() > 0) {
    170                                 cmds.add(new MoveCommand(way2.getNode(i), dx, dy));
    171                                 joinNodeToExistingWays(wss, way2.getNode(i), cmds);
     169                            cmds.add(new AddCommand(wayToAdd.getNode(i)));
     170                        }
     171                        wayToAdd.addNode(wayToAdd.getNode(0)); // close the polygon !
     172                    } else {
     173                        for (int i = 0; i < way2.getNodesCount()-1; i++) {
     174                            Node nearestNode = getNearestNode(way2.getNode(i));
     175                            if (nearestNode == null) {
     176                                // check if we can join new node to existing ways
     177                                List<WaySegment> wss = getNearestWaySegments(way2.getNode(i));
     178                                wayToAdd.addNode(way2.getNode(i));
     179                                cmds.add(new AddCommand(way2.getNode(i)));
     180                                if (wss.size() > 0) {
     181                                    cmds.add(new MoveCommand(way2.getNode(i), dx, dy));
     182                                    joinNodeToExistingWays(wss, way2.getNode(i), cmds);
     183                                }
     184                            } else {
     185                                // replace new node by an existing nearest node
     186                                wayToAdd.addNode(nearestNode);
     187                                cmds.add(new MoveCommand(nearestNode, dx, dy));
    172188                            }
    173                         } else {
    174                             // replace new node by an existing nearest node
    175                             wayToAdd.addNode(nearestNode);
    176                             cmds.add(new MoveCommand(nearestNode, dx, dy));
    177189                        }
    178                     }
    179                     wayToAdd.addNode(wayToAdd.getNode(0)); // close the polygon !
    180                     for (int i = 1; i < wayToAdd.getNodesCount(); i++) {
    181                         List<Node> nodesToJoin = existingNodesInNewSegment(wayToAdd.getNode(i-1), wayToAdd.getNode(i));
    182                         // check if we join new way to existing nodes
    183                         while (nodesToJoin != null && nodesToJoin.size() > 0) {
    184                             List<WaySegment> wss = new LinkedList<WaySegment>();
    185                             wss.add(new WaySegment(wayToAdd, i-1));
    186                             wayToAdd = joinNodeToExistingWays(wss, nodesToJoin.get(0), cmds);
    187                             nodesToJoin = existingNodesInNewSegment(wayToAdd.getNode(i-1), wayToAdd.getNode(i));
     190                        wayToAdd.addNode(wayToAdd.getNode(0)); // close the polygon !
     191                        for (int i = 1; i < wayToAdd.getNodesCount(); i++) {
     192                            Node nodeToJoin = existingNodesInNewSegment(wayToAdd.getNode(i-1), wayToAdd.getNode(i), wayToAdd);
     193                            // check if we join new way to existing nodes
     194                            if (nodeToJoin != null) {
     195                                List<WaySegment> wss = new LinkedList<WaySegment>();
     196                                wss.add(new WaySegment(wayToAdd, i-1));
     197                                wayToAdd = joinNodeToExistingWays(wss, nodeToJoin, cmds);
     198                                cmds.add(new MoveCommand(nodeToJoin, dx, dy));
     199                                i--; // re-assess the new segment (perhaps several nodes to join)
     200                            }
    188201                        }
    189202                    }
     
    322335    }
    323336
    324     private Point getPointInCentimeters(Node n) {
     337    private Point getPointInCm(Node n) {
    325338        return new Point(new Double(n.getEastNorth().getX()*100).intValue(),
    326339                new Double(n.getEastNorth().getY()*100).intValue());
     
    328341   
    329342    public Node getNearestNode(Node newNode) {
    330         Point newP = getPointInCentimeters(newNode);
     343        Point newPoint = getPointInCm(newNode);
    331344        DataSet ds = getCurrentDataSet();
    332345        if (ds == null)
     
    341354            Point sp = new Point(new Double(n.getEastNorth().getX()*100).intValue(),
    342355                    new Double(n.getEastNorth().getY()*100).intValue());
    343             double dist = newP.distanceSq(sp); // in centimeter !
     356            double dist = newPoint.distanceSq(sp); // in centimeter !
    344357            if (dist < minDistanceSq) {
    345358                minDistanceSq = dist;
     
    361374
    362375    private List<WaySegment> getNearestWaySegments(Node newNode) {
    363         Point newP = new Point(new Double(newNode.getEastNorth().getX()*100).intValue(),
     376        Point newPoint = new Point(new Double(newNode.getEastNorth().getX()*100).intValue(),
    364377                new Double(newNode.getEastNorth().getY()*100).intValue());
    365378        TreeMap<Double, List<WaySegment>> nearest = new TreeMap<Double, List<WaySegment>>();
     
    384397                }
    385398
    386                 Point A = getPointInCentimeters(lastN);
    387                 Point B = getPointInCentimeters(n);
     399                Point A = getPointInCm(lastN);
     400                Point B = getPointInCm(n);
    388401                double c = A.distanceSq(B);
    389                 double a = newP.distanceSq(B);
    390                 double b = newP.distanceSq(A);
    391                 double perDist = a - (a - b + c) * (a - b + c) / 4 / c; // perpendicular distance squared
     402                double a = newPoint.distanceSq(B);
     403                double b = newPoint.distanceSq(A);
     404                double perDist = a - (a - b + c) * (a - b + c) / 4 / c;
    392405                if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
    393406                    if (ds.isSelected(w)) {
     
    401414                        nearest.put(perDist, l);
    402415                    }
    403                     Point pivot = A;
    404                     double startAngle = Math.atan2(B.x-pivot.x, B.y-pivot.y);
    405                     double endAngle = Math.atan2(newP.x-pivot.x, newP.y-pivot.y);
    406                     double angle = endAngle - startAngle;
    407                     double ratio = A.distance(newP)/A.distance(B);//).intValue();
     416                    double ratio = A.distance(newPoint)/A.distance(B);
    408417                    Point perP = new Point(A.x+new Double((B.x-A.x)*ratio).intValue(),
    409418                            A.y+new Double((B.y-A.y)*ratio).intValue());
    410                     dx = (perP.x-newP.x)/200.0; // back to meters this time and whole distance by two
    411                     dy = (perP.y-newP.y)/200.0;
    412                     System.out.println(angle+","+ ratio+","+perP );
     419                    dx = (perP.x-newPoint.x)/200.0; // back to meters this time and whole distance by two
     420                    dy = (perP.y-newPoint.y)/200.0;
     421//                    System.out.println(angle+","+ ratio+","+perP );
    413422                    l.add(new WaySegment(w, i));
    414423                }
     
    424433    }
    425434   
    426     private List<Node> existingNodesInNewSegment(Node n1, Node n2) {
     435    private Node existingNodesInNewSegment(Node n1, Node n2, Way way) {
    427436        double minx = Math.min(n1.getEastNorth().getX(), n2.getEastNorth().getX())*100;
    428437        double miny = Math.min(n1.getEastNorth().getY(), n2.getEastNorth().getY())*100;
    429438        double maxx = Math.max(n1.getEastNorth().getX(), n2.getEastNorth().getX())*100;
    430439        double maxy = Math.max(n1.getEastNorth().getY(), n2.getEastNorth().getY())*100;
    431         if ((maxx-minx)/2 < snapDistance && (maxy-miny)/2 < snapDistance) {
     440//        if ((maxx-minx)/2 < snapDistance && (maxy-miny)/2 < snapDistance) {
     441//            return null;
     442//        }
     443        BBox bbox = new BBox( Main.proj.eastNorth2latlon(new EastNorth((minx-snapDistance)/100, (miny-snapDistance)/100)),
     444                Main.proj.eastNorth2latlon(new EastNorth((maxx+snapDistance)/100, (maxy+snapDistance)/100)));
     445        DataSet ds = getCurrentDataSet();
     446        if (ds == null) {
    432447            return null;
    433448        }
    434         BBox bbox = new BBox( Main.proj.eastNorth2latlon(new EastNorth((minx+snapDistance)/100, (miny+snapDistance)/100)),
    435                 Main.proj.eastNorth2latlon(new EastNorth((maxx-snapDistance)/100, (maxy-snapDistance)/100)));
    436         DataSet ds = getCurrentDataSet();
    437         if (ds == null)
    438             return null;
    439         List<Node> ret = new ArrayList<Node>();
    440         for (Node n:ds.searchNodes(bbox))
    441             if (n.isUsable())
    442                 ret.add(n);
    443         System.out.println("Join "+ret.size()+" nodes to new segment");
     449        Node ret = null;
     450        List<Node> nodesInBbox = ds.searchNodes(bbox);
     451        for (Node n:nodesInBbox) {
     452            Point A = getPointInCm(n1);
     453            Point B = getPointInCm(n2);
     454            Point existingPoint = getPointInCm(n);
     455            double c = A.distanceSq(B);
     456            double a = existingPoint.distanceSq(B);
     457            double b = existingPoint.distanceSq(A);
     458            double perDist = a - (a - b + c) * (a - b + c) / 4 / c;
     459            if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq
     460               && n.isUsable() && !way.getNodes().contains(n)) {
     461                ret = n;
     462                // shift the existing node to the half distance of the joined new segment
     463                double ratio = A.distance(existingPoint)/A.distance(B);
     464                Point perP = new Point(A.x+new Double((B.x-A.x)*ratio).intValue(),
     465                        A.y+new Double((B.y-A.y)*ratio).intValue());
     466                dx = (perP.x-existingPoint.x)/200.0; // back to meters this time and whole distance by two
     467                dy = (perP.y-existingPoint.y)/200.0;
     468                break;
     469            }
     470        }
     471//        System.out.println("Found "+nodesInBbox.size()+", join node "+ret+" to new segment; "+Main.proj.latlon2eastNorth(bbox.getBottomRight())+","+Main.proj.latlon2eastNorth(bbox.getTopLeft()));
    444472        return ret;
    445473    }
     
    492520        Collections.reverse(is);
    493521    }
    494 
     522   
     523    /*
     524     * The standard simplifier leaves sometimes closed nodes at buildings corners.
     525     * We remove here the node not altering the building angle.
     526     */
     527    private void simplifyAngles(Way way){
     528        for (int i=1; i<way.getNodes().size(); i++){
     529            Node n1 = way.getNode(i-1);
     530            Node n2 = way.getNode(i);
     531            double dist = getPointInCm(n1).distance(getPointInCm(n2))/100;
     532//            System.out.println("dist="+dist+":"+(dist < cDistanceForOptimization));               
     533            if (dist < cDistanceForOptimization) {
     534                Node n0, n3;
     535                if (i > 1)
     536                    n0 = way.getNode(i-2);
     537                else
     538                    n0 = way.getNode(way.getNodes().size()-1);
     539                if (i < way.getNodes().size()-1)
     540                    n3 = way.getNode(i+1);
     541                else
     542                    n3 = way.getNode(0);
     543                double angle1 = AngleOfView(n1.getCoor().getX(), n1.getCoor().getY(),
     544                        n0.getCoor().getX(), n0.getCoor().getY(),
     545                        n2.getCoor().getX(), n2.getCoor().getY());
     546                System.out.println("angle n0,n1,n2="+(angle1*180/Math.PI));
     547                double angle2 = AngleOfView(n2.getCoor().getX(), n2.getCoor().getY(),
     548                        n1.getCoor().getX(), n1.getCoor().getY(),
     549                        n3.getCoor().getX(), n3.getCoor().getY());
     550                System.out.println("angle n1,n2,n3="+(angle2*180/Math.PI));
     551                if (angle1 > Math.PI*0.9 && angle1 < Math.PI*1.1) {
     552                    way.removeNode(n1);
     553                    System.out.println("remove n1");               
     554                } else if  (angle2 > Math.PI*0.9 && angle2 < Math.PI*1.1) {
     555                    way.removeNode(n2);
     556                    System.out.println("remove n2");               
     557                } else
     558                    System.out.println("no angle near PI");
     559            }               
     560        }       
     561    }
     562
     563    private double AngleOfView ( double ViewPt_X, double ViewPt_Y,
     564            double Pt1_X, double Pt1_Y,
     565            double Pt2_X, double Pt2_Y ) {
     566        double a1, b1, a2, b2, a, b, t, cosinus ;
     567        a1 = Pt1_X - ViewPt_X ;
     568        a2 = Pt1_Y - ViewPt_Y ;
     569        b1 = Pt2_X - ViewPt_X ;
     570        b2 = Pt2_Y - ViewPt_Y ;
     571        a = Math.sqrt( (a1*a1) + (a2*a2) );
     572        b = Math.sqrt ( (b1*b1) + (b2*b2) );
     573        if ( (a == 0.0) || (b == 0.0) )
     574            return (0.0) ;
     575        cosinus = (a1*b1+a2*b2) / (a*b) ;
     576        t = Math.acos ( cosinus );
     577        //t = t * 180.0 / Math.PI ;
     578        return (t);
     579    }
     580   
     581    /*
     582     * coming from SimplifyWayAction
     583     */
     584//    private boolean isRequiredNode(Way way, Node node) {
     585//        boolean isRequired =  Collections.frequency(way.getNodes(), node) > 1;
     586//        if (! isRequired) {
     587//            List<OsmPrimitive> parents = new LinkedList<OsmPrimitive>();
     588//            parents.addAll(node.getReferrers());
     589//            parents.remove(way);
     590//            isRequired = !parents.isEmpty();
     591//        }
     592//        if (!isRequired) {
     593//            isRequired = node.isTagged();
     594//        }
     595//        return isRequired;
     596//    }
    495597}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionNewLocation.java

    r20390 r20585  
    4040        String codeCommune = "";
    4141        boolean changeInterface = false;
    42         JLabel labelSectionNewLocation = new JLabel(tr("Add a new layer"));
     42        JLabel labelSectionNewLocation = new JLabel(tr("Add a new municipality layer"));
    4343        JPanel p = new JPanel(new GridBagLayout());
    44         JLabel labelLocation = new JLabel(tr("Location"));
     44        JLabel labelLocation = new JLabel(tr("Commune"));
    4545        final JTextField inputTown = new JTextField( Main.pref.get("cadastrewms.location") );
    4646        inputTown.setToolTipText(tr("<html>Enter the town,village or city name.<br>"
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/SimplifyWay.java

    r20390 r20585  
    1717        Way wnew = new Way(w);
    1818
    19 //        int toI = wnew.getNodesCount() - 1;
    20 //        List<OsmPrimitive> parents = new ArrayList<OsmPrimitive>();
    21 //        for (int i = wnew.getNodesCount() - 1; i >= 0; i--) {
    22 //            //CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(dataSet, false);
    23 //            //backRefsV.visit(wnew.getNode(i));
    24 //           parents.addAll(w.getNode(i).getReferrers());
    25 //            boolean used = false;
    26 //            if (parents.size() == 1) {
    27 //                used = Collections.frequency(w.getNodes(), wnew.getNode(i)) > 1;
    28 //            } else {
    29 //                //backRefsV.getData().remove(w);
    30 //                parents.remove(w);
    31 //                used = !parents.isEmpty();
    32 //            }
    33 //            if (!used)
    34 //                used = wnew.getNode(i).isTagged();
    35 //
    36 //            if (used) {
    37 //                simplifyWayRange(wnew, i, toI, threshold);
    38 //                toI = i;
    39 //            }
    40 //        }
    4119        simplifyWayRange(wnew, 0, wnew.getNodesCount() - 1, threshold);
    4220        w.setNodes(wnew.getNodes());
    43 //        HashSet<Node> delNodes = new HashSet<Node>();
    44 //        delNodes.addAll(w.getNodes());
    45 //        delNodes.removeAll(wnew.getNodes());
    46 
    47 //        if (wnew.getNodesCount() != w.getNodesCount()) {
    48 //            Collection<Command> cmds = new LinkedList<Command>();
    49 //            cmds.add(new ChangeCommand(w, wnew));
    50 //            cmds.add(new DeleteCommand(delNodes));
    51 //            Main.main.undoRedo.add(new SequenceCommand(trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds));
    52 //            Main.map.repaint();
    53 //        }
    5421    }
    5522
Note: See TracChangeset for help on using the changeset viewer.