Changeset 18922 in josm for trunk/src


Ignore:
Timestamp:
2023-12-20T20:03:45+01:00 (11 months ago)
Author:
taylor.smock
Message:

Fix #23302: Create a preference for address duplicate detection to include buildings and POIs (patch by zyphlar, modified)

Modifications are as follows:

  • Add test case
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/Addresses.java

    r18871 r18922  
    2020import java.util.stream.Stream;
    2121
     22import javax.swing.JCheckBox;
     23import javax.swing.JPanel;
     24
    2225import org.openstreetmap.josm.command.Command;
    2326import org.openstreetmap.josm.command.DeleteCommand;
     
    3134import org.openstreetmap.josm.data.osm.TagMap;
    3235import org.openstreetmap.josm.data.osm.Way;
     36import org.openstreetmap.josm.data.preferences.BooleanProperty;
    3337import org.openstreetmap.josm.data.preferences.DoubleProperty;
     38import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
    3439import org.openstreetmap.josm.data.validation.Severity;
    3540import org.openstreetmap.josm.data.validation.Test;
    3641import org.openstreetmap.josm.data.validation.TestError;
     42import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     43import org.openstreetmap.josm.tools.GBC;
    3744import org.openstreetmap.josm.tools.Geometry;
    3845import org.openstreetmap.josm.tools.Logging;
     
    7178    protected static final String ADDR_POSTCODE      = "addr:postcode";
    7279    protected static final String ASSOCIATED_STREET  = "associatedStreet";
     80    protected static final String NAME_TAG           = "name";
     81    private static final String HOUSE                = "house";
     82    private static final String STREET               = "street";
    7383    // CHECKSTYLE.ON: SingleSpaceSeparator
     84
     85    private static final BooleanProperty PREF_INCLUDE_BLDG_POI =
     86            new BooleanProperty(ValidatorPrefHelper.PREFIX + "." + OpeningHourTest.class.getSimpleName() + "." + "includebuildingpois", false);
     87    private final JCheckBox checkboxIncludeBldgPOI = new JCheckBox(
     88            /* I18n: Label text for checkbox choosing to validate addresses for all types of objects, not just plain addresses */
     89            tr("Include POIs like amenities, offices, and buildings in duplicate address detection"));
     90    private boolean includeBldgAndPOI;
    7491
    7592    private Map<String, Collection<OsmPrimitive>> knownAddresses;
     
    8097     */
    8198    public Addresses() {
     99        /* I18n: Label text for checkbox choosing to validate addresses */
    82100        super(tr("Addresses"), tr("Checks for errors in addresses and associatedStreet relations."));
    83101    }
     
    142160     */
    143161    private void collectAddress(OsmPrimitive p) {
    144         if (!isPOI(p)) {
     162        if (includeBldgAndPOI || !isPOI(p)) {
    145163            for (String simplifiedAddress : getSimplifiedAddresses(p)) {
    146164                if (!ignoredAddresses.contains(simplifiedAddress)) {
     
    155173        ignoredAddresses = new HashSet<>();
    156174        for (OsmPrimitive p : primitive.getDataSet().allNonDeletedPrimitives()) {
    157             if (p instanceof Node && p.hasKey(ADDR_UNIT, ADDR_FLATS)) {
     175            if ((includeBldgAndPOI || p instanceof Node) && p.hasKey(ADDR_UNIT, ADDR_FLATS)) {
    158176                for (OsmPrimitive r : p.getReferrers()) {
    159177                    if (hasAddress(r)) {
     
    177195
    178196    @Override
     197    public void startTest(ProgressMonitor progressMonitor) {
     198        super.startTest(progressMonitor);
     199        this.includeBldgAndPOI = PREF_INCLUDE_BLDG_POI.get();
     200    }
     201
     202    @Override
    179203    public void endTest() {
    180204        knownAddresses = null;
     
    187211            initAddressMap(p);
    188212        }
    189         if (!isPOI(p) && hasAddress(p)) {
     213        if ((includeBldgAndPOI || !isPOI(p)) && hasAddress(p)) {
    190214            List<TestError> result = new ArrayList<>();
    191215            for (String simplifiedAddress : getSimplifiedAddresses(p)) {
     
    199223                        String city1 = p.get(ADDR_CITY);
    200224                        String city2 = p2.get(ADDR_CITY);
     225                        String name1 = p.get(NAME_TAG);
     226                        String name2 = p2.get(NAME_TAG);
    201227                        double distance = getDistance(p, p2);
    202228                        if (city1 != null && city2 != null) {
     
    236262                            }
    237263                        }
     264                        if (severityLevel == Severity.WARNING && !Objects.equals(name1, name2)) {
     265                            // since multiple objects can exist at one address, a different name tag isn't very concerning
     266                            severityLevel = Severity.OTHER;
     267                        }
    238268                        result.add(TestError.builder(this, severityLevel, DUPLICATE_HOUSE_NUMBER)
    239269                                .message(tr("Duplicate house numbers"), marktr("''{0}'' ({1}m)"), simplifiedAddress, (int) distance)
     
    302332                String role = m.getRole();
    303333                OsmPrimitive p = m.getMember();
    304                 if ("house".equals(role)) {
     334                if (HOUSE.equals(role)) {
    305335                    houses.add(p);
    306336                    String number = p.get(ADDR_HOUSE_NUMBER);
     
    316346                        wrongStreetNames.add(p);
    317347                    }
    318                 } else if ("street".equals(role)) {
     348                } else if (STREET.equals(role)) {
    319349                    if (p instanceof Way) {
    320350                        street.add((Way) p);
     
    457487            if ("".equals(role)) {
    458488                if (m.isWay() && m.getMember().hasKey("highway")) {
    459                     role = "street";
     489                    role = STREET;
    460490                } else if (m.getMember().hasTag("building"))
    461                     role = "house";
     491                    role = HOUSE;
    462492            }
    463493            switch (role) {
    464             case "house":
     494            case HOUSE:
    465495            case "addr:houselink":
    466496            case "address":
     
    472502                }
    473503                break;
    474             case "street":
     504            case STREET:
    475505                if (!m.getMember().hasTag("name") && r.hasTag("name"))
    476506                    return;
     
    523553    }
    524554
     555    @Override
     556    public void addGui(JPanel testPanel) {
     557        super.addGui(testPanel);
     558        checkboxIncludeBldgPOI.setSelected(PREF_INCLUDE_BLDG_POI.get());
     559        testPanel.add(checkboxIncludeBldgPOI, GBC.eol().insets(20, 0, 0, 0));
     560    }
     561
     562    @Override
     563    public boolean ok() {
     564        super.ok();
     565        PREF_INCLUDE_BLDG_POI.put(checkboxIncludeBldgPOI.isSelected());
     566        includeBldgAndPOI = PREF_INCLUDE_BLDG_POI.get();
     567        return false;
     568    }
     569
    525570}
Note: See TracChangeset for help on using the changeset viewer.