kopia lustrzana https://github.com/JOSM/MapWithAI
Dynamically get source tags
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
3968aa0519
commit
852fc2a47d
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -65,6 +65,8 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
|
|||
|
||||
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 --
|
||||
* it will be reduced here)
|
||||
|
@ -329,20 +331,21 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
|
|||
dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED);
|
||||
|
||||
clients = new ArrayList<>();
|
||||
urlMaps.forEach(map -> {
|
||||
urlMaps.parallelStream().forEach(map -> {
|
||||
try {
|
||||
clients.add(HttpClient.create(new URL(map.get("url").replace("{bbox}", bbox.toStringCSV(","))
|
||||
.replace("{crop_bbox}", DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(",")))));
|
||||
HttpClient client = HttpClient.create(new URL(map.get("url").replace("{bbox}", bbox.toStringCSV(","))
|
||||
.replace("{crop_bbox}", DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(","))));
|
||||
clients.add(client);
|
||||
clientCall(client, dataSet, map.getOrDefault("source", MapWithAIPlugin.NAME), monitor);
|
||||
} catch (MalformedURLException e1) {
|
||||
Logging.debug(e1);
|
||||
}
|
||||
});
|
||||
clients.forEach(client -> clientCall(client, dataSet, monitor));
|
||||
dataSet.setUploadPolicy(UploadPolicy.BLOCKED);
|
||||
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();
|
||||
client.setReadTimeout(DEFAULT_TIMEOUT);
|
||||
defaultUserAgent.append(client.getHeaders().get("User-Agent"));
|
||||
|
@ -358,6 +361,7 @@ public class GetDataRunnable extends RecursiveTask<DataSet> implements CancelLis
|
|||
final Response response = client.connect();
|
||||
inputStream = response.getContent();
|
||||
final DataSet mergeData = OsmReaderCustom.parseDataSet(inputStream, null, true);
|
||||
addMapWithAISourceTag(mergeData, source);
|
||||
dataSet.mergeFrom(mergeData);
|
||||
response.disconnect();
|
||||
} 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
|
||||
public void operationCanceled() {
|
||||
if (clients != null) {
|
||||
|
|
|
@ -173,7 +173,7 @@ public final class MapWithAIDataUtils {
|
|||
: new Notification(tr("No URLS are enabled"));
|
||||
noUrls.setDuration(Notification.TIME_DEFAULT);
|
||||
noUrls.setIcon(JOptionPane.INFORMATION_MESSAGE);
|
||||
noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences"));
|
||||
noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences"));
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
noUrls.show();
|
||||
} else {
|
||||
|
@ -434,4 +434,13 @@ public final class MapWithAIDataUtils {
|
|||
.filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast)
|
||||
.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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,8 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
|
|||
|
||||
// @Override TODO remove comment on 2020-01-01
|
||||
public String getChangesetSourceTag() {
|
||||
return MapWithAIDataUtils.getAddedObjects() > 0 ? "MapWithAI" : null;
|
||||
return MapWithAIDataUtils.getAddedObjects() > 0 ? String.join("; ", MapWithAIDataUtils.getAddedObjectsSource())
|
||||
: null;
|
||||
}
|
||||
|
||||
public void setMaximumAddition(Integer max) {
|
||||
|
|
|
@ -7,8 +7,11 @@ import java.awt.EventQueue;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openstreetmap.josm.command.Command;
|
||||
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.gui.layer.OsmDataLayer;
|
||||
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.MapWithAILayer;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Pair;
|
||||
|
||||
public class MapWithAIAddCommand extends Command implements Runnable {
|
||||
DataSet editable;
|
||||
|
@ -27,6 +32,7 @@ public class MapWithAIAddCommand extends Command implements Runnable {
|
|||
Collection<OsmPrimitive> primitives;
|
||||
Command command;
|
||||
Lock lock;
|
||||
final Map<OsmPrimitive, String> sources;
|
||||
|
||||
/**
|
||||
* Add primitives from MapWithAI to the OSM data layer
|
||||
|
@ -52,6 +58,9 @@ public class MapWithAIAddCommand extends Command implements Runnable {
|
|||
this.mapWithAI = mapWithAI;
|
||||
this.editable = editable;
|
||||
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
|
||||
|
@ -149,6 +158,12 @@ public class MapWithAIAddCommand extends Command implements Runnable {
|
|||
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
|
||||
public boolean equals(Object other) {
|
||||
boolean returnBoolean = false;
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|||
import org.openstreetmap.josm.data.osm.PrimitiveData;
|
||||
import org.openstreetmap.josm.data.osm.visitor.MergeSourceBuildingVisitor;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnable;
|
||||
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)
|
||||
.collect(Collectors.toList());
|
||||
primitiveAddData.parallelStream().forEach(data -> data.remove(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY));
|
||||
|
||||
commands.add(new AddPrimitivesCommand(primitiveAddData,
|
||||
selection.stream().map(OsmPrimitive::save).collect(Collectors.toList()), to));
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.openstreetmap.josm.data.osm.Way;
|
|||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.layer.Layer;
|
||||
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.testutils.JOSMTestRules;
|
||||
|
||||
|
@ -68,12 +69,14 @@ public class MapWithAILayerTest {
|
|||
DataSet to = new DataSet();
|
||||
DataSet from = new DataSet();
|
||||
Way way = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
|
||||
way.getNodes().stream().forEach(to::addPrimitive);
|
||||
to.addPrimitive(way);
|
||||
way.getNodes().stream().forEach(from::addPrimitive);
|
||||
from.addPrimitive(way);
|
||||
way.put(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY, MapWithAIPlugin.NAME);
|
||||
MapWithAIAddCommand command = new MapWithAIAddCommand(from, to, Collections.singleton(way));
|
||||
UndoRedoHandler.getInstance().add(command);
|
||||
Assert.assertNotNull(layer.getChangesetSourceTag());
|
||||
Assert.assertFalse(layer.getChangesetSourceTag().trim().isEmpty());
|
||||
Assert.assertEquals(layer.getChangesetSourceTag(), MapWithAIPlugin.NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Ładowanie…
Reference in New Issue