Ignore:
Timestamp:
2011-12-28T13:17:21+01:00 (13 years ago)
Author:
oliverw
Message:

Bugfix #7181: FixAddresses plugin works very slow. Guessers work on smaller set now; result is changed after guess task has finished instead of updating the table continously.

Location:
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/GuessAddressRunnable.java

    r27321 r27326  
    2222
    2323import org.openstreetmap.josm.Main;
    24 import org.openstreetmap.josm.data.coor.LatLon;
    25 import org.openstreetmap.josm.data.osm.Changeset;
    2624import org.openstreetmap.josm.data.osm.Node;
    27 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    28 import org.openstreetmap.josm.data.osm.Relation;
    2925import org.openstreetmap.josm.data.osm.Way;
    30 import org.openstreetmap.josm.data.osm.visitor.Visitor;
    3126import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3227import org.openstreetmap.josm.io.OsmTransferException;
     
    4338        private List<OSMAddress> addressesToGuess;
    4439        private List<IProgressMonitorFinishedListener> finishListeners = new ArrayList<IProgressMonitorFinishedListener>();
    45         private double minDist;
    46         private OSMAddress curAddressNode;
    4740        private boolean isRunning = false;
    4841        private boolean canceled;
    4942
     43        private GuessedValueHandler[] wayGuessers = new GuessedValueHandler[]{new GuessStreetValueHandler(TagUtils.ADDR_STREET_TAG)};
     44        private GuessedValueHandler[] nodeGuessers = new GuessedValueHandler[]{
     45                        new GuessedValueHandler(TagUtils.ADDR_POSTCODE_TAG, 500.0),
     46                        new GuessedValueHandler(TagUtils.ADDR_CITY_TAG, 5000.0),
     47                        new GuessedValueHandler(TagUtils.ADDR_STATE_TAG, 5000.0),
     48                        new GuessedValueHandler(TagUtils.ADDR_COUNTRY_TAG, 5000.0),
     49                        new GuessedValueHandler(TagUtils.ADDR_CITY_TAG, 2000.0)
     50        };
    5051
    5152        /**
     
    110111        protected void fireFinished() {
    111112                for (IProgressMonitorFinishedListener l : finishListeners) {
    112                         l.finished();
     113                        l.finished();                   
    113114                }
    114115                // this event is fired only once, then we disconnect all listeners
    115116                finishListeners.clear();
    116117        }
    117        
     118
    118119        /* (non-Javadoc)
    119120         * @see org.openstreetmap.josm.gui.PleaseWaitRunnable#cancel()
     
    137138        @Override
    138139        protected void realRun() throws SAXException, IOException,
    139                         OsmTransferException {
    140                
     140        OsmTransferException {
     141
    141142                if (Main.main.getCurrentDataSet() == null || addressesToGuess == null) return;
    142143
     
    150151                        progressMonitor.setTicksCount(addressesToGuess.size());
    151152
    152 
    153 
    154153                        List<OSMAddress> shadowCopy = new ArrayList<OSMAddress>(addressesToGuess);
    155154                        for (OSMAddress aNode : shadowCopy) {
    156                                 minDist = Double.MAX_VALUE;
    157                                 curAddressNode = aNode;
    158 
    159                                 // setup guessing handlers for address tags
    160                                 GuessedValueHandler[] guessers = new GuessedValueHandler[]{
    161                                                 new GuessStreetValueHandler(TagUtils.ADDR_STREET_TAG, aNode),
    162                                                 new GuessedValueHandler(TagUtils.ADDR_POSTCODE_TAG, aNode, 500.0),
    163                                                 new GuessedValueHandler(TagUtils.ADDR_CITY_TAG, aNode, 5000.0),
    164                                                 new GuessedValueHandler(TagUtils.ADDR_STATE_TAG, aNode, 5000.0),
    165                                                 new GuessedValueHandler(TagUtils.ADDR_COUNTRY_TAG, aNode, 5000.0),
    166                                                 new GuessedValueHandler(TagUtils.ADDR_CITY_TAG, aNode, 2000.0)
    167                                 };
    168 
    169155                                if (!aNode.needsGuess()) { // nothing to do
    170156                                        progressMonitor.worked(1);
     
    176162                                        break;
    177163                                }
     164
    178165                                // Update progress monitor
    179166                                progressMonitor.subTask(tr("Guess values for ") + aNode);
    180167
    181                                 // visit osm data
    182                                 for (OsmPrimitive osmPrimitive : Main.main.getCurrentDataSet().allPrimitives()) {
    183                                         if (canceled) {
    184                                                 break;
    185                                         }
    186 
    187                                         // guess values
    188                                         for (int i = 0; i < guessers.length; i++) {
    189                                                 osmPrimitive.visit(guessers[i]);
    190 
    191                                                 if (guessers[i].currentValue == null && i == 0) {
    192                                                         //System.err.println("Guess #" + i + " failed for " + aNode);
     168                                // Run way-related guessers
     169                                for (int i = 0; i < wayGuessers.length; i++) {
     170                                        GuessedValueHandler guesser = wayGuessers[i];
     171                                       
     172                                        guesser.setAddressNode(aNode);
     173
     174                                        // visit osm data
     175                                        for (Way way : Main.main.getCurrentDataSet().getWays()) {
     176                                                if (canceled) {
     177                                                        break;
    193178                                                }
     179                                                way.visit(guesser);                                             
     180                                        }
     181                                       
     182                                        String guessedVal = guesser.getCurrentValue();
     183                                        if (guessedVal != null) {
     184                                                aNode.setGuessedValue(guesser.getTag(), guessedVal, guesser.getSourceNode());
     185                                        }
     186                                }
     187                               
     188                                // Run node-related guessers
     189                                for (int i = 0; i < nodeGuessers.length; i++) {
     190                                        GuessedValueHandler guesser = nodeGuessers[i];
     191                                       
     192                                        guesser.setAddressNode(aNode);
     193
     194                                        // visit osm data
     195                                        for (Node node : Main.main.getCurrentDataSet().getNodes()) {
     196                                                if (canceled) {
     197                                                        break;
     198                                                }
     199                                                node.visit(guesser);                                           
     200                                        }
     201                                       
     202                                        String guessedVal = guesser.getCurrentValue();
     203                                        if (guessedVal != null) {
     204                                                aNode.setGuessedValue(guesser.getTag(), guessedVal, guesser.getSourceNode());
    194205                                        }
    195206                                }
     
    204215        }
    205216
     217        // TODO: Put in separate file
    206218        private class GuessStreetValueHandler extends GuessedValueHandler {
     219                public GuessStreetValueHandler(String tag) {
     220                        this(tag, null);
     221                }
    207222
    208223                public GuessStreetValueHandler(String tag, OSMAddress aNode) {
     
    225240                        if (TagUtils.isStreetSupportingHousenumbers(w)) {
    226241                                OSMAddress aNode = getAddressNode();
    227                                 double dist = OsmUtils.getMinimumDistanceToWay(aNode.getCoor(), w);
    228 
    229                                 if (dist < minDist && dist < getMaxDistance()) {
    230                                         System.out.println(String.format("New guess %s: %4.2f m", TagUtils.getNameValue(w), dist));
    231                                         minDist = dist;
    232                                         currentValue = TagUtils.getNameValue(w);
    233                                         aNode.setGuessedValue(getTag(), currentValue, w);
    234                                 } else {
    235                                         //System.out.println(String.format("Skipped %s: %4.2f m", TagUtils.getNameValue(w), dist));
     242                                String newVal = TagUtils.getNameValue(w);
     243                               
     244                                if (newVal != null) {
     245                                        double dist = OsmUtils.getMinimumDistanceToWay(aNode.getCoor(), w);
     246                                       
     247                                        if (dist < minDist && dist < getMaxDistance()) {
     248                                                minDist = dist;
     249                                                currentValue = newVal;
     250                                                srcNode = w;
     251                                                //aNode.setGuessedValue(getTag(), currentValue, w);
     252                                        } else {
     253                                                //System.out.println(String.format("Skipped %s: %4.2f m", TagUtils.getNameValue(w), dist));
     254                                        }
    236255                                }
    237256                        }
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/GuessedValueHandler.java

    r25373 r27326  
    1616import org.openstreetmap.josm.data.osm.Changeset;
    1717import org.openstreetmap.josm.data.osm.Node;
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1819import org.openstreetmap.josm.data.osm.Relation;
    1920import org.openstreetmap.josm.data.osm.Way;
     
    3738        private OSMAddress aNode;
    3839        private double maxDist = DEFAULT_MAX_DIST;
    39 
     40        protected OsmPrimitive srcNode;
     41
     42        /**
     43         * Instantiates a new guessed value handler without node and default maximum distance.
     44         *
     45         * @param tag the tag to find the guessed value for.
     46         */
     47        public GuessedValueHandler(String tag) {
     48                this(tag, null, DEFAULT_MAX_DIST);
     49        }
     50       
     51        /**
     52         * Instantiates a new guessed value handler without node.
     53         *
     54         * @param tag the tag to find the guessed value for.
     55         * @param maxDist the maximum distance for a node/way to be considered as guessed value.
     56         */
     57        public GuessedValueHandler(String tag, double maxDist) {
     58                this(tag, null, maxDist);
     59        }
     60       
    4061        /**
    4162         * Instantiates a new guessed value handler.
     
    6384                }
    6485
    65                 if (aNode == null) {
    66                         throw new RuntimeException("Address node must not be null!");
    67                 }
    68 
    6986                if (maxDist < 1.0) { // clip value
    7087                        maxDist = 1.0;
    7188                }
     89               
    7290                this.tag = tag;
    73 
    74                 minDist = Double.MAX_VALUE;
    75                 this.aNode = aNode;
    7691                this.maxDist = maxDist;
     92                setAddressNode(aNode);         
    7793        }
    7894
     
    84100        protected OSMAddress getAddressNode() {
    85101                return aNode;
     102        }
     103       
     104
     105        /**
     106         * Sets the address node to make the guess for.
     107         * @param aNode
     108         */
     109        public void setAddressNode(OSMAddress aNode) {         
     110                this.aNode = aNode;
     111                // reset search results
     112                minDist = Double.MAX_VALUE;
     113                srcNode = null;
     114                currentValue = null;           
    86115        }
    87116
     
    120149         * @return the currentValue
    121150         */
    122         protected String getCurrentValue() {
     151        public String getCurrentValue() {
    123152                return currentValue;
     153        }
     154       
     155       
     156        /**
     157         * Gets the node/way which has been selected for the guess.
     158         * @return The source node or null; if no appropriate source primitive has been found
     159         */
     160        public OsmPrimitive getSourceNode() {
     161                return srcNode;
    124162        }
    125163
     
    138176        @Override
    139177        public void visit(Node n) {
     178                assert aNode != null;
     179               
    140180                if (n.hasKey(tag)) {
    141181                        double dist = n.getCoor().greatCircleDistance(aNode.getCoor());
     
    143183                                minDist = dist;
    144184                                currentValue = n.get(tag);
    145                                 aNode.setGuessedValue(tag, currentValue, n);
     185                                srcNode = n;
    146186                        }
    147187                }
     
    153193        @Override
    154194        public void visit(Way w) {
     195                assert aNode != null;
     196               
    155197                if (w.hasKey(tag)) {
    156198                        double dist = OsmUtils.getMinimumDistanceToWay(aNode.getCoor(), w);
     
    158200                                minDist = dist;
    159201                                currentValue = w.get(tag);
    160                                 aNode.setGuessedValue(tag, currentValue, w);
     202                                srcNode = w;
    161203                        }
    162204                }
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/OSMAddress.java

    r27322 r27326  
    2121import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2222import org.openstreetmap.josm.plugins.fixAddresses.gui.actions.AddressActions;
    23 import org.openstreetmap.josm.plugins.fixAddresses.gui.actions.ApplyAllGuessesAction;
    2423import org.openstreetmap.josm.tools.CheckParameterUtil;
    2524
     
    548547         * @param tag the tag to set the guess for
    549548         * @param value the value of the guessed tag.
     549         * @param osm the (optional) object which was used for the guess
    550550         */
    551551        public void setGuessedValue(String tag, String value, OsmPrimitive osm) {
     
    713713                AddressSolution s = new AddressSolution(
    714714                                String.format("%s '%s'", tr("Assign to"), getGuessedValue(tag)),
    715                                 new ApplyAllGuessesAction(tag),
     715                                AddressActions.getApplyGuessesAction(),
    716716                                SolutionType.Change);
    717717
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AddressEditTableModel.java

    r25373 r27326  
    1919
    2020import javax.swing.JTable;
     21import javax.swing.SwingUtilities;
    2122import javax.swing.event.TableModelEvent;
    2223import javax.swing.table.DefaultTableModel;
     
    4445        @Override
    4546        public void containerChanged(AddressEditContainer container) {
    46                 fireTableDataChanged(); // update model
     47                if (SwingUtilities.isEventDispatchThread()) {
     48                        fireTableDataChanged(); // update model
     49                } else {
     50                        SwingUtilities.invokeLater(new Runnable() {
     51                               
     52                                @Override
     53                                public void run() {
     54                                        fireTableDataChanged(); // update model                                 
     55                                }
     56                        });
     57                }
    4758        }
    4859
Note: See TracChangeset for help on using the changeset viewer.