source: josm/trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java@ 2602

Last change on this file since 2602 was 2575, checked in by stoecker, 14 years ago

include utilsplugin, fix #4086

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