Changeset 10382 in osm for applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
- Timestamp:
- 2008-09-02T11:23:39+02:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
r8721 r10382 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.lang.Math; 5 6 import java.awt.Component; 6 7 import java.awt.Graphics; 8 import java.awt.Point; 7 9 import java.awt.Toolkit; 8 10 import java.awt.event.ActionEvent; … … 30 32 import org.openstreetmap.josm.data.projection.Projection; 31 33 import org.openstreetmap.josm.data.Bounds; 34 import org.openstreetmap.josm.data.coor.LatLon; 32 35 import org.openstreetmap.josm.gui.MapView; 36 import java.util.ArrayList; 33 37 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 34 38 import org.openstreetmap.josm.gui.dialogs.LayerListPopup; … … 46 50 new ImageIcon(Toolkit.getDefaultToolkit().createImage(WMSPlugin.class.getResource("/images/wms_small.png"))); 47 51 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; 51 64 52 65 public WMSLayer() { 53 66 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) { 57 71 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; 65 91 } 66 92 … … 70 96 71 97 @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)); 73 102 } 74 103 … … 80 109 } 81 110 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 82 124 @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 } 84 156 } 85 157 86 158 @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 } 91 165 } 92 166 … … 99 173 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 100 174 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 175 new JSeparator(), 101 176 new JMenuItem(new LoadWmsAction()), 102 177 new JMenuItem(new SaveWmsAction()), 103 178 new JSeparator(), 179 new JMenuItem(new StartWmsAction()), 180 new JMenuItem(new StopWmsAction()), 181 new JSeparator(), 104 182 new JMenuItem(new LayerListPopup.InfoAction(this))}; 183 105 184 } 106 185 107 186 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 } 115 194 return null; 116 195 } 196 117 197 118 198 public class SaveWmsAction extends AbstractAction { … … 122 202 public void actionPerformed(ActionEvent ev) { 123 203 File f = openFileDialog(false); 124 try { 204 try 205 { 125 206 FileOutputStream fos = new FileOutputStream(f); 126 207 ObjectOutputStream oos = new ObjectOutputStream(fos); 127 208 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); 132 215 oos.close(); 133 216 fos.close(); 134 } catch (Exception ex) { 217 } 218 catch (Exception ex) { 135 219 ex.printStackTrace(System.out); 136 220 } 137 221 } 138 222 } 139 223 140 224 public class LoadWmsAction extends AbstractAction { 141 225 public LoadWmsAction() { … … 145 229 File f = openFileDialog(true); 146 230 if (f == null) return; 147 try { 231 try 232 { 148 233 FileInputStream fis = new FileInputStream(f); 149 234 ObjectInputStream ois = new ObjectInputStream(fis); 150 235 int sfv = ois.readInt(); 151 236 if (sfv != serializeFormatVersion) { 152 JOptionPane.showMessageDialog(Main.parent, 237 JOptionPane.showMessageDialog(Main.parent, 153 238 tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion), 154 tr("File Format Error"), 239 tr("File Format Error"), 155 240 JOptionPane.ERROR_MESSAGE); 156 241 return; 157 242 } 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 163 250 ois.close(); 164 251 fis.close(); 165 } catch (Exception ex) { 252 started = false; 253 } 254 catch (Exception ex) { 166 255 // FIXME be more specific 167 256 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 177 284 protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple) { 178 285 String curDir = Main.pref.get("lastDirectory"); … … 184 291 fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]); 185 292 fc.setAcceptAllFileFilterUsed(true); 186 293 187 294 int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent); 188 295 if (answer != JFileChooser.APPROVE_OPTION) 189 296 return null; 190 297 191 298 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) 192 299 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); … … 194 301 if (!open) { 195 302 File file = fc.getSelectedFile(); 196 if (file == null || (file.exists() && JOptionPane.YES_OPTION != 197 303 if (file == null || (file.exists() && JOptionPane.YES_OPTION != 304 JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION))) 198 305 return null; 199 306 } 200 307 201 308 return fc; 202 309 } 203 310 204 311 public static File openFileDialog(boolean open) { 205 312 JFileChooser fc = createAndOpenFileChooser(open, false);
Note:
See TracChangeset
for help on using the changeset viewer.