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

Last change on this file since 12421 was 11348, checked in by mfloryan, 17 years ago

Enhanced translations of UtilsPlugin to cater for plurals better.

File size: 8.1 KB
Line 
1package UtilsPlugin;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.LinkedList;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.JosmAction;
18import org.openstreetmap.josm.command.ChangeCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.DeleteCommand;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.osm.DataSource;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29
30public class SimplifyWayAction extends JosmAction {
31 public SimplifyWayAction() {
32 super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."), KeyEvent.VK_Y,
33 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 LinkedList<Bounds> bounds = new LinkedList<Bounds>();
41 OsmDataLayer dataLayer = Main.main.editLayer();
42 for (DataSource ds : dataLayer.data.dataSources) {
43 if (ds.bounds != null)
44 bounds.add(ds.bounds);
45 }
46 for (OsmPrimitive prim : selection) {
47 if (prim instanceof Way) {
48 if (bounds.size() > 0) {
49 Way way = (Way) prim;
50 // We check if each node of each way is at least in one download
51 // bounding box. Otherwise nodes may get deleted that are necessary by
52 // unloaded ways (see Ticket #1594)
53 for (Node node : way.nodes) {
54 boolean isInsideOneBoundingBox = false;
55 for (Bounds b : bounds) {
56 if (b.contains(node.coor)) {
57 isInsideOneBoundingBox = true;
58 break;
59 }
60 }
61 if (!isInsideOneBoundingBox) {
62 int option = JOptionPane.showConfirmDialog(Main.parent,
63 tr("The selected way(s) have nodes outside of the downloaded data region."
64 + "This can lead to nodes beeing deleted accidentially.\n"
65 + "Are you really sure to continue?"),
66 tr("Plase abort if you are not sure"), JOptionPane.YES_NO_CANCEL_OPTION,
67 JOptionPane.WARNING_MESSAGE);
68
69 if (option != JOptionPane.YES_OPTION)
70 return;
71 break;
72 }
73 }
74 }
75
76 ways++;
77 }
78 }
79
80 if (ways == 0) {
81 JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way to simplify."));
82 return;
83 } else if (ways > 10) {
84 //TRANSLATION: Although for English the use of trn is needless it is important for other languages
85 int option = JOptionPane.showConfirmDialog(Main.parent, trn(
86 "The selection contains {0} way. Are you sure you want to simplify them all?",
87 "The selection contains {0} ways. Are you sure you want to simplify them all?",
88 ways,ways),
89 tr("Are you sure?"), JOptionPane.YES_NO_OPTION);
90 if (option != JOptionPane.YES_OPTION)
91 return;
92 }
93
94 for (OsmPrimitive prim : selection) {
95 if (prim instanceof Way) {
96 simplifyWay((Way) prim);
97 }
98 }
99 }
100
101 public void simplifyWay(Way w) {
102 double threshold = Double.parseDouble(Main.pref.get("simplify-way.max-error", "3"));
103
104 Way wnew = new Way(w);
105
106 int toI = wnew.nodes.size() - 1;
107 for (int i = wnew.nodes.size() - 1; i >= 0; i--) {
108 CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(Main.ds, false);
109 backRefsV.visit(wnew.nodes.get(i));
110 boolean used = false;
111 if (backRefsV.data.size() == 1) {
112 used = Collections.frequency(w.nodes, wnew.nodes.get(i)) > 1;
113 } else {
114 backRefsV.data.remove(w);
115 used = !backRefsV.data.isEmpty();
116 }
117 if (!used)
118 used = wnew.nodes.get(i).tagged;
119
120 if (used) {
121 simplifyWayRange(wnew, i, toI, threshold);
122 toI = i;
123 }
124 }
125 simplifyWayRange(wnew, 0, toI, threshold);
126
127 HashSet<Node> delNodes = new HashSet<Node>();
128 delNodes.addAll(w.nodes);
129 delNodes.removeAll(wnew.nodes);
130
131 if (wnew.nodes.size() != w.nodes.size()) {
132 Collection<Command> cmds = new LinkedList<Command>();
133 cmds.add(new ChangeCommand(w, wnew));
134 cmds.add(new DeleteCommand(delNodes));
135 Main.main.undoRedo.add(new SequenceCommand(trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds));
136 Main.map.repaint();
137 }
138 }
139
140 public void simplifyWayRange(Way wnew, int from, int to, double thr) {
141 if (to - from >= 2) {
142 ArrayList<Node> ns = new ArrayList<Node>();
143 simplifyWayRange(wnew, from, to, ns, thr);
144 for (int j = to - 1; j > from; j--)
145 wnew.nodes.remove(j);
146 wnew.nodes.addAll(from + 1, ns);
147 }
148 }
149
150 /*
151 * Takes an interval [from,to] and adds nodes from (from,to) to ns.
152 * (from and to are indices of wnew.nodes.)
153 */
154 public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
155 Node fromN = wnew.nodes.get(from), toN = wnew.nodes.get(to);
156
157 int imax = -1;
158 double xtemax = 0;
159 for (int i = from + 1; i < to; i++) {
160 Node n = wnew.nodes.get(i);
161 double xte = Math.abs(EARTH_RAD
162 * xtd(fromN.coor.lat() * Math.PI / 180, fromN.coor.lon() * Math.PI / 180, toN.coor.lat() * Math.PI
163 / 180, toN.coor.lon() * Math.PI / 180, n.coor.lat() * Math.PI / 180, n.coor.lon() * Math.PI
164 / 180));
165 if (xte > xtemax) {
166 xtemax = xte;
167 imax = i;
168 }
169 }
170
171 if (imax != -1 && xtemax >= thr) {
172 simplifyWayRange(wnew, from, imax, ns, thr);
173 ns.add(wnew.nodes.get(imax));
174 simplifyWayRange(wnew, imax, to, ns, thr);
175 }
176 }
177
178 public static double EARTH_RAD = 6378137.0;
179
180 /* From Aviaton Formulary v1.3
181 * http://williams.best.vwh.net/avform.htm
182 */
183 public static double dist(double lat1, double lon1, double lat2, double lon2) {
184 return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
185 * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
186 }
187
188 public static double course(double lat1, double lon1, double lat2, double lon2) {
189 return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
190 * Math.cos(lat2) * Math.cos(lon1 - lon2))
191 % (2 * Math.PI);
192 }
193
194 public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
195 double dist_AD = dist(lat1, lon1, lat3, lon3);
196 double crs_AD = course(lat1, lon1, lat3, lon3);
197 double crs_AB = course(lat1, lon1, lat2, lon2);
198 return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB));
199 }
200}
Note: See TracBrowser for help on using the repository browser.