Changeset 36219 in osm for applications/editors/josm/plugins
- Timestamp:
- 2024-03-05T21:53:45+01:00 (10 months ago)
- Location:
- applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles
- Files:
-
- 3 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/actions/downloadtasks/DownloadPMTilesTask.java
r36144 r36219 28 28 */ 29 29 public class DownloadPMTilesTask implements DownloadTask { 30 /** Zoom to the PMTiles bounds after download */ 30 31 private boolean zoomAfterDownload; 32 /** Cancel adding the layer if this is true */ 31 33 private boolean cancel; 34 /** The URL for the tiles */ 32 35 private String url; 36 /** Any recoverable errors from reading the PMTiles */ 33 37 private final List<Object> errorObjects = new ArrayList<>(); 38 /** The bounds for the layer */ 34 39 private Bounds bounds; 35 40 41 /** 42 * Add the appropriate layer to JOSM 43 */ 36 44 private void addLayer() { 37 45 if (this.cancel) { -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/data/imagery/PMTilesImageryInfo.java
r36112 r36219 6 6 import java.util.Objects; 7 7 8 import jakarta.json.JsonObject;9 8 import org.openstreetmap.josm.data.imagery.ImageryInfo; 10 9 import org.openstreetmap.josm.plugins.pmtiles.lib.Header; 11 10 import org.openstreetmap.josm.plugins.pmtiles.lib.PMTiles; 11 12 import jakarta.json.JsonObject; 12 13 13 14 /** … … 15 16 */ 16 17 public class PMTilesImageryInfo extends ImageryInfo { 18 /** The PMTiles header for this info */ 17 19 private final Header header; 20 /** The metadata for this info */ 18 21 private final JsonObject meta; 19 22 -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTileJob.java
r36145 r36219 30 30 */ 31 31 class PMTileJob extends JCSCachedTileLoaderJob<String, CacheEntry> implements TileJob, ICachedLoaderListener { 32 /** The tile we are fetching */ 32 33 private final Tile tile; 34 /** The tiles information */ 33 35 private final Header header; 36 /** The directory cache */ 34 37 private final DirectoryCache directoryCache; 35 38 39 /** 40 * Create a new job 41 * @param cache cache instance that we will work on 42 * @param options options of the request 43 * @param downloadJobExecutor that will be executing the jobs 44 * @param header The header for the tiles 45 * @param tile The tile to fetch 46 * @param directoryCache The cache of directories 47 */ 36 48 PMTileJob(ICacheAccess<String, CacheEntry> cache, 37 49 TileJobOptions options, -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesImageLayer.java
r36125 r36219 2 2 package org.openstreetmap.josm.plugins.pmtiles.gui.layers; 3 3 4 import java.awt.Graphics2D; 5 import java.awt.GridBagConstraints; 4 6 import java.util.Collection; 5 7 import java.util.Collections; 6 8 9 import javax.swing.JLabel; 10 import javax.swing.JPanel; 11 7 12 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 13 import org.openstreetmap.josm.data.Bounds; 8 14 import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTFile; 9 15 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 16 import org.openstreetmap.josm.gui.MapView; 10 17 import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer; 11 18 import org.openstreetmap.josm.plugins.pmtiles.data.imagery.PMTilesImageryInfo; 19 import org.openstreetmap.josm.tools.GBC; 12 20 13 21 /** … … 72 80 73 81 @Override 82 public Object getInfoComponent() { 83 JPanel panel = (JPanel) super.getInfoComponent(); 84 String[][] content = getInfoContent(); 85 for (String[] entry: content) { 86 panel.add(new JLabel(entry[0] + ':'), GBC.std()); 87 panel.add(GBC.glue(5, 0), GBC.std()); 88 panel.add(createTextField(entry[1]), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 89 } 90 return panel; 91 } 92 93 @Override 94 public void paint(Graphics2D g, MapView mv, Bounds box) { 95 super.paint(g, mv, box); 96 PMTilesLayer.super.paint(g, mv, box); 97 } 98 99 @Override 74 100 public void visitBoundingBox(BoundingXYVisitor v) { 75 101 super.visitBoundingBox(v); -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesImageSource.java
r36112 r36219 2 2 package org.openstreetmap.josm.plugins.pmtiles.gui.layers; 3 3 4 import jakarta.json.JsonObject;5 4 import org.openstreetmap.gui.jmapviewer.OsmMercator; 6 5 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; … … 8 7 import org.openstreetmap.josm.plugins.pmtiles.lib.Header; 9 8 9 import jakarta.json.JsonObject; 10 10 11 /** 11 12 * A source for images in PMTiles 12 13 */ 13 14 public class PMTilesImageSource extends AbstractTMSTileSource implements PMTilesTileSource { 15 /** The metadata for this source */ 14 16 private final JsonObject metadata; 17 /** The PMTiles header */ 15 18 private final Header header; 19 /** The {@link OsmMercator} instance for the specified tile size */ 16 20 private final OsmMercator osmMercator; 17 21 -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesLayer.java
r36125 r36219 4 4 import static org.openstreetmap.josm.tools.Utils.getSystemProperty; 5 5 6 import java.awt.BasicStroke; 7 import java.awt.Color; 8 import java.awt.Graphics2D; 9 import java.awt.Point; 10 11 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 12 import org.openstreetmap.josm.data.Bounds; 13 import org.openstreetmap.josm.data.coor.LatLon; 6 14 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 15 import org.openstreetmap.josm.gui.MapView; 16 import org.openstreetmap.josm.gui.layer.MapViewPaintable; 7 17 import org.openstreetmap.josm.plugins.pmtiles.data.imagery.PMTilesImageryInfo; 8 18 import org.openstreetmap.josm.tools.TextUtils; … … 12 22 * A common interface for layers using PMTiles as a source 13 23 */ 14 interface PMTilesLayer {24 interface PMTilesLayer extends MapViewPaintable { 15 25 16 26 /** … … 52 62 53 63 /** 64 * Get info information 65 * @return The information to add to the info panel 66 */ 67 default String[][] getInfoContent() { 68 final var info = getInfo(); 69 final var content = new String[3][]; 70 content[0] = new String[] {"Maximum zoom", String.valueOf(info.getMaxZoom())}; 71 content[1] = new String[] {"Minimum zoom", String.valueOf(info.getMinZoom())}; 72 content[2] = new String[] {"Bounds", info.getBounds().toBBox().toStringCSV(",")}; 73 return content; 74 } 75 76 @Override 77 default void paint(Graphics2D g, MapView mv, Bounds box) { 78 final var info = getInfo(); 79 g.setStroke(new BasicStroke()); 80 g.setColor(Color.DARK_GRAY); 81 if (info.getBounds().getShapes().isEmpty()) { 82 final var lowerLeft = mv.getPoint(info.getBounds().getMin()); 83 final var upperRight = mv.getPoint(info.getBounds().getMax()); 84 g.drawRect(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y); 85 } else { 86 for (var shape : info.getBounds().getShapes()) { 87 Point last = null; 88 for (ICoordinate coord : shape.getPoints()) { 89 final var point = mv.getPoint(new LatLon(coord.getLat(), coord.getLon())); 90 if (last != null) { 91 g.drawLine(last.x, last.y, point.x, point.y); 92 } 93 last = point; 94 } 95 } 96 } 97 } 98 99 /** 54 100 * Visits the content bounds of this layer. The behavior of this method depends on the layer, 55 101 * but each implementation should attempt to cover the relevant content of the layer in this method. -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesLoader.java
r36123 r36219 25 25 */ 26 26 public class PMTilesLoader implements TileLoader { 27 /** The executor for fetching the tiles. */ 27 28 private static final ThreadPoolExecutor EXECUTOR = TMSCachedTileLoader.getNewThreadPoolExecutor("pmtiles"); 29 /** The current jobs for the loader */ 28 30 private final Collection<PMTileJob> jobs = new HashSet<>(); 31 /** The cache for downloaded tiles */ 29 32 private final ICacheAccess<String, CacheEntry> cache; 33 /** The options for the tile loader */ 30 34 private final TileJobOptions options; 31 private final TileLoaderListener listener;35 /** The PMTiles header */ 32 36 private Header header; 37 /** The cache of PMTiles directories */ 33 38 private DirectoryCache directoryCache; 34 39 35 40 /** 36 41 * Create a new tile loader 37 * @param listenerThe listener to notify42 * @param ignored The listener to notify 38 43 * @param cache The cache to use 39 44 * @param options The options to use 40 45 */ 41 public PMTilesLoader(TileLoaderListener listener, ICacheAccess<String, CacheEntry> cache,46 public PMTilesLoader(TileLoaderListener ignored, ICacheAccess<String, CacheEntry> cache, 42 47 TileJobOptions options) { 43 this.listener = listener;44 48 this.cache = cache; 45 49 this.options = options; … … 64 68 } 65 69 70 /** 71 * Set the PMTiles info for this loader 72 * @param info The info to use 73 */ 66 74 void setInfo(PMTilesImageryInfo info) { 67 75 this.header = info.header(); -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesMVTLayer.java
r36125 r36219 2 2 package org.openstreetmap.josm.plugins.pmtiles.gui.layers; 3 3 4 import java.awt.Graphics2D; 5 import java.awt.GridBagConstraints; 4 6 import java.util.Collection; 5 7 import java.util.Collections; 6 8 9 import javax.swing.JLabel; 10 import javax.swing.JPanel; 11 7 12 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 13 import org.openstreetmap.josm.data.Bounds; 8 14 import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTFile; 9 15 import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MapboxVectorTileSource; 10 16 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 17 import org.openstreetmap.josm.gui.MapView; 11 18 import org.openstreetmap.josm.gui.layer.imagery.MVTLayer; 12 19 import org.openstreetmap.josm.plugins.pmtiles.data.imagery.PMTilesImageryInfo; 20 import org.openstreetmap.josm.tools.GBC; 13 21 14 22 /** … … 73 81 74 82 @Override 83 public Object getInfoComponent() { 84 JPanel panel = (JPanel) super.getInfoComponent(); 85 String[][] content = getInfoContent(); 86 for (String[] entry: content) { 87 panel.add(new JLabel(entry[0] + ':'), GBC.std()); 88 panel.add(GBC.glue(5, 0), GBC.std()); 89 panel.add(createTextField(entry[1]), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 90 } 91 return panel; 92 } 93 94 @Override 95 public void paint(Graphics2D g, MapView mv, Bounds box) { 96 super.paint(g, mv, box); 97 PMTilesLayer.super.paint(g, mv, box); 98 } 99 100 @Override 75 101 public void visitBoundingBox(BoundingXYVisitor v) { 76 102 super.visitBoundingBox(v); -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/gui/layers/PMTilesMVTTileSource.java
r36112 r36219 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.pmtiles.gui.layers; 3 4 import jakarta.json.Json;5 import jakarta.json.JsonObject;6 import jakarta.json.JsonValue;7 3 8 4 import org.openstreetmap.gui.jmapviewer.OsmMercator; … … 12 8 import org.openstreetmap.josm.plugins.pmtiles.lib.Header; 13 9 10 import jakarta.json.Json; 11 import jakarta.json.JsonObject; 12 import jakarta.json.JsonValue; 13 14 14 /** 15 15 * The tile source for MVT tiles in PMTiles 16 16 */ 17 17 public class PMTilesMVTTileSource extends MapboxVectorTileSource implements PMTilesTileSource { 18 /** The metadata for the source */ 18 19 private final JsonObject metadata; 20 /** The PMTiles header information */ 19 21 private final Header header; 22 /** The style source for the vector tiles */ 20 23 private final MapboxVectorStyle styleSource; 21 24 -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/DirectoryCache.java
r36112 r36219 10 10 */ 11 11 public final class DirectoryCache implements Iterable<Directory> { 12 /** The cached directories; [0] is the root, [1] is the last read directory */ 12 13 private final Directory[] directories = new Directory[2]; 13 14 /** -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/DirectoryEntry.java
r36112 r36219 12 12 */ 13 13 public record DirectoryEntry(long tileId, long offset, long length, long runLength) implements Serializable { 14 /** Create a new entry with some basic validation */ 14 15 public DirectoryEntry { 15 16 if (length <= 0) { -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/InternalCompression.java
r36112 r36219 6 6 */ 7 7 public enum InternalCompression { 8 /** Unknown compression type */ 8 9 UNKNOWN, 10 /** No compression */ 9 11 NONE, 12 /** The GNU GZIP compression format */ 10 13 GZIP, 14 /** The Google Brotli compression format */ 11 15 BROTLI, 16 /** The Facebook zstandard compression format */ 12 17 ZSTD 13 18 } -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/PMTiles.java
r36156 r36219 27 27 */ 28 28 public final class PMTiles { 29 /** An empty byte array for reuse */ 29 30 private static final byte[] EMPTY_BYTE = new byte[0]; 30 31 32 /** The constructor for this class. Hidden, so we don't have instances of this class. */ 31 33 private PMTiles() {/* hide the constructor */} 32 34 … … 180 182 } 181 183 // We need to sum up the previous z levels 182 var start = 0;184 long start = 0L; 183 185 var currentZoom = z; 184 186 // TODO profile this and the integral form (4^x)/(log(4)). Might not be as accurate though due to fp issues. … … 186 188 while (currentZoom > 0) { 187 189 currentZoom--; 188 start += (1 << currentZoom) * (1<< currentZoom);190 start += (1L << currentZoom) * (1L << currentZoom); 189 191 } 190 192 // Now we need to calculate the coordinates inside the specified zoom level 191 193 long d = 0; 192 varn = 1 << z;194 int n = 1 << z; 193 195 final var xy = new int[]{x, y}; 194 for ( vars = n / 2; s > 0; s /= 2) {195 varrx = (xy[0] & s) > 0 ? 1 : 0;196 varry = (xy[1] & s) > 0 ? 1 : 0;197 d += s* s * ((3 * rx) ^ ry);196 for (int s = n / 2; s > 0; s /= 2) { 197 int rx = (xy[0] & s) > 0 ? 1 : 0; 198 int ry = (xy[1] & s) > 0 ? 1 : 0; 199 d += ((long) s) * s * ((3 * rx) ^ ry); 198 200 rotate(n, xy, rx, ry); 199 201 } … … 210 212 var start = 0; 211 213 while (true) { 212 final var zTiles = Math. pow(4, z);214 final var zTiles = Math.toIntExact(Math.round(Math.pow(4, z))); 213 215 if (start + zTiles > hilbert) { 214 216 break; … … 250 252 } 251 253 254 /** 255 * Decompress a stream 256 * @param compression The compression the stream uses 257 * @param inputStream The stream to decompress 258 * @return The decompressed stream 259 * @throws IOException if one of the decompressors had an issue 260 */ 252 261 private static InputStream decompressInputStream(InternalCompression compression, InputStream inputStream) throws IOException { 253 262 return switch (compression) { … … 260 269 } 261 270 271 /** 272 * Get the stream for a given range and location 273 * @param location The location of the data 274 * @param start The start byte 275 * @param length The end byte (exclusive) 276 * @return The stream to read for the data 277 * @throws IOException If there is something that prevents reading the stream from the given location. 278 */ 262 279 private static InputStream getInputStream(URI location, long start, long length) throws IOException { 263 280 if (Utils.isLocalUrl(location.toString())) { -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/TileType.java
r36112 r36219 6 6 */ 7 7 public enum TileType { 8 /** Unknown tile type */ 8 9 UNKNOWN, 10 /** Mapbox Vector Tiles */ 9 11 MVT, 12 /** Portable Network Graphics */ 10 13 PNG, 14 /** Joint Photographic Experts Group */ 11 15 JPEG, 16 /** Google's WebP format */ 12 17 WEBP, 18 /** AV1 Image File Format */ 13 19 AVIF 14 20 } -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/internal/DirectoryParser.java
r36112 r36219 12 12 */ 13 13 public final class DirectoryParser { 14 /** Hide the constructor */ 14 15 private DirectoryParser() { /* Hide the constructor */ } 15 16 17 /** 18 * Parse a directory 19 * @param inputStream The stream to read 20 * @return The parsed directory 21 * @throws IOException See {@link InputStream#read()} 22 */ 16 23 public static Directory parse(InputStream inputStream) throws IOException { 17 24 int lastByte = inputStream.read(); -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/internal/HeaderParser.java
r36112 r36219 14 14 */ 15 15 public final class HeaderParser { 16 /** Hide the constructor */ 16 17 private HeaderParser() { /* Hide constructor */ } 17 18 … … 47 48 } 48 49 50 /** 51 * Read the next degrees from a stream 52 * @param inputStream The stream to read 53 * @return The degrees read 54 * @throws IOException See {@link InputStream#read()} 55 */ 49 56 private static double nextDegrees(InputStream inputStream) throws IOException { 50 57 return Util.nextInt(inputStream, 4) / 10_000_000d; 51 58 } 52 59 60 /** 61 * Get the next int 62 * @param inputStream The stream to read 63 * @return The next (unsigned) int 64 * @throws IOException See {@link InputStream#read()} 65 */ 53 66 private static long nextInt(InputStream inputStream) throws IOException { 54 67 return Util.nextInt(inputStream, 8); 55 68 } 56 69 70 /** 71 * Get the next compression type 72 * @param inputStream The stream to read 73 * @return The compression type 74 * @throws IOException See {@link InputStream#read()} 75 */ 57 76 private static InternalCompression nextCompressionType(InputStream inputStream) throws IOException { 58 77 final var type = inputStream.read(); … … 67 86 } 68 87 88 /** 89 * Get the tile type for the next tile 90 * @param inputStream The stream to read 91 * @return The tile type 92 * @throws IOException See {@link InputStream#read()} 93 */ 69 94 private static TileType nextTileType(InputStream inputStream) throws IOException { 70 95 final var type = inputStream.read(); -
applications/editors/josm/plugins/pmtiles/src/main/java/org/openstreetmap/josm/plugins/pmtiles/lib/internal/Util.java
r36112 r36219 11 11 */ 12 12 final class Util { 13 /** The private constructor to avoid instantiation */ 13 14 private Util() {/* Hide constructor */} 14 15
Note:
See TracChangeset
for help on using the changeset viewer.