Skip to content

Commit

Permalink
Allow filtering by users (since Mapillary added the data back to the …
Browse files Browse the repository at this point in the history
…vector tiles)

Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Apr 18, 2024
1 parent c551d3e commit cc2193b
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -17,8 +18,8 @@
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import javax.swing.JOptionPane;
Expand All @@ -38,6 +39,8 @@
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.plugins.mapillary.model.UserProfile;
import org.openstreetmap.josm.plugins.mapillary.oauth.OAuthUtils;
import org.openstreetmap.josm.plugins.mapillary.utils.api.JsonUserProfileDecoder;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.tools.JosmRuntimeException;
import org.openstreetmap.josm.tools.Logging;
Expand Down Expand Up @@ -103,6 +106,16 @@ public final class Caches {
final IElementAttributes fullImageElementCacheAttributes = FULL_IMAGE_CACHE.getDefaultElementAttributes();
fullImageElementCacheAttributes.setMaxLife(maxTime);
FULL_IMAGE_CACHE.setDefaultElementAttributes(fullImageElementCacheAttributes);

USER_PROFILE_CACHE.setDefaultSupplier(url -> {
try {
final var data = OAuthUtils.getWithHeader(URI.create(url));
return JsonUserProfileDecoder.decodeUserProfile(data);
} catch (IOException e) {
Logging.error(e);
return null;
}
});
}

public static File getCacheDirectory() {
Expand Down Expand Up @@ -131,7 +144,7 @@ public static class MapillaryCacheAccess<V> {
@Nonnull
private final List<Predicate<V>> validators;
@Nullable
private Supplier<V> defaultSupplier;
private Function<String, V> defaultSupplier;

private boolean rateLimited;

Expand All @@ -150,7 +163,7 @@ public MapillaryCacheAccess(@Nonnull CacheAccess<String, V> cacheAccess, @Nullab
*
* @param defaultSupplier The default supplier
*/
public void setDefaultSupplier(@Nullable Supplier<V> defaultSupplier) {
public void setDefaultSupplier(@Nullable Function<String, V> defaultSupplier) {
this.defaultSupplier = defaultSupplier;
}

Expand Down Expand Up @@ -185,7 +198,7 @@ public V get(@Nonnull String url) {
if (this.defaultSupplier != null) {
return this.get(url, this.defaultSupplier);
} else {
return this.get(url, () -> null);
return this.get(url, (u) -> null);
}
}

Expand All @@ -202,7 +215,7 @@ public Future<V> get(@Nonnull String url, @Nonnull ForkJoinPool pool) {
if (this.defaultSupplier != null) {
return this.get(url, pool, this.defaultSupplier);
} else {
return this.get(url, pool, () -> null);
return this.get(url, pool, (u) -> null);
}
}

Expand All @@ -214,7 +227,7 @@ public Future<V> get(@Nonnull String url, @Nonnull ForkJoinPool pool) {
* @return The type the supplier returns
*/
@Nullable
public V get(@Nonnull String url, @Nonnull Supplier<V> supplier) {
public V get(@Nonnull String url, @Nonnull Function<String, V> supplier) {
if (this.cacheAccess.get(url) != null) {
return this.cacheAccess.get(url);
}
Expand All @@ -228,7 +241,7 @@ public V get(@Nonnull String url, @Nonnull Supplier<V> supplier) {
}
final V newReturnObject;
synchronized (this) {
newReturnObject = this.cacheAccess.get(url) == null ? supplier.get() : this.cacheAccess.get(url);
newReturnObject = this.cacheAccess.get(url) == null ? supplier.apply(url) : this.cacheAccess.get(url);
if (newReturnObject != null && this.validators.stream().allMatch(p -> p.test(newReturnObject))) {
this.cacheAccess.put(url, newReturnObject);
} else if (newReturnObject != null) {
Expand All @@ -253,7 +266,7 @@ public V get(@Nonnull String url, @Nonnull Supplier<V> supplier) {
* @param supplier The supplier to get the object with
* @return A future with the object, when it completes.
*/
public Future<V> get(@Nonnull String url, @Nonnull ForkJoinPool pool, @Nonnull Supplier<V> supplier) {
public Future<V> get(@Nonnull String url, @Nonnull ForkJoinPool pool, @Nonnull Function<String, V> supplier) {
if (this.cacheAccess.get(url) != null) {
return CompletableFuture.completedFuture(this.cacheAccess.get(url));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static Map<String, List<MapillaryNode>> downloadImages(final long... imag
}
final Caches.MapillaryCacheAccess<String> metaDataCache = Caches.META_DATA_CACHE;
String url = MapillaryConfig.getUrls().getImageInformation(images);
String stringJson = metaDataCache.get(url, () -> {
final JsonObject jsonObject = getUrlResponse(url);
String stringJson = metaDataCache.get(url, (u) -> {
final JsonObject jsonObject = getUrlResponse(u);
return jsonObject != null ? jsonObject.toString() : null;
});
if (stringJson == null) {
Expand Down Expand Up @@ -118,8 +118,8 @@ public static long[] downloadSequence(@Nonnull String sequence) {
}
}
final String url = MapillaryConfig.getUrls().getImagesBySequences(sequence);
final String response = Caches.META_DATA_CACHE.get(url, () -> {
final JsonObject urlResponse = getUrlResponse(url);
final String response = Caches.META_DATA_CACHE.get(url, (u) -> {
final JsonObject urlResponse = getUrlResponse(u);
return urlResponse == null ? null : urlResponse.toString();
});
if (response != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
package org.openstreetmap.josm.plugins.mapillary.data.mapillary;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -16,6 +18,7 @@

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTTile;
import org.openstreetmap.josm.data.osm.INode;
Expand Down Expand Up @@ -63,7 +66,7 @@ public record OrganizationRecord(long id, String name, String niceName, String d
@Nonnull
private static ImageIcon createAvatarIcon(@Nullable String avatar) {
if (avatar != null && !avatar.isEmpty()) {
final BufferedImage avatarImage = Caches.META_IMAGES.get(avatar, () -> fetchAvatarIcon(avatar));
final BufferedImage avatarImage = Caches.META_IMAGES.get(avatar, OrganizationRecord::fetchAvatarIcon);
return avatarImage != null ? new ImageIcon(avatarImage) : ImageProvider.createBlankIcon(ImageSizes.DEFAULT);
}
return ImageProvider.getEmpty(ImageSizes.DEFAULT);
Expand Down Expand Up @@ -106,7 +109,13 @@ public static OrganizationRecord getOrganization(long id) {
* @return The organization
*/
public static OrganizationRecord getOrganization(String id) {
return getOrganization(Long.parseLong(id));
if (id.matches("^\\d+$")) {
return getOrganization(Long.parseLong(id));
} else { // Assume json
try (var reader = Json.createReader(new ByteArrayInputStream(id.getBytes(StandardCharsets.UTF_8)))) {
return getOrganization(Long.parseLong(reader.readObject().getString("id")));
}
}
}

/**
Expand Down
Loading

0 comments on commit cc2193b

Please sign in to comment.