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