source: osm/applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/SimplifyWayAction.java@ 10703

Last change on this file since 10703 was 10656, checked in by frederik, 17 years ago
  • modified SimplifyWay action to ask "are you sure" when simplifying more than 10 ways at once
File size: 5.2 KB
Line 
1package UtilsPlugin;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.event.ActionEvent;
6import java.awt.event.KeyEvent;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.LinkedList;
12import java.util.List;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.command.ChangeCommand;
18import org.openstreetmap.josm.command.Command;
19import org.openstreetmap.josm.command.DeleteCommand;
20import org.openstreetmap.josm.command.SequenceCommand;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
26
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.actions.JosmAction;
29
30public class SimplifyWayAction extends JosmAction {
31 public SimplifyWayAction() {
32 super(tr("Simplify Way"), "simplify",
33 tr("Delete unnecessary nodes from a way."), KeyEvent.VK_Y, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK, true);
34 }
35
36 public void actionPerformed(ActionEvent e) {
37 Collection<OsmPrimitive> selection = Main.ds.getSelected();
38
39 int ways = 0;
40 for (OsmPrimitive prim : selection) {
41 if (prim instanceof Way) {
42 ways++;
43 }
44 }
45
46 if (ways == 0) {
47 JOptionPane.showMessageDialog(Main.parent,
48 tr("Please select at least one way to simplify."));
49 return;
50 } else if (ways > 10) {
51 int option = JOptionPane.showConfirmDialog(Main.parent,
52 tr("The selection contains {0} ways. Are you sure you want to simplify them all?", ways), tr("Are you sure?"), JOptionPane.YES_NO_OPTION);
53 if (option != JOptionPane.YES_OPTION)
54 return;
55 }
56
57 for (OsmPrimitive prim : selection) {
58 if (prim instanceof Way) {
59 simplifyWay((Way) prim);
60 }
61 }
62 }
63
64 public void simplifyWay(Way w) {
65 double threshold = Double.parseDouble(
66 Main.pref.get("simplify-way.max-error", "3"));
67
68 Way wnew = new Way(w);
69
70 int toI = wnew.nodes.size() - 1;
71 for (int i = wnew.nodes.size() - 1; i >= 0; i--) {
72 CollectBackReferencesVisitor backRefsV =
73 new CollectBackReferencesVisitor(Main.ds, false);
74 backRefsV.visit(wnew.nodes.get(i));
75 boolean used = false;
76 if (backRefsV.data.size() == 1) {
77 used = Collections.frequency(
78 w.nodes, wnew.nodes.get(i)) > 1;
79 } else {
80 backRefsV.data.remove(w);
81 used = !backRefsV.data.isEmpty();
82 }
83 if (!used) used = wnew.nodes.get(i).tagged;
84
85 if (used) {
86 simplifyWayRange(wnew, i, toI, threshold);
87 toI = i;
88 }
89 }
90 simplifyWayRange(wnew, 0, toI, threshold);
91
92 HashSet<Node> delNodes = new HashSet<Node>();
93 delNodes.addAll(w.nodes);
94 delNodes.removeAll(wnew.nodes);
95
96 if (wnew.nodes.size() != w.nodes.size()) {
97 Collection<Command> cmds = new LinkedList<Command>();
98 cmds.add(new ChangeCommand(w, wnew));
99 cmds.add(new DeleteCommand(delNodes));
100 Main.main.undoRedo.add(
101 new SequenceCommand(tr("Simplify Way (remove {0} nodes)",
102 delNodes.size()),
103 cmds));
104 Main.map.repaint();
105 }
106 }
107
108 public void simplifyWayRange(Way wnew, int from, int to, double thr) {
109 if (to - from >= 2) {
110 ArrayList<Node> ns = new ArrayList<Node>();
111 simplifyWayRange(wnew, from, to, ns, thr);
112 for (int j = to-1; j > from; j--) wnew.nodes.remove(j);
113 wnew.nodes.addAll(from+1, ns);
114 }
115 }
116
117 /*
118 * Takes an interval [from,to] and adds nodes from (from,to) to ns.
119 * (from and to are indices of wnew.nodes.)
120 */
121 public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
122 Node fromN = wnew.nodes.get(from), toN = wnew.nodes.get(to);
123
124 int imax = -1;
125 double xtemax = 0;
126 for (int i = from+1; i < to; i++) {
127 Node n = wnew.nodes.get(i);
128 double xte = Math.abs(EARTH_RAD * xtd(
129 fromN.coor.lat() * Math.PI/180, fromN.coor.lon() * Math.PI/180,
130 toN.coor.lat() * Math.PI/180, toN.coor.lon() * Math.PI/180,
131 n.coor.lat() * Math.PI/180, n.coor.lon() * Math.PI/180));
132 if (xte > xtemax) {
133 xtemax = xte;
134 imax = i;
135 }
136 }
137
138 if (imax != -1 && xtemax >= thr) {
139 simplifyWayRange(wnew, from, imax, ns, thr);
140 ns.add(wnew.nodes.get(imax));
141 simplifyWayRange(wnew, imax, to, ns, thr);
142 }
143 }
144
145 public static double EARTH_RAD = 6378137.0;
146
147 /* From Aviaton Formulary v1.3
148 * http://williams.best.vwh.net/avform.htm
149 */
150 public static double dist(double lat1, double lon1, double lat2, double lon2) {
151 return 2*Math.asin(Math.sqrt(Math.pow(Math.sin((lat1-lat2)/2), 2) +
152 Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin((lon1-lon2)/2), 2)));
153 }
154
155 public static double course(double lat1, double lon1, double lat2, double lon2) {
156 return Math.atan2(Math.sin(lon1-lon2)*Math.cos(lat2),
157 Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(lon1-lon2)) % (2*Math.PI);
158 }
159
160 public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
161 double dist_AD = dist(lat1, lon1, lat3, lon3);
162 double crs_AD = course(lat1, lon1, lat3, lon3);
163 double crs_AB = course(lat1, lon1, lat2, lon2);
164 return Math.asin(Math.sin(dist_AD)*Math.sin(crs_AD-crs_AB));
165 }
166}
Note: See TracBrowser for help on using the repository browser.