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

Last change on this file since 3719 was 3719, checked in by bastiK, 13 years ago

added missing license information

File size: 3.6 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.coor.EastNorth;
7import org.openstreetmap.josm.data.imagery.GeorefImage.State;
8import org.openstreetmap.josm.data.projection.Projection;
9import org.openstreetmap.josm.gui.MapView;
10import org.openstreetmap.josm.gui.layer.WMSLayer;
11import org.openstreetmap.josm.io.CacheFiles;
12
13abstract public class Grabber implements Runnable {
14 public final static CacheFiles cache = new CacheFiles("imagery");
15
16 protected final MapView mv;
17 protected final WMSLayer layer;
18
19 protected ProjectionBounds b;
20 protected Projection proj;
21 protected double pixelPerDegree;
22 protected WMSRequest request;
23 protected volatile boolean canceled;
24
25 Grabber(MapView mv, WMSLayer layer, CacheFiles cache) {
26 this.mv = mv;
27 this.layer = layer;
28 }
29
30 private void updateState(WMSRequest request) {
31 b = new ProjectionBounds(
32 layer.getEastNorth(request.getXIndex(), request.getYIndex()),
33 layer.getEastNorth(request.getXIndex() + 1, request.getYIndex() + 1));
34 if (b.min != null && b.max != null && WMSLayer.PROP_OVERLAP.get()) {
35 double eastSize = b.max.east() - b.min.east();
36 double northSize = b.max.north() - b.min.north();
37
38 double eastCoef = WMSLayer.PROP_OVERLAP_EAST.get() / 100.0;
39 double northCoef = WMSLayer.PROP_OVERLAP_NORTH.get() / 100.0;
40
41 this.b = new ProjectionBounds( new EastNorth(b.min.east(),
42 b.min.north()),
43 new EastNorth(b.max.east() + eastCoef * eastSize,
44 b.max.north() + northCoef * northSize));
45 }
46
47 this.proj = Main.proj;
48 this.pixelPerDegree = request.getPixelPerDegree();
49 this.request = request;
50 }
51
52 abstract void fetch(WMSRequest request) throws Exception; // the image fetch code
53
54 int width(){
55 return layer.getBaseImageWidth();
56 }
57 int height(){
58 return layer.getBaseImageHeight();
59 }
60
61 @Override
62 public void run() {
63 while (true) {
64 if (canceled)
65 return;
66 WMSRequest request = layer.getRequest();
67 if (request == null)
68 return;
69 updateState(request);
70 if(!loadFromCache(request)){
71 attempt(request);
72 }
73 if (request.getState() != null) {
74 layer.finishRequest(request);
75 mv.repaint();
76 }
77 }
78 }
79
80 protected void attempt(WMSRequest request){ // try to fetch the image
81 int maxTries = 5; // n tries for every image
82 for (int i = 1; i <= maxTries; i++) {
83 if (canceled)
84 return;
85 try {
86 if (!layer.requestIsValid(request))
87 return;
88 fetch(request);
89 break; // break out of the retry loop
90 } catch (Exception e) {
91 try { // sleep some time and then ask the server again
92 Thread.sleep(random(1000, 2000));
93 } catch (InterruptedException e1) {}
94
95 if(i == maxTries) {
96 e.printStackTrace();
97 request.finish(State.FAILED, null);
98 }
99 }
100 }
101 }
102
103 public static int random(int min, int max) {
104 return (int)(Math.random() * ((max+1)-min) ) + min;
105 }
106
107 abstract public boolean loadFromCache(WMSRequest request);
108
109 public void cancel() {
110 canceled = true;
111 }
112
113}
Note: See TracBrowser for help on using the repository browser.