source: josm/trunk/src/org/openstreetmap/josm/io/imagery/Grabber.java@ 7132

Last change on this file since 7132 was 7132, checked in by Don-vip, 10 years ago

fix #9984 - Add support for WMS tiles defining a transparent color in RGB space (tRNS PNG chunk for example), instead of a proper alpha channel. Surprisingly, Java does not support that out of the box, ImageIO.read always returns opaque images. Allows to switch between this mode and standard mode using WMS layer contextual entry "Use Alpha Channel", for consistency with images defining an alpha channel. Does not impact other images than WMS tiles.

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.imagery;
3
4import org.openstreetmap.josm.Main;
5import org.openstreetmap.josm.data.ProjectionBounds;
6import org.openstreetmap.josm.data.imagery.GeorefImage.State;
7import org.openstreetmap.josm.gui.MapView;
8import org.openstreetmap.josm.gui.layer.WMSLayer;
9
10public abstract class Grabber implements Runnable {
11 protected final MapView mv;
12 protected final WMSLayer layer;
13 private final boolean localOnly;
14
15 protected ProjectionBounds b;
16 protected volatile boolean canceled;
17
18 Grabber(MapView mv, WMSLayer layer, boolean localOnly) {
19 this.mv = mv;
20 this.layer = layer;
21 this.localOnly = localOnly;
22 }
23
24 abstract void fetch(WMSRequest request, int attempt) throws Exception; // the image fetch code
25
26 int width(){
27 return layer.getBaseImageWidth();
28 }
29
30 int height(){
31 return layer.getBaseImageHeight();
32 }
33
34 @Override
35 public void run() {
36 while (true) {
37 if (canceled)
38 return;
39 WMSRequest request = layer.getRequest(localOnly);
40 if (request == null)
41 return;
42 this.b = layer.getBounds(request);
43 if (request.isPrecacheOnly()) {
44 if (!layer.cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth)) {
45 attempt(request);
46 } else if (Main.isDebugEnabled()) {
47 Main.debug("Ignoring "+request+" (precache only + exact match)");
48 }
49 } else if (!loadFromCache(request)){
50 attempt(request);
51 } else if (Main.isDebugEnabled()) {
52 Main.debug("Ignoring "+request+" (loaded from cache)");
53 }
54 layer.finishRequest(request);
55 }
56 }
57
58 protected void attempt(WMSRequest request){ // try to fetch the image
59 int maxTries = 5; // n tries for every image
60 for (int i = 1; i <= maxTries; i++) {
61 if (canceled)
62 return;
63 try {
64 if (!request.isPrecacheOnly() && !layer.requestIsVisible(request))
65 return;
66 fetch(request, i);
67 break; // break out of the retry loop
68 } catch (Exception e) {
69 try { // sleep some time and then ask the server again
70 Thread.sleep(random(1000, 2000));
71 } catch (InterruptedException e1) {
72 Main.debug("InterruptedException in "+getClass().getSimpleName()+" during WMS request");
73 }
74 if (i == maxTries) {
75 Main.error(e);
76 request.finish(State.FAILED, null);
77 }
78 }
79 }
80 }
81
82 public static int random(int min, int max) {
83 return (int)(Math.random() * ((max+1)-min) ) + min;
84 }
85
86 public abstract boolean loadFromCache(WMSRequest request);
87
88 public void cancel() {
89 canceled = true;
90 }
91}
Note: See TracBrowser for help on using the repository browser.