Skip to content

Commit

Permalink
added replace building functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
r00tat committed Jul 14, 2017
1 parent 56b30f2 commit 2e00478
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand All @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Command> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ public class PreferencesPanel extends JPanel {

private JCheckBox ckbxHSV;

protected JCheckBox debug;
protected JCheckBox ckbxDebug;

protected JCheckBox ckbxAustriaAdressHelper;

private JComboBox<String> algorithm;

protected JComponent ref;

private JCheckBox ckbxReplaceBuilding;

/**
* Constructs a new {@code PreferencesPanel}.
*/
Expand Down Expand Up @@ -105,8 +107,11 @@ private void initialize() {
ckbxAustriaAdressHelper = new JCheckBox("<html><p><b>" + tr("use austria address helper") + "</b></p></html>");
this.addCheckbox(tr("Automatically try to find the correct address via Austria Address Helper plugin"), ckbxAustriaAdressHelper);

debug = new JCheckBox("<html><p><b>" + tr("Debug") + "</b></p></html>");
this.addCheckbox(tr("Debugging mode will write images for each processing step."), debug);
ckbxReplaceBuilding = new JCheckBox("<html><p><b>" + tr("Replace existing buildings") + "</b></p></html>");
this.addCheckbox(tr("Replace an existing building with the new one."), ckbxReplaceBuilding);

ckbxDebug = new JCheckBox("<html><p><b>" + tr("Debug") + "</b></p></html>");
this.addCheckbox(tr("Debugging mode will write images for each processing step."), ckbxDebug);

}

Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}
}

0 comments on commit 2e00478

Please sign in to comment.