Changeset 25033 in osm
- Timestamp:
- 2011-01-12T23:33:13+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GrabThread.java
r24934 r25033 22 22 private WMSLayer wmsLayer; 23 23 24 private Lock lock = new ReentrantLock();24 private Lock lockImagesToGrag = new ReentrantLock(); 25 25 26 26 private ArrayList<EastNorthBound> imagesToGrab = new ArrayList<EastNorthBound>(); … … 30 30 private EastNorthBound currentGrabImage; 31 31 32 private Lock lockCurrentGrabImage = new ReentrantLock(); 33 34 /** 35 * Call directly grabber for raster images or prepare thread for vector images 36 * @param moreImages 37 */ 32 38 public void addImages(ArrayList<EastNorthBound> moreImages) { 33 lock .lock();39 lockImagesToGrag.lock(); 34 40 imagesToGrab.addAll(moreImages); 35 lock .unlock();41 lockImagesToGrag.unlock(); 36 42 synchronized(this) { 37 43 this.notify(); 38 44 } 39 45 System.out.println("Added " + moreImages.size() + " to the grab thread"); 46 if (wmsLayer.isRaster()) { 47 waitNotification(); 48 } 40 49 } 41 50 42 51 public int getImagesToGrabSize() { 43 lock .lock();52 lockImagesToGrag.lock(); 44 53 int size = imagesToGrab.size(); 45 lock .unlock();54 lockImagesToGrag.unlock(); 46 55 return size; 47 56 } … … 49 58 public ArrayList<EastNorthBound> getImagesToGrabCopy() { 50 59 ArrayList<EastNorthBound> copyList = new ArrayList<EastNorthBound>(); 51 lock .lock();60 lockImagesToGrag.lock(); 52 61 for (EastNorthBound img : imagesToGrab) { 53 62 EastNorthBound imgCpy = new EastNorthBound(img.min, img.max); 54 63 copyList.add(imgCpy); 55 64 } 56 lock .unlock();65 lockImagesToGrag.unlock(); 57 66 return copyList; 58 67 } 59 68 60 69 public void clearImagesToGrab() { 61 lock .lock();70 lockImagesToGrag.lock(); 62 71 imagesToGrab.clear(); 63 lock .unlock();72 lockImagesToGrag.unlock(); 64 73 } 65 74 … … 68 77 for (;;) { 69 78 while (getImagesToGrabSize() > 0) { 70 lock.lock(); 79 lockImagesToGrag.lock(); 80 lockCurrentGrabImage.lock(); 71 81 currentGrabImage = imagesToGrab.get(0); 82 lockCurrentGrabImage.unlock(); 72 83 imagesToGrab.remove(0); 73 lock .unlock();84 lockImagesToGrag.unlock(); 74 85 if (cancelled) { 75 86 break; … … 77 88 GeorefImage newImage; 78 89 try { 90 Main.map.repaint(); // paint the current grab box 79 91 newImage = grabber.grab(wmsLayer, currentGrabImage.min, currentGrabImage.max); 80 92 } catch (IOException e) { … … 93 105 break; 94 106 } 107 try { 95 108 if (CadastrePlugin.backgroundTransparent) { 96 109 wmsLayer.imagesLock.lock(); … … 109 122 Main.map.mapView.repaint(); 110 123 saveToCache(newImage); 124 } catch (NullPointerException e) { 125 System.out.println("Layer destroyed. Cancel grab thread"); 126 setCancelled(true); 127 } 111 128 } 112 129 } 113 130 System.out.println("grab thread list empty"); 131 lockCurrentGrabImage.lock(); 114 132 currentGrabImage = null; 133 lockCurrentGrabImage.unlock(); 115 134 if (cancelled) { 116 135 clearImagesToGrab(); 117 136 cancelled = false; 118 137 } 119 synchronized(this) { 120 try { 121 wait(); 122 } catch (InterruptedException e) { 123 e.printStackTrace(System.out); 124 } 125 } 126 } 138 if (wmsLayer.isRaster()) { 139 notifyWaiter(); 140 } 141 waitNotification(); } 127 142 } 128 143 … … 165 180 166 181 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 } 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(); 171 189 if (currentGrabImage != null) { 172 190 paintBox(g, mv, currentGrabImage, Color.orange); 173 191 } 192 lockCurrentGrabImage.unlock(); 174 193 } 175 194 … … 203 222 } 204 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 e.printStackTrace(System.out); 233 } 234 } 235 205 236 } -
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java
r24955 r25033 108 108 // if the layer is currently saving the images in the cache, wait until it's finished 109 109 grabThread.cancel(); 110 grabThread = null; 110 111 super.destroy(); 111 112 images = null; … … 141 142 divideBbox(b, Integer.parseInt(Main.pref.get("cadastrewms.scale", Scale.X1.toString()))); 142 143 } 143 144 144 grabThread.addImages(dividedBbox); 145 Main.map.repaint(); 146 } 147 148 /** 149 * Divides the bounding box in smaller polygons. 145 } 146 147 /** 148 * Divides the bounding box in smaller squares. Their size (and quantity) is configurable in Preferences. 150 149 * 151 150 * @param b the original bbox, usually the current bbox on screen … … 172 171 } else { 173 172 // divide to fixed size squares 174 int cSquare = Integer.parseInt(Main.pref.get("cadastrewms.squareSize", "100")); 175 minEast = minEast - minEast % cSquare; 176 minNorth = minNorth - minNorth % cSquare; 177 for (int xEast = (int)minEast; xEast < lambertMax.east(); xEast+=cSquare) 178 for (int xNorth = (int)minNorth; xNorth < lambertMax.north(); xNorth+=cSquare) { 179 dividedBbox.add(new EastNorthBound(new EastNorth(xEast, xNorth), 180 new EastNorth(xEast + cSquare, xNorth + cSquare))); 181 } 173 // grab all square in a spiral starting from the center (usually the most interesting place) 174 int c = Integer.parseInt(Main.pref.get("cadastrewms.squareSize", "100")); 175 lambertMin = lambertMin.add(- minEast%c, - minNorth%c); 176 lambertMax = lambertMax.add(c - lambertMax.east()%c, c - lambertMax.north()%c); 177 EastNorth mid = lambertMax.getCenter(lambertMin); 178 mid = mid.add(-1, 1); // in case the boxes side is a pair, select the one one top,left to follow the rotation 179 mid = mid.add(- mid.east()%c, - mid.north()%c); 180 int x = (int)(lambertMax.east() - lambertMin.east())/100; 181 int y = (int)(lambertMax.north() - lambertMin.north())/100; 182 int dx[] = {+1, 0,-1, 0}; 183 int dy[] = {0,-1, 0,+1}; 184 int currDir = -1, lDir = 1, i = 1, j = 0, k = -1; 185 if (x == 1) 186 currDir = 0; 187 dividedBbox.add(new EastNorthBound(mid, new EastNorth(mid.east()+c, mid.north()+c))); 188 while (i < (x*y)) { 189 i++; 190 j++; 191 if (j >= lDir) { 192 k++; 193 if (k > 1) { 194 lDir++; 195 k = 0; 196 } 197 j = 0; 198 currDir = (currDir+1)%4; 199 } else if (currDir >= 0 && j >= (currDir == 0 || currDir == 2 ? x-1 : y-1)) { 200 // the overall is a rectangle, not a square. Jump to the other side to grab next square. 201 k++; 202 if (k > 1) { 203 lDir++; 204 k = 0; 205 } 206 j = lDir-1; 207 currDir = (currDir+1)%4; 208 mid = new EastNorth(mid.east() + dx[currDir]*c*(lDir-1), mid.north() + dy[currDir]*c*(lDir-1)); 209 } 210 mid = new EastNorth(mid.east() + dx[currDir]*c, mid.north() + dy[currDir]*c); 211 dividedBbox.add(new EastNorthBound(mid, new EastNorth(mid.east()+c, mid.north()+c))); 212 } 213 // // simple algorithm to grab all squares 214 // minEast = minEast - minEast % cSquare; 215 // minNorth = minNorth - minNorth % cSquare; 216 // for (int xEast = (int)minEast; xEast < lambertMax.east(); xEast+=cSquare) 217 // for (int xNorth = (int)minNorth; xNorth < lambertMax.north(); xNorth+=cSquare) { 218 // dividedBbox.add(new EastNorthBound(new EastNorth(xEast, xNorth), 219 // new EastNorth(xEast + cSquare, xNorth + cSquare))); 220 // } 182 221 } 183 222 } … … 231 270 paintCrosspieces(g, mv); 232 271 } 233 // if (grabThread.getImagesToGrabSize() > 0) { 234 grabThread.paintBoxesToGrab(g, mv); 235 // } 272 grabThread.paintBoxesToGrab(g, mv); 236 273 if (this.adjustModeEnabled) { 237 274 WMSAdjustAction.paintAdjustFrames(g, mv); … … 379 416 */ 380 417 public void write(ObjectOutputStream oos) throws IOException { 381 // Set currentFormat to the serializeFormatVersion382 418 currentFormat = this.serializeFormatVersion; 383 419 oos.writeInt(this.serializeFormatVersion);
Note:
See TracChangeset
for help on using the changeset viewer.