Ignore:
Timestamp:
2008-09-02T11:23:39+02:00 (16 years ago)
Author:
petrdlouhy
Message:

Added automatic tiles downloading and Yahoo support.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java

    r8721 r10382  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.lang.Math;
    56import java.awt.Component;
    67import java.awt.Graphics;
     8import java.awt.Point;
    79import java.awt.Toolkit;
    810import java.awt.event.ActionEvent;
     
    3032import org.openstreetmap.josm.data.projection.Projection;
    3133import org.openstreetmap.josm.data.Bounds;
     34import org.openstreetmap.josm.data.coor.LatLon;
    3235import org.openstreetmap.josm.gui.MapView;
     36import java.util.ArrayList;
    3337import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    3438import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
     
    4650                new ImageIcon(Toolkit.getDefaultToolkit().createImage(WMSPlugin.class.getResource("/images/wms_small.png")));
    4751
    48         protected ArrayList<GeorefImage> images = new ArrayList<GeorefImage>();
    49         protected Grabber grabber;
    50         protected final int serializeFormatVersion = 2;
     52        public int messageNum = 5; //limit for messages per layer
     53        protected boolean started = true;
     54        protected boolean stopAfterPaint = false;
     55        protected int ImageSize = 500;
     56        protected int dax = 10;
     57        protected int day = 10;
     58        protected int minZoom = 3;
     59        protected double pixelPerDegree;
     60        protected GeorefImage[][] images = new GeorefImage[dax][day];
     61
     62        protected String baseURL;
     63        protected final int serializeFormatVersion = 3;
    5164
    5265        public WMSLayer() {
    5366                this(tr("Blank Layer"), null);
    54         }
    55 
    56         public WMSLayer(String name, Grabber grabber) {
     67                initializeImages();
     68        }
     69
     70        public WMSLayer(String name, String baseURL) {
    5771                super(name);
    58                 this.grabber = grabber;
    59         }
    60 
    61         public void grab(Bounds b, double pixelPerDegree) throws IOException {
    62                 if (grabber == null) return;
    63                 images.add(grabber.grab(b, Main.main.proj, pixelPerDegree));
    64                 Main.map.mapView.repaint();
     72                initializeImages();
     73                this.baseURL = baseURL;
     74        }
     75
     76        public void initializeImages() {
     77                images = new GeorefImage[dax][day];
     78                for(int x = 0; x<dax; ++x)
     79                        for(int y = 0; y<day; ++y)
     80                                images[x][y]= new GeorefImage(false);
     81        }
     82
     83        public void grab(Bounds b, double _pixelPerDegree) {
     84                if (baseURL == null) return;
     85                //set resolution
     86                if(started || Math.round(pixelPerDegree/10000) != Math.round(_pixelPerDegree/10000))
     87                        initializeImages();
     88                pixelPerDegree = _pixelPerDegree;
     89                if(!started)stopAfterPaint = true;
     90                started = true;
    6591        }
    6692
     
    7096
    7197        @Override public String getToolTipText() {
    72                 return tr("WMS layer ({0}), {1} tile(s) loaded", name, images.size());
     98                if(started)
     99                        return tr("WMS layer ({0}), automaticaly downloading in zoom {1}", name, Math.round(pixelPerDegree/10000));
     100                else
     101                        return tr("WMS layer ({0}), downloading in zoom {1}", name, Math.round(pixelPerDegree/10000));
    73102        }
    74103
     
    80109        }
    81110
     111        private Bounds XYtoBounds (int x, int y) {
     112                return new Bounds(
     113                        new LatLon( x * ImageSize / pixelPerDegree,
     114                                         y * ImageSize / pixelPerDegree),
     115                        new LatLon((x + 1) *  ImageSize / pixelPerDegree,
     116                                   (y + 1) * ImageSize / pixelPerDegree));
     117        }
     118
     119        private int modulo (int a, int b) {
     120          if(a%b>=0)return a%b;
     121          else return a%b+b;
     122        }         
     123
    82124        @Override public void paint(Graphics g, final MapView mv) {
    83                 for (GeorefImage img : images) img.paint(g, mv);
     125                Bounds b = new Bounds(
     126                        mv.getLatLon(0, mv.getHeight()),
     127                        mv.getLatLon(mv.getWidth(), 0));
     128                int bminx= (int)Math.floor ((b.min.lat() * pixelPerDegree ) / ImageSize );
     129                int bminy= (int)Math.floor ((b.min.lon() * pixelPerDegree ) / ImageSize );
     130                int bmaxx= (int)Math.ceil  ((b.max.lat() * pixelPerDegree ) / ImageSize );
     131                int bmaxy= (int)Math.ceil  ((b.max.lon() * pixelPerDegree ) / ImageSize );
     132
     133
     134                if( !started || (pixelPerDegree / (mv.getWidth() / (b.max.lon() - b.min.lon())) > minZoom) ){ //don't download when it's too outzoomed
     135                        for(int x = 0; x<dax; ++x)
     136                                for(int y = 0; y<day; ++y){
     137                                                images[modulo(x,dax)][modulo(y,day)].paint(g, mv);
     138                                }
     139                } else {
     140                        for(int x = bminx; x<bmaxx; ++x)
     141                                for(int y = bminy; y<bmaxy; ++y){
     142                                        GeorefImage img = images[modulo(x,dax)][modulo(y,day)];
     143                                        if(!img.paint(g, mv) && !img.downloadingStarted){
     144                                                //System.out.println(tr("------{0}|{1}|{2}|{3}", modulo(x,dax), modulo(y,day), img.downloadingStarted, img.isVisible(mv)));
     145                                                img.downloadingStarted = true;
     146                                                img.image = null;
     147                                                Grabber gr = WMSPlugin.getGrabber(baseURL, XYtoBounds(x,y), Main.main.proj, pixelPerDegree, img, mv, this);
     148                                                gr.start();
     149                                }
     150                        }
     151                }
     152                if(stopAfterPaint){
     153                        started = false;
     154                        stopAfterPaint = false;
     155                }
    84156        }
    85157
    86158        @Override public void visitBoundingBox(BoundingXYVisitor v) {
    87                 for (GeorefImage img : images) {
    88                         v.visit(img.min);
    89                         v.visit(img.max);
    90                 }
     159                for(int x = 0; x<dax; ++x)
     160                        for(int y = 0; y<day; ++y)
     161                                        if(images[x][y]!=null){
     162                                                v.visit(images[x][y].min);
     163                                                v.visit(images[x][y].max);
     164                                        }
    91165        }
    92166
     
    99173                                new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
    100174                                new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),                             
     175                                new JSeparator(),
    101176                                new JMenuItem(new LoadWmsAction()),
    102177                                new JMenuItem(new SaveWmsAction()),
    103178                                new JSeparator(),
     179                                new JMenuItem(new StartWmsAction()),
     180                                new JMenuItem(new StopWmsAction()),
     181                                new JSeparator(),
    104182                                new JMenuItem(new LayerListPopup.InfoAction(this))};
     183
    105184        }
    106185
    107186        public GeorefImage findImage(EastNorth eastNorth) {
    108                 // Iterate in reverse, so we return the image which is painted last.
    109                 // (i.e. the topmost one)
    110                 for (int i = images.size() - 1; i >= 0; i--) {
    111                         if (images.get(i).contains(eastNorth)) {
    112                                 return images.get(i);
    113                         }
    114                 }
     187                for(int x = 0; x<dax; ++x)
     188                        for(int y = 0; y<day; ++y)
     189                                        if(images[x][y]!=null && images[x][y].image!=null && images[x][y].min!=null && images[x][y].max!=null){
     190                                                if (images[x][y].contains(eastNorth)) {
     191                                                        return images[x][y];
     192                                                }
     193                                        }
    115194                return null;
    116195        }
     196
    117197
    118198        public class SaveWmsAction extends AbstractAction {
     
    122202                public void actionPerformed(ActionEvent ev) {
    123203                        File f = openFileDialog(false);
    124                         try {
     204                        try
     205                        {
    125206                                FileOutputStream fos = new FileOutputStream(f);
    126207                                ObjectOutputStream oos = new ObjectOutputStream(fos);
    127208                                oos.writeInt(serializeFormatVersion);
    128                                 oos.writeInt(images.size());
    129                                 for (GeorefImage img : images) {
    130                                         oos.writeObject(img);
    131                                 }
     209                                oos.writeInt(dax);
     210                                oos.writeInt(day);
     211                                oos.writeInt(ImageSize);
     212                                oos.writeDouble(pixelPerDegree);
     213                                oos.writeObject(baseURL);
     214                                oos.writeObject(images);
    132215                                oos.close();
    133216                                fos.close();
    134                         } catch (Exception ex) {
     217                        }
     218                        catch (Exception ex) {
    135219                                ex.printStackTrace(System.out);
    136220                        }
    137221                }
    138222        }
    139        
     223
    140224        public class LoadWmsAction extends AbstractAction {
    141225                public LoadWmsAction() {
     
    145229                        File f = openFileDialog(true);
    146230                        if (f == null) return;
    147                         try {
     231                        try
     232                        {
    148233                                FileInputStream fis = new FileInputStream(f);
    149234                                ObjectInputStream ois = new ObjectInputStream(fis);
    150235                                int sfv = ois.readInt();
    151236                                if (sfv != serializeFormatVersion) {
    152                                         JOptionPane.showMessageDialog(Main.parent, 
     237                                        JOptionPane.showMessageDialog(Main.parent,
    153238                                                tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion),
    154                                                 tr("File Format Error"), 
     239                                                tr("File Format Error"),
    155240                                                JOptionPane.ERROR_MESSAGE);
    156241                                        return;
    157242                                }
    158                                 int numImg = ois.readInt();
    159                                 for (int i=0; i< numImg; i++) {
    160                                         GeorefImage img = (GeorefImage) ois.readObject();
    161                                         images.add(img);
    162                                 }
     243                                dax = ois.readInt();
     244                                day = ois.readInt();
     245                                ImageSize = ois.readInt();
     246                                pixelPerDegree = ois.readDouble();
     247                                baseURL = (String) ois.readObject();
     248                                images = (GeorefImage[][])ois.readObject();
     249
    163250                                ois.close();
    164251                                fis.close();
    165                         } catch (Exception ex) {
     252                                started = false;
     253                        }
     254                        catch (Exception ex) {
    166255                                // FIXME be more specific
    167256                                ex.printStackTrace(System.out);
    168                                 JOptionPane.showMessageDialog(Main.parent,
    169                                                 tr("Error loading file"),
    170                                                 tr("Error"),
    171                                                 JOptionPane.ERROR_MESSAGE);
    172                                         return;
    173                         }
    174                 }
    175         }
    176        
     257                                JOptionPane.showMessageDialog(Main.parent,
     258                                        tr("Error loading file"),
     259                                        tr("Error"),
     260                                        JOptionPane.ERROR_MESSAGE);
     261                                return;
     262                        }
     263                }
     264        }
     265
     266        public class StartWmsAction extends AbstractAction {
     267                public StartWmsAction() {
     268                        super(tr("Start automatic downloading"), null);
     269                }
     270                public void actionPerformed(ActionEvent ev) {
     271                        started = true;
     272                }
     273        }
     274
     275        public class StopWmsAction extends AbstractAction {
     276                public StopWmsAction() {
     277                        super(tr("Stop automatic downloading"), null);
     278                }
     279                public void actionPerformed(ActionEvent ev) {
     280                        started = false;
     281                }
     282        }
     283
    177284        protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple) {
    178285                String curDir = Main.pref.get("lastDirectory");
     
    184291                        fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
    185292                fc.setAcceptAllFileFilterUsed(true);
    186        
     293
    187294                int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent);
    188295                if (answer != JFileChooser.APPROVE_OPTION)
    189296                        return null;
    190                
     297
    191298                if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
    192299                        Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
     
    194301                if (!open) {
    195302                        File file = fc.getSelectedFile();
    196                         if (file == null || (file.exists() && JOptionPane.YES_OPTION != 
    197                                         JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION)))
     303                        if (file == null || (file.exists() && JOptionPane.YES_OPTION !=
     304                                JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION)))
    198305                                return null;
    199306                }
    200                
     307
    201308                return fc;
    202309        }
    203        
     310
    204311        public static File openFileDialog(boolean open) {
    205312                JFileChooser fc = createAndOpenFileChooser(open, false);
Note: See TracChangeset for help on using the changeset viewer.