Skip to content

Commit

Permalink
Merge pull request #119 from JOSM/feature/performance-and-fixups
Browse files Browse the repository at this point in the history
Feature/performance and fixups
  • Loading branch information
tsmock authored May 11, 2020
2 parents d889e27 + 6f1fe9c commit 305c390
Show file tree
Hide file tree
Showing 71 changed files with 1,197 additions and 787 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ josm {
debugPort = 7051
manifest {
// See https://floscher.gitlab.io/gradle-josm-plugin/kdoc/latest/gradle-josm-plugin/org.openstreetmap.josm.gradle.plugin.config/-josm-manifest/old-version-download-link.html
oldVersionDownloadLink(16114, "v1.5.22", URL("https://github.com/JOSM/Mapillary/releases/download/v1.5.22/Mapillary.jar"))
oldVersionDownloadLink(15909, "v1.5.20", URL("https://github.com/JOSM/Mapillary/releases/download/v1.5.20/Mapillary.jar"))
oldVersionDownloadLink(14149, "v1.5.16", URL("https://github.com/JOSM/Mapillary/releases/download/v1.5.16/Mapillary.jar"))
oldVersionDownloadLink(13733, "v1.5.15", URL("https://github.com/JOSM/Mapillary/releases/download/v1.5.15/Mapillary.jar"))
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ plugin.icon=images/mapillary-logo.svg
plugin.link=https://wiki.openstreetmap.org/wiki/JOSM/Plugins/Mapillary
# Minimum required JOSM version to run this plugin, choose the lowest version possible that is compatible.
# You can check if the plugin compiles against this version by executing `./gradlew compileJava_minJosm`.
plugin.main.version=16114
plugin.main.version=16402
# Version of JOSM against which the plugin is compiled
# Please check, if the specified version is available for download from https://josm.openstreetmap.de/download/ .
# If not, choose the next higher number that is available, or the gradle build will break.
plugin.compile.version=16114
plugin.compile.version=16402
plugin.requires=apache-commons;apache-http;javafx

# Character encoding of Gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
package org.openstreetmap.josm.plugins.mapillary;

import java.awt.Color;
import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.ImageIcon;

import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.gpx.GpxImageEntry;
import org.openstreetmap.josm.plugins.mapillary.utils.LocalDateConverter;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryProperties;
import org.openstreetmap.josm.tools.ImageProvider;

/**
* Abstract superclass for all image objects. At the moment there are just 2,
Expand All @@ -20,6 +24,18 @@
*
*/
public abstract class MapillaryAbstractImage extends GpxImageEntry {
/** The common directory for the Mapillary image sprites (for Mapillary Images) */
protected static final String IMAGE_SPRITE_DIR = "josm-ca";
/** The default sprite for a Mapillary image */
protected static final ImageIcon DEFAULT_SPRITE = new ImageProvider(IMAGE_SPRITE_DIR, "default-ca")
.setMaxWidth(ImageProvider.ImageSizes.MAP.getAdjustedHeight()).get();
/** The sprite to use for the active Mapillary sequence */
public static final ImageIcon ACTIVE_SEQUENCE_SPRITE = new ImageProvider(IMAGE_SPRITE_DIR, "sequence-ca")
.setMaxWidth(ImageProvider.ImageSizes.MAP.getAdjustedHeight()).get();
/** The sprite to use for the currently selected image */
public static final ImageIcon SELECTED_IMAGE = new ImageProvider(IMAGE_SPRITE_DIR, "current-ca")
.setMaxWidth(ImageProvider.ImageSizes.MAP.getAdjustedHeight()).get();

/**
* If two values for field ca differ by less than EPSILON both values are considered equal.
*/
Expand Down Expand Up @@ -314,15 +330,28 @@ public void turn(final double ca) {
this.movingCa = this.tempCa + ca;
}

public abstract Color paintHighlightedColour();

public abstract Color paintHighlightedAngleColour();

public abstract Color paintSelectedColour();

public abstract Color paintSelectedAngleColour();

public abstract Color paintUnselectedColour();

public abstract Color paintUnselectedAngleColour();

/**
* @return The default image to represent this particular type of image
*/
public abstract Image getDefaultImage();

/**
* @return The default image to indicate that this particular image is selected
*/
public Image getSelectedImage() {
return SELECTED_IMAGE.getImage();
}

/**
* @return The image to indicate that the current sequence is active
*/
public Image getActiveSequenceImage() {
return ACTIVE_SEQUENCE_SPRITE.getImage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonReader;
import javax.swing.SwingUtilities;

import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs3.access.CacheAccess;

import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.Data;
Expand All @@ -31,8 +32,10 @@
import org.openstreetmap.josm.gui.MapView;
import org.openstreetmap.josm.plugins.mapillary.cache.CacheUtils;
import org.openstreetmap.josm.plugins.mapillary.cache.Caches;
import org.openstreetmap.josm.plugins.mapillary.cache.MapillaryCache;
import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryMainDialog;
import org.openstreetmap.josm.plugins.mapillary.gui.imageinfo.ImageInfoPanel;
import org.openstreetmap.josm.plugins.mapillary.gui.layer.MapillaryLayer;
import org.openstreetmap.josm.plugins.mapillary.gui.layer.PointObjectLayer;
import org.openstreetmap.josm.plugins.mapillary.model.ImageDetection;
import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser;
Expand Down Expand Up @@ -85,7 +88,7 @@ public class MapillaryData implements Data {
/**
* Creates a new object and adds the initial set of listeners.
*/
protected MapillaryData() {
public MapillaryData() {
this.selectedImage = null;
this.dataSources = new ArrayList<>();

Expand Down Expand Up @@ -352,25 +355,35 @@ public void setSelectedImage(MapillaryAbstractImage image, boolean zoom) {
*/
public static boolean inCurrentlySelectedDetection(MapillaryImage image) {
return MainApplication.getLayerManager().getLayersOfType(PointObjectLayer.class).parallelStream()
.map(PointObjectLayer::getDataSet).flatMap(d -> d.getSelected().parallelStream())
.filter(p -> p.hasTag("detections"))
.flatMap(p -> PointObjectLayer.parseDetections(p.get("detections")).parallelStream())
.anyMatch(p -> image.getKey().equals(p.getOrDefault("image_key", null)));
.map(PointObjectLayer::getDataSet).flatMap(d -> d.getSelected().parallelStream())
.filter(p -> p.hasTag("detections"))
.flatMap(p -> PointObjectLayer.parseDetections(p.get("detections")).parallelStream())
.anyMatch(p -> image.getKey().equals(p.getOrDefault("image_key", null)));
}

/**
* Downloads surrounding images of this mapillary image in background threads
*
* @param mapillaryImage the image for which the surrounding images should be downloaded
*/
private static void downloadSurroundingImages(MapillaryImage mapillaryImage) {
MainApplication.worker.execute(() -> {
final int prefetchCount = MapillaryProperties.PRE_FETCH_IMAGE_COUNT.get();
CacheAccess<String, BufferedImageCacheEntry> imageCache = Caches.ImageCache.getInstance().getCache();
CacheAccess<String, BufferedImageCacheEntry> imageCache = Caches.ImageCache.getInstance()
.getCache(MapillaryCache.Type.FULL_IMAGE);

MapillaryAbstractImage nextImage = mapillaryImage.next();
MapillaryAbstractImage prevImage = mapillaryImage.previous();

long freeMemory = Runtime.getRuntime().freeMemory();
// 3 bytes for RGB (jpg doesn't support the Alpha channel). I'm using 4 bytes instead of 3 for a buffer.
long estimatedImageSize = Stream.of(MapillaryCache.Type.values()).mapToLong(v -> v.getHeight() * v.getWidth() * 4)
.sum();

for (int i = 0; i < prefetchCount; i++) {
if (freeMemory - estimatedImageSize < 0) {
break; // It doesn't make sense to try to cache images that won't be kept.
}
if (nextImage != null) {
if ((nextImage instanceof MapillaryImage) &&
(imageCache.get(((MapillaryImage) nextImage).getKey()) == null)) {
Expand All @@ -385,6 +398,11 @@ private static void downloadSurroundingImages(MapillaryImage mapillaryImage) {
}
prevImage = prevImage.previous();
}
imageCache.getCacheControl();
imageCache.get(mapillaryImage.getKey());
if (mapillaryImage.next() != null) {
imageCache.get(mapillaryImage.getKey());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
package org.openstreetmap.josm.plugins.mapillary;

import java.awt.Color;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;

import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.gpx.GpxImageEntry;
import org.openstreetmap.josm.plugins.mapillary.gui.layer.MapillaryLayer;
import org.openstreetmap.josm.plugins.mapillary.model.ImageDetection;
import org.openstreetmap.josm.plugins.mapillary.model.UserProfile;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryColorScheme;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.Logging;

/**
Expand All @@ -35,6 +40,10 @@ public class MapillaryImage extends MapillaryAbstractImage {
*/
private final boolean privateImage;

/** The default sprite for a private Mapillary image */
public static final ImageIcon PRIVATE_SPRITE = new ImageProvider(IMAGE_SPRITE_DIR, "private-ca")
.setMaxWidth(ImageProvider.ImageSizes.MAP.getAdjustedHeight()).get();

/**
* Main constructor of the class MapillaryImage
*
Expand Down Expand Up @@ -124,33 +133,23 @@ public void turn(double ca) {
checkModified();
}

@Override
public Color paintHighlightedColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_HIGHLIGHTED : MapillaryColorScheme.SEQ_HIGHLIGHTED;
}

@Override
public Color paintHighlightedAngleColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_HIGHLIGHTED_CA : MapillaryColorScheme.SEQ_HIGHLIGHTED_CA;
}

@Override
public Color paintSelectedColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_SELECTED : MapillaryColorScheme.SEQ_SELECTED;
}

@Override
public Color paintSelectedAngleColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_SELECTED_CA : MapillaryColorScheme.SEQ_SELECTED_CA;
}

@Override
public Color paintUnselectedColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_UNSELECTED : MapillaryColorScheme.SEQ_UNSELECTED;
public Color paintUnselectedAngleColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_UNSELECTED_CA : MapillaryColorScheme.SEQ_UNSELECTED_CA;
}

@Override
public Color paintUnselectedAngleColour() {
return privateImage ? MapillaryColorScheme.SEQ_PRIVATE_UNSELECTED_CA : MapillaryColorScheme.SEQ_UNSELECTED_CA;
public Image getDefaultImage() {
return privateImage ? PRIVATE_SPRITE.getImage() : DEFAULT_SPRITE.getImage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.openstreetmap.josm.plugins.mapillary;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -52,12 +53,14 @@ public MapillaryImportedImage(final LatLon latLon, final double ca, final File f
* @param datetimeOriginal The date the picture was taken.
* @param pano The property to indicate whether image is panorama or not.
*/
public MapillaryImportedImage(final LatLon latLon, final double ca, final File file, final boolean pano, final String datetimeOriginal) {
public MapillaryImportedImage(final LatLon latLon, final double ca, final File file, final boolean pano,
final String datetimeOriginal) {
this(latLon, ca, file, pano, parseTimestampElseCurrentTime(datetimeOriginal));
}

/**
* Constructs a new image from an image entry of a {@link GeoImageLayer}.
*
* @param geoImage the {@link ImageEntry}, from which the corresponding fields are taken
* @return new image
*/
Expand All @@ -81,7 +84,7 @@ public static MapillaryImportedImage createInstance(final ImageEntry geoImage) {
boolean pano = false;
try (FileInputStream fis = new FileInputStream(geoImage.getFile())) {
pano = ImageMetaDataUtil.isPanorama(fis);
} catch(IOException ex) {
} catch (IOException ex) {
Logging.trace(ex);
}
return new MapillaryImportedImage(coord, ca, geoImage.getFile(), pano, time);
Expand All @@ -99,7 +102,8 @@ private static long parseTimestampElseCurrentTime(final String timestamp) {
}
}

public MapillaryImportedImage(final LatLon latLon, final double ca, final File file, final boolean pano, final long capturedAt) {
public MapillaryImportedImage(final LatLon latLon, final double ca, final File file, final boolean pano,
final long capturedAt) {
super(latLon, ca, pano);
super.setFile(file);
this.capturedAt = capturedAt;
Expand Down Expand Up @@ -156,34 +160,24 @@ public boolean equals(Object obj) {
return true;
}

@Override
public Color paintHighlightedColour() {
return MapillaryColorScheme.SEQ_IMPORTED_HIGHLIGHTED;
}

@Override
public Color paintHighlightedAngleColour() {
return MapillaryColorScheme.SEQ_IMPORTED_HIGHLIGHTED_CA;
}

@Override
public Color paintSelectedColour() {
return MapillaryColorScheme.SEQ_IMPORTED_SELECTED;
}

@Override
public Color paintSelectedAngleColour() {
return MapillaryColorScheme.SEQ_IMPORTED_SELECTED_CA;
}

@Override
public Color paintUnselectedColour() {
return MapillaryColorScheme.SEQ_IMPORTED_UNSELECTED;
public Color paintUnselectedAngleColour() {
return MapillaryColorScheme.SEQ_IMPORTED_UNSELECTED_CA;
}

@Override
public Color paintUnselectedAngleColour() {
return MapillaryColorScheme.SEQ_IMPORTED_UNSELECTED_CA;
public Image getDefaultImage() {
return DEFAULT_SPRITE.getImage();
}

}
Loading

0 comments on commit 305c390

Please sign in to comment.