Changeset 23961 in osm for applications/editors
- Timestamp:
- 2010-10-31T14:56:39+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressEditContainer.java
r23947 r23961 63 63 private List<AddressNode> unresolvedAddresses = new ArrayList<AddressNode>(100); 64 64 private List<AddressNode> incompleteAddresses = new ArrayList<AddressNode>(100); 65 66 private HashMap<String, StreetNode> shadowStreetDict = new HashMap<String, StreetNode>(100); 67 private List<AddressNode> shadowUnresolvedAddresses = new ArrayList<AddressNode>(100); 68 private List<AddressNode> shadowIncompleteAddresses = new ArrayList<AddressNode>(100); 69 65 70 private HashMap<String, AddressNode> addressCache = new HashMap<String, AddressNode>(); 66 71 private HashSet<Node> visitedNodes = new HashSet<Node>(); … … 145 150 if (!assignAddressToStreet(aNode)) { 146 151 // Assignment failed: Street is not known (yet) -> add to 'unresolved' list 147 unresolvedAddresses.add(aNode);152 shadowUnresolvedAddresses.add(aNode); 148 153 } 149 154 150 155 if (!aNode.isComplete()) { 151 incompleteAddresses.add(aNode);156 shadowIncompleteAddresses.add(aNode); 152 157 } 153 158 } else { … … 181 186 } 182 187 188 /** 189 * Creates the node from an OSM way instance. 190 * 191 * @param w the w 192 */ 183 193 private void createNodeFromWay(Way w) { 184 194 StreetSegmentNode newSegment = NodeFactory.createNodeFromWay(w); … … 189 199 190 200 StreetNode sNode = null; 191 if (s treetDict.containsKey(name)) {192 sNode = s treetDict.get(name);193 } else { 201 if (shadowStreetDict.containsKey(name)) { // street exists? 202 sNode = shadowStreetDict.get(name); 203 } else { // new street name -> add to dict 194 204 sNode = new StreetNode(w); 195 s treetDict.put(name, sNode);205 shadowStreetDict.put(name, sNode); 196 206 } 197 207 … … 242 252 } 243 253 254 /** 255 * Gets the unresolved (addresses without valid street name) addresses. 256 * 257 * @return the unresolved addresses 258 */ 244 259 public List<AddressNode> getUnresolvedAddresses() { 245 260 return unresolvedAddresses; 246 261 } 247 262 263 /** 264 * Gets the list with incomplete addresses. 265 * 266 * @return the incomplete addresses 267 */ 248 268 public List<AddressNode> getIncompleteAddresses() { 249 269 return incompleteAddresses; 250 270 } 251 271 252 public List<StreetNode> getStreetList() { 253 272 /** 273 * Gets the street list. 274 * 275 * @return the street list 276 */ 277 public List<StreetNode> getStreetList() { 254 278 ArrayList<StreetNode> sortedList = new ArrayList<StreetNode>(streetDict.values()); 255 279 Collections.sort(sortedList); … … 257 281 } 258 282 283 /** 284 * Gets all addresses without valid street. 285 * @return 286 */ 259 287 public List<AddressNode> getUnresolvedItems() { 260 288 return unresolvedAddresses; 261 289 } 262 290 291 /** 292 * Gets the tags used in the data layer. 293 * @return 294 */ 263 295 public HashSet<String> getTags() { 264 296 return tags; … … 363 395 if (osmData == null || osmData.isEmpty()) 364 396 return; 365 366 clearData(); 367 for (OsmPrimitive osmPrimitive : osmData) { 368 osmPrimitive.visit(this); 369 } 370 371 resolveAddresses(); 372 373 Collections.sort(incompleteAddresses); 374 Collections.sort(unresolvedAddresses); 375 376 fireContainerChanged(); 397 398 synchronized (this) { 399 clearData(); 400 for (OsmPrimitive osmPrimitive : osmData) { 401 osmPrimitive.visit(this); 402 } 403 404 resolveAddresses(); 405 // sort lists 406 Collections.sort(shadowIncompleteAddresses); 407 Collections.sort(shadowIncompleteAddresses); 408 409 // put results from shadow copy into real lists 410 incompleteAddresses = new ArrayList<AddressNode>(shadowIncompleteAddresses); 411 unresolvedAddresses = new ArrayList<AddressNode>(shadowUnresolvedAddresses); 412 streetDict = new HashMap<String, StreetNode>(shadowStreetDict); 413 // remove temp data 414 shadowStreetDict.clear(); 415 shadowUnresolvedAddresses.clear(); 416 shadowIncompleteAddresses.clear(); 417 // update clients 418 fireContainerChanged(); 419 } 377 420 } 378 421 379 422 public void clearData() { 380 s treetDict.clear();381 unresolvedAddresses.clear();382 incompleteAddresses.clear();423 shadowStreetDict.clear(); 424 shadowUnresolvedAddresses.clear(); 425 shadowIncompleteAddresses.clear(); 383 426 visitedNodes.clear(); 384 427 visitedWays.clear(); -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressFinderThread.java
r23949 r23961 14 14 package org.openstreetmap.josm.plugins.fixAddresses; 15 15 16 import static org.openstreetmap.josm.tools.I18n.tr; 17 18 import java.io.IOException; 19 import java.util.ArrayList; 16 20 import java.util.ConcurrentModificationException; 21 import java.util.List; 17 22 18 23 import org.openstreetmap.josm.Main; … … 24 29 import org.openstreetmap.josm.data.osm.Way; 25 30 import org.openstreetmap.josm.data.osm.visitor.Visitor; 31 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 32 import org.openstreetmap.josm.io.OsmTransferException; 33 import org.xml.sax.SAXException; 26 34 27 public class AddressFinderThread implements Runnable,Visitor {28 private Address Node addressNode;35 public class AddressFinderThread extends PleaseWaitRunnable implements Visitor { 36 private AddressEditContainer container; 29 37 private double minDist; 30 38 private Node nearestNode; 31 private Node osmAddressNode;39 private AddressNode curAddressNode; 32 40 private boolean isRunning = false; 33 41 private String nearestName = null; 34 42 private String currentName = null; 43 private boolean cancelled; 35 44 36 45 /** 37 * @param addressNode46 * @param AddressEditContainer 38 47 */ 39 public AddressFinderThread(Address Node addressNode) {40 super( );41 setAddress Node(addressNode);48 public AddressFinderThread(AddressEditContainer AddressEditContainer, String title) { 49 super(title != null ? title : tr("Searching")); 50 setAddressEditContainer(AddressEditContainer); 42 51 } 43 52 44 public AddressFinderThread() { 45 this(null); 46 } 47 48 public void setAddressNode(AddressNode addressNode) { 53 public void setAddressEditContainer(AddressEditContainer AddressEditContainer) { 49 54 if (isRunning) { 50 55 throw new ConcurrentModificationException(); 51 56 } 52 this.addressNode = addressNode; 53 if (addressNode != null && addressNode.getOsmObject() instanceof Node) { 54 osmAddressNode = (Node) addressNode.getOsmObject(); 55 } 57 this.container = AddressEditContainer; 56 58 } 57 59 58 public Address Node getAddressNode() {59 return addressNode;60 public AddressEditContainer getAddressEditContainer() { 61 return container; 60 62 } 61 62 public double getMinDist() {63 return minDist;64 }65 66 public Node getNearestNode() {67 return nearestNode;68 }69 70 /**71 * @return the nearestName72 */73 public String getGuessedName() {74 return nearestName;75 }76 77 63 /** 78 64 * @return the isRunning … … 82 68 } 83 69 84 @Override 85 public void run() { 86 if (Main.main.getCurrentDataSet() == null || osmAddressNode == null) return; 87 88 isRunning = true; 89 synchronized(this) { 90 try { 91 minDist = Double.MAX_VALUE; 92 for (OsmPrimitive osmPrimitive : Main.main.getCurrentDataSet().getWays()) { 93 osmPrimitive.visit(this); 94 } 95 96 if (nearestName != null) { 97 addressNode.setGuessedStreetName(nearestName); 98 } 99 } finally { 100 isRunning = false; 101 } 102 } 103 } 104 105 70 /* (non-Javadoc) 71 * @see org.openstreetmap.josm.data.osm.visitor.Visitor#visit(org.openstreetmap.josm.data.osm.Node) 72 */ 106 73 @Override 107 74 public void visit(Node n) { 108 75 if (n == null) return; 76 if (curAddressNode == null) return; 109 77 110 78 // If the coordinates are null, we are screwed anyway 111 double dist = osmAddressNode.getCoor().greatCircleDistance(n.getCoor()); 79 LatLon ll = curAddressNode.getCoor(); 80 if (ll == null) return; 81 82 double dist = ll.greatCircleDistance(n.getCoor()); 112 83 113 84 if (dist < minDist) { … … 118 89 } 119 90 120 91 /* (non-Javadoc) 92 * @see org.openstreetmap.josm.data.osm.visitor.Visitor#visit(org.openstreetmap.josm.data.osm.Way) 93 */ 121 94 @Override 122 95 public void visit(Way w) { … … 128 101 for (Node node : w.getNodes()) { 129 102 visit(node); 130 } 131 103 } 132 104 } 133 105 134 106 /* (non-Javadoc) 107 * @see org.openstreetmap.josm.data.osm.visitor.Visitor#visit(org.openstreetmap.josm.data.osm.Relation) 108 */ 135 109 @Override 136 110 public void visit(Relation e) { 137 // TODO Auto-generated method stub 138 111 // nothing to do yet 139 112 } 140 113 141 114 /* (non-Javadoc) 115 * @see org.openstreetmap.josm.data.osm.visitor.Visitor#visit(org.openstreetmap.josm.data.osm.Changeset) 116 */ 142 117 @Override 143 118 public void visit(Changeset cs) { 144 // TODO Auto-generated method stub 145 119 // nothing to do yet 146 120 } 147 121 122 /* (non-Javadoc) 123 * @see org.openstreetmap.josm.gui.PleaseWaitRunnable#cancel() 124 */ 125 @Override 126 protected void cancel() { 127 cancelled = true; 128 } 129 130 /* (non-Javadoc) 131 * @see org.openstreetmap.josm.gui.PleaseWaitRunnable#finish() 132 */ 133 @Override 134 protected void finish() { 135 // nothing to do yet 136 } 137 138 /* (non-Javadoc) 139 * @see org.openstreetmap.josm.gui.PleaseWaitRunnable#realRun() 140 */ 141 @Override 142 protected void realRun() throws SAXException, IOException, 143 OsmTransferException { 144 if (Main.main.getCurrentDataSet() == null || container == null) return; 145 146 isRunning = true; 147 cancelled = false; 148 149 progressMonitor.subTask(tr("Searching") + "..."); 150 151 try { 152 progressMonitor.setTicksCount(container.getNumberOfUnresolvedAddresses()); 153 154 List<AddressNode> shadowCopy = new ArrayList<AddressNode>(container.getUnresolvedAddresses()); 155 for (AddressNode aNode : shadowCopy) { 156 minDist = Double.MAX_VALUE; 157 curAddressNode = aNode; 158 159 // check for cancel 160 if (cancelled) { 161 break; 162 } 163 164 // visit osm data 165 for (OsmPrimitive osmPrimitive : Main.main.getCurrentDataSet().getWays()) { 166 if (cancelled) { 167 break; 168 } 169 osmPrimitive.visit(this); 170 171 } 172 173 // we found something 174 if (nearestName != null) { 175 progressMonitor.subTask(String.format("%s: %s (%4.1f m)", tr("Guess"), nearestName, minDist)); 176 aNode.setGuessedStreetName(nearestName); 177 nearestName = null; 178 } 179 // report progress 180 progressMonitor.worked(1); 181 } 182 // request container update 183 container.invalidate(); 184 } finally { 185 isRunning = false; 186 } 187 } 148 188 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressNode.java
r23957 r23961 75 75 public void setGuessedStreetName(String guessedStreetName) { 76 76 this.guessedStreetName = guessedStreetName; 77 fireEntityChanged(this);77 //fireEntityChanged(this); 78 78 } 79 79 -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/INodeEntity.java
r23933 r23961 16 16 import java.util.List; 17 17 18 import org.openstreetmap.josm.data.coor.LatLon; 18 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 21 /** 22 * Base interface for all node entities. A node entity is a lightweight wrapper 23 * around OSM objects in order to ease up some tasks like tag handling. 24 * @author Oliver Wieland <oliver.wieland@online.de> 25 * 26 */ 19 27 20 28 public interface INodeEntity extends Comparable<INodeEntity> { … … 42 50 */ 43 51 public List<INodeEntity> getChildren(); 52 53 /** 54 * Gets the coordinate of the node. If the the underlying object is a 55 * node, it just returns the node coordinate. For ways and areas, this 56 * method returns the coordinate of the center (balance point). 57 * @return 58 */ 59 public LatLon getCoor(); 44 60 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/NodeEntityBase.java
r23933 r23961 21 21 import org.openstreetmap.josm.Main; 22 22 import org.openstreetmap.josm.command.ChangeCommand; 23 import org.openstreetmap.josm.data.coor.LatLon; 24 import org.openstreetmap.josm.data.osm.BBox; 23 25 import org.openstreetmap.josm.data.osm.Node; 24 26 import org.openstreetmap.josm.data.osm.OsmPrimitive; 27 import org.openstreetmap.josm.data.osm.Way; 25 28 26 29 public class NodeEntityBase implements INodeEntity, Comparable<INodeEntity> { … … 126 129 } 127 130 131 /* (non-Javadoc) 132 * @see java.lang.Comparable#compareTo(java.lang.Object) 133 */ 128 134 @Override 129 135 public int compareTo(INodeEntity o) { … … 132 138 } 133 139 134 140 /* (non-Javadoc) 141 * @see org.openstreetmap.josm.plugins.fixAddresses.INodeEntity#getCoor() 142 */ 143 @Override 144 public LatLon getCoor() { 145 OsmPrimitive osm = getOsmObject(); 146 if (osm == null) return null; 147 148 if (osm instanceof Node) { 149 return ((Node)osm).getCoor(); 150 // way: return center 151 } else if (osm instanceof Way) { 152 Way w = (Way) osm; 153 BBox bb = w.getBBox(); 154 return bb.getBottomRight().getCenter(bb.getTopLeft()); 155 } 156 // relations?? 157 return null; 158 } 135 159 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AddressEditTableModel.java
r23933 r23961 36 36 @Override 37 37 public void containerChanged(AddressEditContainer container) { 38 fireTableDataChanged(); 38 fireTableDataChanged(); // update model 39 39 } 40 40 41 41 @Override 42 42 public void entityChanged(INodeEntity entity) { 43 fireTableDataChanged(); 43 int row = getRowOfEntity(entity); 44 if (row != -1) { // valid row? -> update model 45 System.out.println("Update row " + row); 46 fireTableRowsUpdated(row, row); 47 } // else we don't do anything 44 48 } 45 49 … … 50 54 */ 51 55 public abstract INodeEntity getEntityOfRow(int row); 56 57 /** 58 * Gets the row for the given node entity or -1; if the model does not contain the entity. 59 * @param entity The entity to get the row for. 60 * @return 61 */ 62 public abstract int getRowOfEntity(INodeEntity entity); 52 63 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/GuessAddressDataAction.java
r23944 r23961 16 16 import static org.openstreetmap.josm.tools.I18n.tr; 17 17 18 import java.util.ArrayList;19 18 import java.util.List; 20 19 20 import org.openstreetmap.josm.Main; 21 21 import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer; 22 22 import org.openstreetmap.josm.plugins.fixAddresses.AddressFinderThread; … … 47 47 } 48 48 49 /* (non-Javadoc) 50 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AbstractAddressEditAction#updateEnabledState(org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer) 51 */ 49 52 @Override 50 53 protected void updateEnabledState(AddressEditContainer container) { … … 52 55 } 53 56 57 /* (non-Javadoc) 58 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AbstractAddressEditAction#addressEditActionPerformed(org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer) 59 */ 54 60 @Override 55 61 public void addressEditActionPerformed(AddressEditContainer container) { … … 60 66 } 61 67 68 /* (non-Javadoc) 69 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AbstractAddressEditAction#addressEditActionPerformed(org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditSelectionEvent) 70 */ 62 71 @Override 63 72 public void addressEditActionPerformed(AddressEditSelectionEvent ev) { … … 65 74 66 75 // guess tags for selected addresses only 67 internalGuessAddresses(ev.getSelectedUnresolvedAddresses()); 76 internalGuessAddresses(ev.getSelectedUnresolvedAddresses()); 68 77 } 69 78 … … 73 82 */ 74 83 private void internalGuessAddresses(List<AddressNode> nodes) { 75 // setup thread pool 76 for (int i = 0; i < threads.length; i++) { 77 threads[i] = new AddressFinderThread(); 78 } 79 80 // work on a shadowed copy 81 List<AddressNode> addrNodes = new ArrayList<AddressNode>(nodes); 82 for (AddressNode aNode : addrNodes) { 83 if (aNode.hasStreetName()) continue; 84 85 while(!scheduleNode(aNode)) { 86 try { 87 Thread.sleep(100); 88 } catch (InterruptedException e) { 89 return; 90 } 91 } 92 } 93 container.containerChanged(container); 84 Main.worker.submit(new AddressFinderThread(container, tr("Guess street names"))); 94 85 } 95 96 private boolean scheduleNode(AddressNode aNode) {97 for (int i = 0; i < threads.length; i++) {98 if (!threads[i].isRunning()) {99 threads[i].setAddressNode(aNode);100 threads[i].run();101 return true;102 }103 }104 return false;105 }106 107 86 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/IncompleteAddressesTableModel.java
r23933 r23961 105 105 return null; 106 106 } 107 if (row < 0 || row > addressContainer.getNumberOfIncompleteAddresses()) {107 if (row < 0 || row >= addressContainer.getNumberOfIncompleteAddresses()) { 108 108 return null; 109 109 } 110 110 return addressContainer.getIncompleteAddresses().get(row); 111 111 } 112 113 @Override 114 public int getRowOfEntity(INodeEntity entity) { 115 if (addressContainer == null || addressContainer.getIncompleteAddresses() == null) { 116 return -1; 117 } 118 119 return addressContainer.getIncompleteAddresses().indexOf(entity); 120 } 112 121 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/StreetTableModel.java
r23933 r23961 111 111 return null; 112 112 } 113 if (row < 0 || row > addressContainer.getNumberOfStreets()) {113 if (row < 0 || row >= addressContainer.getNumberOfStreets()) { 114 114 return null; 115 115 } 116 116 return addressContainer.getStreetList().get(row); 117 117 } 118 119 @Override 120 public int getRowOfEntity(INodeEntity entity) { 121 if (addressContainer == null || addressContainer.getStreetList() == null) { 122 return -1; 123 } 124 125 return addressContainer.getStreetList().indexOf(entity); 126 } 118 127 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/UnresolvedAddressesTableModel.java
r23944 r23961 147 147 return null; 148 148 } 149 if (row < 0 || row > addressContainer.getNumberOfUnresolvedAddresses()) {149 if (row < 0 || row >= addressContainer.getNumberOfUnresolvedAddresses()) { 150 150 return null; 151 151 } 152 152 return addressContainer.getUnresolvedAddresses().get(row); 153 153 } 154 155 @Override 156 public int getRowOfEntity(INodeEntity entity) { 157 if (addressContainer == null || addressContainer.getUnresolvedAddresses() == null) { 158 return -1; 159 } 160 161 return addressContainer.getUnresolvedAddresses().indexOf(entity); 162 } 154 163 }
Note:
See TracChangeset
for help on using the changeset viewer.