Skip to content

Commit

Permalink
Add recursive option for name changes
Browse files Browse the repository at this point in the history
Also add a progress indicator for address changes

Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Nov 6, 2024
1 parent a7bb40f commit b9a2772
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.osm.BBox;
import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
Expand All @@ -27,9 +23,12 @@
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.io.OsmTransferException;
import org.openstreetmap.josm.io.OverpassDownloadReader;
import org.openstreetmap.josm.tools.Logging;

import jakarta.annotation.Nonnull;

/**
* Download additional highways
* @author Taylor Smock
Expand Down Expand Up @@ -72,10 +71,10 @@ public static <T extends OsmPrimitive> boolean checkIfDownloaded(Collection<T> w
* @param oldNames The original name(s) of the highway
* @param <T> Some class that extends {@link OsmPrimitive}
* @return A future which will "finish" when the additional data is downloaded.
* May be {@code null} if we have already downloaded the data for this
* layer.
*/
public static <T extends OsmPrimitive> Future<?> getAdditionalWays(Collection<T> highways, String... oldNames) {
@Nonnull
public static <T extends OsmPrimitive> CompletableFuture<Collection<OsmPrimitive>> getAdditionalWays(
@Nonnull Collection<T> highways, @Nonnull String... oldNames) {
HashSet<T> notDownloaded = new HashSet<>();
HashSet<String> otherNames = new HashSet<>();
HashMap<String, HashSet<OsmPrimitive>> tDownloadedWays = new HashMap<>();
Expand All @@ -101,7 +100,7 @@ public static <T extends OsmPrimitive> Future<?> getAdditionalWays(Collection<T>
}
downloadedLayerWays.put(layer, downloadedWays);
if (notDownloaded.isEmpty())
return null;
return CompletableFuture.completedFuture(Collections.emptyList());
T initialWay = notDownloaded.iterator().next();
final Bounds bound = new Bounds(initialWay.getBBox().getBottomRight());
final DataSet ds1 = initialWay.getDataSet();
Expand All @@ -115,7 +114,7 @@ public static <T extends OsmPrimitive> Future<?> getAdditionalWays(Collection<T>
bound.extend(bbox.getBottomRight());
for (Bounds bounding : ds1.getDataSourceBounds()) {
if (bounding.toBBox().bounds(bbox))
return null;
return CompletableFuture.completedFuture(Collections.emptyList());
}
}
final StringBuilder overpassQuery = new StringBuilder(118);
Expand All @@ -138,93 +137,29 @@ public static <T extends OsmPrimitive> Future<?> getAdditionalWays(Collection<T>
final OverpassDownloadReader overpass = new OverpassDownloadReader(bound,
OverpassDownloadReader.OVERPASS_SERVER.get(), overpassQuery.toString());

final DownloadOsmTask download = new DownloadOsmTask();
download.setZoomAfterDownload(false);
DownloadParams params = new DownloadParams();
params.withNewLayer(true);
params.withLayerName("haMoyQ4uVVcYTJR4");
Future<?> future = download.download(overpass, params, bound, NullProgressMonitor.INSTANCE);

RunnableFuture<Collection<OsmPrimitive>> mergeData = new RunnableFuture<Collection<OsmPrimitive>>() {
private boolean canceled;
private boolean done;
private Collection<OsmPrimitive> primitives;

@Override
public void run() {
ds1.beginUpdate();
try {
future.get();
DataSet dataSet = download.getDownloadedData();
if (dataSet != null) {
primitives = dataSet.allPrimitives();
new DataSetMerger(ds1, dataSet).merge(null, false);
}
} catch (ExecutionException | DataIntegrityProblemException e) {
Logging.error(e);
} catch (InterruptedException e) {
Logging.error(e);
Thread.currentThread().interrupt();
} finally {
ds1.endUpdate();
List<Layer> layers = MainApplication.getLayerManager().getLayers();
for (Layer layer : layers) {
if (params.getLayerName().equals(layer.getName())) {
MainApplication.getLayerManager().removeLayer(layer);
}
}
}
downloadedWays.putAll(tDownloadedWays);
done = true;
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
canceled = true;
return false;
}

@Override
public boolean isCancelled() {
return canceled;
}

@Override
public boolean isDone() {
return done;
}

@Override
public Collection<OsmPrimitive> get() throws InterruptedException, ExecutionException {
synchronized (this) {
while (!done && !canceled) {
this.wait(100);
}
}
if (canceled)
throw new InterruptedException();
return primitives;
Supplier<Collection<OsmPrimitive>> mergeData = () -> {
Collection<OsmPrimitive> primitives = Collections.emptyList();
final DataSet dataSet;
try {
dataSet = overpass.parseOsm(NullProgressMonitor.INSTANCE);
} catch (DataIntegrityProblemException | OsmTransferException e) {
Logging.error(e);
return Collections.emptyList();
}

@Override
public Collection<OsmPrimitive> get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
synchronized (this) {
long waitTime = 0;
timeout = unit.toMillis(timeout);
while (!done && waitTime < timeout) {
this.wait(timeout / 100);
waitTime += timeout / 100;
}
if (waitTime >= timeout)
throw new TimeoutException();
try {
ds1.beginUpdate();
if (dataSet != null) {
primitives = new HashSet<>(dataSet.allPrimitives());
new DataSetMerger(ds1, dataSet).merge(null, false);
primitives = primitives.stream().map(ds1::getPrimitiveById)
.filter(p -> p.hasTag("name", otherNames)).collect(Collectors.toList());
}
if (canceled)
throw new InterruptedException();
return primitives;
} finally {
ds1.endUpdate();
}
downloadedWays.putAll(tDownloadedWays);
return primitives;
};
MainApplication.worker.execute(mergeData);
return mergeData;
return CompletableFuture.supplyAsync(mergeData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public HighwayNameChangeAction(String name, String imageIcon, HighwayNameListene
public void actionPerformed(ActionEvent e) {
DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
Collection<OsmPrimitive> selection = ds.getAllSelected();
ModifyWays modifyWays = new ModifyWays(selection, null, true);
modifyWays.setDownloadTask(true);
ModifyWays modifyWays = new ModifyWays(selection, null, true, true, null);
MainApplication.worker.execute(modifyWays);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.highwaynamemodification;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -77,19 +76,21 @@ public void dataChanged(DataChangedEvent event) {
if (event == null || event.getEvents() == null)
return;
final Map<String, List<TagsChangedEvent>> groupedEvents = event.getEvents().stream()
.filter(tEvent -> DatasetEventType.TAGS_CHANGED == tEvent.getType())
.map(TagsChangedEvent.class::cast)
.filter(tEvent -> DatasetEventType.TAGS_CHANGED == tEvent.getType()).map(TagsChangedEvent.class::cast)
.collect(Collectors.groupingBy(tEvent -> String.join("----xxxx----", getOldNewName(tEvent))));
for (List<TagsChangedEvent> events : groupedEvents.values()) {
String oldName = getOldNewName(events.get(0))[0];
performTagChanges(oldName, events);
String[] newOldName = getOldNewName(events.get(0));
if (newOldName.length == 2) {
String oldName = newOldName[0];
performTagChanges(oldName, events);
}
}
}

private void performTagChanges(String oldName, Collection<TagsChangedEvent> events) {
final Collection<OsmPrimitive> objects = events.stream().flatMap(event -> event.getPrimitives().stream()).collect(Collectors.toList());
final ModifyWays modifyWays = new ModifyWays(objects, oldName);
modifyWays.setDownloadTask(true);
final Collection<OsmPrimitive> objects = events.stream().flatMap(event -> event.getPrimitives().stream())
.collect(Collectors.toList());
final ModifyWays modifyWays = new ModifyWays(objects, oldName, false, true, null);
MainApplication.worker.execute(modifyWays);
}

Expand All @@ -100,7 +101,7 @@ private String[] getOldNewName(TagsChangedEvent event) {
String newName = osm.get("name");
String oldName = originalKeys.get("name");
if (!newName.equals(oldName)) {
return new String[] {oldName, newName};
return new String[] { oldName, newName };
}
}
return EMPTY_STRING_ARRAY;
Expand Down
Loading

0 comments on commit b9a2772

Please sign in to comment.