diff --git a/src/org/openstreetmap/josm/plugins/areaselector/AreaSelectorAction.java b/src/org/openstreetmap/josm/plugins/areaselector/AreaSelectorAction.java index cf1835e..bfe73a9 100644 --- a/src/org/openstreetmap/josm/plugins/areaselector/AreaSelectorAction.java +++ b/src/org/openstreetmap/josm/plugins/areaselector/AreaSelectorAction.java @@ -34,6 +34,7 @@ import org.openstreetmap.josm.command.Command; import org.openstreetmap.josm.command.DeleteCommand; import org.openstreetmap.josm.command.PseudoCommand; +import org.openstreetmap.josm.command.RemoveNodesCommand; import org.openstreetmap.josm.command.SequenceCommand; import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.data.osm.Node; @@ -61,12 +62,13 @@ public class AreaSelectorAction extends MapMode implements MouseListener { protected double toleranceDist = ImageAnalyzer.DEFAULT_TOLERANCEDIST, toleranceAngle = ImageAnalyzer.DEFAULT_TOLERANCEANGLE; - protected boolean showAddressDialog = true, mergeNodes = true, useAustriaAdressHelper = false; + protected boolean showAddressDialog = true, mergeNodes = true, useAustriaAdressHelper = false, replaceBuildings = true; public static final String PLUGIN_NAME = "areaselector"; public static final String KEY_SHOWADDRESSDIALOG = PLUGIN_NAME + ".showaddressdialog", - KEY_MERGENODES = PLUGIN_NAME + ".mergenodes", KEY_AAH = PLUGIN_NAME + ".austriaadresshelper"; + KEY_MERGENODES = PLUGIN_NAME + ".mergenodes", KEY_AAH = PLUGIN_NAME + ".austriaadresshelper", + KEY_REPLACEBUILDINGS = PLUGIN_NAME + ".replacebuildings"; protected Logger log = LogManager.getLogger(AreaSelectorAction.class.getCanonicalName()); @@ -86,6 +88,7 @@ protected void readPrefs() { this.mergeNodes = new BooleanProperty(KEY_MERGENODES, true).get(); this.showAddressDialog = new BooleanProperty(KEY_SHOWADDRESSDIALOG, true).get(); useAustriaAdressHelper = new BooleanProperty(KEY_AAH, false).get(); + replaceBuildings = new BooleanProperty(KEY_REPLACEBUILDINGS, true).get(); } private static Cursor getCursor() { @@ -187,6 +190,8 @@ public void createArea() { Polygon polygon = imgAnalyzer.getArea(); if (polygon != null) { + Way existingWay = Main.map.mapView.getNearestWay(clickPoint, OsmPrimitive::isUsable); + Way way = createWayFromPolygon(mapView, polygon), newWay = null; way.put(AddressDialog.TAG_BUILDING, "yes"); @@ -217,6 +222,13 @@ public void createArea() { Main.main.undoRedo.add(c); Main.getLayerManager().getEditDataSet().setSelected(way); + if (replaceBuildings && existingWay != null){ + if (way.getBBox().bounds(existingWay.getBBox().getCenter())){ + log.info("existing way is inside of new building: "+existingWay.toString() + " is in " + way.toString()); + Main.main.undoRedo.add(replaceWay(existingWay, way)); + } + } + if (mergeNodes) { mergeNodes(way); } @@ -263,6 +275,37 @@ public OsmPrimitive fetchAddress(OsmPrimitive selectedObject) { return null; } + /** + * replace an existing way with the new detected one + * @param existingWay old way + * @param newWay new way + * @return replaced way + */ + public Command replaceWay(Way existingWay, Way newWay){ + if (existingWay == null || newWay == null){ + return null; + } + ArrayList cmds = new ArrayList<>(); + + cmds.add(new RemoveNodesCommand(existingWay, existingWay.getNodes())); + + for (Node existingNode : existingWay.getNodes()){ + if (existingNode.getParentWays().size() == 1){ + cmds.add(new DeleteCommand(existingNode)); + } + } + if(existingWay.getNodesCount() > 2 && existingWay.getRealNodesCount() > 1){ + // do not try to delete the first node again, as it will be twice in the way + cmds.remove(cmds.size()-1); + } + + for (Node newNode : newWay.getNodes()){ + existingWay.addNode(newNode); + } + + return new SequenceCommand(tr("replace bauilding"), cmds); + } + public Way createWayFromPolygon(MapView mapView, Polygon polygon) { Way way = new Way(); diff --git a/src/org/openstreetmap/josm/plugins/areaselector/preferences/PreferencesPanel.java b/src/org/openstreetmap/josm/plugins/areaselector/preferences/PreferencesPanel.java index fc5c7bc..560f7c0 100644 --- a/src/org/openstreetmap/josm/plugins/areaselector/preferences/PreferencesPanel.java +++ b/src/org/openstreetmap/josm/plugins/areaselector/preferences/PreferencesPanel.java @@ -39,7 +39,7 @@ public class PreferencesPanel extends JPanel { private JCheckBox ckbxHSV; - protected JCheckBox debug; + protected JCheckBox ckbxDebug; protected JCheckBox ckbxAustriaAdressHelper; @@ -47,6 +47,8 @@ public class PreferencesPanel extends JPanel { protected JComponent ref; + private JCheckBox ckbxReplaceBuilding; + /** * Constructs a new {@code PreferencesPanel}. */ @@ -105,8 +107,11 @@ private void initialize() { ckbxAustriaAdressHelper = new JCheckBox("

" + tr("use austria address helper") + "

"); this.addCheckbox(tr("Automatically try to find the correct address via Austria Address Helper plugin"), ckbxAustriaAdressHelper); - debug = new JCheckBox("

" + tr("Debug") + "

"); - this.addCheckbox(tr("Debugging mode will write images for each processing step."), debug); + ckbxReplaceBuilding = new JCheckBox("

" + tr("Replace existing buildings") + "

"); + this.addCheckbox(tr("Replace an existing building with the new one."), ckbxReplaceBuilding); + + ckbxDebug = new JCheckBox("

" + tr("Debug") + "

"); + this.addCheckbox(tr("Debugging mode will write images for each processing step."), ckbxDebug); } @@ -182,8 +187,9 @@ public void savePreferences() { new BooleanProperty(AreaSelectorAction.KEY_MERGENODES, true).put(ckbxMergeNodes.isSelected()); new BooleanProperty(AreaSelectorAction.KEY_SHOWADDRESSDIALOG, true).put(ckbxShowAddressDialog.isSelected()); new BooleanProperty(ImageAnalyzer.KEY_HSV, false).put(ckbxHSV.isSelected()); - new BooleanProperty(ImageAnalyzer.KEY_DEBUG, false).put(debug.isSelected()); + new BooleanProperty(ImageAnalyzer.KEY_DEBUG, false).put(ckbxDebug.isSelected()); new BooleanProperty(AreaSelectorAction.KEY_AAH, false).put(ckbxAustriaAdressHelper.isSelected()); + new BooleanProperty(AreaSelectorAction.KEY_REPLACEBUILDINGS, true).put(ckbxReplaceBuilding.isSelected()); } /** @@ -203,7 +209,7 @@ public void readPreferences() { int algorithmIdx = new IntegerProperty(ImageAnalyzer.KEY_ALGORITHM, ImageAnalyzer.DEFAULT_ALGORITHM).get(); algorithm.setSelectedIndex(algorithmIdx < algorithm.getMaximumRowCount() ? algorithmIdx : ImageAnalyzer.DEFAULT_ALGORITHM); - debug.setSelected(new BooleanProperty(ImageAnalyzer.KEY_DEBUG, false).get()); - + ckbxDebug.setSelected(new BooleanProperty(ImageAnalyzer.KEY_DEBUG, false).get()); + ckbxReplaceBuilding.setSelected(new BooleanProperty(AreaSelectorAction.KEY_REPLACEBUILDINGS, true).get()); } }