kopia lustrzana https://github.com/JOSM/MapWithAI
Add code to show the number of added objects from MapWithAI and some code fixups
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
94154f9f0e
commit
39a06eb936
|
@ -3,10 +3,13 @@ package org.openstreetmap.josm.plugins.mapwithai;
|
|||
|
||||
import java.awt.Component;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.swing.Action;
|
||||
|
@ -14,9 +17,9 @@ import javax.swing.JMenu;
|
|||
import javax.swing.JMenuItem;
|
||||
|
||||
import org.openstreetmap.josm.actions.JosmAction;
|
||||
import org.openstreetmap.josm.actions.UploadAction;
|
||||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.MainMenu;
|
||||
import org.openstreetmap.josm.gui.MapFrame;
|
||||
import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
||||
import org.openstreetmap.josm.io.remotecontrol.RequestProcessor;
|
||||
import org.openstreetmap.josm.plugins.Plugin;
|
||||
|
@ -26,6 +29,7 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIArbitraryAction
|
|||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIRemoteControl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIUploadHook;
|
||||
import org.openstreetmap.josm.tools.Destroyable;
|
||||
|
@ -36,10 +40,10 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
public static final String NAME = "MapWithAI";
|
||||
private static String versionInfo;
|
||||
|
||||
private final MapWithAIUploadHook uploadHook;
|
||||
|
||||
private final PreferenceSetting preferenceSetting;
|
||||
|
||||
private final List<Destroyable> destroyables;
|
||||
|
||||
private static final Map<Class<? extends JosmAction>, Boolean> MENU_ENTRIES = new LinkedHashMap<>();
|
||||
static {
|
||||
MENU_ENTRIES.put(MapWithAIAction.class, false);
|
||||
|
@ -51,7 +55,6 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
super(info);
|
||||
|
||||
preferenceSetting = new MapWithAIPreferences();
|
||||
uploadHook = new MapWithAIUploadHook(info);
|
||||
|
||||
final JMenu dataMenu = MainApplication.getMenu().dataMenu;
|
||||
for (final Entry<Class<? extends JosmAction>, Boolean> entry : MENU_ENTRIES.entrySet()) {
|
||||
|
@ -75,10 +78,28 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
|
||||
MapWithAIDataUtils.addMapWithAIPaintStyles();
|
||||
|
||||
UploadAction.registerUploadHook(uploadHook);
|
||||
|
||||
setVersionInfo(info.localversion);
|
||||
RequestProcessor.addRequestHandlerClass("mapwithai", MapWithAIRemoteControl.class);
|
||||
destroyables = new ArrayList<>();
|
||||
destroyables.add(new MapWithAIUploadHook(info));
|
||||
mapFrameInitialized(null, MainApplication.getMap());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
||||
final Optional<MapWithAIObject> possibleMapWithAIObject = destroyables.parallelStream()
|
||||
.filter(MapWithAIObject.class::isInstance).map(MapWithAIObject.class::cast).findFirst();
|
||||
final MapWithAIObject mapWithAIObject = possibleMapWithAIObject.orElse(new MapWithAIObject());
|
||||
if (oldFrame != null && oldFrame.statusLine != null) {
|
||||
mapWithAIObject.removeMapStatus(oldFrame.statusLine);
|
||||
}
|
||||
if (newFrame != null && newFrame.statusLine != null) {
|
||||
mapWithAIObject.addMapStatus(newFrame.statusLine);
|
||||
}
|
||||
if (!destroyables.contains(mapWithAIObject)) {
|
||||
destroyables.add(mapWithAIObject);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -108,14 +129,16 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
final Map<Action, Component> actions = Arrays.asList(dataMenu.getComponents()).stream()
|
||||
.filter(component -> component instanceof JMenuItem).map(component -> (JMenuItem) component)
|
||||
.collect(Collectors.toMap(JMenuItem::getAction, component -> component));
|
||||
|
||||
for (final Entry<Action, Component> action : actions.entrySet()) {
|
||||
if (MENU_ENTRIES.containsKey(action.getKey().getClass())) {
|
||||
dataMenu.remove(action.getValue());
|
||||
}
|
||||
}
|
||||
UploadAction.unregisterUploadHook(uploadHook);
|
||||
|
||||
MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).stream()
|
||||
.forEach(layer -> MainApplication.getLayerManager().removeLayer(layer));
|
||||
|
||||
destroyables.forEach(Destroyable::destroy);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ public final class MapWithAIAvailability {
|
|||
}
|
||||
|
||||
private static Map<String, Map<String, Boolean>> parseForCountries(JsonArray countries) {
|
||||
Map<String, Map<String, Boolean>> returnCountries = new TreeMap<>();
|
||||
final Map<String, Map<String, Boolean>> returnCountries = new TreeMap<>();
|
||||
for (int i = 0; i < countries.size(); i++) {
|
||||
final JsonObject country = countries.getJsonObject(i).getJsonObject("properties");
|
||||
final String countryName = cornerCaseNames(country.getString("Country"));
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.concurrent.locks.Lock;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openstreetmap.josm.data.Bounds;
|
||||
import org.openstreetmap.josm.data.UndoRedoHandler;
|
||||
import org.openstreetmap.josm.data.coor.LatLon;
|
||||
import org.openstreetmap.josm.data.osm.BBox;
|
||||
import org.openstreetmap.josm.data.osm.DataSet;
|
||||
|
@ -38,6 +39,7 @@ import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
|||
import org.openstreetmap.josm.io.IllegalDataException;
|
||||
import org.openstreetmap.josm.io.OsmReader;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
|
||||
import org.openstreetmap.josm.tools.HttpClient;
|
||||
import org.openstreetmap.josm.tools.HttpClient.Response;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
@ -48,8 +50,27 @@ import org.openstreetmap.josm.tools.Utils;
|
|||
*
|
||||
*/
|
||||
public final class MapWithAIDataUtils {
|
||||
public static final int MAXIMUM_SIDE_DIMENSIONS = 1000; // 1 km
|
||||
private static ForkJoinPool forkJoinPool;
|
||||
static final Object LAYER_LOCK = new Object();
|
||||
|
||||
private static class GetDataRunnable extends RecursiveTask<DataSet> {
|
||||
private static final long serialVersionUID = 258423685658089715L;
|
||||
private final transient List<BBox> bbox;
|
||||
private final transient DataSet dataSet;
|
||||
private final transient PleaseWaitProgressMonitor monitor;
|
||||
|
||||
public GetDataRunnable(BBox bbox, DataSet dataSet, PleaseWaitProgressMonitor monitor) {
|
||||
this(Arrays.asList(bbox), dataSet, monitor);
|
||||
}
|
||||
|
||||
public GetDataRunnable(List<BBox> bbox, DataSet dataSet, PleaseWaitProgressMonitor monitor) {
|
||||
super();
|
||||
this.bbox = bbox;
|
||||
this.dataSet = dataSet;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
private static DataSet getDataReal(BBox bbox) {
|
||||
InputStream inputStream = null;
|
||||
final DataSet dataSet = new DataSet();
|
||||
|
@ -91,31 +112,17 @@ public final class MapWithAIDataUtils {
|
|||
}
|
||||
return dataSet;
|
||||
}
|
||||
private final transient List<BBox> bbox;
|
||||
private final transient DataSet dataSet;
|
||||
|
||||
private final transient PleaseWaitProgressMonitor monitor;
|
||||
|
||||
public GetDataRunnable(BBox bbox, DataSet dataSet, PleaseWaitProgressMonitor monitor) {
|
||||
this(Arrays.asList(bbox), dataSet, monitor);
|
||||
}
|
||||
|
||||
public GetDataRunnable(List<BBox> bbox, DataSet dataSet, PleaseWaitProgressMonitor monitor) {
|
||||
this.bbox = bbox;
|
||||
this.dataSet = dataSet;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSet compute() {
|
||||
List<BBox> bboxes = reduceBBoxSize(bbox);
|
||||
final List<BBox> bboxes = reduceBBoxSize(bbox);
|
||||
if (bboxes.size() == 1) {
|
||||
final DataSet temporaryDataSet = getDataReal(bboxes.get(0));
|
||||
synchronized (MapWithAIDataUtils.GetDataRunnable.class) {
|
||||
dataSet.mergeFrom(temporaryDataSet);
|
||||
}
|
||||
} else {
|
||||
Collection<GetDataRunnable> tasks = bboxes.parallelStream()
|
||||
final Collection<GetDataRunnable> tasks = bboxes.parallelStream()
|
||||
.map(tBbox -> new GetDataRunnable(tBbox, dataSet, null)).collect(Collectors.toList());
|
||||
tasks.forEach(GetDataRunnable::fork);
|
||||
tasks.forEach(GetDataRunnable::join);
|
||||
|
@ -131,12 +138,6 @@ public final class MapWithAIDataUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static final int MAXIMUM_SIDE_DIMENSIONS = 1000; // 1 km
|
||||
|
||||
private static ForkJoinPool forkJoinPool;
|
||||
|
||||
static final Object LAYER_LOCK = new Object();
|
||||
|
||||
private MapWithAIDataUtils() {
|
||||
// Hide the constructor
|
||||
}
|
||||
|
@ -313,7 +314,7 @@ public final class MapWithAIDataUtils {
|
|||
getForkJoinPool().execute(() -> {
|
||||
layer.getDataSet().clear();
|
||||
final DataSet newData = getData(editSetBBoxes);
|
||||
Lock lock = layer.getLock();
|
||||
final Lock lock = layer.getLock();
|
||||
lock.lock();
|
||||
try {
|
||||
layer.mergeFrom(newData);
|
||||
|
@ -384,7 +385,7 @@ public final class MapWithAIDataUtils {
|
|||
* {@link MAXIMUM_SIDE_DIMENSIONS}
|
||||
*/
|
||||
public static List<BBox> reduceBBoxSize(List<BBox> bboxes) {
|
||||
List<BBox> returnBBoxes = new ArrayList<>();
|
||||
final List<BBox> returnBBoxes = new ArrayList<>();
|
||||
bboxes.forEach(bbox -> returnBBoxes.addAll(reduceBBoxSize(bbox)));
|
||||
return returnBBoxes;
|
||||
}
|
||||
|
@ -412,4 +413,13 @@ public final class MapWithAIDataUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of objects added from the MapWithAI data layer
|
||||
*/
|
||||
public static Long getAddedObjects() {
|
||||
return UndoRedoHandler.getInstance().getUndoCommands().parallelStream()
|
||||
.filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast)
|
||||
.mapToLong(MapWithAIAddCommand::getAddedObjects).sum();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ public class MapWithAILayer extends OsmDataLayer {
|
|||
|
||||
@Override
|
||||
public Action[] getMenuEntries() {
|
||||
List<Action> actions = Arrays.asList(super.getMenuEntries()).stream()
|
||||
final List<Action> actions = Arrays.asList(super.getMenuEntries()).stream()
|
||||
.filter(action -> !(action instanceof LayerSaveAction) && !(action instanceof LayerSaveAsAction))
|
||||
.collect(Collectors.toList());
|
||||
return actions.toArray(new Action[0]);
|
||||
|
@ -108,6 +108,10 @@ public class MapWithAILayer extends OsmDataLayer {
|
|||
private static final long serialVersionUID = 5441350396443132682L;
|
||||
private boolean dataSetLocked;
|
||||
|
||||
public MapLock() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lock() {
|
||||
super.lock();
|
||||
|
|
|
@ -36,20 +36,17 @@ public class MapWithAIMoveAction extends JosmAction {
|
|||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
for (final MapWithAILayer mapWithAI : MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class)) {
|
||||
DataSet ds = mapWithAI.getDataSet();
|
||||
final DataSet ds = mapWithAI.getDataSet();
|
||||
final List<OsmDataLayer> osmLayers = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class);
|
||||
OsmDataLayer editLayer = null;
|
||||
final int maxAddition = MapWithAIPreferenceHelper.getMaximumAddition();
|
||||
Collection<OsmPrimitive> selected;
|
||||
List<Node> nodes = ds.getSelectedNodes().stream().filter(node -> !node.getReferrers().isEmpty())
|
||||
final List<Node> nodes = ds.getSelectedNodes().stream().filter(node -> !node.getReferrers().isEmpty())
|
||||
.collect(Collectors.toList());
|
||||
ds.clearSelection(nodes);
|
||||
nodes.stream().map(Node::getReferrers).forEach(ds::addSelected);
|
||||
if (maxAddition > 0) {
|
||||
selected = ds.getSelected().stream().limit(maxAddition).collect(Collectors.toList());
|
||||
} else {
|
||||
selected = ds.getSelected();
|
||||
}
|
||||
final Collection<OsmPrimitive> selected = maxAddition > 0
|
||||
? ds.getSelected().stream().limit(maxAddition).collect(Collectors.toList())
|
||||
: ds.getSelected();
|
||||
for (final OsmDataLayer osmLayer : osmLayers) {
|
||||
if (!osmLayer.isLocked() && osmLayer.isVisible() && osmLayer.isUploadable()
|
||||
&& osmLayer.getClass().equals(OsmDataLayer.class)) {
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
package org.openstreetmap.josm.plugins.mapwithai.backend;
|
||||
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.openstreetmap.josm.data.UndoRedoHandler;
|
||||
import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
|
||||
import org.openstreetmap.josm.gui.MapStatus;
|
||||
import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.tools.Destroyable;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
|
||||
public class MapWithAIObject implements CommandQueueListener, Destroyable {
|
||||
private final JosmTextField mapWithAIObjects;
|
||||
private final List<MapStatus> statusLines;
|
||||
|
||||
public MapWithAIObject() {
|
||||
mapWithAIObjects = new JosmTextField(null, null, "MapWithAI Objects Added: 1000".length() - 10, false);
|
||||
mapWithAIObjects.setBackground(MapStatus.PROP_BACKGROUND_COLOR.get());
|
||||
mapWithAIObjects.setEditable(false);
|
||||
statusLines = new ArrayList<>();
|
||||
UndoRedoHandler.getInstance().addCommandQueueListener(this);
|
||||
setText();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mapStatus The status bar to add a count to
|
||||
*/
|
||||
public void addMapStatus(MapStatus mapStatus) {
|
||||
statusLines.add(mapStatus);
|
||||
mapStatus.add(mapWithAIObjects, GBC.std().insets(3, 0, 0, 0), mapStatus.getComponentCount() - 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mapStatus The status bar to remove a count from
|
||||
*/
|
||||
public void removeMapStatus(MapStatus mapStatus) {
|
||||
mapStatus.remove(mapWithAIObjects);
|
||||
statusLines.remove(mapStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandChanged(int queueSize, int redoSize) {
|
||||
setText();
|
||||
}
|
||||
|
||||
private void setText() {
|
||||
final Long addedObjects = MapWithAIDataUtils.getAddedObjects();
|
||||
if (addedObjects != 0L) {
|
||||
mapWithAIObjects.setVisible(true);
|
||||
mapWithAIObjects.setText(tr("{0} Objects Added: {1}", MapWithAIPlugin.NAME, addedObjects));
|
||||
} else {
|
||||
mapWithAIObjects.setVisible(false);
|
||||
}
|
||||
final int maxAdd = MapWithAIPreferenceHelper.getMaximumAddition();
|
||||
if (addedObjects == 0) {
|
||||
mapWithAIObjects.setBackground(MapStatus.PROP_BACKGROUND_COLOR.get());
|
||||
} else if (addedObjects < maxAdd) {
|
||||
mapWithAIObjects.setBackground(Color.GREEN);
|
||||
} else if (addedObjects < 10 * maxAdd) {
|
||||
mapWithAIObjects.setBackground(Color.YELLOW);
|
||||
} else {
|
||||
mapWithAIObjects.setBackground(Color.RED);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
statusLines.forEach(mapStatus -> mapStatus.remove(mapWithAIObjects));
|
||||
UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ import java.util.List;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
|
||||
public class MapWithAIPreferenceHelper {
|
||||
public final class MapWithAIPreferenceHelper {
|
||||
public static final String DEFAULT_MAPWITHAI_API = "https://www.facebook.com/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=josm&token=ASb3N5o9HbX8QWn8G_NtHIRQaYv3nuG2r7_f3vnGld3KhZNCxg57IsaQyssIaEw5rfRNsPpMwg4TsnrSJtIJms5m&hash=ASawRla3rBcwEjY4HIY&result_type=road_building_vector_xml&bbox={bbox}";
|
||||
private static final int DEFAULT_MAXIMUM_ADDITION = 5;
|
||||
private static final String AUTOSWITCHLAYERS = MapWithAIPlugin.NAME.concat(".autoswitchlayers");
|
||||
|
@ -100,6 +100,7 @@ public class MapWithAIPreferenceHelper {
|
|||
*/
|
||||
public static void setMapWithAIUrl(String url, boolean permanent) {
|
||||
final MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
|
||||
String setUrl = url;
|
||||
if (permanent) {
|
||||
final List<String> urls = new ArrayList<>(getMapWithAIURLs());
|
||||
if (!urls.contains(url)) {
|
||||
|
@ -107,11 +108,11 @@ public class MapWithAIPreferenceHelper {
|
|||
setMapWithAIURLs(urls);
|
||||
}
|
||||
if (DEFAULT_MAPWITHAI_API.equals(url)) {
|
||||
url = "";
|
||||
setUrl = "";
|
||||
}
|
||||
Config.getPref().put(MapWithAIPlugin.NAME.concat(".current_api"), url);
|
||||
Config.getPref().put(MapWithAIPlugin.NAME.concat(".current_api"), setUrl);
|
||||
} else if (layer != null) {
|
||||
layer.setMapWithAIUrl(url);
|
||||
layer.setMapWithAIUrl(setUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,10 @@ public class MapWithAIRemoteControl extends RequestHandler.RawURLParseRequestHan
|
|||
private static final String MAX_OBJ = "max_obj";
|
||||
private static final String SWITCH_LAYER = "switch_layer";
|
||||
|
||||
public MapWithAIRemoteControl() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validateRequest() throws RequestHandlerBadRequestException {
|
||||
if (args != null) {
|
||||
|
|
|
@ -3,25 +3,26 @@ package org.openstreetmap.josm.plugins.mapwithai.backend;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import org.openstreetmap.josm.actions.UploadAction;
|
||||
import org.openstreetmap.josm.actions.upload.UploadHook;
|
||||
import org.openstreetmap.josm.data.UndoRedoHandler;
|
||||
import org.openstreetmap.josm.plugins.PluginInformation;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
|
||||
import org.openstreetmap.josm.tools.Destroyable;
|
||||
|
||||
/**
|
||||
* @author Taylor Smock
|
||||
*
|
||||
*/
|
||||
public class MapWithAIUploadHook implements UploadHook {
|
||||
public class MapWithAIUploadHook implements UploadHook, Destroyable {
|
||||
private final String version;
|
||||
|
||||
public MapWithAIUploadHook(PluginInformation info) {
|
||||
version = info.localversion;
|
||||
UploadAction.registerUploadHook(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifyChangesetTags(Map<String, String> tags) {
|
||||
final Integer addedObjects = getAddedObjects();
|
||||
final Long addedObjects = MapWithAIDataUtils.getAddedObjects();
|
||||
if (addedObjects != 0) {
|
||||
tags.put("mapwithai", addedObjects.toString());
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
@ -42,9 +43,8 @@ public class MapWithAIUploadHook implements UploadHook {
|
|||
}
|
||||
}
|
||||
|
||||
private static int getAddedObjects() {
|
||||
return UndoRedoHandler.getInstance().getUndoCommands().parallelStream()
|
||||
.filter(object -> object instanceof MapWithAIAddCommand).map(object -> (MapWithAIAddCommand) object)
|
||||
.mapToInt(MapWithAIAddCommand::getAddedObjects).sum();
|
||||
@Override
|
||||
public void destroy() {
|
||||
UploadAction.unregisterUploadHook(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,7 +75,6 @@ public abstract class AbstractConflationCommand extends Command {
|
|||
final OsmPrimitive[] primitiveConnections = new OsmPrimitive[connections.length];
|
||||
for (int i = 0; i < connections.length; i++) {
|
||||
final String member = connections[i];
|
||||
final long id = Long.parseLong(member.substring(1));
|
||||
final char firstChar = member.charAt(0);
|
||||
OsmPrimitiveType type = null;
|
||||
if (firstChar == 'w') {
|
||||
|
@ -88,6 +87,7 @@ public abstract class AbstractConflationCommand extends Command {
|
|||
throw new IllegalArgumentException(
|
||||
tr("{0}: We don't know how to handle {1} types", MapWithAIPlugin.NAME, firstChar));
|
||||
}
|
||||
final long id = Long.parseLong(member.substring(1));
|
||||
primitiveConnections[i] = dataSet.getPrimitiveById(id, type);
|
||||
if (primitiveConnections[i] == null) {
|
||||
missingPrimitives.put(i, new Pair<>(id, type));
|
||||
|
|
|
@ -85,14 +85,12 @@ public class ConnectedCommand extends AbstractConflationCommand {
|
|||
|
||||
@Override
|
||||
public Command getRealCommand() {
|
||||
List<Command> commands = new ArrayList<>();
|
||||
final List<Command> commands = new ArrayList<>();
|
||||
possiblyAffectedPrimitives.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
.forEach(node -> commands.addAll(connectedCommand(getAffectedDataSet(), node)));
|
||||
Command returnCommand;
|
||||
Command returnCommand = null;
|
||||
if (!commands.isEmpty()) {
|
||||
returnCommand = new SequenceCommand(getDescriptionText(), commands);
|
||||
} else {
|
||||
returnCommand = null;
|
||||
}
|
||||
return returnCommand;
|
||||
}
|
||||
|
|
|
@ -84,15 +84,13 @@ public class DuplicateCommand extends AbstractConflationCommand {
|
|||
|
||||
@Override
|
||||
public Command getRealCommand() {
|
||||
List<Command> commands = new ArrayList<>();
|
||||
final List<Command> commands = new ArrayList<>();
|
||||
possiblyAffectedPrimitives.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
.filter(node -> node.hasKey(DUPE_KEY))
|
||||
.forEach(node -> commands.addAll(duplicateNode(getAffectedDataSet(), node)));
|
||||
Command returnCommand;
|
||||
Command returnCommand = null;
|
||||
if (!commands.isEmpty()) {
|
||||
returnCommand = new SequenceCommand(getDescriptionText(), commands);
|
||||
} else {
|
||||
returnCommand = null;
|
||||
}
|
||||
return returnCommand;
|
||||
}
|
||||
|
|
|
@ -55,23 +55,21 @@ public class MergeAddressBuildings extends AbstractConflationCommand {
|
|||
.forEach(rel -> commands.addAll(mergeAddressBuilding(getAffectedDataSet(), rel)));
|
||||
}
|
||||
|
||||
Command returnCommand;
|
||||
Command returnCommand = null;
|
||||
if (!commands.isEmpty()) {
|
||||
returnCommand = new SequenceCommand(getDescriptionText(), commands);
|
||||
} else {
|
||||
returnCommand = null;
|
||||
}
|
||||
return returnCommand;
|
||||
}
|
||||
|
||||
private Collection<? extends Command> mergeAddressBuilding(DataSet affectedDataSet, Relation rel) {
|
||||
List<IPrimitive> toCheck = new ArrayList<>();
|
||||
private static Collection<? extends Command> mergeAddressBuilding(DataSet affectedDataSet, Relation rel) {
|
||||
final List<IPrimitive> toCheck = new ArrayList<>();
|
||||
toCheck.addAll(affectedDataSet.searchNodes(rel.getBBox()));
|
||||
Collection<IPrimitive> nodesInside = Geometry.filterInsideMultipolygon(toCheck, rel);
|
||||
List<Node> nodesWithAddresses = nodesInside.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
final Collection<IPrimitive> nodesInside = Geometry.filterInsideMultipolygon(toCheck, rel);
|
||||
final List<Node> nodesWithAddresses = nodesInside.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
.filter(node -> node.hasKey("addr:housenumber", "addr:housename")).collect(Collectors.toList());
|
||||
|
||||
List<Command> commandList = new ArrayList<>();
|
||||
final List<Command> commandList = new ArrayList<>();
|
||||
if (nodesWithAddresses.size() == 1) {
|
||||
commandList.add(ReplaceGeometryUtils.buildUpgradeNodeCommand(nodesWithAddresses.get(0), rel));
|
||||
}
|
||||
|
@ -79,13 +77,13 @@ public class MergeAddressBuildings extends AbstractConflationCommand {
|
|||
}
|
||||
|
||||
private Collection<? extends Command> mergeAddressBuilding(DataSet affectedDataSet, Way way) {
|
||||
List<IPrimitive> toCheck = new ArrayList<>();
|
||||
final List<IPrimitive> toCheck = new ArrayList<>();
|
||||
toCheck.addAll(affectedDataSet.searchNodes(way.getBBox()));
|
||||
Collection<IPrimitive> nodesInside = Geometry.filterInsidePolygon(toCheck, way);
|
||||
List<Node> nodesWithAddresses = nodesInside.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
final Collection<IPrimitive> nodesInside = Geometry.filterInsidePolygon(toCheck, way);
|
||||
final List<Node> nodesWithAddresses = nodesInside.stream().filter(Node.class::isInstance).map(Node.class::cast)
|
||||
.filter(node -> node.hasKey("addr:housenumber", "addr:housename")).collect(Collectors.toList());
|
||||
|
||||
List<Command> commandList = new ArrayList<>();
|
||||
final List<Command> commandList = new ArrayList<>();
|
||||
if (nodesWithAddresses.size() == 1) {
|
||||
commandList.add(ReplaceGeometryUtils.buildUpgradeNodeCommand(nodesWithAddresses.get(0), way));
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ public class CreateConnectionsCommand extends Command {
|
|||
final Collection<OsmPrimitive> realPrimitives = collection.stream().map(dataSet::getPrimitiveById)
|
||||
.filter(Objects::nonNull).collect(Collectors.toList());
|
||||
for (Class<? extends AbstractConflationCommand> abstractCommandClass : getConflationCommands()) {
|
||||
AbstractConflationCommand abstractCommand;
|
||||
final AbstractConflationCommand abstractCommand;
|
||||
try {
|
||||
abstractCommand = abstractCommandClass.getConstructor(DataSet.class).newInstance(dataSet);
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
|
@ -80,11 +80,11 @@ public class CreateConnectionsCommand extends Command {
|
|||
Logging.debug(e);
|
||||
continue;
|
||||
}
|
||||
Collection<OsmPrimitive> tPrimitives = new TreeSet<>();
|
||||
final Collection<OsmPrimitive> tPrimitives = new TreeSet<>();
|
||||
abstractCommand.getInterestedTypes()
|
||||
.forEach(clazz -> tPrimitives.addAll(Utils.filteredCollection(realPrimitives, clazz)));
|
||||
|
||||
Command actualCommand = abstractCommand.getCommand(tPrimitives.stream()
|
||||
final Command actualCommand = abstractCommand.getCommand(tPrimitives.stream()
|
||||
.filter(prim -> prim.hasKey(abstractCommand.getKey())).collect(Collectors.toList()));
|
||||
if (Objects.nonNull(actualCommand)) {
|
||||
changedKeyList.add(actualCommand);
|
||||
|
|
|
@ -7,10 +7,12 @@ import java.awt.EventQueue;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.openstreetmap.josm.command.Command;
|
||||
import org.openstreetmap.josm.command.SequenceCommand;
|
||||
import org.openstreetmap.josm.data.UndoRedoHandler;
|
||||
import org.openstreetmap.josm.data.osm.DataSet;
|
||||
import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
||||
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||
|
@ -132,7 +134,34 @@ public class MapWithAIAddCommand extends Command implements Runnable {
|
|||
modified.addAll(primitives);
|
||||
}
|
||||
|
||||
public int getAddedObjects() {
|
||||
return primitives.size();
|
||||
/**
|
||||
* @return The number of MapWithAI objects added in this command that are not
|
||||
* deleted
|
||||
*/
|
||||
public Long getAddedObjects() {
|
||||
Long returnLong;
|
||||
if (this.equals(UndoRedoHandler.getInstance().getLastCommand())) {
|
||||
returnLong = Long.valueOf(primitives.size());
|
||||
} else {
|
||||
returnLong = primitives.stream().map(editable::getPrimitiveById).filter(Objects::nonNull)
|
||||
.filter(prim -> !prim.isDeleted()).count();
|
||||
}
|
||||
return returnLong;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
boolean returnBoolean = false;
|
||||
if (other instanceof MapWithAIAddCommand && hashCode() == other.hashCode()) {
|
||||
returnBoolean = true;
|
||||
}
|
||||
return returnBoolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
synchronized (this) {
|
||||
return Objects.hash(editable, mapWithAI, primitives, command, lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue