Changeset 33443 in osm for applications
- Timestamp:
- 2017-07-14T16:27:00+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/AddStopPositionAction.java
r33435 r33443 7 7 import java.awt.event.KeyEvent; 8 8 import java.awt.event.MouseEvent; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.HashMap; 9 12 import java.util.HashSet; 10 13 import java.util.List; 14 import java.util.Map; 15 import java.util.Map.Entry; 11 16 import java.util.Set; 12 17 … … 14 19 import org.openstreetmap.josm.actions.JoinNodeWayAction; 15 20 import org.openstreetmap.josm.actions.SplitWayAction; 21 import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult; 16 22 import org.openstreetmap.josm.actions.mapmode.MapMode; 17 23 import org.openstreetmap.josm.command.AddCommand; 18 24 import org.openstreetmap.josm.command.ChangeCommand; 19 import org.openstreetmap.josm.data.osm.DataSet; 25 import org.openstreetmap.josm.command.Command; 26 import org.openstreetmap.josm.command.SequenceCommand; 20 27 import org.openstreetmap.josm.data.osm.Node; 21 28 import org.openstreetmap.josm.data.osm.OsmPrimitive; 29 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 30 import org.openstreetmap.josm.data.osm.Relation; 31 import org.openstreetmap.josm.data.osm.RelationMember; 32 import org.openstreetmap.josm.data.osm.Way; 22 33 import org.openstreetmap.josm.data.osm.WaySegment; 34 import org.openstreetmap.josm.plugins.pt_assistant.data.PTStop; 35 import org.openstreetmap.josm.plugins.pt_assistant.utils.RouteUtils; 23 36 import org.openstreetmap.josm.tools.ImageProvider; 24 37 import org.openstreetmap.josm.tools.Shortcut; … … 33 46 public class AddStopPositionAction extends MapMode { 34 47 35 private static final String mapModeName= "Add stop position";48 private static final String MAP_MODE_NAME = "Add stop position"; 36 49 37 50 private transient Set<OsmPrimitive> newHighlights = new HashSet<>(); … … 45 58 */ 46 59 public AddStopPositionAction() { 47 super(tr( mapModeName), "bus", tr(mapModeName),60 super(tr(MAP_MODE_NAME), "bus", tr(MAP_MODE_NAME), 48 61 Shortcut.registerShortcut("mapmode:stop_position", 49 tr("Mode: {0}", tr( mapModeName)),62 tr("Mode: {0}", tr(MAP_MODE_NAME)), 50 63 KeyEvent.VK_K, Shortcut.CTRL_SHIFT), 51 64 getCursor()); … … 128 141 } else { 129 142 Main.main.undoRedo.add(new ChangeCommand(n, newStopPos)); 130 }131 132 DataSet ds = Main.getLayerManager().getEditLayer().data; 133 ds.setSelected(newStopPos);143 newStopPos = n; 144 } 145 146 Main.getLayerManager().getEditLayer().data.setSelected(newStopPos); 134 147 135 148 //join the node to the way only if the node is new … … 139 152 } 140 153 141 // split the way in any case 142 SplitWayAction splitWayAction = new SplitWayAction(); 143 splitWayAction.actionPerformed(null); 154 if (newStopPos.getParentWays().isEmpty()) 155 return; 156 157 Way affected = newStopPos.getParentWays().get(0); 158 Map<Relation, Boolean> needPostProcess = getAffectedRelation(affected); 159 160 if (needPostProcess.isEmpty()) 161 return; 162 163 SplitWayResult result = SplitWayAction.split(getLayerManager().getEditLayer(), 164 affected, Collections.singletonList(newStopPos), Collections.emptyList()); 165 Main.main.undoRedo.add(result.getCommand()); 166 167 List<Command> cmds = new ArrayList<>(); 168 for (Entry<Relation, Boolean> route : needPostProcess.entrySet()) { 169 Relation r = new Relation(route.getKey()); 170 if (route.getValue()) 171 deleteFirstWay(r); 172 else 173 deleteLastWay(r); 174 cmds.add(new ChangeCommand(route.getKey(), r)); 175 } 176 Main.main.undoRedo.add(new SequenceCommand("Update PT Relations", cmds)); 177 } 178 179 private void deleteLastWay(Relation r) { 180 int delete = 0; 181 for (int i = r.getMembersCount() - 1; i >= 0; i--) { 182 RelationMember rm = r.getMember(i); 183 if (rm.getType() == OsmPrimitiveType.WAY && 184 !PTStop.isPTPlatform(rm)) { 185 delete = i; 186 break; 187 } 188 } 189 r.removeMember(delete); 190 } 191 192 private void deleteFirstWay(Relation r) { 193 int delete = 0; 194 for (int i = 0; i < r.getMembersCount(); i++) { 195 RelationMember rm = r.getMember(i); 196 if (rm.getType() == OsmPrimitiveType.WAY && 197 !PTStop.isPTPlatform(rm)) { 198 delete = i; 199 break; 200 } 201 } 202 r.removeMember(delete); 203 } 204 205 private Map<Relation, Boolean> getAffectedRelation(Way affected) { 206 Map<Relation, Boolean> ret = new HashMap<>(); 207 for (Relation route : getPTRouteParents(affected)) { 208 if (isFirstMember(affected, route)) { 209 ret.put(route, true); 210 } else if (isLastMember(affected, route)) { 211 ret.put(route, false); 212 } 213 } 214 return ret; 215 } 216 217 private boolean isFirstMember(Way affected, Relation route) { 218 for (int i = 0; i < route.getMembersCount(); i++) { 219 RelationMember rm = route.getMember(i); 220 if (rm.getMember().equals(affected)) { 221 return true; 222 } else if (rm.getType() == OsmPrimitiveType.WAY && 223 !PTStop.isPTPlatform(rm)) { 224 return false; 225 } 226 } 227 return true; 228 } 229 230 private boolean isLastMember(Way affected, Relation route) { 231 for (int i = route.getMembersCount() - 1; i >= 0; i--) { 232 RelationMember rm = route.getMember(i); 233 if (rm.getMember().equals(affected)) { 234 return true; 235 } else if (rm.getType() == OsmPrimitiveType.WAY && 236 !PTStop.isPTPlatform(rm)) { 237 return false; 238 } 239 } 240 241 return true; 144 242 } 145 243 … … 149 247 } 150 248 249 } 250 251 private List<Relation> getPTRouteParents(Way way) { 252 List<Relation> referrers = OsmPrimitive.getFilteredList( 253 way.getReferrers(), Relation.class); 254 referrers.removeIf(r -> !RouteUtils.isPTRoute(r)); 255 return referrers; 151 256 } 152 257
Note:
See TracChangeset
for help on using the changeset viewer.