Dynamically get source tags

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-11-14 16:43:02 -07:00
rodzic 3968aa0519
commit 852fc2a47d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
7 zmienionych plików z 58 dodań i 26 usunięć

Wyświetl plik

@ -1,17 +0,0 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai;
import org.openstreetmap.josm.gui.preferences.advanced.PrefEntry;
import org.openstreetmap.josm.spi.preferences.Setting;
/**
* @author Taylor Smock
*
*/
public class TriPrefEntry extends PrefEntry {
public TriPrefEntry(String key, Setting<?> value, Setting<?> defaultValue, boolean isDefault) {
super(key, value, defaultValue, isDefault);
}
}

Wyświetl plik

@ -65,6 +65,8 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
private static final double ARTIFACT_ANGLE = 0.1745; // 10 degrees in radians private static final double ARTIFACT_ANGLE = 0.1745; // 10 degrees in radians
public static final String MAPWITHAI_SOURCE_TAG_KEY = "mapwithai:source";
/** /**
* @param bbox The initial bbox to get data from (don't reduce beforehand -- * @param bbox The initial bbox to get data from (don't reduce beforehand --
* it will be reduced here) * it will be reduced here)
@ -329,20 +331,21 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED); dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED);
clients = new ArrayList<>(); clients = new ArrayList<>();
urlMaps.forEach(map -> { urlMaps.parallelStream().forEach(map -> {
try { try {
clients.add(HttpClient.create(new URL(map.get("url").replace("{bbox}", bbox.toStringCSV(",")) HttpClient client = HttpClient.create(new URL(map.get("url").replace("{bbox}", bbox.toStringCSV(","))
.replace("{crop_bbox}", DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(","))))); .replace("{crop_bbox}", DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(","))));
clients.add(client);
clientCall(client, dataSet, map.getOrDefault("source", MapWithAIPlugin.NAME), monitor);
} catch (MalformedURLException e1) { } catch (MalformedURLException e1) {
Logging.debug(e1); Logging.debug(e1);
} }
}); });
clients.forEach(client -> clientCall(client, dataSet, monitor));
dataSet.setUploadPolicy(UploadPolicy.BLOCKED); dataSet.setUploadPolicy(UploadPolicy.BLOCKED);
return dataSet; return dataSet;
} }
private static void clientCall(HttpClient client, DataSet dataSet, ProgressMonitor monitor) { private static void clientCall(HttpClient client, DataSet dataSet, String source, ProgressMonitor monitor) {
final StringBuilder defaultUserAgent = new StringBuilder(); final StringBuilder defaultUserAgent = new StringBuilder();
client.setReadTimeout(DEFAULT_TIMEOUT); client.setReadTimeout(DEFAULT_TIMEOUT);
defaultUserAgent.append(client.getHeaders().get("User-Agent")); defaultUserAgent.append(client.getHeaders().get("User-Agent"));
@ -358,6 +361,7 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
final Response response = client.connect(); final Response response = client.connect();
inputStream = response.getContent(); inputStream = response.getContent();
final DataSet mergeData = OsmReaderCustom.parseDataSet(inputStream, null, true); final DataSet mergeData = OsmReaderCustom.parseDataSet(inputStream, null, true);
addMapWithAISourceTag(mergeData, source);
dataSet.mergeFrom(mergeData); dataSet.mergeFrom(mergeData);
response.disconnect(); response.disconnect();
} catch (SocketException e) { } catch (SocketException e) {
@ -378,6 +382,21 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
} }
} }
/**
* @param dataSet The dataset to add the mapwithai source tag to
* @param source The source to associate with the data
* @return The dataset for easy chaining
*/
protected static DataSet addMapWithAISourceTag(DataSet dataSet, String source) {
dataSet.getNodes().parallelStream().filter(node -> !node.isDeleted() && node.getReferrers().isEmpty())
.forEach(node -> node.put(MAPWITHAI_SOURCE_TAG_KEY, source));
dataSet.getWays().parallelStream().filter(way -> !way.isDeleted())
.forEach(way -> way.put(MAPWITHAI_SOURCE_TAG_KEY, source));
dataSet.getRelations().parallelStream().filter(rel -> !rel.isDeleted())
.forEach(rel -> rel.put(MAPWITHAI_SOURCE_TAG_KEY, source));
return dataSet;
}
@Override @Override
public void operationCanceled() { public void operationCanceled() {
if (clients != null) { if (clients != null) {

Wyświetl plik

@ -173,7 +173,7 @@ public final class MapWithAIDataUtils {
: new Notification(tr("No URLS are enabled")); : new Notification(tr("No URLS are enabled"));
noUrls.setDuration(Notification.TIME_DEFAULT); noUrls.setDuration(Notification.TIME_DEFAULT);
noUrls.setIcon(JOptionPane.INFORMATION_MESSAGE); noUrls.setIcon(JOptionPane.INFORMATION_MESSAGE);
noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences")); noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences"));
if (SwingUtilities.isEventDispatchThread()) { if (SwingUtilities.isEventDispatchThread()) {
noUrls.show(); noUrls.show();
} else { } else {
@ -434,4 +434,13 @@ public final class MapWithAIDataUtils {
.filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast) .filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast)
.mapToLong(MapWithAIAddCommand::getAddedObjects).sum(); .mapToLong(MapWithAIAddCommand::getAddedObjects).sum();
} }
/**
* @return The source tags for Objects added from the MapWithAI data layer
*/
public static List<String> getAddedObjectsSource() {
return UndoRedoHandler.getInstance().getUndoCommands().parallelStream()
.filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast)
.flatMap(com -> com.getSourceTags().stream()).distinct().collect(Collectors.toList());
}
} }

Wyświetl plik

@ -55,7 +55,8 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
// @Override TODO remove comment on 2020-01-01 // @Override TODO remove comment on 2020-01-01
public String getChangesetSourceTag() { public String getChangesetSourceTag() {
return MapWithAIDataUtils.getAddedObjects() > 0 ? "MapWithAI" : null; return MapWithAIDataUtils.getAddedObjects() > 0 ? String.join("; ", MapWithAIDataUtils.getAddedObjectsSource())
: null;
} }
public void setMaximumAddition(Integer max) { public void setMaximumAddition(Integer max) {

Wyświetl plik

@ -7,8 +7,11 @@ import java.awt.EventQueue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
import org.openstreetmap.josm.command.Command; import org.openstreetmap.josm.command.Command;
import org.openstreetmap.josm.command.SequenceCommand; import org.openstreetmap.josm.command.SequenceCommand;
@ -17,9 +20,11 @@ import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.OsmPrimitive; import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnable;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Pair;
public class MapWithAIAddCommand extends Command implements Runnable { public class MapWithAIAddCommand extends Command implements Runnable {
DataSet editable; DataSet editable;
@ -27,6 +32,7 @@ public class MapWithAIAddCommand extends Command implements Runnable {
Collection<OsmPrimitive> primitives; Collection<OsmPrimitive> primitives;
Command command; Command command;
Lock lock; Lock lock;
final Map<OsmPrimitive, String> sources;
/** /**
* Add primitives from MapWithAI to the OSM data layer * Add primitives from MapWithAI to the OSM data layer
@ -52,6 +58,9 @@ public class MapWithAIAddCommand extends Command implements Runnable {
this.mapWithAI = mapWithAI; this.mapWithAI = mapWithAI;
this.editable = editable; this.editable = editable;
this.primitives = selection; this.primitives = selection;
sources = selection.parallelStream()
.map(prim -> new Pair<OsmPrimitive, String>(prim, prim.get(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY)))
.filter(pair -> pair.b != null).collect(Collectors.toMap(pair -> pair.a, pair -> pair.b));
} }
@Override @Override
@ -149,6 +158,12 @@ public class MapWithAIAddCommand extends Command implements Runnable {
return returnLong; return returnLong;
} }
public Collection<String> getSourceTags() {
return sources.entrySet().parallelStream()
.filter(entry -> !editable.getPrimitiveById(entry.getKey()).isDeleted()).map(Entry::getValue)
.filter(Objects::nonNull).distinct().sorted().collect(Collectors.toList());
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
boolean returnBoolean = false; boolean returnBoolean = false;

Wyświetl plik

@ -17,6 +17,7 @@ import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.PrimitiveData; import org.openstreetmap.josm.data.osm.PrimitiveData;
import org.openstreetmap.josm.data.osm.visitor.MergeSourceBuildingVisitor; import org.openstreetmap.josm.data.osm.visitor.MergeSourceBuildingVisitor;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnable;
import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Logging;
/** /**
@ -63,6 +64,7 @@ public class MovePrimitiveDataSetCommand extends Command {
final List<PrimitiveData> primitiveAddData = hull.allPrimitives().stream().map(OsmPrimitive::save) final List<PrimitiveData> primitiveAddData = hull.allPrimitives().stream().map(OsmPrimitive::save)
.collect(Collectors.toList()); .collect(Collectors.toList());
primitiveAddData.parallelStream().forEach(data -> data.remove(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY));
commands.add(new AddPrimitivesCommand(primitiveAddData, commands.add(new AddPrimitivesCommand(primitiveAddData,
selection.stream().map(OsmPrimitive::save).collect(Collectors.toList()), to)); selection.stream().map(OsmPrimitive::save).collect(Collectors.toList()), to));

Wyświetl plik

@ -30,6 +30,7 @@ import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.Layer; import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand; import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
import org.openstreetmap.josm.testutils.JOSMTestRules; import org.openstreetmap.josm.testutils.JOSMTestRules;
@ -68,12 +69,14 @@ public class MapWithAILayerTest {
DataSet to = new DataSet(); DataSet to = new DataSet();
DataSet from = new DataSet(); DataSet from = new DataSet();
Way way = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); Way way = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
way.getNodes().stream().forEach(to::addPrimitive); way.getNodes().stream().forEach(from::addPrimitive);
to.addPrimitive(way); from.addPrimitive(way);
way.put(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY, MapWithAIPlugin.NAME);
MapWithAIAddCommand command = new MapWithAIAddCommand(from, to, Collections.singleton(way)); MapWithAIAddCommand command = new MapWithAIAddCommand(from, to, Collections.singleton(way));
UndoRedoHandler.getInstance().add(command); UndoRedoHandler.getInstance().add(command);
Assert.assertNotNull(layer.getChangesetSourceTag()); Assert.assertNotNull(layer.getChangesetSourceTag());
Assert.assertFalse(layer.getChangesetSourceTag().trim().isEmpty()); Assert.assertFalse(layer.getChangesetSourceTag().trim().isEmpty());
Assert.assertEquals(layer.getChangesetSourceTag(), MapWithAIPlugin.NAME);
} }
@Test @Test