Skip to content

Commit

Permalink
ImageDetections: Correctly paint the selected detection outline cyan
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <tsmock@fb.com>
  • Loading branch information
tsmock committed Jun 30, 2021
1 parent bdaa000 commit 24fbeb9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader;
import org.openstreetmap.josm.plugins.mapillary.io.download.TileAddEventSource;
import org.openstreetmap.josm.plugins.mapillary.io.download.TileAddListener;
import org.openstreetmap.josm.plugins.mapillary.model.ImageDetection;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryImageUtils;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryKeys;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryMapFeatureUtils;
Expand All @@ -71,6 +72,7 @@
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Pair;

import javax.annotation.Nonnull;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonException;
Expand Down Expand Up @@ -284,8 +286,12 @@ public <L extends TileAddListener<MVTTile>> boolean removeListener(L listener) {
@Override
public <N extends INode> void setSelected(final Collection<N> nodes) {
this.selected = nodes.stream().map(MapillaryMapFeatureUtils::getId).collect(Collectors.toList());
getMapFeatureDetections(this.selected);
this.getData().setSelected(nodes);
if (!nodes.isEmpty() && MapillaryLayer.hasInstance()) {
if (!nodes.isEmpty() && MapillaryLayer.hasInstance()
&& MapillaryLayer.getInstance().getSelected()
.noneMatch(image -> nodes.stream().map(MapillaryMapFeatureUtils::getImageIds).flatMapToLong(LongStream::of)
.distinct().mapToObj(Long::toString).anyMatch(id -> id.equals(MapillaryImageUtils.getKey(image))))) {
final BBox searchBBox = new BBox();
nodes.forEach(node -> searchBBox.addPrimitive(node, 0.005));
final Set<String> idArray = nodes.stream().map(MapillaryMapFeatureUtils::getImageIds)
Expand All @@ -302,6 +308,24 @@ public <N extends INode> void setSelected(final Collection<N> nodes) {
}
}

/**
* Get the image detections for the specified map features
*
* @param keys The key/id of the map feature
*/
private static void getMapFeatureDetections(@Nonnull final Collection<String> keys) {
final Collection<String> finished = new ArrayList<>(keys.size());
for (String key : keys) {
// Repaint as we get features
ImageDetection.getDetections(key, (tKey, detections) -> {
finished.add(tKey);
if (finished.size() == keys.size()) {
MapillaryMainDialog.getInstance().imageViewer.invalidate();
}
});
}
}

@Override
public Stream<INode> getSelected() {
return this.getData().getSelectedNodes().stream()
Expand Down Expand Up @@ -608,15 +632,15 @@ public static List<Map<String, String>> parseDetections(String detectionsValue)
private static List<INode> getImagesForDetections(VectorDataSet data, List<Map<String, String>> detections) {
List<String> keys = detections.stream().filter(m -> m.containsKey(MapillaryKeys.IMAGE_KEY))
.map(m -> m.get(MapillaryKeys.IMAGE_KEY)).collect(Collectors.toList());
Set<String> keySet = data.getNodes().stream().filter(img -> img.hasKey(MapillaryKeys.KEY))
.map(img -> img.get(MapillaryKeys.KEY)).collect(Collectors.toSet());
Set<String> keySet = data.getNodes().stream().map(MapillaryImageUtils::getKey).filter(Objects::nonNull)
.collect(Collectors.toSet());
String[] missing = keys.stream().filter(key -> !keySet.contains(key)).toArray(String[]::new);
if (keys.isEmpty())
MapillaryDownloader.downloadImages(missing);
else
MapillaryUtils.getForkJoinPool(MapillaryCache.class).execute(() -> MapillaryDownloader.downloadImages(missing));
Map<String, INode> nodeMap = data.getNodes().stream().filter(img -> img.hasKey(MapillaryKeys.KEY))
.collect(Collectors.toMap(i -> i.get(MapillaryKeys.KEY), i -> i));
Map<String, INode> nodeMap = data.getNodes().stream().filter(img -> MapillaryImageUtils.getKey(img) != null)
.collect(Collectors.toMap(MapillaryImageUtils::getKey, i -> i));
return keys.stream().map(nodeMap::get).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openstreetmap.josm.plugins.mapillary.oauth.OAuthUtils;
import org.openstreetmap.josm.plugins.mapillary.utils.DetectionVerification;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryColorScheme;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryMapFeatureUtils;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryProperties;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils;
Expand Down Expand Up @@ -226,8 +227,9 @@ public boolean isTrafficSign() {
*/
public Color getColor() {
if (MainApplication.getLayerManager().getLayersOfType(PointObjectLayer.class).parallelStream()
.map(PointObjectLayer::getData).flatMap(ds -> ds.getSelected().parallelStream())
.filter(prim -> prim.hasKey(DETECTIONS)).anyMatch(prim -> prim.get(DETECTIONS).contains(getKey()))) {
.flatMap(PointObjectLayer::getSelected).map(MapillaryMapFeatureUtils::getId)
.flatMap(id -> ImageDetection.getDetections(id, false).stream()).filter(Objects::nonNull)
.anyMatch(id -> id.getKey().equals(this.getKey()))) {
return isRejected() || Boolean.TRUE.equals(MapillaryProperties.SMART_EDIT.get()) ? Color.RED : Color.CYAN;
}
if (this.isTrafficSign())
Expand Down

0 comments on commit 24fbeb9

Please sign in to comment.