Changeset 27623 in osm for applications/editors/josm/plugins
- Timestamp:
- 2012-01-26T04:59:08+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/dumbutils/ReplaceGeometryAction.java
r27564 r27623 1 1 package utilsplugin2.dumbutils; 2 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.KeyEvent; 5 import java.awt.geom.Area; 3 6 import java.awt.geom.Point2D; 4 import java.awt.geom.Area; 7 import java.util.*; 8 import javax.swing.JOptionPane; 9 import org.openstreetmap.josm.Main; 10 import org.openstreetmap.josm.actions.JosmAction; 11 import org.openstreetmap.josm.command.*; 12 import org.openstreetmap.josm.data.coor.LatLon; 5 13 import org.openstreetmap.josm.data.osm.Node; 6 import java.util.*; 7 import org.openstreetmap.josm.command.*; 8 import org.openstreetmap.josm.Main; 9 import javax.swing.JOptionPane; 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 import org.openstreetmap.josm.data.osm.Relation; 10 16 import org.openstreetmap.josm.data.osm.Way; 11 import java.awt.event.KeyEvent;12 import org.openstreetmap.josm.tools.Shortcut;13 import java.awt.event.ActionEvent;14 import org.openstreetmap.josm.actions.JosmAction;15 import org.openstreetmap.josm.data.osm.OsmPrimitive;16 17 import org.openstreetmap.josm.gui.DefaultNameFormatter; 17 18 import static org.openstreetmap.josm.tools.I18n.tr; 19 import org.openstreetmap.josm.tools.Shortcut; 18 20 19 21 /** … … 24 26 public class ReplaceGeometryAction extends JosmAction { 25 27 private static final String TITLE = tr("Replace Geometry"); 26 private static final double MAX_NODE_REPLACEMENT_DISTANCE = 3e-4;27 28 28 29 public ReplaceGeometryAction() { … … 39 40 40 41 // There must be two ways selected: one with id > 0 and one new. 41 List<OsmPrimitive> selection = new ArrayList (getCurrentDataSet().getSelected());42 List<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected()); 42 43 if (selection.size() != 2) { 43 44 JOptionPane.showMessageDialog(Main.parent, … … 75 76 Collection<Node> nodePool = getUnimportantNodes(way); 76 77 nodeToReplace = findNearestNode(node, nodePool); 77 78 if (nodeToReplace == null && !nodePool.isEmpty()) {79 // findNearestNode failed, just pick the first unimportant node80 nodeToReplace = nodePool.iterator().next();81 }82 78 } 83 79 … … 124 120 125 121 public void replaceWayWithWay(List<Way> selection) { 122 // determine which way will be replaced and which will provide the geometry 126 123 boolean overrideNewCheck = false; 127 124 int idxNew = selection.get(0).isNew() ? 0 : 1; 128 129 125 if( selection.get(1-idxNew).isNew() ) { 130 126 // if both are new, select the one with all the DB nodes … … 145 141 Way geometry = selection.get(idxNew); 146 142 Way way = selection.get(1 - idxNew); 143 147 144 if( !overrideNewCheck && (way.isNew() || !geometry.isNew()) ) { 148 145 JOptionPane.showMessageDialog(Main.parent, 149 146 tr("Please select one way that exists in the database and one new way with correct geometry."), 147 TITLE, JOptionPane.WARNING_MESSAGE); 148 return; 149 } 150 151 Area a = getCurrentDataSet().getDataSourceArea(); 152 if (!isInArea(way, a) || !isInArea(geometry, a)) { 153 JOptionPane.showMessageDialog(Main.parent, 154 tr("The ways must be entirely within the downloaded area."), 155 TITLE, JOptionPane.WARNING_MESSAGE); 156 return; 157 } 158 159 if (hasImportantNode(way)) { 160 JOptionPane.showMessageDialog(Main.parent, 161 tr("The way to be replaced cannot have any nodes with properties or relation memberships."), 150 162 TITLE, JOptionPane.WARNING_MESSAGE); 151 163 return; … … 160 172 List<OsmPrimitive> referrers = node.getReferrers(); 161 173 if( node.isNew() && !node.isDeleted() && referrers.size() == 1 162 && referrers.get(0).equals(geometry) && !way.containsNode(node) ) 174 && referrers.get(0).equals(geometry) && !way.containsNode(node) 175 && !hasInterestingKey(node)) 163 176 geometryPool.add(node); 164 177 } … … 221 234 protected Collection<Node> getUnimportantNodes(Way way) { 222 235 Set<Node> nodePool = new HashSet<Node>(); 223 Area a = getCurrentDataSet().getDataSourceArea();224 236 for (Node n : way.getNodes()) { 225 237 List<OsmPrimitive> referrers = n.getReferrers(); 226 238 if (!n.isDeleted() && referrers.size() == 1 && referrers.get(0).equals(way) 227 && (n.isNewOrUndeleted() || a == null || a.contains(n.getCoor()))) {239 && !hasInterestingKey(n)) { 228 240 nodePool.add(n); 229 241 } 230 242 } 231 243 return nodePool; 244 } 245 246 /** 247 * Checks if a way has at least one important node (e.g. interesting tag, 248 * role membership), and thus cannot be safely modified. 249 * 250 * @param way 251 * @return 252 */ 253 protected boolean hasImportantNode(Way way) { 254 for (Node n : way.getNodes()) { 255 //TODO: if way is connected to other ways, warn or disallow? 256 for (OsmPrimitive o : n.getReferrers()) { 257 if (o instanceof Relation) { 258 return true; 259 } 260 } 261 if (hasInterestingKey(n)) { 262 return true; 263 } 264 } 265 return false; 266 } 267 268 protected boolean hasInterestingKey(OsmPrimitive object) { 269 for (String key : object.getKeys().keySet()) { 270 if (!OsmPrimitive.isUninterestingKey(key)) { 271 return true; 272 } 273 } 274 return false; 275 } 276 277 protected static boolean isInArea(Node node, Area area) { 278 if (node.isNewOrUndeleted() || area == null || area.contains(node.getCoor())) { 279 return true; 280 } 281 return false; 282 } 283 284 protected static boolean isInArea(Way way, Area area) { 285 if (area == null) { 286 return true; 287 } 288 289 for (Node n : way.getNodes()) { 290 if (!isInArea(n, area)) { 291 return false; 292 } 293 } 294 295 return true; 232 296 } 233 297 … … 241 305 242 306 Node nearest = null; 243 double distance = MAX_NODE_REPLACEMENT_DISTANCE; 307 // TODO: use meters instead of degrees, but do it fast 308 double distance = Double.parseDouble(Main.pref.get("utilsplugin2.replace-geometry.max-distance", "1")); 244 309 Point2D coor = node.getCoor(); 310 245 311 for( Node n : nodes ) { 246 312 double d = n.getCoor().distance(coor);
Note:
See TracChangeset
for help on using the changeset viewer.