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