| 1 | package UtilsPlugin;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.event.ActionEvent;
|
|---|
| 6 | import java.awt.event.KeyEvent;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.Collections;
|
|---|
| 10 | import java.util.HashSet;
|
|---|
| 11 | import java.util.LinkedList;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.Main;
|
|---|
| 15 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 16 | import org.openstreetmap.josm.command.Command;
|
|---|
| 17 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 18 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 19 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
|---|
| 24 |
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 26 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 27 |
|
|---|
| 28 | public class SimplifyWayAction extends JosmAction {
|
|---|
| 29 | public SimplifyWayAction() {
|
|---|
| 30 | super(tr("Simplify Way"), "simplify",
|
|---|
| 31 | tr("Delete unnecessary nodes from a way."), KeyEvent.VK_Y, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK, true);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public void actionPerformed(ActionEvent e) {
|
|---|
| 35 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
|---|
| 36 |
|
|---|
| 37 | if (selection.size() == 1 && selection.iterator().next() instanceof Way) {
|
|---|
| 38 | simplifyWay((Way) selection.iterator().next());
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | public void simplifyWay(Way w) {
|
|---|
| 43 | double threshold = Double.parseDouble(
|
|---|
| 44 | Main.pref.get("simplify-way.max-error", "50"));
|
|---|
| 45 |
|
|---|
| 46 | Way wnew = new Way(w);
|
|---|
| 47 |
|
|---|
| 48 | int toI = wnew.nodes.size() - 1;
|
|---|
| 49 | for (int i = wnew.nodes.size() - 1; i >= 0; i--) {
|
|---|
| 50 | CollectBackReferencesVisitor backRefsV =
|
|---|
| 51 | new CollectBackReferencesVisitor(Main.ds, false);
|
|---|
| 52 | backRefsV.visit(wnew.nodes.get(i));
|
|---|
| 53 | boolean used = false;
|
|---|
| 54 | if (backRefsV.data.size() == 1) {
|
|---|
| 55 | used = Collections.frequency(
|
|---|
| 56 | w.nodes, wnew.nodes.get(i)) > 1;
|
|---|
| 57 | } else {
|
|---|
| 58 | backRefsV.data.remove(w);
|
|---|
| 59 | used = !backRefsV.data.isEmpty();
|
|---|
| 60 | }
|
|---|
| 61 | if (!used) used = wnew.nodes.get(i).tagged;
|
|---|
| 62 |
|
|---|
| 63 | if (used) {
|
|---|
| 64 | simplifyWayRange(wnew, i, toI, threshold);
|
|---|
| 65 | toI = i;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | simplifyWayRange(wnew, 0, toI, threshold);
|
|---|
| 69 |
|
|---|
| 70 | HashSet<Node> delNodes = new HashSet<Node>();
|
|---|
| 71 | delNodes.addAll(w.nodes);
|
|---|
| 72 | delNodes.removeAll(wnew.nodes);
|
|---|
| 73 |
|
|---|
| 74 | if (wnew.nodes.size() != w.nodes.size()) {
|
|---|
| 75 | Collection<Command> cmds = new LinkedList<Command>();
|
|---|
| 76 | cmds.add(new ChangeCommand(w, wnew));
|
|---|
| 77 | cmds.add(new DeleteCommand(delNodes));
|
|---|
| 78 | Main.main.undoRedo.add(
|
|---|
| 79 | new SequenceCommand(tr("Simplify Way (remove {0} nodes)",
|
|---|
| 80 | delNodes.size()),
|
|---|
| 81 | cmds));
|
|---|
| 82 | Main.map.repaint();
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public void simplifyWayRange(Way wnew, int from, int to, double thr) {
|
|---|
| 87 | if (to - from >= 2) {
|
|---|
| 88 | ArrayList<Node> ns = new ArrayList<Node>();
|
|---|
| 89 | simplifyWayRange(wnew, from, to, ns, thr);
|
|---|
| 90 | for (int j = to-1; j > from; j--) wnew.nodes.remove(j);
|
|---|
| 91 | wnew.nodes.addAll(from+1, ns);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /*
|
|---|
| 96 | * Takes an interval [from,to] and adds nodes from (from,to) to ns.
|
|---|
| 97 | * (from and to are indices of wnew.nodes.)
|
|---|
| 98 | */
|
|---|
| 99 | public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
|
|---|
| 100 | Node fromN = wnew.nodes.get(from), toN = wnew.nodes.get(to);
|
|---|
| 101 |
|
|---|
| 102 | int imax = -1;
|
|---|
| 103 | double xtemax = 0;
|
|---|
| 104 | for (int i = from+1; i < to; i++) {
|
|---|
| 105 | Node n = wnew.nodes.get(i);
|
|---|
| 106 | double xte = Math.abs(EARTH_RAD * xtd(
|
|---|
| 107 | fromN.coor.lat(), fromN.coor.lon(),
|
|---|
| 108 | toN.coor.lat(), toN.coor.lon(),
|
|---|
| 109 | n.coor.lat(), n.coor.lon()));
|
|---|
| 110 | if (xte > xtemax) {
|
|---|
| 111 | xtemax = xte;
|
|---|
| 112 | imax = i;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if (imax != -1 && xtemax >= thr) {
|
|---|
| 117 | simplifyWayRange(wnew, from, imax, ns, thr);
|
|---|
| 118 | ns.add(wnew.nodes.get(imax));
|
|---|
| 119 | simplifyWayRange(wnew, imax, to, ns, thr);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public static double EARTH_RAD = 6378137.0;
|
|---|
| 124 |
|
|---|
| 125 | /* From Aviaton Formulary v1.3
|
|---|
| 126 | * http://williams.best.vwh.net/avform.htm
|
|---|
| 127 | */
|
|---|
| 128 | public static double dist(double lat1, double lon1, double lat2, double lon2) {
|
|---|
| 129 | return 2*Math.asin(Math.sqrt(Math.pow(Math.sin((lat1-lat2)/2), 2) +
|
|---|
| 130 | Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin((lon1-lon2)/2), 2)));
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | public static double course(double lat1, double lon1, double lat2, double lon2) {
|
|---|
| 134 | return Math.atan2(Math.sin(lon1-lon2)*Math.cos(lat2),
|
|---|
| 135 | Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(lon1-lon2)) % (2*Math.PI);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
|
|---|
| 139 | double dist_AD = dist(lat1, lon1, lat3, lon3);
|
|---|
| 140 | double crs_AD = course(lat1, lon1, lat3, lon3);
|
|---|
| 141 | double crs_AB = course(lat1, lon1, lat2, lon2);
|
|---|
| 142 | return Math.asin(Math.sin(dist_AD)*Math.sin(crs_AD-crs_AB));
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|