Ignore:
Timestamp:
2011-01-12T23:33:13+01:00 (14 years ago)
Author:
pieren
Message:

change the way we grab the small squares with a spiral algorithm to visualize the first square in the middle of the screen

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  
    2222    private WMSLayer wmsLayer;
    2323
    24     private Lock lock = new ReentrantLock();
     24    private Lock lockImagesToGrag = new ReentrantLock();
    2525
    2626    private ArrayList<EastNorthBound> imagesToGrab = new ArrayList<EastNorthBound>();
     
    3030    private EastNorthBound currentGrabImage;
    3131
     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     */
    3238    public void addImages(ArrayList<EastNorthBound> moreImages) {
    33         lock.lock();
     39        lockImagesToGrag.lock();
    3440        imagesToGrab.addAll(moreImages);
    35         lock.unlock();
     41        lockImagesToGrag.unlock();
    3642        synchronized(this) {
    3743            this.notify();
    3844        }
    3945        System.out.println("Added " + moreImages.size() + " to the grab thread");
     46        if (wmsLayer.isRaster()) {
     47            waitNotification();
     48        }
    4049    }
    4150
    4251    public int getImagesToGrabSize() {
    43         lock.lock();
     52        lockImagesToGrag.lock();
    4453        int size = imagesToGrab.size();
    45         lock.unlock();
     54        lockImagesToGrag.unlock();
    4655        return size;
    4756    }
     
    4958    public ArrayList<EastNorthBound> getImagesToGrabCopy() {
    5059        ArrayList<EastNorthBound> copyList = new ArrayList<EastNorthBound>();
    51         lock.lock();
     60        lockImagesToGrag.lock();
    5261        for (EastNorthBound img : imagesToGrab) {
    5362            EastNorthBound imgCpy = new EastNorthBound(img.min, img.max);
    5463            copyList.add(imgCpy);
    5564        }
    56         lock.unlock();
     65        lockImagesToGrag.unlock();
    5766        return copyList;
    5867    }
    5968   
    6069    public void clearImagesToGrab() {       
    61         lock.lock();
     70        lockImagesToGrag.lock();
    6271        imagesToGrab.clear();
    63         lock.unlock();
     72        lockImagesToGrag.unlock();
    6473    }
    6574   
     
    6877        for (;;) {
    6978            while (getImagesToGrabSize() > 0) {
    70                 lock.lock();
     79                lockImagesToGrag.lock();
     80                lockCurrentGrabImage.lock();
    7181                currentGrabImage = imagesToGrab.get(0);
     82                lockCurrentGrabImage.unlock();
    7283                imagesToGrab.remove(0);
    73                 lock.unlock();
     84                lockImagesToGrag.unlock();
    7485                if (cancelled) {
    7586                    break;
     
    7788                    GeorefImage newImage;
    7889                    try {
     90                        Main.map.repaint(); // paint the current grab box
    7991                        newImage = grabber.grab(wmsLayer, currentGrabImage.min, currentGrabImage.max);
    8092                    } catch (IOException e) {
     
    93105                        break;
    94106                    }
     107                    try {
    95108                    if (CadastrePlugin.backgroundTransparent) {
    96109                        wmsLayer.imagesLock.lock();
     
    109122                    Main.map.mapView.repaint();
    110123                    saveToCache(newImage);
     124                    } catch (NullPointerException e) {
     125                        System.out.println("Layer destroyed. Cancel grab thread");
     126                        setCancelled(true);
     127                    }
    111128                }
    112129            }
    113130            System.out.println("grab thread list empty");
     131            lockCurrentGrabImage.lock();
    114132            currentGrabImage = null;
     133            lockCurrentGrabImage.unlock();
    115134            if (cancelled) {
    116135                clearImagesToGrab();
    117136                cancelled = false;
    118137            }
    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();        }
    127142    }
    128143
     
    165180
    166181    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();
    171189        if (currentGrabImage != null) {
    172190            paintBox(g, mv, currentGrabImage, Color.orange);
    173191        }
     192        lockCurrentGrabImage.unlock();
    174193    }
    175194   
     
    203222    }
    204223
     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
    205236}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java

    r24955 r25033  
    108108        // if the layer is currently saving the images in the cache, wait until it's finished
    109109        grabThread.cancel();
     110        grabThread = null;
    110111        super.destroy();
    111112        images = null;
     
    141142                divideBbox(b, Integer.parseInt(Main.pref.get("cadastrewms.scale", Scale.X1.toString())));
    142143        }
    143 
    144144        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.
    150149     *
    151150     * @param b      the original bbox, usually the current bbox on screen
     
    172171        } else {
    173172            // 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//            }
    182221        }
    183222    }
     
    231270            paintCrosspieces(g, mv);
    232271        }
    233         //        if (grabThread.getImagesToGrabSize() > 0) {
    234             grabThread.paintBoxesToGrab(g, mv);
    235             //        }
     272        grabThread.paintBoxesToGrab(g, mv);
    236273        if (this.adjustModeEnabled) {
    237274            WMSAdjustAction.paintAdjustFrames(g, mv);
     
    379416     */
    380417    public void write(ObjectOutputStream oos) throws IOException {
    381         // Set currentFormat to the serializeFormatVersion
    382418        currentFormat = this.serializeFormatVersion;
    383419        oos.writeInt(this.serializeFormatVersion);
Note: See TracChangeset for help on using the changeset viewer.