Changeset 23808 in osm
- Timestamp:
- 2010-10-24T15:22:24+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/AddressEdit/src/org/openstreetmap/josm/plugins/addressEdit
- Files:
-
- 6 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/AddressEdit/src/org/openstreetmap/josm/plugins/addressEdit/AddressEditAction.java
r23806 r23808 32 32 @Override 33 33 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { 34 AddressVisitor addrVisitor = new AddressVisitor(); 35 36 for (OsmPrimitive osm : newSelection) { 37 osm.visit(addrVisitor); 38 } 39 /* This code is abused to generate tag utility code 34 synchronized (this) { 35 AddressVisitor addrVisitor = new AddressVisitor(); 36 37 for (OsmPrimitive osm : newSelection) { 38 osm.visit(addrVisitor); 39 } 40 //generateTagCode(addrVisitor); 41 42 addrVisitor.resolveAddresses(); 43 } 44 } 45 46 private void generateTagCode(AddressVisitor addrVisitor) { 47 /* This code is abused to generate tag utility code */ 40 48 for (String tag : addrVisitor.getTags()) { 41 String methodName = tag.replaceAll(":", ""); 42 methodName = Character.toUpperCase(methodName.charAt(0)) + methodName.substring(1); 43 System.out.println(String.format("/** Check if OSM primitive has a tag '%s'. /\npublic static boolean is%s(OsmPrimitive osmPrimitive) {\n return osmPrimitive != null ? osmPrimitive.hasKey(%s_TAG) : false;\n}\n", 49 String methodName = createMethodName(tag); 50 System.out.println(String.format("/** Check if OSM primitive has a tag '%s'.\n * @param osmPrimitive The OSM entity to check.*/\npublic static boolean has%sTag(OsmPrimitive osmPrimitive) {\n return osmPrimitive != null ? osmPrimitive.hasKey(%s_TAG) : false;\n}\n", 51 tag, 52 methodName, 53 tag.toUpperCase().replaceAll(":", "_"))); 54 System.out.println(String.format("/** Gets the value of tag '%s'.\n * @param osmPrimitive The OSM entity to check.*/\npublic static String get%sValue(OsmPrimitive osmPrimitive) {\n return osmPrimitive != null ? osmPrimitive.get(%s_TAG) : null;\n}\n", 44 55 tag, 45 56 methodName, … … 49 60 for (String tag : addrVisitor.getTags()) { 50 61 System.out.println(String.format("public static final String %s_TAG = \"%s\";", tag.toUpperCase().replaceAll(":", "_"), tag)); 51 }*/ 62 } 63 } 64 65 private String createMethodName(String osmName) { 66 StringBuffer result = new StringBuffer(osmName.length()); 67 boolean nextUp = false; 68 for (int i = 0; i < osmName.length(); i++) { 69 char c = osmName.charAt(i); 70 if (i == 0) { 71 result.append(Character.toUpperCase(c)); 72 continue; 73 } 74 if (c == '_' || c == ':') { 75 nextUp = true; 76 } else { 77 if (nextUp) { 78 result.append(Character.toUpperCase(c)); 79 nextUp = false; 80 } else { 81 result.append(c); 82 } 83 } 84 } 85 86 return result.toString(); 52 87 } 53 88 -
applications/editors/josm/plugins/AddressEdit/src/org/openstreetmap/josm/plugins/addressEdit/AddressVisitor.java
r23806 r23808 29 29 package org.openstreetmap.josm.plugins.addressEdit; 30 30 31 import java.util.ArrayList; 31 32 import java.util.HashMap; 32 33 import java.util.HashSet; 34 import java.util.List; 33 35 34 36 import org.openstreetmap.josm.data.osm.Changeset; 35 37 import org.openstreetmap.josm.data.osm.Node; 36 import org.openstreetmap.josm.data.osm.OsmPrimitive;37 38 import org.openstreetmap.josm.data.osm.Relation; 38 39 import org.openstreetmap.josm.data.osm.Way; … … 47 48 public class AddressVisitor implements Visitor { 48 49 private HashMap<String, StreetNode> streetDict = new HashMap<String, StreetNode>(100); 49 private HashMap<String, OsmPrimitive> unresolvedItems = new HashMap<String, OsmPrimitive>(100); 50 private List<AddressNode> unresolvedAddresses = new ArrayList<AddressNode>(100); 51 50 52 private HashSet<String> tags = new HashSet<String>(); 51 53 … … 55 57 @Override 56 58 public void visit(Node n) { 59 AddressNode aNode = NodeFactory.createNode(n); 57 60 61 if (aNode == null) return; 62 63 if (!assignAddressToStreet(aNode)) { 64 // Assignment failed: Street is not known (yet) -> add to 'unresolved' list 65 unresolvedAddresses.add(aNode); 66 } 58 67 } 59 68 60 69 /* (non-Javadoc) 61 70 * @see org.openstreetmap.josm.data.osm.visitor.Visitor#visit(org.openstreetmap.josm.data.osm.Way) … … 65 74 if (w.isIncomplete()) return; 66 75 76 StreetSegmentNode newSegment = NodeFactory.createNodeFromWay(w); 77 78 if (newSegment != null) { 79 String name = newSegment.getName(); 80 StreetNode sNode = null; 81 if (streetDict.containsKey(name)) { 82 sNode = streetDict.get(name); 83 } else { 84 sNode = new StreetNode(w); 85 streetDict.put(name, sNode); 86 } 87 88 if (sNode != null) { 89 sNode.addStreetSegment(newSegment); 90 } else { 91 throw new RuntimeException("Street node is null!"); 92 } 93 } 94 67 95 for (String key : w.keySet()) { 68 96 if (!tags.contains(key)) { … … 70 98 } 71 99 } 72 100 /* 73 101 for (Node n : w.getNodes()) { 74 102 75 } 103 }*/ 76 104 } 77 105 … … 102 130 } 103 131 104 public HashMap<String, OsmPrimitive> getUnresolvedItems() {105 return unresolved Items;132 public List<AddressNode> getUnresolvedItems() { 133 return unresolvedAddresses; 106 134 } 107 135 … … 109 137 return tags; 110 138 } 139 140 /** 141 * Tries to assign an address to a street. 142 * @param aNode 143 */ 144 private boolean assignAddressToStreet(AddressNode aNode) { 145 String streetName = aNode.getStreet(); 146 if (streetName != null && streetDict.containsKey(streetName)) { 147 StreetNode sNode = streetDict.get(streetName); 148 sNode.addAddress(aNode); 149 //System.out.println("Resolved address " + aNode + ": " + sNode); 150 return true; 151 } 152 153 return false; 154 } 155 156 /** 157 * Walks through the list of unassigned addresses and tries to assign them to streets. 158 */ 159 public void resolveAddresses() { 160 List<AddressNode> resolvedAddresses = new ArrayList<AddressNode>(); 161 for (AddressNode node : unresolvedAddresses) { 162 if (assignAddressToStreet(node)) { 163 resolvedAddresses.add(node); 164 } 165 } 166 167 System.out.println("Resolved " + resolvedAddresses.size() + " addresses"); 168 169 /* Remove all resolves nodes from unresolved list */ 170 for (AddressNode resolved : resolvedAddresses) { 171 unresolvedAddresses.remove(resolved); 172 } 173 174 System.out.println("Still unresolved: " + unresolvedAddresses.size() + " addresses"); 175 } 111 176 } -
applications/editors/josm/plugins/AddressEdit/src/org/openstreetmap/josm/plugins/addressEdit/StreetNode.java
r23806 r23808 19 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 20 21 public class StreetNode { 22 private OsmPrimitive osmPrimitive; 23 private List<StreetNode> children; 24 private List<OsmPrimitive> addresses; 25 21 /** 22 * This class is the container for all street segments with the same name. Every street 23 * consists at least of one segment. 24 * 25 * @author Oliver Wieland <oliver.wieland@online.de> 26 */ 27 public class StreetNode extends NodeEntityBase { 28 private List<INodeEntity> children; 29 private List<AddressNode> addresses; 30 26 31 /** 27 32 * @param osmPrimitive 28 33 */ 29 34 public StreetNode(OsmPrimitive osmPrimitive) { 30 super(); 31 this.osmPrimitive = osmPrimitive; 35 super(osmPrimitive); 32 36 } 33 37 34 public List< StreetNode> getChildren() {38 public List<INodeEntity> getChildren() { 35 39 return children; 36 40 } 37 41 38 public void AddAddress(OsmPrimitive address) { 39 LazyCreateAddresses(); 40 addresses.add(address); 42 /** 43 * Adds a street segment to the street node. 44 * @param segment 45 */ 46 public void addStreetSegment(StreetSegmentNode segment) { 47 lazyCreateChildren(); 48 49 children.add(segment); 41 50 } 42 43 private void LazyCreateAddresses() {44 if ( addresses== null) {45 addresses = new ArrayList<OsmPrimitive>();51 52 private void lazyCreateChildren() { 53 if (children == null) { 54 children = new ArrayList<INodeEntity>(); 46 55 } 47 56 } 48 57 49 public List<OsmPrimitive> getAddresses() { 58 public void addAddress(AddressNode aNode) { 59 lazyCreateAddresses(); 60 addresses.add(aNode); 61 } 62 63 private void lazyCreateAddresses() { 64 if (addresses == null) { 65 addresses = new ArrayList<AddressNode>(); 66 } 67 } 68 69 public List<AddressNode> getAddresses() { 50 70 return addresses; 51 71 } 52 public void setAddresses(List<OsmPrimitive> addresses) { 72 73 public void setAddresses(List<AddressNode> addresses) { 53 74 this.addresses = addresses; 54 75 } 55 76 56 77 @Override 78 public String toString() { 79 StringBuffer sb = new StringBuffer(getName()); 80 81 if (children != null) { 82 sb.append(String.format(", %d segments", children.size())); 83 } 84 85 if (addresses != null) { 86 sb.append(String.format(", %d address entries", addresses.size())); 87 } 88 89 return sb.toString(); 90 } 91 57 92 }
Note:
See TracChangeset
for help on using the changeset viewer.