Changeset 24982 in osm
- Timestamp:
- 2011-01-05T16:05:05+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressEditContainer.java
r24978 r24982 71 71 */ 72 72 73 public class AddressEditContainer implements Visitor, DataSetListener, IAddressEditContainerListener, IProblemVisitor {73 public class AddressEditContainer implements Visitor, DataSetListener, IAddressEditContainerListener, IProblemVisitor, IAllKnowingTrashHeap { 74 74 75 75 private Collection<? extends OsmPrimitive> workingSet; … … 218 218 if (aNode != null) { 219 219 addAndClassifyAddress(aNode); 220 aNode.visit(this );220 aNode.visit(this, this); 221 221 } 222 222 markNodeAsVisited(n); … … 542 542 synchronized (this) { 543 543 clearData(); 544 clearProblems(); 545 544 546 for (OsmPrimitive osmPrimitive : osmData) { 545 547 osmPrimitive.visit(this); … … 721 723 } 722 724 } 725 726 /* (non-Javadoc) 727 * @see org.openstreetmap.josm.plugins.fixAddresses.IAllKnowingTrashHeap#getClosestStreetName(java.lang.String) 728 */ 729 @Override 730 public String getClosestStreetName(String name) { 731 List<String> matches = getClosestStreetNames(name, 1); 732 733 if (matches != null && matches.size() > 0) { 734 return matches.get(0); 735 } 736 737 return null; 738 } 739 740 /* (non-Javadoc) 741 * @see org.openstreetmap.josm.plugins.fixAddresses.IAllKnowingTrashHeap#getClosestStreetNames(java.lang.String, int) 742 */ 743 @Override 744 public List<String> getClosestStreetNames(String name, int maxEntries) { 745 CheckParameterUtil.ensureParameterNotNull(name, "name"); 746 747 // ensure right number of entries 748 if (maxEntries < 1) maxEntries = 1; 749 750 List<StreetScore> scores = new ArrayList<StreetScore>(); 751 List<String> matches = new ArrayList<String>(); 752 753 // Find the longest common sub string 754 for (String streetName : streetDict.keySet()) { 755 int score = StringUtils.lcsLength(name, streetName); 756 757 if (score > 3) { // reasonable value? 758 StreetScore sc = new StreetScore(streetName, score); 759 scores.add(sc); 760 } 761 } 762 763 // sort by score 764 Collections.sort(scores); 765 766 // populate result list 767 int n = Math.min(maxEntries, scores.size()); 768 for (int i = 0; i < n; i++) { 769 matches.add(scores.get(i).getName()); 770 } 771 772 return matches; 773 } 774 775 /* (non-Javadoc) 776 * @see org.openstreetmap.josm.plugins.fixAddresses.IAllKnowingTrashHeap#isValidStreetName(java.lang.String) 777 */ 778 @Override 779 public boolean isValidStreetName(String name) { 780 if (streetDict == null) return false; 781 782 return streetDict.containsKey(name); 783 } 784 785 /** 786 * Internal class to handle results of {@link AddressEditContainer#getClosestStreetNames(String, int)}. 787 */ 788 private class StreetScore implements Comparable<StreetScore> { 789 private String name; 790 private int score; 791 792 /** 793 * @param name Name of the street. 794 * @param score Score of the street (length of longest common substring) 795 */ 796 public StreetScore(String name, int score) { 797 super(); 798 this.name = name; 799 this.score = score; 800 } 801 802 /** 803 * @return the name of the street. 804 */ 805 protected String getName() { 806 return name; 807 } 808 809 /** 810 * @return the score of the street. 811 */ 812 protected int getScore() { 813 return score; 814 } 815 816 @Override 817 public int compareTo(StreetScore arg0) { 818 if (arg0 == null) return 1; 819 820 return new Integer(score).compareTo(new Integer(arg0.score)); 821 } 822 } 723 823 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/IOSMEntity.java
r24978 r24982 74 74 * Collects problems and possible solutions. 75 75 * 76 * @param trashHeap the trash heap to ask for possible solutions 76 77 * @param visitor the problem visitor 77 78 */ 78 public void visit(I ProblemVisitor visitor);79 public void visit(IAllKnowingTrashHeap trashHeap, IProblemVisitor visitor); 79 80 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/OSMAddress.java
r24978 r24982 14 14 package org.openstreetmap.josm.plugins.fixAddresses; 15 15 16 import static org.openstreetmap.josm.tools.I18n.tr; 16 17 import java.util.Collection; 17 18 import java.util.HashMap; 18 19 19 20 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 import org.openstreetmap.josm.plugins.fixAddresses.gui.actions.ApplyAllGuessesAction; 22 import org.openstreetmap.josm.plugins.fixAddresses.gui.actions.RemoveAddressTagsAction; 20 23 import org.openstreetmap.josm.tools.CheckParameterUtil; 21 24 … … 87 90 88 91 /** 89 * Gets the tag value with guess. If the object does not have the given tag, this me htod looks for92 * Gets the tag value with guess. If the object does not have the given tag, this method looks for 90 93 * an appropriate guess. If both, real value and guess, are missing, a question mark is returned. 91 94 * … … 214 217 public void applyAllGuesses() { 215 218 for (String tag : guessedValues.keySet()) { 219 applyGuessForTag(tag); 220 } 221 222 // Clear all guesses 223 guessedValues.clear(); 224 guessedObjects.clear(); 225 } 226 227 /** 228 * Apply the guessed value for the given tag. 229 * 230 * @param tag the tag to apply the guessed value for. 231 */ 232 public void applyGuessForTag(String tag) { 233 if (guessedValues.containsKey(tag)) { 216 234 String val = guessedValues.get(tag); 217 235 if (!StringUtils.isNullOrEmpty(val)) { … … 219 237 } 220 238 } 221 222 // Clear all guesses223 guessedValues.clear();224 guessedObjects.clear();225 239 } 226 240 … … 539 553 if (value != null && osm != null) { 540 554 guessedValues.put(tag, value); 541 guessedObjects.put(tag, osm); 555 if (osm != null) { 556 guessedObjects.put(tag, osm); 557 } 542 558 fireEntityChanged(this); 543 559 } … … 631 647 * @see org.openstreetmap.josm.plugins.fixAddresses.OSMEntityBase#visit(org.openstreetmap.josm.plugins.fixAddresses.IProblemVisitor) 632 648 */ 633 public void visit(IProblemVisitor visitor) { 649 @Override 650 public void visit(IAllKnowingTrashHeap trashHeap, IProblemVisitor visitor) { 634 651 CheckParameterUtil.ensureParameterNotNull(visitor, "visitor"); 635 652 653 // Check for street 636 654 if (!hasStreetName()) { 655 AddressProblem p = new AddressProblem(this, tr("Address has no street")); 656 if (hasGuessedStreetName()) { // guess exists -> add solution entry 657 String tag = TagUtils.ADDR_STREET_TAG; 658 addGuessValueSolution(p, tag); 659 } 660 addRemoveAddressTagsSolution(p); 661 visitor.addProblem(p); 662 // Street name exists, but is invalid -> ask the all knowing trash heap 663 } else if (!trashHeap.isValidStreetName(getStreetName())) { 664 AddressProblem p = new AddressProblem(this, tr("Address has no valid street")); 665 String match = trashHeap.getClosestStreetName(getStreetName()); 637 666 638 } 667 if (!StringUtils.isNullOrEmpty(match)) { 668 setGuessedStreetName(match, null); 669 addGuessValueSolution(p, TagUtils.ADDR_STREET_TAG); 670 } 671 visitor.addProblem(p); 672 } 673 674 // Check for postal code 675 if (!hasPostalCode()) { 676 AddressProblem p = new AddressProblem(this, tr("Address has no post code")); 677 if (hasGuessedStreetName()) { 678 String tag = TagUtils.ADDR_POSTCODE_TAG; 679 addGuessValueSolution(p, tag); 680 } 681 addRemoveAddressTagsSolution(p); 682 visitor.addProblem(p); 683 } 684 685 // Check for city 686 if (!hasCity()) { 687 AddressProblem p = new AddressProblem(this, tr("Address has no city")); 688 if (hasGuessedStreetName()) { 689 String tag = TagUtils.ADDR_CITY_TAG; 690 addGuessValueSolution(p, tag); 691 } 692 addRemoveAddressTagsSolution(p); 693 visitor.addProblem(p); 694 } 695 696 // Check for country 697 if (!hasCountry()) { 698 // TODO: Add guess for country 699 AddressProblem p = new AddressProblem(this, tr("Address has no country")); 700 addRemoveAddressTagsSolution(p); 701 visitor.addProblem(p); 702 } 703 } 704 705 /** 706 * Adds the guess value solution to a problem. 707 * 708 * @param p the problem to add the solution to. 709 * @param tag the tag to change. 710 */ 711 private void addGuessValueSolution(AddressProblem p, String tag) { 712 AddressSolution s = new AddressSolution( 713 String.format("%s '%s'", tr("Assign to"), getGuessedValue(tag)), 714 new ApplyAllGuessesAction(tag), 715 SolutionType.Change); 716 717 p.addSolution(s); 718 } 719 720 /** 721 * Adds the remove address tags solution entry to a problem. 722 * 723 * @param problem the problem 724 */ 725 private void addRemoveAddressTagsSolution(IProblem problem) { 726 CheckParameterUtil.ensureParameterNotNull(problem, "problem"); 727 728 AddressSolution s = new AddressSolution( 729 tr("Remove all address tags"), 730 new RemoveAddressTagsAction(), 731 SolutionType.Remove); 732 problem.addSolution(s); 639 733 } 640 734 -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/OSMEntityBase.java
r24978 r24982 203 203 } 204 204 205 @Override 206 public void visit(IProblemVisitor visitor) { 205 /* (non-Javadoc) 206 * @see org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity#visit(org.openstreetmap.josm.plugins.fixAddresses.IAllKnowingTrashHeap, org.openstreetmap.josm.plugins.fixAddresses.IProblemVisitor) 207 */ 208 @Override 209 public void visit(IAllKnowingTrashHeap trashHeap, IProblemVisitor visitor) { 207 210 // do nothing 208 211 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/actions/AbstractAddressEditAction.java
r24978 r24982 176 176 public void entityChanged(IOSMEntity node) { 177 177 container.removeProblemsOfSource(node); // clear problems of changed node... 178 node.visit(container ); // .. and revisit it.178 node.visit(container, container); // .. and revisit it. 179 179 updateEnabledState(); 180 180 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/actions/ApplyAllGuessesAction.java
r24231 r24982 27 27 import org.openstreetmap.josm.plugins.fixAddresses.OSMAddress; 28 28 import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity; 29 import org.openstreetmap.josm.plugins.fixAddresses.StringUtils; 29 30 import org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditSelectionEvent; 30 31 import org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel; … … 38 39 @SuppressWarnings("serial") 39 40 public class ApplyAllGuessesAction extends AbstractAddressEditAction implements MouseListener{ 40 41 private String tag; 42 /** 43 * Instantiates a new "apply all guesses" action. 44 */ 45 public ApplyAllGuessesAction(String tag) { 46 super(tr("Apply"), "applyguesses_24", tr("Turns all guesses into the corresponding tag values.")); 47 this.tag = tag; 48 } 49 41 50 /** 42 51 * Instantiates a new "apply all guesses" action. 43 52 */ 44 53 public ApplyAllGuessesAction() { 45 super(tr("Apply"), "applyguesses_24", tr("Turns all guesses into the corresponding tag values."));54 this(null); 46 55 } 47 56 … … 54 63 55 64 if (ev.getSelectedUnresolvedAddresses() != null) { 56 // fix SELECTED items only57 65 List<OSMAddress> addrToFix = ev.getSelectedUnresolvedAddresses(); 58 66 applyGuesses(addrToFix); … … 60 68 61 69 if (ev.getSelectedIncompleteAddresses() != null) { 62 // fix SELECTED items only63 70 List<OSMAddress> addrToFix = ev.getSelectedIncompleteAddresses(); 64 71 applyGuesses(addrToFix); … … 84 91 for (OSMAddress aNode : addrToFixShadow) { 85 92 beginObjectTransaction(aNode); 86 aNode.applyAllGuesses(); 93 94 if (StringUtils.isNullOrEmpty(tag)) { // tag given? 95 aNode.applyAllGuesses(); // no -> apply all guesses 96 } else { // apply guessed values for single tag only 97 aNode.applyGuessForTag(tag); 98 } 87 99 finishObjectTransaction(aNode); 88 100 }
Note:
See TracChangeset
for help on using the changeset viewer.