Changeset 27971 in osm for applications


Ignore:
Timestamp:
2012-03-02T06:02:28+01:00 (12 years ago)
Author:
joshdoe
Message:

utilsplugin2,conflation: refactor for clarity

Location:
applications/editors/josm/plugins
Files:
3 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationCandidate.java

    r27959 r27971  
    66import org.openstreetmap.josm.data.osm.OsmPrimitive;
    77import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     8import static org.openstreetmap.josm.tools.I18n.tr;
    89
    910/**
     
    1314public class ConflationCandidate {
    1415
    15     OsmPrimitive sourcePrimitive;
    16     OsmDataLayer sourceLayer;
    17     OsmPrimitive targetPrimitive;
    18     OsmDataLayer targetLayer;
     16    OsmPrimitive referenceObject;
     17    OsmDataLayer referenceLayer;
     18    OsmPrimitive subjectObject;
     19    OsmDataLayer subjectLayer;
    1920    double cost;
    2021    double distance;
    2122
    22     public ConflationCandidate(OsmPrimitive source, OsmDataLayer sourceLayer,
    23             OsmPrimitive target, OsmDataLayer targetLayer, double cost) {
    24         if (source == null || target == null) {
    25             throw new IllegalArgumentException("Invalid source or target");
     23    public ConflationCandidate(OsmPrimitive referenceObject, OsmDataLayer referenceLayer,
     24            OsmPrimitive subjectObject, OsmDataLayer subjectLayer, double cost) {
     25        if (referenceObject == null || subjectObject == null) {
     26            throw new IllegalArgumentException(tr("Invalid reference or subject"));
    2627        }
    27         this.sourcePrimitive = source;
    28         this.sourceLayer = sourceLayer;
    29         this.targetPrimitive = target;
    30         this.targetLayer = targetLayer;
     28        this.referenceObject = referenceObject;
     29        this.referenceLayer = referenceLayer;
     30        this.subjectObject = subjectObject;
     31        this.subjectLayer = subjectLayer;
    3132        this.cost = cost;
    3233        // TODO: use distance calculated in cost function, and make sure it's in meters?
    33         this.distance = ConflationUtils.getCenter(source).distance(ConflationUtils.getCenter(target));
     34        this.distance = ConflationUtils.getCenter(referenceObject).distance(ConflationUtils.getCenter(subjectObject));
    3435    }
    3536
    36     public OsmPrimitive getSourcePrimitive() {
    37         return sourcePrimitive;
     37    public OsmPrimitive getReferenceObject() {
     38        return referenceObject;
    3839    }
    3940   
    40     public OsmDataLayer getSourceLayer() {
    41         return sourceLayer;
     41    public OsmDataLayer getReferenceLayer() {
     42        return referenceLayer;
    4243    }
    4344   
    44     public OsmDataLayer getTargetLayer() {
    45         return targetLayer;
     45    public OsmDataLayer getSubjectLayer() {
     46        return subjectLayer;
    4647    }
    4748
    48     public OsmPrimitive getTargetPrimitive() {
    49         return targetPrimitive;
     49    public OsmPrimitive getSubjectObject() {
     50        return subjectObject;
    5051    }
    5152
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationCandidateList.java

    r27959 r27971  
    55package org.openstreetmap.josm.plugins.conflation;
    66
    7 import java.util.Collection;
    87import java.util.Iterator;
    98import java.util.LinkedList;
     
    2524
    2625    public boolean hasCandidate(ConflationCandidate c) {
    27         return hasCandidateForSource(c.getSourcePrimitive());
     26        return hasCandidateForReference(c.getReferenceObject());
    2827    }
    2928
    30     public boolean hasCandidate(OsmPrimitive src, OsmPrimitive tgt) {
    31         return hasCandidateForSource(src) || hasCandidateForTarget(tgt);
     29    public boolean hasCandidate(OsmPrimitive referenceObject, OsmPrimitive subjectObject) {
     30        return hasCandidateForReference(referenceObject) || hasCandidateForSubject(subjectObject);
    3231    }
    3332
    34     public boolean hasCandidateForSource(OsmPrimitive src) {
    35         return getCandidateBySource(src) != null;
     33    public boolean hasCandidateForReference(OsmPrimitive referenceObject) {
     34        return getCandidateByReference(referenceObject) != null;
    3635    }
    3736
    38     public boolean hasCandidateForTarget(OsmPrimitive tgt) {
    39         return getCandidateByTarget(tgt) != null;
     37    public boolean hasCandidateForSubject(OsmPrimitive subjectObject) {
     38        return getCandidateBySubject(subjectObject) != null;
    4039    }
    4140
    42     public ConflationCandidate getCandidateBySource(OsmPrimitive src) {
     41    public ConflationCandidate getCandidateByReference(OsmPrimitive referenceObject) {
    4342        for (ConflationCandidate c : candidates) {
    44             if (c.getSourcePrimitive() == src) {
     43            if (c.getReferenceObject() == referenceObject) {
    4544                return c;
    4645            }
     
    4948    }
    5049
    51     public ConflationCandidate getCandidateByTarget(OsmPrimitive tgt) {
     50    public ConflationCandidate getCandidateBySubject(OsmPrimitive subjectObject) {
    5251        for (ConflationCandidate c : candidates) {
    53             if (c.getTargetPrimitive() == tgt) {
     52            if (c.getSubjectObject() == subjectObject) {
    5453                return c;
    5554            }
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationLayer.java

    r27959 r27971  
    6060                g2.setColor(Color.cyan);
    6161            }
    62             OsmPrimitive src = candidate.getSourcePrimitive();
    63             OsmPrimitive tgt = candidate.getTargetPrimitive();
    64             if (src != null && tgt != null) {
     62            OsmPrimitive reference = candidate.getReferenceObject();
     63            OsmPrimitive subject = candidate.getSubjectObject();
     64            if (reference != null && subject != null) {
    6565                GeneralPath path = new GeneralPath();
    6666                // we have a pair, so draw line between them, FIXME: not good to use getCenter() from here, move to utils?
    67                 Point p1 = mv.getPoint(ConflationUtils.getCenter(src));
    68                 Point p2 = mv.getPoint(ConflationUtils.getCenter(tgt));
     67                Point p1 = mv.getPoint(ConflationUtils.getCenter(reference));
     68                Point p2 = mv.getPoint(ConflationUtils.getCenter(subject));
    6969                path.moveTo(p1.x, p1.y);
    7070                path.lineTo(p2.x, p2.y);
     
    118118        for (Iterator<ConflationCandidate> it = this.candidates.iterator(); it.hasNext();) {
    119119            ConflationCandidate candidate = it.next();
    120             OsmPrimitive src = candidate.getSourcePrimitive();
    121             OsmPrimitive tgt = candidate.getTargetPrimitive();
    122             if (src != null && src instanceof Node)
    123                 v.visit((Node)src);
    124             if (tgt != null && tgt instanceof Node)
    125                 v.visit((Node)tgt);
     120            OsmPrimitive reference = candidate.getReferenceObject();
     121            OsmPrimitive subject = candidate.getSubjectObject();
     122            if (reference != null && reference instanceof Node)
     123                v.visit((Node)reference);
     124            if (subject != null && subject instanceof Node)
     125                v.visit((Node)subject);
    126126        }
    127127    }
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationPlugin.java

    r27959 r27971  
    2626        if (oldFrame == null && newFrame != null) {
    2727            if (dialog == null) {
    28                 Shortcut shortcut = Shortcut.registerShortcut("Conflation", tr("Toggle: {0}", tr("Open Conflation")),
    29                         KeyEvent.VK_0, Shortcut.ALT_SHIFT);
     28//                Shortcut shortcut = null; Shortcut.registerShortcut("Conflation", tr("Toggle: {0}", tr("Open Conflation")),
     29//                        KeyEvent.VK_0, Shortcut.ALT_SHIFT);
     30                Shortcut shortcut = null;
    3031                String name = "Conflation";
    3132                String tooltip = "Activates the conflation plugin";
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationToggleDialog.java

    r27959 r27971  
    1 /*
    2  * To change this template, choose Tools | Templates
    3  * and open the template in the editor.
    4  */
    51package org.openstreetmap.josm.plugins.conflation;
    62
     
    132128            if (!lsm.isSelectionEmpty() && firstIndex == lastIndex && firstIndex < candidates.size()) {
    133129                ConflationCandidate c = candidates.get(firstIndex);
    134                 OsmPrimitive src = c.getSourcePrimitive();
    135                 OsmPrimitive tgt = c.getTargetPrimitive();
     130                OsmPrimitive reference = c.getReferenceObject();
     131                OsmPrimitive subject = c.getSubjectObject();
    136132
    137133                conflationLayer.setSelectedCandidate(c);
    138134
    139                 src.getDataSet().clearSelection();
    140                 tgt.getDataSet().clearSelection();
    141                 src.getDataSet().addSelected(src);
    142                 tgt.getDataSet().addSelected(tgt);
     135                reference.getDataSet().clearSelection();
     136                subject.getDataSet().clearSelection();
     137                reference.getDataSet().addSelected(reference);
     138                subject.getDataSet().addSelected(subject);
    143139
    144140                // zoom/center on pair
    145141                BoundingXYVisitor box = new BoundingXYVisitor();
    146                 box.computeBoundingBox(Arrays.asList(src, tgt));
     142                box.computeBoundingBox(Arrays.asList(reference, subject));
    147143                if (box.getBounds() == null) {
    148144                    return;
     
    212208            ReplaceGeometryAction rg = new ReplaceGeometryAction();
    213209            ConflationCandidate c = conflationLayer.getSelectedCandidate();
    214             if (rg.replace(c.getSourcePrimitive(), c.getTargetPrimitive())) {
     210            if (rg.replace(c.getReferenceObject(), c.getSubjectObject())) {
    215211                candidates.remove(c);
    216212            }
     
    222218        private JPanel costsPanel;
    223219        private JCheckBox distanceCheckBox;
    224         private JButton freezeSourceButton;
    225         private JButton freezeTargetButton;
     220        private JButton freezeReferenceButton;
     221        private JButton freezeSubjectButton;
    226222        private JPanel jPanel3;
    227223        private JPanel jPanel5;
    228         private JButton restoreSourceButton;
    229         private JButton restoreTargetButton;
    230         private JLabel sourceLayerLabel;
    231         private JPanel sourcePanel;
    232         private JLabel sourceSelectionLabel;
    233         private JLabel targetLayerLabel;
    234         private JPanel targetPanel;
    235         private JLabel targetSelectionLabel;
    236         ArrayList<OsmPrimitive> tgtSelection = null;
    237         ArrayList<OsmPrimitive> srcSelection = null;
    238         OsmDataLayer srcLayer;
    239         DataSet tgtDataSet;
    240         OsmDataLayer tgtLayer;
    241         DataSet srcDataSet;
    242         private boolean canceled = false;
     224        private JButton restoreReferenceButton;
     225        private JButton restoreSubjectButton;
     226        private JLabel referenceLayerLabel;
     227        private JPanel referencePanel;
     228        private JLabel referenceSelectionLabel;
     229        private JLabel subjectLayerLabel;
     230        private JPanel subjectPanel;
     231        private JLabel subjectSelectionLabel;
     232        ArrayList<OsmPrimitive> subjectSelection = null;
     233        ArrayList<OsmPrimitive> referenceSelection = null;
     234        OsmDataLayer referenceLayer;
     235        DataSet subjectDataSet;
     236        OsmDataLayer subjectLayer;
     237        DataSet referenceDataSet;
    243238
    244239        public ConflationOptionsDialog() {
     
    251246
    252247        private void initComponents() {
    253             sourcePanel = new JPanel();
    254             sourceLayerLabel = new JLabel();
    255             sourceSelectionLabel = new JLabel();
     248            referencePanel = new JPanel();
     249            referenceLayerLabel = new JLabel();
     250            referenceSelectionLabel = new JLabel();
    256251            jPanel3 = new JPanel();
    257             restoreSourceButton = new JButton(new RestoreSourceAction());
    258             freezeSourceButton = new JButton(new FreezeSourceAction());
    259             targetPanel = new JPanel();
    260             targetLayerLabel = new JLabel();
    261             targetSelectionLabel = new JLabel();
     252            restoreReferenceButton = new JButton(new RestoreReferenceAction());
     253            freezeReferenceButton = new JButton(new FreezeReferenceAction());
     254            subjectPanel = new JPanel();
     255            subjectLayerLabel = new JLabel();
     256            subjectSelectionLabel = new JLabel();
    262257            jPanel5 = new JPanel();
    263             restoreTargetButton = new JButton(new RestoreTargetAction());
    264             freezeTargetButton = new JButton(new FreezeTargetAction());
     258            restoreSubjectButton = new JButton(new RestoreSubjectAction());
     259            freezeSubjectButton = new JButton(new FreezeSubjectAction());
    265260            costsPanel = new JPanel();
    266261            distanceCheckBox = new JCheckBox();
     
    269264            pnl.setLayout(new BoxLayout(pnl, BoxLayout.PAGE_AXIS));
    270265
    271             sourcePanel.setBorder(BorderFactory.createTitledBorder("Source"));
    272             sourcePanel.setLayout(new BoxLayout(sourcePanel, BoxLayout.PAGE_AXIS));
    273 
    274             sourceLayerLabel.setText("layer");
    275             sourcePanel.add(sourceLayerLabel);
    276 
    277             sourceSelectionLabel.setText("Rel.:0 / Ways:0 / Nodes: 0");
    278             sourcePanel.add(sourceSelectionLabel);
     266            referencePanel.setBorder(BorderFactory.createTitledBorder(tr("Reference")));
     267            referencePanel.setLayout(new BoxLayout(referencePanel, BoxLayout.PAGE_AXIS));
     268
     269            referenceLayerLabel.setText("(none)");
     270            referencePanel.add(referenceLayerLabel);
     271
     272            referenceSelectionLabel.setText("Rel.:0 / Ways:0 / Nodes: 0");
     273            referencePanel.add(referenceSelectionLabel);
    279274
    280275            jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.LINE_AXIS));
    281276
    282             restoreSourceButton.setText("Restore");
    283             jPanel3.add(restoreSourceButton);
    284 
    285             jPanel3.add(freezeSourceButton);
    286 
    287             sourcePanel.add(jPanel3);
    288 
    289             pnl.add(sourcePanel);
    290 
    291             targetPanel.setBorder(BorderFactory.createTitledBorder("Target"));
    292             targetPanel.setLayout(new BoxLayout(targetPanel, BoxLayout.PAGE_AXIS));
    293 
    294             targetLayerLabel.setText("layer");
    295             targetPanel.add(targetLayerLabel);
    296 
    297             targetSelectionLabel.setText("Rel.:0 / Ways:0 / Nodes: 0");
    298             targetPanel.add(targetSelectionLabel);
     277            restoreReferenceButton.setText(tr("Restore"));
     278            jPanel3.add(restoreReferenceButton);
     279
     280            jPanel3.add(freezeReferenceButton);
     281
     282            referencePanel.add(jPanel3);
     283
     284            pnl.add(referencePanel);
     285
     286            subjectPanel.setBorder(BorderFactory.createTitledBorder(tr("Subject")));
     287            subjectPanel.setLayout(new BoxLayout(subjectPanel, BoxLayout.PAGE_AXIS));
     288
     289            subjectLayerLabel.setText("(none)");
     290            subjectPanel.add(subjectLayerLabel);
     291
     292            subjectSelectionLabel.setText("Rel.:0 / Ways:0 / Nodes: 0");
     293            subjectPanel.add(subjectSelectionLabel);
    299294
    300295            jPanel5.setLayout(new BoxLayout(jPanel5, BoxLayout.LINE_AXIS));
    301296
    302             restoreTargetButton.setText("Restore");
    303             jPanel5.add(restoreTargetButton);
    304 
    305             freezeTargetButton.setText("Freeze");
    306             jPanel5.add(freezeTargetButton);
    307 
    308             targetPanel.add(jPanel5);
    309 
    310             pnl.add(targetPanel);
    311 
    312             costsPanel.setBorder(BorderFactory.createTitledBorder("Costs"));
     297            restoreSubjectButton.setText(tr("Restore"));
     298            jPanel5.add(restoreSubjectButton);
     299
     300            freezeSubjectButton.setText(tr("Freeze"));
     301            jPanel5.add(freezeSubjectButton);
     302
     303            subjectPanel.add(jPanel5);
     304
     305            pnl.add(subjectPanel);
     306
     307            costsPanel.setBorder(BorderFactory.createTitledBorder(tr("Costs")));
    313308            costsPanel.setLayout(new BoxLayout(costsPanel, BoxLayout.LINE_AXIS));
    314309
    315310            distanceCheckBox.setSelected(true);
    316             distanceCheckBox.setText("Distance");
     311            distanceCheckBox.setText(tr("Distance"));
    317312            distanceCheckBox.setEnabled(false);
    318313            costsPanel.add(distanceCheckBox);
     
    328323            super.buttonAction(buttonIndex, evt);
    329324            if (buttonIndex == 0) {
    330                 criteriaTabConflateButtonActionPerformed();
    331             }
    332         }
    333 
    334         private void criteriaTabConflateButtonActionPerformed() {
     325                performConflation();
     326            }
     327        }
     328
     329        private void performConflation() {
    335330
    336331            // some initialization
    337             int n = tgtSelection.size();
    338             int m = srcSelection.size();
     332            int n = subjectSelection.size();
     333            int m = referenceSelection.size();
    339334            int maxLen = Math.max(n, m);
    340335            double cost[][] = new double[maxLen][maxLen];
     
    343338            for (int i = 0; i < n; i++) {
    344339                for (int j = 0; j < m; j++) {
    345                     cost[i][j] = ConflationUtils.calcCost(tgtSelection.get(i), srcSelection.get(j));
     340                    cost[i][j] = ConflationUtils.calcCost(subjectSelection.get(i), referenceSelection.get(j));
    346341                }
    347342            }
     
    349344            // perform assignment using Hungarian algorithm
    350345            int[][] assignment = HungarianAlgorithm.hgAlgorithm(cost, "min");
    351             OsmPrimitive tgt, src;
     346            OsmPrimitive subObject, refObject;
    352347            candidates.clear();
    353348            for (int i = 0; i < maxLen; i++) {
    354                 int tgtIdx = assignment[i][0];
    355                 int srcIdx = assignment[i][1];
    356                 if (tgtIdx < n) {
    357                     tgt = tgtSelection.get(tgtIdx);
     349                int subIdx = assignment[i][0];
     350                int refIdx = assignment[i][1];
     351                if (subIdx < n) {
     352                    subObject = subjectSelection.get(subIdx);
    358353                } else {
    359                     tgt = null;
    360                 }
    361                 if (srcIdx < m) {
    362                     src = srcSelection.get(srcIdx);
     354                    subObject = null;
     355                }
     356                if (refIdx < m) {
     357                    refObject = referenceSelection.get(refIdx);
    363358                } else {
    364                     src = null;
    365                 }
    366 
    367                 if (tgt != null && src != null) {
     359                    refObject = null;
     360                }
     361
     362                if (subObject != null && refObject != null) {
    368363                    // TODO: do something!
    369                     if (!(candidates.hasCandidate(src, tgt) || candidates.hasCandidate(tgt, src))) {
    370                         candidates.add(new ConflationCandidate(src, srcLayer, tgt, tgtLayer, cost[tgtIdx][srcIdx]));
     364                    if (!(candidates.hasCandidate(refObject, subObject) || candidates.hasCandidate(subObject, refObject))) {
     365                        candidates.add(new ConflationCandidate(refObject, referenceLayer, subObject, subjectLayer, cost[subIdx][refIdx]));
    371366                    }
    372367                }
     
    375370            // add conflation layer
    376371            try {
    377                 conflationLayer = new ConflationLayer(tgtLayer.data, candidates);
     372                conflationLayer = new ConflationLayer(subjectLayer.data, candidates);
    378373                Main.main.addLayer(conflationLayer);
    379374            } catch (Exception ex) {
     
    390385        }
    391386
    392         class RestoreTargetAction extends JosmAction {
    393 
    394             public RestoreTargetAction() {
    395                 super(tr("Restore"), null, tr("Restore target selection"), null, false);
     387        class RestoreSubjectAction extends JosmAction {
     388
     389            public RestoreSubjectAction() {
     390                super(tr("Restore"), null, tr("Restore subject selection"), null, false);
    396391            }
    397392
    398393            @Override
    399394            public void actionPerformed(ActionEvent e) {
    400                 if (tgtLayer != null && tgtDataSet != null && tgtSelection != null && !tgtSelection.isEmpty()) {
    401                     Main.map.mapView.setActiveLayer(tgtLayer);
    402                     tgtLayer.setVisible(true);
    403                     tgtDataSet.setSelected(tgtSelection);
    404                 }
    405             }
    406         }
    407 
    408         class RestoreSourceAction extends JosmAction {
    409 
    410             public RestoreSourceAction() {
    411                 super(tr("Restore"), null, tr("Restore source selection"), null, false);
     395                if (subjectLayer != null && subjectDataSet != null && subjectSelection != null && !subjectSelection.isEmpty()) {
     396                    Main.map.mapView.setActiveLayer(subjectLayer);
     397                    subjectLayer.setVisible(true);
     398                    subjectDataSet.setSelected(subjectSelection);
     399                }
     400            }
     401        }
     402
     403        class RestoreReferenceAction extends JosmAction {
     404
     405            public RestoreReferenceAction() {
     406                super(tr("Restore"), null, tr("Restore reference selection"), null, false);
    412407            }
    413408
    414409            @Override
    415410            public void actionPerformed(ActionEvent e) {
    416                 if (srcLayer != null && srcDataSet != null && srcSelection != null && !srcSelection.isEmpty()) {
    417                     Main.map.mapView.setActiveLayer(srcLayer);
    418                     srcLayer.setVisible(true);
    419                     srcDataSet.setSelected(srcSelection);
    420                 }
    421             }
    422         }
    423 
    424         class FreezeTargetAction extends JosmAction {
    425 
    426             public FreezeTargetAction() {
    427                 super(tr("Freeze"), null, tr("Freeze target selection"), null, false);
     411                if (referenceLayer != null && referenceDataSet != null && referenceSelection != null && !referenceSelection.isEmpty()) {
     412                    Main.map.mapView.setActiveLayer(referenceLayer);
     413                    referenceLayer.setVisible(true);
     414                    referenceDataSet.setSelected(referenceSelection);
     415                }
     416            }
     417        }
     418
     419        class FreezeSubjectAction extends JosmAction {
     420
     421            public FreezeSubjectAction() {
     422                super(tr("Freeze"), null, tr("Freeze subject selection"), null, false);
    428423            }
    429424
    430425            @Override
    431426            public void actionPerformed(ActionEvent e) {
    432                 if (tgtDataSet != null && tgtDataSet == Main.main.getCurrentDataSet()) {
    433 //                targetDataSet.removeDataSetListener(this); FIXME:
    434                 }
    435                 tgtDataSet = Main.main.getCurrentDataSet();
    436 //            targetDataSet.addDataSetListener(tableModel); FIXME:
    437                 tgtLayer = Main.main.getEditLayer();
    438                 if (tgtDataSet == null || tgtLayer == null) {
     427                if (subjectDataSet != null && subjectDataSet == Main.main.getCurrentDataSet()) {
     428//                subjectDataSet.removeDataSetListener(this); FIXME:
     429                }
     430                subjectDataSet = Main.main.getCurrentDataSet();
     431//            subjectDataSet.addDataSetListener(tableModel); FIXME:
     432                subjectLayer = Main.main.getEditLayer();
     433                if (subjectDataSet == null || subjectLayer == null) {
    439434                    JOptionPane.showMessageDialog(Main.parent, tr("No valid OSM data layer present."),
    440435                            tr("Error freezing selection"), JOptionPane.ERROR_MESSAGE);
    441436                    return;
    442437                }
    443                 tgtSelection = new ArrayList<OsmPrimitive>(tgtDataSet.getSelected());
    444                 if (tgtSelection.isEmpty()) {
     438                subjectSelection = new ArrayList<OsmPrimitive>(subjectDataSet.getSelected());
     439                if (subjectSelection.isEmpty()) {
    445440                    JOptionPane.showMessageDialog(Main.parent, tr("Nothing is selected, please try again."),
    446441                            tr("Empty selection"), JOptionPane.ERROR_MESSAGE);
     
    451446                int numWays = 0;
    452447                int numRelations = 0;
    453                 for (OsmPrimitive p : tgtSelection) {
     448                for (OsmPrimitive p : subjectSelection) {
    454449                    switch (p.getType()) {
    455450                        case NODE:
     
    466461
    467462                // FIXME: translate correctly
    468                 targetLayerLabel.setText(tgtLayer.getName());
    469                 targetSelectionLabel.setText(String.format("Rel.: %d / Ways: %d / Nodes: %d", numRelations, numWays, numNodes));
    470             }
    471         }
    472 
    473         class FreezeSourceAction extends JosmAction {
    474 
    475             public FreezeSourceAction() {
    476                 super(tr("Freeze"), null, tr("Freeze target selection"), null, false);
     463                subjectLayerLabel.setText(subjectLayer.getName());
     464                subjectSelectionLabel.setText(String.format("Rel.: %d / Ways: %d / Nodes: %d", numRelations, numWays, numNodes));
     465            }
     466        }
     467
     468        class FreezeReferenceAction extends JosmAction {
     469
     470            public FreezeReferenceAction() {
     471                super(tr("Freeze"), null, tr("Freeze subject selection"), null, false);
    477472            }
    478473
    479474            @Override
    480475            public void actionPerformed(ActionEvent e) {
    481                 if (srcDataSet != null && srcDataSet == Main.main.getCurrentDataSet()) {
    482 //                sourceDataSet.removeDataSetListener(this); FIXME:
    483                 }
    484                 srcDataSet = Main.main.getCurrentDataSet();
    485 //            sourceDataSet.addDataSetListener(this); FIXME:
    486                 srcLayer = Main.main.getEditLayer();
    487                 if (srcDataSet == null || srcLayer == null) {
     476                if (referenceDataSet != null && referenceDataSet == Main.main.getCurrentDataSet()) {
     477//                referenceDataSet.removeDataSetListener(this); FIXME:
     478                }
     479                referenceDataSet = Main.main.getCurrentDataSet();
     480//            referenceDataSet.addDataSetListener(this); FIXME:
     481                referenceLayer = Main.main.getEditLayer();
     482                if (referenceDataSet == null || referenceLayer == null) {
    488483                    JOptionPane.showMessageDialog(Main.parent, tr("No valid OSM data layer present."),
    489484                            tr("Error freezing selection"), JOptionPane.ERROR_MESSAGE);
    490485                    return;
    491486                }
    492                 srcSelection = new ArrayList<OsmPrimitive>(srcDataSet.getSelected());
    493                 if (srcSelection.isEmpty()) {
     487                referenceSelection = new ArrayList<OsmPrimitive>(referenceDataSet.getSelected());
     488                if (referenceSelection.isEmpty()) {
    494489                    JOptionPane.showMessageDialog(Main.parent, tr("Nothing is selected, please try again."),
    495490                            tr("Empty selection"), JOptionPane.ERROR_MESSAGE);
     
    500495                int numWays = 0;
    501496                int numRelations = 0;
    502                 for (OsmPrimitive p : srcSelection) {
     497                for (OsmPrimitive p : referenceSelection) {
    503498                    switch (p.getType()) {
    504499                        case NODE:
     
    515510
    516511                // FIXME: translate correctly
    517                 sourceLayerLabel.setText(srcLayer.getName());
    518                 sourceSelectionLabel.setText(String.format("Rel.: %d / Ways: %d / Nodes: %d", numRelations, numWays, numNodes));
     512                referenceLayerLabel.setText(referenceLayer.getName());
     513                referenceSelectionLabel.setText(String.format("Rel.: %d / Ways: %d / Nodes: %d", numRelations, numWays, numNodes));
    519514            }
    520515        }
     
    530525        for (OsmPrimitive p : prims) {
    531526            for (ConflationCandidate c : candidates) {
    532                 if (c.getSourcePrimitive().equals(p) || c.getTargetPrimitive().equals(p)) {
     527                if (c.getReferenceObject().equals(p) || c.getSubjectObject().equals(p)) {
    533528                    candidates.remove(c);
    534529                    break;
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/ConflationUtils.java

    r27959 r27971  
    1 /*
    2  * To change this template, choose Tools | Templates
    3  * and open the template in the editor.
    4  */
    51package org.openstreetmap.josm.plugins.conflation;
    62
     
    106import org.openstreetmap.josm.data.osm.OsmPrimitive;
    117
    12 /**
    13  *
    14  * @author Josh
    15  */
    168public final class ConflationUtils {
    179    private final static double MAX_COST = Double.MAX_VALUE;
     
    2719     * now, later we can also use dissimilarity between tags.
    2820     *
    29      * @param   source      the reference <code>OsmPrimitive</code>.
    30      * @param   target   the non-reference <code>OsmPrimitive</code>.
     21     * @param   referenceObject      the reference <code>OsmPrimitive</code>.
     22     * @param   subjectObject   the non-reference <code>OsmPrimitive</code>.
    3123     */
    32     public static double calcCost(OsmPrimitive source, OsmPrimitive target) {
    33         if (source==target) {
     24    public static double calcCost(OsmPrimitive referenceObject, OsmPrimitive subjectObject) {
     25        if (referenceObject==subjectObject) {
    3426            return MAX_COST;
    3527        }
    3628       
    3729        try {
    38             return getCenter(source).distance(getCenter(target));
     30            return getCenter(referenceObject).distance(getCenter(subjectObject));
    3931        } catch (Exception e) {
    4032            return MAX_COST;
  • applications/editors/josm/plugins/conflation/src/org/openstreetmap/josm/plugins/conflation/MatchTableModel.java

    r27959 r27971  
    1 /*
    2  * To change this template, choose Tools | Templates
    3  * and open the template in the editor.
    4  */
    51package org.openstreetmap.josm.plugins.conflation;
    62
     
    106import org.openstreetmap.josm.data.osm.OsmPrimitive;
    117import org.openstreetmap.josm.data.osm.TagCollection;
     8import static org.openstreetmap.josm.tools.I18n.tr;
    129
    1310/**
     
    1714
    1815    private ConflationCandidateList candidates = null;
    19     private final static String[] columnNames = {"Mine", "Theirs", "Distance (m)", "Cost", "Tags"};
     16    private final static String[] columnNames = {tr("Reference"), tr("Subject"), "Distance (m)", "Cost", "Tags"};
    2017
    2118    @Override
     
    4239        ConflationCandidate c = candidates.get(row);
    4340        if (col == 0) {
    44             return c.getSourcePrimitive();
     41            return c.getReferenceObject();
    4542        } else if (col == 1) {
    46             return c.getTargetPrimitive();
     43            return c.getSubjectObject();
    4744        } else if (col == 2) {
    4845            return c.getDistance();
     
    5249        if (col == 4) {
    5350            HashSet<OsmPrimitive> set = new HashSet<OsmPrimitive>();
    54             set.add(c.getSourcePrimitive());
    55             set.add(c.getTargetPrimitive());
     51            set.add(c.getReferenceObject());
     52            set.add(c.getSubjectObject());
    5653            TagCollection tags = TagCollection.unionOfAllPrimitives(set);
    5754            Set<String> keys = tags.getKeysWithMultipleValues();
  • applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/dumbutils/ReplaceGeometryAction.java

    r27937 r27971  
    8080     * Replace or upgrade a node to a way or multipolygon
    8181     *
    82      * @param node
    83      * @param target
     82     * @param subjectNode node to be replaced
     83     * @param referenceObject object with greater spatial quality
    8484     */
    85     public boolean replaceNode(Node node, OsmPrimitive target) {
    86         if (!OsmPrimitive.getFilteredList(node.getReferrers(), Way.class).isEmpty()) {
     85    public boolean replaceNode(Node subjectNode, OsmPrimitive referenceObject) {
     86        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
    8787            JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
    8888                    TITLE, JOptionPane.INFORMATION_MESSAGE);
     
    9090        }
    9191
    92         if (target instanceof Relation && !((Relation) target).isMultipolygon()) {
     92        if (referenceObject instanceof Relation && !((Relation) referenceObject).isMultipolygon()) {
    9393            JOptionPane.showMessageDialog(Main.parent, tr("Relation is not a multipolygon, cannot be used as a replacement."),
    9494                    TITLE, JOptionPane.INFORMATION_MESSAGE);
     
    9898        Node nodeToReplace = null;
    9999        // see if we need to replace a node in the replacement way to preserve connection in history
    100         if (!node.isNew()) {
     100        if (!subjectNode.isNew()) {
    101101            // Prepare a list of nodes that are not important
    102102            Collection<Node> nodePool = new HashSet<Node>();
    103             if (target instanceof Way) {
    104                 nodePool.addAll(getUnimportantNodes((Way) target));
    105             } else if (target instanceof Relation) {
    106                 for (RelationMember member : ((Relation) target).getMembers()) {
     103            if (referenceObject instanceof Way) {
     104                nodePool.addAll(getUnimportantNodes((Way) referenceObject));
     105            } else if (referenceObject instanceof Relation) {
     106                for (RelationMember member : ((Relation) referenceObject).getMembers()) {
    107107                    if ((member.getRole().equals("outer") || member.getRole().equals("inner"))
    108108                            && member.isWay()) {
     
    115115                assert false;
    116116            }
    117             nodeToReplace = findNearestNode(node, nodePool);
     117            nodeToReplace = findNearestNode(subjectNode, nodePool);
    118118        }
    119119
    120120        List<Command> commands = new ArrayList<Command>();
    121         AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) node.getKeys();
     121        AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) subjectNode.getKeys();
    122122
    123123        // merge tags
    124         Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(node, target);
     124        Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(subjectNode, referenceObject);
    125125        if (tagResolutionCommands == null) {
    126126            // user canceled tag merge dialog
     
    135135            List<Node> wayNodes = parentWay.getNodes();
    136136            int idx = wayNodes.indexOf(nodeToReplace);
    137             wayNodes.set(idx, node);
     137            wayNodes.set(idx, subjectNode);
    138138            if (idx == 0 && parentWay.isClosed()) {
    139139                // node is at start/end of way
    140                 wayNodes.set(wayNodes.size() - 1, node);
     140                wayNodes.set(wayNodes.size() - 1, subjectNode);
    141141            }
    142142            commands.add(new ChangeNodesCommand(parentWay, wayNodes));
    143             commands.add(new MoveCommand(node, nodeToReplace.getCoor()));
     143            commands.add(new MoveCommand(subjectNode, nodeToReplace.getCoor()));
    144144            commands.add(new DeleteCommand(nodeToReplace));
    145145
     
    147147            if (!nodeTags.isEmpty()) {
    148148                for (String key : nodeTags.keySet()) {
    149                     commands.add(new ChangePropertyCommand(node, key, null));
     149                    commands.add(new ChangePropertyCommand(subjectNode, key, null));
    150150                }
    151151
     
    153153        } else {
    154154            // no node to replace, so just delete the original node
    155             commands.add(new DeleteCommand(node));
    156         }
    157 
    158         getCurrentDataSet().setSelected(target);
     155            commands.add(new DeleteCommand(subjectNode));
     156        }
     157
     158        getCurrentDataSet().setSelected(referenceObject);
    159159
    160160        Main.main.undoRedo.add(new SequenceCommand(
    161                 tr("Replace geometry for node {0}", node.getDisplayName(DefaultNameFormatter.getInstance())),
     161                tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())),
    162162                commands));
    163163        return true;
     
    184184            }
    185185        }
    186         Way geometry = selection.get(idxNew);
    187         Way way = selection.get(1 - idxNew);
    188        
    189         if( !overrideNewCheck && (way.isNew() || !geometry.isNew()) ) {
     186        Way referenceWay = selection.get(idxNew);
     187        Way subjectWay = selection.get(1 - idxNew);
     188       
     189        if( !overrideNewCheck && (subjectWay.isNew() || !referenceWay.isNew()) ) {
    190190            JOptionPane.showMessageDialog(Main.parent,
    191191                    tr("Please select one way that exists in the database and one new way with correct geometry."),
     
    193193            return false;
    194194        }
     195        return replaceWayWithWay(subjectWay, referenceWay);
     196    }
     197   
     198    public static boolean replaceWayWithWay(Way subjectWay, Way referenceWay) {
    195199
    196200        Area a = getCurrentDataSet().getDataSourceArea();
    197         if (!isInArea(way, a) || !isInArea(geometry, a)) {
     201        if (!isInArea(subjectWay, a) || !isInArea(referenceWay, a)) {
    198202            JOptionPane.showMessageDialog(Main.parent,
    199203                    tr("The ways must be entirely within the downloaded area."),
     
    202206        }
    203207       
    204         if (hasImportantNode(geometry, way)) {
     208        if (hasImportantNode(referenceWay, subjectWay)) {
    205209            JOptionPane.showMessageDialog(Main.parent,
    206210                    tr("The way to be replaced cannot have any nodes with properties or relation memberships unless they belong to both ways."),
     
    212216               
    213217        // merge tags
    214         Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(geometry, way);
     218        Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(referenceWay, subjectWay);
    215219        if (tagResolutionCommands == null) {
    216220            // user canceled tag merge dialog
     
    220224       
    221225        // Prepare a list of nodes that are not used anywhere except in the way
    222         Collection<Node> nodePool = getUnimportantNodes(way);
     226        Collection<Node> nodePool = getUnimportantNodes(subjectWay);
    223227
    224228        // And the same for geometry, list nodes that can be freely deleted
    225229        Set<Node> geometryPool = new HashSet<Node>();
    226         for( Node node : geometry.getNodes() ) {
     230        for( Node node : referenceWay.getNodes() ) {
    227231            List<OsmPrimitive> referrers = node.getReferrers();
    228232            if( node.isNew() && !node.isDeleted() && referrers.size() == 1
    229                     && referrers.get(0).equals(geometry) && !way.containsNode(node)
     233                    && referrers.get(0).equals(referenceWay) && !subjectWay.containsNode(node)
    230234                    && !hasInterestingKey(node))
    231235                geometryPool.add(node);
     
    248252
    249253        // And prepare a list of nodes with all the replacements
    250         List<Node> geometryNodes = geometry.getNodes();
     254        List<Node> geometryNodes = referenceWay.getNodes();
    251255        for( int i = 0; i < geometryNodes.size(); i++ )
    252256            if( nodeAssoc.containsKey(geometryNodes.get(i)) )
     
    254258
    255259        // Now do the replacement
    256         commands.add(new ChangeNodesCommand(way, geometryNodes));
     260        commands.add(new ChangeNodesCommand(subjectWay, geometryNodes));
    257261
    258262        // Move old nodes to new positions
     
    261265
    262266        // Remove geometry way from selection
    263         getCurrentDataSet().clearSelection(geometry);
     267        getCurrentDataSet().clearSelection(referenceWay);
    264268
    265269        // And delete old geometry way
    266         commands.add(new DeleteCommand(geometry));
     270        commands.add(new DeleteCommand(referenceWay));
    267271
    268272        // Delete nodes that are not used anymore
     
    272276        // Two items in undo stack: change original way and delete geometry way
    273277        Main.main.undoRedo.add(new SequenceCommand(
    274                 tr("Replace geometry for way {0}", way.getDisplayName(DefaultNameFormatter.getInstance())),
     278                tr("Replace geometry for way {0}", subjectWay.getDisplayName(DefaultNameFormatter.getInstance())),
    275279                commands));
    276280        return true;
     
    283287     * @return
    284288     */
    285     protected Collection<Node> getUnimportantNodes(Way way) {
     289    protected static Collection<Node> getUnimportantNodes(Way way) {
    286290        Set<Node> nodePool = new HashSet<Node>();
    287291        for (Node n : way.getNodes()) {
     
    302306     * @return
    303307     */
    304     protected boolean hasImportantNode(Way geometry, Way way) {
     308    protected static boolean hasImportantNode(Way geometry, Way way) {
    305309        for (Node n : way.getNodes()) {
    306310            // if original and replacement way share a node, it's safe to replace
     
    321325    }
    322326   
    323     protected boolean hasInterestingKey(OsmPrimitive object) {
     327    protected static boolean hasInterestingKey(OsmPrimitive object) {
    324328        for (String key : object.getKeys().keySet()) {
    325329            if (!OsmPrimitive.isUninterestingKey(key)) {
     
    355359     * needed.
    356360     *
    357      * @param source
    358      * @param target
     361     * @param source object tags are merged from
     362     * @param target object tags are merged to
    359363     * @return
    360364     */
    361     public List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) {
     365    protected static List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) {
    362366        Collection<OsmPrimitive> primitives = Arrays.asList(source, target);
    363367       
     
    394398     * @return null if there is no such node.
    395399     */
    396     private Node findNearestNode( Node node, Collection<Node> nodes ) {
     400    protected static Node findNearestNode( Node node, Collection<Node> nodes ) {
    397401        if( nodes.contains(node) )
    398402            return node;
Note: See TracChangeset for help on using the changeset viewer.