Changeset 33440 in osm for applications/editors/josm/plugins/pt_assistant/src/org
- Timestamp:
- 2017-07-12T11:29:19+02:00 (7 years ago)
- Location:
- applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/SortPTStopsAction.java
r33434 r33440 20 20 import org.openstreetmap.josm.actions.JosmAction; 21 21 import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction; 22 import org.openstreetmap.josm.command.ChangeCommand; 23 import org.openstreetmap.josm.command.Command; 24 import org.openstreetmap.josm.data.coor.EastNorth; 25 import org.openstreetmap.josm.data.osm.Node; 22 26 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 27 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; … … 32 36 import org.openstreetmap.josm.tools.Utils; 33 37 38 /** 39 * Sorts the stop positions in a PT route according to the assigned ways 40 * 41 * @author giacomo 42 * 43 */ 34 44 public class SortPTStopsAction extends JosmAction { 35 45 … … 77 87 78 88 private void continueAfterDownload(Relation rel) { 79 List<RelationMember> members = rel.getMembers(); 80 89 Main.main.undoRedo.add(getSortPTStopCommand(rel)); 90 } 91 92 public Command getSortPTStopCommand(Relation rel) { 93 Relation newRel = new Relation(rel); 94 List<RelationMember> members = newRel.getMembers(); 81 95 for (int i = 0; i < members.size(); i++) { 82 rel.removeMember(0); 83 } 84 96 newRel.removeMember(0); 97 } 85 98 members = new RelationSorter().sortMembers(members); 86 99 … … 130 143 }); 131 144 132 wayMembers.forEach(wm -> { 133 if (wm.getType() != OsmPrimitiveType.WAY) 134 return; 135 List<PTStop> stps = wayStop.get(wm.getWay()); 136 if (stps == null) 137 return; 138 stps.forEach(stop -> { 139 if (stop != null) { 140 if (stop.getStopPositionRM() != null) 141 rel.addMember(stop.getStopPositionRM()); 142 if (stop.getPlatformRM() != null) 143 rel.addMember(stop.getPlatformRM()); 145 Way prev = null; 146 for (RelationMember wm : wayMembers) { 147 if (wm.getType() == OsmPrimitiveType.WAY) { 148 Way curr = wm.getWay(); 149 List<PTStop> stps = wayStop.get(curr); 150 if (stps != null) { 151 if (stps.size() > 1) 152 stps = sortSameWayStops(stps, curr, prev); 153 stps.forEach(stop -> { 154 if (stop != null) { 155 if (stop.getStopPositionRM() != null) 156 newRel.addMember(stop.getStopPositionRM()); 157 if (stop.getPlatformRM() != null) 158 newRel.addMember(stop.getPlatformRM()); 159 } 160 }); 144 161 } 145 }); 146 }); 147 148 wayMembers.forEach(rel::addMember); 162 prev = curr; 163 } 164 } 165 166 wayMembers.forEach(newRel::addMember); 167 168 return new ChangeCommand(rel, newRel); 169 } 170 171 private List<PTStop> sortSameWayStops(List<PTStop> stps, Way way, Way prev) { 172 Map<Node, List<PTStop>> closeNodes = new HashMap<>(); 173 List<PTStop> noLocationStops = new ArrayList<>(); 174 List<Node> nodes = way.getNodes(); 175 for (PTStop stop : stps) { 176 Node closest = findClosestNode(stop, nodes); 177 if (closest == null) { 178 noLocationStops.add(stop); 179 continue; 180 } 181 if (!closeNodes.containsKey(closest)) { 182 closeNodes.put(closest, new ArrayList<>()); 183 } 184 closeNodes.get(closest).add(stop); 185 } 186 187 boolean reverse = prev.firstNode().equals(way.lastNode()) 188 || prev.lastNode().equals(way.lastNode()); 189 190 if (reverse) 191 Collections.reverse(nodes); 192 193 List<PTStop> ret = new ArrayList<>(); 194 for (int i = 0; i < nodes.size(); i++) { 195 Node n = nodes.get(i); 196 Node prevNode = i > 0 ? nodes.get(i - 1) : n; 197 List<PTStop> stops = closeNodes.get(n); 198 if (stops != null) { 199 if (stops.size() > 1) { 200 stops.sort((s1, s2) -> { 201 Double d1 = stopEastNorth(s1).distance(prevNode.getEastNorth()); 202 Double d2 = stopEastNorth(s2).distance(prevNode.getEastNorth()); 203 return d1.compareTo(d2); 204 }); 205 } 206 stops.forEach(ret::add); 207 } 208 } 209 210 ret.addAll(noLocationStops); 211 return ret; 212 } 213 214 private Node findClosestNode(PTStop stop, List<Node> nodes) { 215 EastNorth stopEN = stopEastNorth(stop); 216 if (stopEN == null) 217 return null; 218 double minDist = Double.MAX_VALUE; 219 Node closest = null; 220 for (Node node : nodes) { 221 double dist = node.getEastNorth().distance(stopEN); 222 if (dist < minDist) { 223 minDist = dist; 224 closest = node; 225 } 226 } 227 return closest; 228 } 229 230 private EastNorth stopEastNorth(PTStop stop) { 231 if (stop.getStopPosition() != null) 232 return stop.getStopPosition().getEastNorth(); 233 OsmPrimitive prim = stop.getPlatform(); 234 if (prim.getType() == OsmPrimitiveType.WAY) 235 return ((Way) prim).firstNode().getEastNorth(); 236 else if (prim.getType() == OsmPrimitiveType.NODE) 237 return ((Node) prim).getEastNorth(); 238 else 239 return null; 149 240 } 150 241 -
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/data/PTStop.java
r33433 r33440 150 150 151 151 Collection<Node> allNodes = platform.getDataSet().getNodes(); 152 String platName = platform.get("name"); 152 153 for (Node currentNode : allNodes) { 153 if (platformBBox.bounds(currentNode.getBBox()) && currentNode.hasTag("public_transport", "stop_position")) { 154 potentialStopPositions.add(currentNode); 154 String nodeName = currentNode.get("name"); 155 if (platformBBox.bounds(currentNode.getBBox()) 156 && currentNode.hasTag("public_transport", "stop_position") 157 && ((platName == null || nodeName == null) 158 || platName.equals(nodeName))) { 159 potentialStopPositions.add(currentNode); 155 160 } 156 161 }
Note:
See TracChangeset
for help on using the changeset viewer.