1 | package cadastre_fr;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.concurrent.locks.Lock;
|
---|
9 | import java.util.concurrent.locks.ReentrantLock;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.gui.MapView;
|
---|
14 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
15 |
|
---|
16 | public class GrabThread extends Thread {
|
---|
17 |
|
---|
18 | private boolean cancelled;
|
---|
19 |
|
---|
20 | private CadastreGrabber grabber;
|
---|
21 |
|
---|
22 | private WMSLayer wmsLayer;
|
---|
23 |
|
---|
24 | private Lock lock = new ReentrantLock();
|
---|
25 |
|
---|
26 | private ArrayList<EastNorthBound> imagesToGrab = new ArrayList<EastNorthBound>();
|
---|
27 |
|
---|
28 | private CacheControl cacheControl = null;
|
---|
29 |
|
---|
30 | private EastNorthBound currentGrabImage;
|
---|
31 |
|
---|
32 | public void addImages(ArrayList<EastNorthBound> moreImages) {
|
---|
33 | lock.lock();
|
---|
34 | imagesToGrab.addAll(moreImages);
|
---|
35 | lock.unlock();
|
---|
36 | synchronized(this) {
|
---|
37 | this.notify();
|
---|
38 | }
|
---|
39 | System.out.println("Added " + moreImages.size() + " to the grab thread");
|
---|
40 | }
|
---|
41 |
|
---|
42 | public int getImagesToGrabSize() {
|
---|
43 | lock.lock();
|
---|
44 | int size = imagesToGrab.size();
|
---|
45 | lock.unlock();
|
---|
46 | return size;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ArrayList<EastNorthBound> getImagesToGrabCopy() {
|
---|
50 | ArrayList<EastNorthBound> copyList = new ArrayList<EastNorthBound>();
|
---|
51 | lock.lock();
|
---|
52 | for (EastNorthBound img : imagesToGrab) {
|
---|
53 | EastNorthBound imgCpy = new EastNorthBound(img.min, img.max);
|
---|
54 | copyList.add(imgCpy);
|
---|
55 | }
|
---|
56 | lock.unlock();
|
---|
57 | return copyList;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void clearImagesToGrab() {
|
---|
61 | lock.lock();
|
---|
62 | imagesToGrab.clear();
|
---|
63 | lock.unlock();
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void run() {
|
---|
68 | for (;;) {
|
---|
69 | while (getImagesToGrabSize() > 0) {
|
---|
70 | lock.lock();
|
---|
71 | currentGrabImage = imagesToGrab.get(0);
|
---|
72 | imagesToGrab.remove(0);
|
---|
73 | lock.unlock();
|
---|
74 | if (cancelled) {
|
---|
75 | break;
|
---|
76 | } else {
|
---|
77 | GeorefImage newImage;
|
---|
78 | try {
|
---|
79 | newImage = grabber.grab(wmsLayer, currentGrabImage.min, currentGrabImage.max);
|
---|
80 | } catch (IOException e) {
|
---|
81 | System.out
|
---|
82 | .println("Download action cancelled by user or server did not respond");
|
---|
83 | setCancelled(true);
|
---|
84 | break;
|
---|
85 | } catch (OsmTransferException e) {
|
---|
86 | System.out.println("OSM transfer failed");
|
---|
87 | setCancelled(true);
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | if (grabber.getWmsInterface().downloadCancelled) {
|
---|
91 | System.out.println("Download action cancelled by user");
|
---|
92 | setCancelled(true);
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | if (CadastrePlugin.backgroundTransparent) {
|
---|
96 | wmsLayer.imagesLock.lock();
|
---|
97 | for (GeorefImage img : wmsLayer.getImages()) {
|
---|
98 | if (img.overlap(newImage))
|
---|
99 | // mask overlapping zone in already grabbed image
|
---|
100 | img.withdraw(newImage);
|
---|
101 | else
|
---|
102 | // mask overlapping zone in new image only when new image covers completely the
|
---|
103 | // existing image
|
---|
104 | newImage.withdraw(img);
|
---|
105 | }
|
---|
106 | wmsLayer.imagesLock.unlock();
|
---|
107 | }
|
---|
108 | wmsLayer.addImage(newImage);
|
---|
109 | Main.map.mapView.repaint();
|
---|
110 | saveToCache(newImage);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | System.out.println("grab thread list empty");
|
---|
114 | currentGrabImage = null;
|
---|
115 | if (cancelled) {
|
---|
116 | clearImagesToGrab();
|
---|
117 | cancelled = false;
|
---|
118 | }
|
---|
119 | synchronized(this) {
|
---|
120 | try {
|
---|
121 | wait();
|
---|
122 | } catch (InterruptedException e) {
|
---|
123 | e.printStackTrace(System.out);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void saveToCache(GeorefImage image) {
|
---|
130 | if (CacheControl.cacheEnabled && !wmsLayer.isRaster()) {
|
---|
131 | getCacheControl().saveCache(image);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void saveNewCache() {
|
---|
136 | if (CacheControl.cacheEnabled) {
|
---|
137 | getCacheControl().deleteCacheFile();
|
---|
138 | wmsLayer.imagesLock.lock();
|
---|
139 | for (GeorefImage image : wmsLayer.getImages())
|
---|
140 | getCacheControl().saveCache(image);
|
---|
141 | wmsLayer.imagesLock.unlock();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void cancel() {
|
---|
146 | clearImagesToGrab();
|
---|
147 | if (cacheControl != null) {
|
---|
148 | while (!cacheControl.isCachePipeEmpty()) {
|
---|
149 | System.out
|
---|
150 | .println("Try to close a WMSLayer which is currently saving in cache : wait 1 sec.");
|
---|
151 | CadastrePlugin.safeSleep(1000);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public CacheControl getCacheControl() {
|
---|
157 | if (cacheControl == null)
|
---|
158 | cacheControl = new CacheControl(wmsLayer);
|
---|
159 | return cacheControl;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public GrabThread(WMSLayer wmsLayer) {
|
---|
163 | this.wmsLayer = wmsLayer;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void paintBoxesToGrab(Graphics g, MapView mv) {
|
---|
167 | ArrayList<EastNorthBound> imagesToGrab = getImagesToGrabCopy();
|
---|
168 | for (EastNorthBound img : imagesToGrab) {
|
---|
169 | paintBox(g, mv, img, Color.red);
|
---|
170 | }
|
---|
171 | if (currentGrabImage != null) {
|
---|
172 | paintBox(g, mv, currentGrabImage, Color.orange);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void paintBox(Graphics g, MapView mv, EastNorthBound img, Color color) {
|
---|
177 | Point[] croppedPoint = new Point[5];
|
---|
178 | croppedPoint[0] = mv.getPoint(img.min);
|
---|
179 | croppedPoint[1] = mv.getPoint(new EastNorth(img.min.east(), img.max.north()));
|
---|
180 | croppedPoint[2] = mv.getPoint(img.max);
|
---|
181 | croppedPoint[3] = mv.getPoint(new EastNorth(img.max.east(), img.min.north()));
|
---|
182 | croppedPoint[4] = croppedPoint[0];
|
---|
183 | for (int i=0; i<4; i++) {
|
---|
184 | g.setColor(color);
|
---|
185 | g.drawLine(croppedPoint[i].x, croppedPoint[i].y, croppedPoint[i+1].x, croppedPoint[i+1].y);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public boolean isCancelled() {
|
---|
190 | return cancelled;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void setCancelled(boolean cancelled) {
|
---|
194 | this.cancelled = cancelled;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public CadastreGrabber getGrabber() {
|
---|
198 | return grabber;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void setGrabber(CadastreGrabber grabber) {
|
---|
202 | this.grabber = grabber;
|
---|
203 | }
|
---|
204 |
|
---|
205 | }
|
---|