source: josm/trunk/src/org/openstreetmap/josm/actions/CreateMultipolygonAction.java@ 6032

Last change on this file since 6032 was 5818, checked in by stoecker, 11 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 10.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14
15import java.util.Set;
16import java.util.TreeSet;
17import javax.swing.JOptionPane;
18
19import javax.swing.SwingUtilities;
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.command.AddCommand;
22import org.openstreetmap.josm.command.ChangeCommand;
23import org.openstreetmap.josm.command.ChangePropertyCommand;
24import org.openstreetmap.josm.command.Command;
25import org.openstreetmap.josm.command.SequenceCommand;
26import org.openstreetmap.josm.data.osm.MultipolygonCreate;
27import org.openstreetmap.josm.data.osm.MultipolygonCreate.JoinedPolygon;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.Relation;
30import org.openstreetmap.josm.data.osm.RelationMember;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
33import org.openstreetmap.josm.tools.Shortcut;
34
35/**
36 * Create multipolygon from selected ways automatically.
37 *
38 * New relation with type=multipolygon is created
39 *
40 * If one or more of ways is already in relation with type=multipolygon or the
41 * way is not closed, then error is reported and no relation is created
42 *
43 * The "inner" and "outer" roles are guessed automatically. First, bbox is
44 * calculated for each way. then the largest area is assumed to be outside and
45 * the rest inside. In cases with one "outside" area and several cut-ins, the
46 * guess should be always good ... In more complex (multiple outer areas) or
47 * buggy (inner and outer ways intersect) scenarios the result is likely to be
48 * wrong.
49 */
50public class CreateMultipolygonAction extends JosmAction {
51
52 public CreateMultipolygonAction() {
53 super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon."),
54 Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")),
55 KeyEvent.VK_A, Shortcut.ALT_CTRL), true);
56 }
57 /**
58 * The action button has been clicked
59 *
60 * @param e Action Event
61 */
62 public void actionPerformed(ActionEvent e) {
63 if (Main.main.getEditLayer() == null) {
64 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
65 return;
66 }
67
68 Collection<Way> selectedWays = Main.main.getCurrentDataSet().getSelectedWays();
69
70 if (selectedWays.size() < 1) {
71 // Sometimes it make sense creating multipoly of only one way (so it will form outer way)
72 // and then splitting the way later (so there are multiple ways forming outer way)
73 JOptionPane.showMessageDialog(Main.parent, tr("You must select at least one way."));
74 return;
75 }
76
77 MultipolygonCreate polygon = this.analyzeWays(selectedWays);
78
79 if (polygon == null)
80 return; //could not make multipolygon.
81
82 final Relation relation = this.createRelation(polygon);
83
84 if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) {
85 //Open relation edit window, if set up in preferences
86 RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null);
87
88 editor.setModal(true);
89 editor.setVisible(true);
90
91 //TODO: cannot get the resulting relation from RelationEditor :(.
92 /*
93 if (relationCountBefore < relationCountAfter) {
94 //relation saved, clean up the tags
95 List<Command> list = this.removeTagsFromInnerWays(relation);
96 if (list.size() > 0)
97 {
98 Main.main.undoRedo.add(new SequenceCommand(tr("Remove tags from multipolygon inner ways"), list));
99 }
100 }
101 */
102
103 } else {
104 //Just add the relation
105 List<Command> list = this.removeTagsFromWaysIfNeeded(relation);
106 list.add(new AddCommand(relation));
107 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), list));
108 // Use 'SwingUtilities.invokeLater' to make sure the relationListDialog
109 // knows about the new relation before we try to select it.
110 // (Yes, we are already in event dispatch thread. But DatasetEventManager
111 // uses 'SwingUtilities.invokeLater' to fire events so we have to do
112 // the same.)
113 SwingUtilities.invokeLater(new Runnable() {
114 public void run() {
115 Main.map.relationListDialog.selectRelation(relation);
116 }
117 });
118 }
119
120
121 }
122
123 /** Enable this action only if something is selected */
124 @Override protected void updateEnabledState() {
125 if (getCurrentDataSet() == null) {
126 setEnabled(false);
127 } else {
128 updateEnabledState(getCurrentDataSet().getSelected());
129 }
130 }
131
132 /**
133 * Enable this action only if something is selected
134 *
135 * @param selection the current selection, gets tested for emptyness
136 */
137 @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
138 setEnabled(selection != null && !selection.isEmpty());
139 }
140
141 /**
142 * This method analyzes ways and creates multipolygon.
143 * @param selectedWays list of selected ways
144 * @return <code>null</code>, if there was a problem with the ways.
145 */
146 private MultipolygonCreate analyzeWays(Collection < Way > selectedWays) {
147
148 MultipolygonCreate pol = new MultipolygonCreate();
149 String error = pol.makeFromWays(selectedWays);
150
151 if (error != null) {
152 JOptionPane.showMessageDialog(Main.parent, error);
153 return null;
154 } else {
155 return pol;
156 }
157 }
158
159 /**
160 * Builds a relation from polygon ways.
161 * @param pol data storage class containing polygon information
162 * @return multipolygon relation
163 */
164 private Relation createRelation(MultipolygonCreate pol) {
165 // Create new relation
166 Relation rel = new Relation();
167 rel.put("type", "multipolygon");
168 // Add ways to it
169 for (JoinedPolygon jway:pol.outerWays) {
170 for (Way way:jway.ways) {
171 rel.addMember(new RelationMember("outer", way));
172 }
173 }
174
175 for (JoinedPolygon jway:pol.innerWays) {
176 for (Way way:jway.ways) {
177 rel.addMember(new RelationMember("inner", way));
178 }
179 }
180 return rel;
181 }
182
183 static public final List<String> DEFAULT_LINEAR_TAGS = Arrays.asList(new String[] {"barrier", "source"});
184
185 /**
186 * This method removes tags/value pairs from inner and outer ways and put them on relation if necessary
187 * Function was extended in reltoolbox plugin by Zverikk and copied back to the core
188 * @param relation the multipolygon style relation to process
189 * @return a list of commands to execute
190 */
191 private List<Command> removeTagsFromWaysIfNeeded( Relation relation ) {
192 Map<String, String> values = new HashMap<String, String>();
193
194 if( relation.hasKeys() ) {
195 for( String key : relation.keySet() ) {
196 values.put(key, relation.get(key));
197 }
198 }
199
200 List<Way> innerWays = new ArrayList<Way>();
201 List<Way> outerWays = new ArrayList<Way>();
202
203 Set<String> conflictingKeys = new TreeSet<String>();
204
205 for( RelationMember m : relation.getMembers() ) {
206
207 if( m.hasRole() && "inner".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys() ) {
208 innerWays.add(m.getWay());
209 }
210
211 if( m.hasRole() && "outer".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys() ) {
212 Way way = m.getWay();
213 outerWays.add(way);
214
215 for( String key : way.keySet() ) {
216 if( !values.containsKey(key) ) { //relation values take precedence
217 values.put(key, way.get(key));
218 } else if( !relation.hasKey(key) && !values.get(key).equals(way.get(key)) ) {
219 conflictingKeys.add(key);
220 }
221 }
222 }
223 }
224
225 // filter out empty key conflicts - we need second iteration
226 if( !Main.pref.getBoolean("multipoly.alltags", false) )
227 for( RelationMember m : relation.getMembers() )
228 if( m.hasRole() && m.getRole().equals("outer") && m.isWay() )
229 for( String key : values.keySet() )
230 if( !m.getWay().hasKey(key) && !relation.hasKey(key) )
231 conflictingKeys.add(key);
232
233 for( String key : conflictingKeys )
234 values.remove(key);
235
236 for( String linearTag : Main.pref.getCollection("multipoly.lineartagstokeep", DEFAULT_LINEAR_TAGS) )
237 values.remove(linearTag);
238
239 if( values.containsKey("natural") && values.get("natural").equals("coastline") )
240 values.remove("natural");
241
242 values.put("area", "yes");
243
244 List<Command> commands = new ArrayList<Command>();
245 boolean moveTags = Main.pref.getBoolean("multipoly.movetags", true);
246
247 for( String key : values.keySet() ) {
248 List<OsmPrimitive> affectedWays = new ArrayList<OsmPrimitive>();
249 String value = values.get(key);
250
251 for( Way way : innerWays ) {
252 if( way.hasKey(key) && (value.equals(way.get(key))) ) {
253 affectedWays.add(way);
254 }
255 }
256
257 if( moveTags ) {
258 // remove duplicated tags from outer ways
259 for( Way way : outerWays ) {
260 if( way.hasKey(key) ) {
261 affectedWays.add(way);
262 }
263 }
264 }
265
266 if( affectedWays.size() > 0 ) {
267 // reset key tag on affected ways
268 commands.add(new ChangePropertyCommand(affectedWays, key, null));
269 }
270 }
271
272 if( moveTags ) {
273 // add those tag values to the relation
274
275 boolean fixed = false;
276 Relation r2 = new Relation(relation);
277 for( String key : values.keySet() ) {
278 if( !r2.hasKey(key) && !key.equals("area") ) {
279 if( relation.isNew() )
280 relation.put(key, values.get(key));
281 else
282 r2.put(key, values.get(key));
283 fixed = true;
284 }
285 }
286 if( fixed && !relation.isNew() )
287 commands.add(new ChangeCommand(relation, r2));
288 }
289
290 return commands;
291 }
292}
Note: See TracBrowser for help on using the repository browser.