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