Skip to content

Commit

Permalink
Merge pull request #26 from JOSM/austriaaddresshelper
Browse files Browse the repository at this point in the history
Austriaaddresshelper
  • Loading branch information
r00tat authored Mar 10, 2017
2 parents aa392df + 01deaad commit 0ef3c03
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 44 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
<classpathentry kind="lib" path="lib/xstream-jar.jar"/>
<classpathentry kind="lib" path="lib/marvin-custom.jar"/>
<classpathentry kind="lib" path="lib/marvinplugins-custom.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/JOSM-austriaaddresshelper"/>
<classpathentry kind="output" path="bin"/>
</classpath>
7 changes: 6 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

<!-- Configure these properties (replace "..." accordingly).
See https://josm.openstreetmap.de/wiki/DevelopersGuide/DevelopingPlugins
-->
-->

<property name="plugin.author" value="Paul Woelfel, Thomas Konrad" />
<property name="plugin.class" value="org.openstreetmap.josm.plugins.areaselector.AreaSelectorPlugin" />
<property name="plugin.description" value="Allows selection of areas in an layer and automatic creation of a way as polygon. Built to ease mapping of building from background layers. Optimized for basemap.at." />
Expand Down Expand Up @@ -38,6 +39,7 @@
** compile - complies the source tree
* and includes log4j.jar
**********************************************************
-->
<target name="compile" depends="init, fetch_dependencies">
<echo message="compiling sources for ${plugin.jar} ... "/>
Expand All @@ -51,6 +53,9 @@
<include name="*-custom.jar"/>
<include name="*-jar.jar"/>
</fileset>
<fileset dir="${plugin.dist.dir}">
<include name="austriaaddresshelper.jar"/>
</fileset>

</classpath>
<compilerarg value="-Xlint:deprecation"/>
Expand Down
96 changes: 60 additions & 36 deletions src/org/openstreetmap/josm/plugins/areaselector/AddressDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class AddressDialog extends ExtendedDialog implements ChangeListener {
public static final String TAG_POSTCODE = "addr:postcode";
public static final String TAG_COUNTRY = "addr:country";
public static final String TAG_BUILDING = "building";
public static final String TAG_SOURCE = "source";
public static final String[] TAGS = {TAG_HOUSENAME, TAG_HOUSENUM, TAG_STREETNAME, TAG_CITY, TAG_POSTCODE, TAG_COUNTRY, TAG_BUILDING, TAG_SOURCE};

public static final String PREF = AreaSelectorAction.PLUGIN_NAME+".last.";

Expand All @@ -64,22 +66,21 @@ public class AddressDialog extends ExtendedDialog implements ChangeListener {
PREF_BUILDING = PREF+"building",
PREF_TAGS = PREF+"tags",
PREF_HOUSENUM_CHANGE = PREF+"housenum.change",
PREF_SOURCE = PREF + "source",
PREF_DIALOG_X = PREF+"dialog.x",
PREF_DIALOG_Y = PREF+"dialog.y";

protected String houseNum, streetName, city, postCode, country, houseName, building, tags;

protected JTextField houseNumField;
protected ButtonGroup houseNumChange;

protected AutoCompletingComboBox streetNameField, cityField, postCodeField, countryField, houseNameField, buildingField, tagsField;
protected AutoCompletingComboBox streetNameField, cityField, postCodeField, countryField, houseNameField, buildingField, tagsField, sourceField;

protected static final String[] BUTTON_TEXTS = new String[] {tr("OK"), tr("Cancel")};
protected static final String[] BUTTON_ICONS = new String[] {"ok.png", "cancel.png"};

protected final JPanel panel = new JPanel(new GridBagLayout());

protected OsmPrimitive way;
protected OsmPrimitive originalOsmObject, osmObject;

protected static Collection<AutoCompletionListItem> aciTags;

Expand All @@ -96,10 +97,16 @@ protected final void addLabelled(String str, Component c) {
panel.add(c, GBC.eol().fill(GBC.HORIZONTAL));
}

public AddressDialog(OsmPrimitive way) {
public AddressDialog(OsmPrimitive selectedOsmObject){
this(selectedOsmObject, null);
}

public AddressDialog(OsmPrimitive selectedOsmObject, OsmPrimitive originalOsmObject) {
super(Main.parent, tr("Building address"), BUTTON_TEXTS, true);

this.way = way;
this.originalOsmObject = originalOsmObject != null ? originalOsmObject : selectedOsmObject;
this.osmObject = selectedOsmObject;
// this.osmObject = selectedOsmObject instanceof Node ? new Node(((Node) selectedOsmObject)) : selectedOsmObject instanceof Way ? new Way((Way) selectedOsmObject) : selectedOsmObject instanceof Relation ? new Relation((Relation) selectedOsmObject): selectedOsmObject;

contentInsets = new Insets(15, 15, 5, 15);
setButtonIcons(BUTTON_ICONS);
Expand Down Expand Up @@ -197,6 +204,12 @@ public void actionPerformed(ActionEvent e) {
tagsField.setEditable(true);
tagsField.setSelectedItem(Main.pref.get(PREF_TAGS));

sourceField = new AutoCompletingComboBox();
sourceField.setPossibleACItems(acm.getValues(TAG_SOURCE));
sourceField.setEditable(true);
sourceField.setPreferredSize(new Dimension(400, 24));
sourceField.setSelectedItem(Main.pref.get(PREF_SOURCE));

JLabel houseNumLabel = new JLabel(tr("House number:"));
houseNumLabel.setLabelFor(houseNumField);
panel.add(houseNumLabel, GBC.std());
Expand All @@ -208,6 +221,7 @@ public void actionPerformed(ActionEvent e) {
addLabelled(tr("Country:"), countryField);
addLabelled(tr("Building:"), buildingField);
addLabelled(tr("Tags:"), tagsField);
addLabelled(tr("Source:"), sourceField);

addLabelled(tr("Name:"), houseNameField);
houseNumField.setName(TAG_HOUSENUM);
Expand All @@ -217,13 +231,28 @@ public void actionPerformed(ActionEvent e) {
buildingField.setName(TAG_BUILDING);
tagsField.setName("tags");
houseNameField.setName(TAG_HOUSENAME);
sourceField.setName(TAG_SOURCE);

fields = new Vector<>();
Component[] fieldArr = {
houseNumField, streetNameField, cityField, postCodeField,
countryField, buildingField, tagsField, houseNameField};
countryField, buildingField, tagsField, sourceField, houseNameField};
fields.addAll(Arrays.asList(fieldArr));

for (Component field: fields){
if (field instanceof AutoCompletingComboBox){
AutoCompletingComboBox combox = (AutoCompletingComboBox) field;
if (selectedOsmObject.hasKey(combox.getName())){
combox.setSelectedItem(selectedOsmObject.get(combox.getName()));
}
}else if (field instanceof JTextField){
JTextField textField = (JTextField) field;
if (selectedOsmObject.hasKey(textField.getName())){
textField.setText(selectedOsmObject.get(textField.getName()));
}
}
}

this.setFocusTraversalPolicy(new FocusTraversalPolicy() {

@Override
Expand Down Expand Up @@ -271,7 +300,7 @@ public int getIndex(Component c) {

setContent(panel);
setupDialog();
this.setSize(630, 350);
this.setSize(700, 400);

try {
this.setLocation(Integer.parseInt(Main.pref.get(PREF_DIALOG_X)), Integer.parseInt(Main.pref.get(PREF_DIALOG_Y)));
Expand All @@ -296,32 +325,26 @@ protected String getAutoCompletingComboBoxValue(AutoCompletingComboBox box) {
}

public final void saveValues() {
houseName = getAutoCompletingComboBoxValue(houseNameField);
houseNum = houseNumField.getText();
streetName = getAutoCompletingComboBoxValue(streetNameField);
city = getAutoCompletingComboBoxValue(cityField);
postCode = getAutoCompletingComboBoxValue(postCodeField);
country = getAutoCompletingComboBoxValue(countryField);
building = getAutoCompletingComboBoxValue(buildingField);
tags = getAutoCompletingComboBoxValue(tagsField);

Main.pref.put(PREF_HOUSENUM, houseNum);
Main.pref.put(PREF_STREETNAME, streetName);
Main.pref.put(PREF_CITY, city);
Main.pref.put(PREF_POSTCODE, postCode);
Main.pref.put(PREF_COUNTRY, country);
Main.pref.put(PREF_BUILDING, building);
String tags = getAutoCompletingComboBoxValue(tagsField);

Main.pref.put(PREF_HOUSENUM, houseNumField.getText());
Main.pref.put(PREF_STREETNAME, getAutoCompletingComboBoxValue(streetNameField));
Main.pref.put(PREF_CITY, getAutoCompletingComboBoxValue(cityField));
Main.pref.put(PREF_POSTCODE, getAutoCompletingComboBoxValue(postCodeField));
Main.pref.put(PREF_COUNTRY, getAutoCompletingComboBoxValue(countryField));
Main.pref.put(PREF_BUILDING, getAutoCompletingComboBoxValue(buildingField));
Main.pref.put(PREF_SOURCE, getAutoCompletingComboBoxValue(sourceField));
Main.pref.put(PREF_TAGS, tags);
Main.pref.put(PREF_HOUSENUM_CHANGE, houseNumChange.getSelection().getActionCommand());


updateTag(TAG_HOUSENAME, houseName);
updateTag(TAG_HOUSENUM, houseNum);
updateTag(TAG_STREETNAME, streetName);
updateTag(TAG_CITY, city);
updateTag(TAG_POSTCODE, postCode);
updateTag(TAG_COUNTRY, country);
updateTag(TAG_BUILDING, building);
updateTag(TAG_HOUSENAME, getAutoCompletingComboBoxValue(houseNameField));
updateTag(TAG_HOUSENUM, houseNumField.getText());
updateTag(TAG_STREETNAME, getAutoCompletingComboBoxValue(streetNameField));
updateTag(TAG_CITY, getAutoCompletingComboBoxValue(cityField));
updateTag(TAG_POSTCODE, getAutoCompletingComboBoxValue(postCodeField));
updateTag(TAG_COUNTRY, getAutoCompletingComboBoxValue(countryField));
updateTag(TAG_BUILDING, getAutoCompletingComboBoxValue(buildingField));

if (!tags.isEmpty()) {
AutoCompletionListItem aci = new AutoCompletionListItem(tags);
Expand All @@ -345,11 +368,11 @@ public void setHouseNumChange(int num) {

public void updateTag(String tag, String value) {
if (value == null || value.isEmpty()) {
if (way.get(tag) != null) {
way.remove(tag);
if (osmObject.get(tag) != null) {
osmObject.remove(tag);
}
} else {
way.put(tag, value);
osmObject.put(tag, value);
}
}

Expand All @@ -358,16 +381,17 @@ public OsmPrimitive showAndSave() {
if (this.getValue() == 1) {
this.saveValues();
Collection<Command> cmds = new LinkedList<>();
cmds.add(new ChangeCommand(way, way));
log.info("updated properties "+osmObject + "\n" + osmObject.getKeys());
cmds.add(new ChangeCommand(originalOsmObject, osmObject));
Command c = new SequenceCommand(tr("updated building info"), cmds);
Main.main.undoRedo.add(c);
Main.getLayerManager().getEditDataSet().setSelected(way);
Main.getLayerManager().getEditDataSet().setSelected(osmObject);
}

Main.pref.put(PREF_DIALOG_X, Integer.toString(this.getLocation().x));
Main.pref.put(PREF_DIALOG_Y, Integer.toString(this.getLocation().y));

return way;
return osmObject;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.openstreetmap.josm.plugins.areaselector;

import static org.openstreetmap.josm.tools.I18n.tr;
import static org.openstreetmap.josm.tools.I18n.trn;

import java.awt.AlphaComposite;
import java.awt.Composite;
Expand All @@ -17,6 +18,7 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
Expand All @@ -27,6 +29,7 @@
import org.openstreetmap.josm.actions.MergeNodesAction;
import org.openstreetmap.josm.actions.mapmode.MapMode;
import org.openstreetmap.josm.command.AddCommand;
import org.openstreetmap.josm.command.ChangeCommand;
import org.openstreetmap.josm.command.Command;
import org.openstreetmap.josm.command.DeleteCommand;
import org.openstreetmap.josm.command.PseudoCommand;
Expand All @@ -42,6 +45,7 @@
import org.openstreetmap.josm.gui.MapView;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.plugins.austriaaddresshelper.AustriaAddressHelperAction;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.Shortcut;

Expand All @@ -54,13 +58,14 @@ public class AreaSelectorAction extends MapMode implements MouseListener {
protected int colorThreshold = ImageAnalyzer.DEFAULT_COLORTHRESHOLD, thinningIterations = ImageAnalyzer.DEFAULT_THINNING_ITERATIONS;
protected double toleranceDist = ImageAnalyzer.DEFAULT_TOLERANCEDIST, toleranceAngle = ImageAnalyzer.DEFAULT_TOLERANCEANGLE;

protected boolean showAddressDialog = true, mergeNodes = true;
protected boolean showAddressDialog = true, mergeNodes = true, useAustriaAdressHelper = false;

public static final String PLUGIN_NAME = "areaselector";

public static final String
KEY_SHOWADDRESSDIALOG = PLUGIN_NAME + ".showaddressdialog",
KEY_MERGENODES = PLUGIN_NAME + ".mergenodes";
KEY_MERGENODES = PLUGIN_NAME + ".mergenodes",
KEY_AAH = PLUGIN_NAME + ".austriaadresshelper";


protected Logger log = LogManager.getLogger(AreaSelectorAction.class.getCanonicalName());
Expand All @@ -79,6 +84,7 @@ public AreaSelectorAction(MapFrame mapFrame) {
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();
}

private static Cursor getCursor() {
Expand Down Expand Up @@ -181,10 +187,21 @@ public void createArea() {
Polygon polygon = imgAnalyzer.getArea();

if (polygon != null) {
Way way = createWayFromPolygon(mapView, polygon);
Way way = createWayFromPolygon(mapView, polygon), newWay = null;

way.put(AddressDialog.TAG_BUILDING, "yes");

ArrayList<String> sources = new ArrayList<>();
for (Layer layer: mapView.getLayerManager().getVisibleLayersInZOrder()) {
if (layer.isVisible() && layer.isBackgroundLayer()) {
sources.add(layer.getName());
}
}
String source = sources.stream().
map(Object::toString).
collect(Collectors.joining("; ")).toString();
if (!source.isEmpty()){
way.put(AddressDialog.TAG_SOURCE, source);
}

Collection<Command> cmds = new LinkedList<>();
List<Node> nodes = way.getNodes();
Expand All @@ -202,17 +219,46 @@ public void createArea() {
mergeNodes(way);
}

if (useAustriaAdressHelper){
log.info("trying to fetch address ");
newWay = (Way) fetchAddress(way);
if(newWay != null){
if(!showAddressDialog){
final List<Command> commands = new ArrayList<>();
commands.add(new ChangeCommand(way, newWay));
Main.main.undoRedo.add(new SequenceCommand(trn("Add address", "Add addresses", commands.size()), commands));
}
}
}

if (showAddressDialog) {
showAddressDialog(way);
if (newWay == null){
newWay = way;
}
new AddressDialog(newWay, way).showAndSave();
}
} else {
JOptionPane.showMessageDialog(Main.map,
tr("Unable to detect a polygon where you clicked."), tr("Area Selector"), JOptionPane.WARNING_MESSAGE);
}
}

public OsmPrimitive showAddressDialog(Way way) {
return new AddressDialog(way).showAndSave();
/**
* fetch Address using Austria Adress Helper
*/
public OsmPrimitive fetchAddress(OsmPrimitive selectedObject){
try{
return AustriaAddressHelperAction.loadAddress(selectedObject);
}
catch(Throwable t){
if (t instanceof NoClassDefFoundError){
Main.warn("Austria Address Helper not loaded");
}else {
Main.warn("Something went wrong with Austria Adress Helper");
Main.warn(t);
}
}
return null;
}

public Way createWayFromPolygon(MapView mapView, Polygon polygon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class PreferencesPanel extends JPanel {

protected JCheckBox debug;

protected JCheckBox ckbxAustriaAdressHelper;

private JComboBox<String> algorithm;

protected JComponent ref;
Expand Down Expand Up @@ -100,6 +102,9 @@ private void initialize() {
ckbxMergeNodes = new JCheckBox("<html><p><b>" + tr("merge nodes") + "</b></p></html>");
this.addCheckbox(tr("Merge nodes with existing nodes"), ckbxMergeNodes);

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);

Expand Down Expand Up @@ -178,6 +183,7 @@ public void savePreferences() {
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(AreaSelectorAction.KEY_AAH, false).put(ckbxAustriaAdressHelper.isSelected());
}

/**
Expand All @@ -193,6 +199,7 @@ public void readPreferences() {
ckbxMergeNodes.setSelected(new BooleanProperty(AreaSelectorAction.KEY_MERGENODES, true).get());
ckbxShowAddressDialog.setSelected(new BooleanProperty(AreaSelectorAction.KEY_SHOWADDRESSDIALOG, true).get());
ckbxHSV.setSelected(new BooleanProperty(ImageAnalyzer.KEY_HSV, false).get());
ckbxAustriaAdressHelper.setSelected(new BooleanProperty(AreaSelectorAction.KEY_AAH, false).get());

int algorithmIdx = new IntegerProperty(ImageAnalyzer.KEY_ALGORITHM, ImageAnalyzer.DEFAULT_ALGORITHM).get();
algorithm.setSelectedIndex(algorithmIdx < algorithm.getMaximumRowCount() ? algorithmIdx : ImageAnalyzer.DEFAULT_ALGORITHM);
Expand Down

0 comments on commit 0ef3c03

Please sign in to comment.