Changeset 12778 in osm for applications/editors/josm/plugins/wmsplugin
- Timestamp:
- 2009-01-01T18:28:53+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/wmsplugin/src
- Files:
-
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/LatLng.java
r6775 r12778 3 3 /** 4 4 * Class to represent a latitude/longitude pair. 5 * 5 * 6 6 * (c) 2006 Jonathan Stott 7 * 7 * 8 8 * Created on 11-02-2006 9 * 9 * 10 10 * @author Jonathan Stott 11 11 * @version 1.0 … … 27 27 /** 28 28 * Create a new LatLng object to represent a latitude/longitude pair. 29 * 29 * 30 30 * @param lat 31 31 * the latitude in degrees … … 42 42 /** 43 43 * Get a String representation of this LatLng object. 44 * 44 * 45 45 * @return a String representation of this LatLng object. 46 46 * @since 1.0 … … 54 54 * Convert this latitude and longitude into an OSGB (Ordnance Survey of Great 55 55 * Britain) grid reference. 56 * 56 * 57 57 * @return the converted OSGB grid reference 58 58 * @since 1.0 … … 124 124 /** 125 125 * Convert this latitude and longitude to a UTM reference. 126 * 126 * 127 127 * @return the converted UTM reference 128 128 * @since 1.0 … … 212 212 * Convert this LatLng from the OSGB36 datum to the WGS84 datum using an 213 213 * approximate Helmert transformation. 214 * 214 * 215 215 * @since 1.0 216 216 */ … … 260 260 * Convert this LatLng from the WGS84 datum to the OSGB36 datum using an 261 261 * approximate Helmert transformation. 262 * 262 * 263 263 * @since 1.0 264 264 */ … … 310 310 * Calculate the surface distance in kilometres from the this LatLng to the 311 311 * given LatLng. 312 * 312 * 313 313 * @param ll 314 314 * @return the surface distance in km … … 334 334 /** 335 335 * Return the latitude in degrees. 336 * 336 * 337 337 * @return the latitude in degrees 338 338 * @since 1.0 … … 345 345 /** 346 346 * Return the longitude in degrees. 347 * 347 * 348 348 * @return the longitude in degrees 349 349 * @since 1.0 -
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/OSRef.java
r6775 r12778 3 3 /** 4 4 * Class to represent an Ordnance Survey grid reference 5 * 5 * 6 6 * (c) 2006 Jonathan Stott 7 * 7 * 8 8 * Created on 11-02-2006 9 * 9 * 10 10 * @author Jonathan Stott 11 11 * @version 1.0 … … 27 27 /** 28 28 * Create a new Ordnance Survey grid reference. 29 * 29 * 30 30 * @param easting 31 31 * the easting in metres … … 45 45 * first character must be H, N, S, O or T. The second character can be any 46 46 * uppercase character from A through Z excluding I. 47 * 47 * 48 48 * @param ref 49 49 * a String representing a six-figure Ordnance Survey grid reference … … 83 83 * Return a String representation of this OSGB grid reference showing the 84 84 * easting and northing. 85 * 85 * 86 86 * @return a String represenation of this OSGB grid reference 87 87 * @since 1.0 … … 95 95 * Return a String representation of this OSGB grid reference using the 96 96 * six-figure notation in the form XY123456 97 * 97 * 98 98 * @return a String representing this OSGB grid reference in six-figure 99 99 * notation … … 147 147 * OSGB36 datum. Note that, the LatLng object may need to be converted to the 148 148 * WGS84 datum depending on the application. 149 * 149 * 150 150 * @return a LatLng object representing this OSGB grid reference using the 151 151 * OSGB36 datum … … 225 225 /** 226 226 * Get the easting. 227 * 227 * 228 228 * @return the easting in metres 229 229 * @since 1.0 … … 236 236 /** 237 237 * Get the northing. 238 * 238 * 239 239 * @return the northing in metres 240 240 * @since 1.0 -
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/RefEll.java
r6775 r12778 4 4 * Class to represent a reference ellipsoid. Also provides a number of 5 5 * pre-determined reference ellipsoids as constants. 6 * 6 * 7 7 * (c) 2006 Jonathan Stott 8 * 8 * 9 9 * Created on 11-Feb-2006 10 * 10 * 11 11 * @author Jonathan Stott 12 12 * @version 1.0 … … 43 43 /** 44 44 * Create a new reference ellipsoid 45 * 45 * 46 46 * @param maj 47 47 * semi-major axis … … 59 59 /** 60 60 * Return the semi-major axis. 61 * 61 * 62 62 * @return the semi-major axis 63 63 * @since 1.0 … … 70 70 /** 71 71 * Return the semi-minor axis 72 * 72 * 73 73 * @return the semi-minor axis 74 74 * @since 1.0 … … 81 81 /** 82 82 * Return the eccentricity. 83 * 83 * 84 84 * @return the eccentricity 85 85 * @since 1.0 -
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/Test.java
r6775 r12778 4 4 * Class to illustrate the use of the various functions of the classes in the 5 5 * Jcoord package. 6 * 6 * 7 7 * (c) 2006 Jonathan Stott 8 * 8 * 9 9 * Created on 11-Feb-2006 10 * 10 * 11 11 * @author Jonathan Stott 12 12 * @version 1.0 … … 17 17 /** 18 18 * Main method 19 * 19 * 20 20 * @param args 21 21 * not used … … 26 26 /* 27 27 * Calculate Surface Distance between two Latitudes/Longitudes 28 * 28 * 29 29 * The distance() function takes a reference to a LatLng object as a 30 30 * parameter and calculates the surface distance between the the given … … 45 45 /* 46 46 * Convert OS Grid Reference to Latitude/Longitude 47 * 47 * 48 48 * Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by 49 49 * default. The majority of applications use the WGS84 datum, for which the … … 76 76 /* 77 77 * Convert Latitude/Longitude to OS Grid Reference 78 * 78 * 79 79 * Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by 80 80 * default. The majority of applications use the WGS84 datum, for which the … … 107 107 /* 108 108 * Convert Six-Figure OS Grid Reference String to an OSRef Object 109 * 109 * 110 110 * To convert a string representing a six-figure OSGB grid reference: 111 111 */ -
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/UTMRef.java
r6775 r12778 3 3 /** 4 4 * Class to represent a UTM reference 5 * 5 * 6 6 * (c) 2006 Jonathan Stott 7 * 7 * 8 8 * Created on 11-Feb-2006 9 * 9 * 10 10 * @author Jonathan Stott 11 11 * @version 1.0 … … 37 37 /** 38 38 * Create a new UTM reference object. 39 * 39 * 40 40 * @param easting 41 41 * the easting … … 58 58 /** 59 59 * Convert this UTM reference to a latitude and longitude. 60 * 60 * 61 61 * @return the converted latitude and longitude 62 62 * @since 1.0 … … 130 130 /** 131 131 * Work out the UTM latitude zone from the latitude. 132 * 132 * 133 133 * @param latitude 134 134 * the latitude to find the UTM latitude zone for … … 184 184 /** 185 185 * Convert this UTM reference to a String representation for printing out. 186 * 186 * 187 187 * @return a String representation of this UTM reference 188 188 * @since 1.0 … … 196 196 /** 197 197 * Get the easting. 198 * 198 * 199 199 * @return the easting 200 200 * @since 1.0 … … 207 207 /** 208 208 * Get the northing. 209 * 209 * 210 210 * @return the northing 211 211 * @since 1.0 … … 218 218 /** 219 219 * Get the latitude zone character. 220 * 220 * 221 221 * @return the latitude zone character 222 222 * @since 1.0 … … 229 229 /** 230 230 * Get the longitude zone number. 231 * 231 * 232 232 * @return the longitude zone number 233 233 * @since 1.0 -
applications/editors/josm/plugins/wmsplugin/src/uk/me/jstott/jcoord/Util.java
r6775 r12778 3 3 /** 4 4 * Some utility functions used by classes in the uk.me.jstott.jcoord package. 5 * 5 * 6 6 * (c) 2006 Jonathan Stott 7 * 7 * 8 8 * Created on 11-Feb-2006 9 * 9 * 10 10 * @author Jonathan Stott 11 11 * @version 1.0 … … 16 16 /** 17 17 * Calculate sin^2(x). 18 * 18 * 19 19 * @param x 20 20 * x … … 29 29 /** 30 30 * Calculate cos^2(x). 31 * 31 * 32 32 * @param x 33 33 * x … … 42 42 /** 43 43 * Calculate tan^2(x). 44 * 44 * 45 45 * @param x 46 46 * x … … 55 55 /** 56 56 * Calculate sec(x). 57 * 57 * 58 58 * @param x 59 59 * x -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/GeorefImage.java
r12438 r12778 15 15 16 16 public class GeorefImage implements Serializable { 17 18 19 17 public BufferedImage image = null; 18 public EastNorth min, max; 19 public boolean downloadingStarted; 20 20 21 22 23 21 public GeorefImage(boolean downloadingStarted) { 22 this.downloadingStarted = downloadingStarted; 23 } 24 24 25 26 27 28 25 public boolean contains(EastNorth en, double dx, double dy) { 26 return min.east()+dx <= en.east() && en.east() <= max.east()+dx 27 && min.north()+dy <= en.north() && en.north() <= max.north()+dy; 28 } 29 29 30 31 32 33 30 /* this does not take dx and dy offset into account! */ 31 public boolean isVisible(NavigatableComponent nc) { 32 Point minPt = nc.getPoint(min), maxPt = nc.getPoint(max); 33 Graphics g = nc.getGraphics(); 34 34 35 36 37 35 return (g.hitClip(minPt.x, maxPt.y, 36 maxPt.x - minPt.x, minPt.y - maxPt.y)); 37 } 38 38 39 40 39 public boolean paint(Graphics g, NavigatableComponent nc, double dx, double dy) { 40 if (image == null || min == null || max == null) return false; 41 41 42 43 44 42 EastNorth mi = new EastNorth(min.east()+dx, min.north()+dy); 43 EastNorth ma = new EastNorth(max.east()+dx, max.north()+dy); 44 Point minPt = nc.getPoint(mi), maxPt = nc.getPoint(ma); 45 45 46 47 48 49 46 /* this is isVisible() but taking dx, dy into account */ 47 if(!(g.hitClip(minPt.x, maxPt.y, 48 maxPt.x - minPt.x, minPt.y - maxPt.y))) 49 return false; 50 50 51 52 53 54 51 g.drawImage(image, 52 minPt.x, maxPt.y, maxPt.x, minPt.y, // dest 53 0, 0, image.getWidth(), image.getHeight(), // src 54 null); 55 55 56 57 56 return true; 57 } 58 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 59 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 60 max = (EastNorth) in.readObject(); 61 min = (EastNorth) in.readObject(); 62 image = (BufferedImage) ImageIO.read(ImageIO.createImageInputStream(in)); 63 } 64 65 private void writeObject(ObjectOutputStream out) throws IOException { 66 out.writeObject(max); 67 out.writeObject(min); 68 if(image == null) 69 out.writeObject(null); 70 else 71 ImageIO.write(image, "png", ImageIO.createImageOutputStream(out)); 72 } 73 73 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java
r11301 r12778 14 14 15 15 abstract public class Grabber implements Runnable { 16 17 18 19 20 21 16 protected Bounds b; 17 protected Projection proj; 18 protected double pixelPerDegree; 19 protected MapView mv; 20 protected WMSLayer layer; 21 protected GeorefImage image; 22 22 23 24 25 26 27 28 29 30 31 23 Grabber(Bounds b, Projection proj, 24 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 25 this.b = b; 26 this.proj = proj; 27 this.pixelPerDegree = pixelPerDegree; 28 this.image = image; 29 this.mv = mv; 30 this.layer = layer; 31 } 32 32 33 33 abstract void fetch() throws Exception; // the image fetch code 34 34 35 36 37 38 39 40 35 int width(){ 36 return (int) ((b.max.lon() - b.min.lon()) * pixelPerDegree); 37 } 38 int height(){ 39 return (int) ((b.max.lat() - b.min.lat()) * pixelPerDegree); 40 } 41 41 42 protected void grabError(Exception e){ // report error when grabing image 43 e.printStackTrace(); 44 45 BufferedImage img = new BufferedImage(width(), height(), BufferedImage.TYPE_INT_ARGB); 46 Graphics g = img.getGraphics(); 47 g.setColor(Color.RED); 48 g.fillRect(0, 0, width(), height()); 49 Font font = g.getFont(); 50 Font tempFont = font.deriveFont(Font.PLAIN).deriveFont(36.0f); 51 g.setFont(tempFont); 52 g.setColor(Color.BLACK); 53 g.drawString(tr("Exception occurred"), 10, height()/2); 54 image.image = img; 55 g.setFont(font); 56 } 42 protected void grabError(Exception e){ // report error when grabing image 43 e.printStackTrace(); 57 44 58 protected void attempt(){ // try to fetch the image 59 int maxTries = 5; // n tries for every image 60 for (int i = 1; i <= maxTries; i++) { 61 try { 62 fetch(); 63 break; // break out of the retry loop 64 } catch (Exception e) { 65 try { // sleep some time and then ask the server again 66 Thread.sleep(random(1000, 2000)); 67 } catch (InterruptedException e1) {} 45 BufferedImage img = new BufferedImage(width(), height(), BufferedImage.TYPE_INT_ARGB); 46 Graphics g = img.getGraphics(); 47 g.setColor(Color.RED); 48 g.fillRect(0, 0, width(), height()); 49 Font font = g.getFont(); 50 Font tempFont = font.deriveFont(Font.PLAIN).deriveFont(36.0f); 51 g.setFont(tempFont); 52 g.setColor(Color.BLACK); 53 g.drawString(tr("Exception occurred"), 10, height()/2); 54 image.image = img; 55 g.setFont(font); 56 } 68 57 69 if(i == maxTries) grabError(e); 70 } 71 } 72 } 58 protected void attempt(){ // try to fetch the image 59 int maxTries = 5; // n tries for every image 60 for (int i = 1; i <= maxTries; i++) { 61 try { 62 fetch(); 63 break; // break out of the retry loop 64 } catch (Exception e) { 65 try { // sleep some time and then ask the server again 66 Thread.sleep(random(1000, 2000)); 67 } catch (InterruptedException e1) {} 73 68 74 public static int random(int min, int max) { 75 return (int)(Math.random() * ((max+1)-min) ) + min; 76 } 69 if(i == maxTries) grabError(e); 70 } 71 } 72 } 73 74 public static int random(int min, int max) { 75 return (int)(Math.random() * ((max+1)-min) ) + min; 76 } 77 77 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Help_WMSmenuAction.java
r12707 r12778 11 11 public class Help_WMSmenuAction extends JosmAction { 12 12 13 14 15 16 13 /** 14 * 15 */ 16 17 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 18 public Help_WMSmenuAction() { 19 //super("Help / About"); 20 super(tr("help"), "help", tr("Help / About"), null, false); 21 22 } 23 24 public void actionPerformed(ActionEvent e) { 25 //todo - put this into a txt file? 26 String helptext = 27 tr("You can add, edit and delete WMS entries in the WMSplugin Preference Tab - " + 28 "these will then show up in the WMS menu.\n\n"+ 29 30 "You can also do this manually in the Advanced Preferences, using the following schema:\n"+ 31 "wmsplugin.url.1.name=Landsat\n"+ 32 "wmsplugin.url.1.url=http://onearth.jpl.nasa.gov....\n"+ 33 "wmsplugin.url.2.name=NPE Maps... etc\n\n"+ 34 35 "Full WMS URL input format example (landsat)\n"+ 36 "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&\n"+ 37 "layers=global_mosaic&styles=&srs=EPSG:4326&\n"+ 38 "format=image/jpeg \n\n"+ 39 40 "For Metacarta's Map Rectifier http://labs.metacarta.com/rectifier/ , you only need to input the relevant 'id'.\n" + 41 "To add a Metacarta Map Rectifier menu item, manually create the URL like in this example," + 42 "replacing 73 with your image id: \n" + 43 "http://labs.metacarta.com/rectifier/wms.cgi?id=73\n" + 44 "&srs=EPSG:4326&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png\n" + 45 " \n" + 46 "Note: Make sure the image is suitable, copyright-wise, if in doubt, don't use."); 47 48 JTextPane tp = new JTextPane(); 49 JScrollPane js = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 50 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 51 52 53 js.getViewport().add(tp); 54 JFrame jf = new JFrame(tr("WMS Plugin Help")); 55 jf.getContentPane().add(js); 56 jf.pack(); 57 jf.setSize(400,500); 58 jf.setVisible(true); 59 tp.setText(helptext); 60 } 61 61 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Map_Rectifier_WMSmenuAction.java
r12707 r12778 12 12 public class Map_Rectifier_WMSmenuAction extends JosmAction { 13 13 14 15 16 17 14 /** 15 * tim waters "chippy" 16 */ 17 private static final long serialVersionUID = 1L; 18 18 19 20 21 19 public Map_Rectifier_WMSmenuAction() { 20 super(tr("Rectified Image ..."), "OLmarker", tr("Download Rectified Image from Metacarta's Map Rectifier WMS"), null, false); 21 } 22 22 23 24 25 23 public void actionPerformed(ActionEvent e) { 24 String newid = JOptionPane.showInputDialog(Main.parent, tr("Metacarta Map Rectifier image id"), 25 Main.pref.get("wmsplugin.rectifier_id")); 26 26 27 28 29 27 if (newid != null && !newid.equals("")) { 28 String newURL = "http://labs.metacarta.com/rectifier/wms.cgi?id="+newid+ 29 "&srs=EPSG:4326&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png"; 30 30 31 32 33 34 35 31 Main.pref.put("wmsplugin.rectifier_id", newid); 32 WMSLayer wmsLayer = new WMSLayer(tr("rectifier id={0}",newid), newURL); 33 Main.main.addLayer(wmsLayer); 34 } 35 } 36 36 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/OSGBGrabber.java
r10382 r12778 15 15 // FIXME: Remove this hack when we have proper projection support. 16 16 public class OSGBGrabber extends WMSGrabber { 17 18 19 17 public OSGBGrabber(String baseURL) { 18 super(baseURL); 19 } 20 20 21 21 private Epsg4326 latlonProj = new Epsg4326(); 22 22 23 24 25 26 27 28 23 @Override public GeorefImage grab(Bounds b, Projection proj, 24 double pixelPerDegree) throws IOException { 25 Bounds bnew = toOSGB(b); 26 double pixelPerDegreeNew = 27 pixelPerDegree / (bnew.max.lon() - bnew.min.lon()) 28 * (b.max.lon() - b.min.lon()); 29 29 30 30 GeorefImage img = super.grab(bnew, latlonProj, pixelPerDegreeNew); 31 31 32 33 32 img.min = proj.latlon2eastNorth(fromOSGB(img.min)); 33 img.max = proj.latlon2eastNorth(fromOSGB(img.max)); 34 34 35 36 35 return img; 36 } 37 37 38 39 40 41 42 43 38 protected static Bounds toOSGB(Bounds b) { 39 LatLng[] lls = new LatLng[] { 40 new LatLng(b.min.lat(), b.min.lon()), 41 new LatLng(b.min.lat(), b.max.lon()), 42 new LatLng(b.max.lat(), b.min.lon()), 43 new LatLng(b.max.lat(), b.max.lon()) }; 44 44 45 45 for (LatLng ll : lls) ll.toOSGB36(); 46 46 47 48 47 OSRef[] grs = new OSRef[lls.length]; 48 for (int i = 0; i < lls.length; i++) grs[i] = lls[i].toOSRef(); 49 49 50 51 52 53 50 LatLon latlon = new LatLon(grs[0].getNorthing(), grs[0].getEasting()); 51 Bounds bnew = new Bounds(latlon, latlon); 52 for (int i = 1; i < grs.length; i++) 53 bnew.extend(new LatLon(grs[i].getNorthing(), grs[i].getEasting())); 54 54 55 56 55 return bnew; 56 } 57 57 58 59 60 61 62 58 protected static LatLon fromOSGB(EastNorth en) { 59 LatLng ll = new OSRef(en.east(), en.north()).toLatLng(); 60 ll.toWGS84(); 61 return new LatLon(ll.getLat(), ll.getLng()); 62 } 63 63 } 64 64 */ -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSAdjustAction.java
r12438 r12778 17 17 18 18 public class WMSAdjustAction extends MapMode implements 19 19 MouseListener, MouseMotionListener{ 20 20 21 22 23 24 21 GeorefImage selectedImage; 22 WMSLayer selectedLayer; 23 boolean mouseDown; 24 EastNorth prevEastNorth; 25 25 26 27 28 29 30 26 public WMSAdjustAction(MapFrame mapFrame) { 27 super(tr("Adjust WMS"), "adjustwms", 28 tr("Adjust the position of the WMS layer"), mapFrame, 29 ImageProvider.getCursor("normal", "move")); 30 } 31 31 32 33 34 35 36 32 @Override public void enterMode() { 33 super.enterMode(); 34 Main.map.mapView.addMouseListener(this); 35 Main.map.mapView.addMouseMotionListener(this); 36 } 37 37 38 39 40 41 42 38 @Override public void exitMode() { 39 super.exitMode(); 40 Main.map.mapView.removeMouseListener(this); 41 Main.map.mapView.removeMouseMotionListener(this); 42 } 43 43 44 45 46 44 @Override public void mousePressed(MouseEvent e) { 45 if (e.getButton() != MouseEvent.BUTTON1) 46 return; 47 47 48 49 50 51 52 53 54 55 56 57 58 59 48 for(Layer layer:Main.map.mapView.getAllLayers()) { 49 if (layer.visible && layer instanceof WMSLayer) { 50 prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY()); 51 selectedLayer = ((WMSLayer)layer); 52 selectedImage = selectedLayer.findImage(prevEastNorth); 53 if(selectedImage!=null){ 54 Main.map.mapView.setCursor 55 (Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 56 } 57 } 58 } 59 } 60 60 61 62 63 64 65 61 @Override public void mouseDragged(MouseEvent e) { 62 /* 63 if (e.getButton() != MouseEvent.BUTTON1) 64 return; 65 */ 66 66 67 68 69 70 71 72 73 74 75 67 if(selectedImage!=null) { 68 EastNorth eastNorth= 69 Main.map.mapView.getEastNorth(e.getX(),e.getY()); 70 selectedLayer.displace(eastNorth.east()-prevEastNorth.east(), 71 eastNorth.north()-prevEastNorth.north()); 72 prevEastNorth = eastNorth; 73 Main.map.mapView.repaint(); 74 } 75 } 76 76 77 78 79 80 81 82 83 77 @Override public void mouseReleased(MouseEvent e) { 78 Main.map.mapView.repaint(); 79 Main.map.mapView.setCursor(Cursor.getDefaultCursor()); 80 selectedImage = null; 81 prevEastNorth = null; 82 selectedLayer = null; 83 } 84 84 85 86 87 88 89 90 85 public void mouseEntered(MouseEvent e) { 86 } 87 public void mouseExited(MouseEvent e) { 88 } 89 public void mouseMoved(MouseEvent e) { 90 } 91 91 92 93 92 @Override public void mouseClicked(MouseEvent e) { 93 } 94 94 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSDownloadAction.java
r12187 r12778 13 13 public class WMSDownloadAction extends JosmAction { 14 14 15 private WMSInfo info; 16 17 public WMSDownloadAction(WMSInfo info) { 18 super(info.name, "wmsmenu", tr("Download WMS tile from {0}",info.name), null, false); 19 this.info = info; 20 } 21 22 public void actionPerformed(ActionEvent e) { 23 System.out.println(info.url); 24 25 WMSLayer wmsLayer = new WMSLayer(info.name, info.url); 26 Main.main.addLayer(wmsLayer); 27 } 15 private WMSInfo info; 28 16 29 public static WMSLayer getLayer(WMSInfo info) { 30 // FIXME: move this to WMSPlugin/WMSInfo/preferences. 31 WMSLayer wmsLayer = new WMSLayer(info.name, info.url); 32 Main.main.addLayer(wmsLayer); 33 return wmsLayer; 34 } 17 public WMSDownloadAction(WMSInfo info) { 18 super(info.name, "wmsmenu", tr("Download WMS tile from {0}",info.name), null, false); 19 this.info = info; 20 } 21 22 public void actionPerformed(ActionEvent e) { 23 System.out.println(info.url); 24 25 WMSLayer wmsLayer = new WMSLayer(info.name, info.url); 26 Main.main.addLayer(wmsLayer); 27 } 28 29 public static WMSLayer getLayer(WMSInfo info) { 30 // FIXME: move this to WMSPlugin/WMSInfo/preferences. 31 WMSLayer wmsLayer = new WMSLayer(info.name, info.url); 32 Main.main.addLayer(wmsLayer); 33 return wmsLayer; 34 } 35 35 }; -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java
r11400 r12778 25 25 26 26 public class WMSGrabber extends Grabber { 27 27 protected String baseURL; 28 28 29 30 31 32 33 29 WMSGrabber(String baseURL, Bounds b, Projection proj, 30 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 31 super(b, proj, pixelPerDegree, image, mv, layer); 32 this.baseURL = baseURL; 33 } 34 34 35 36 37 38 35 public void run() { 36 attempt(); 37 mv.repaint(); 38 } 39 39 40 void fetch() throws Exception{ 41 URL url = null; 42 try { 43 url = getURL( 44 b.min.lon(), b.min.lat(), 45 b.max.lon(), b.max.lat(), 46 width(), height()); 47 48 image.min = proj.latlon2eastNorth(b.min); 49 image.max = proj.latlon2eastNorth(b.max); 40 void fetch() throws Exception{ 41 URL url = null; 42 try { 43 url = getURL( 44 b.min.lon(), b.min.lat(), 45 b.max.lon(), b.max.lat(), 46 width(), height()); 50 47 51 if(image.isVisible(mv)) //don't download, if the image isn't visible already 52 image.image = grab(url); 53 image.downloadingStarted = false; 54 } catch(Exception e) { 55 throw new Exception(e.getMessage() + "\nImage couldn't be fetched: " + (url != null ? url.toString() : "")); 56 } 57 } 48 image.min = proj.latlon2eastNorth(b.min); 49 image.max = proj.latlon2eastNorth(b.max); 58 50 59 public static final NumberFormat 60 latLonFormat = new DecimalFormat("###0.0000000", 61 new DecimalFormatSymbols(Locale.US)); 51 if(image.isVisible(mv)) //don't download, if the image isn't visible already 52 image.image = grab(url); 53 image.downloadingStarted = false; 54 } catch(Exception e) { 55 throw new Exception(e.getMessage() + "\nImage couldn't be fetched: " + (url != null ? url.toString() : "")); 56 } 57 } 62 58 63 protected URL getURL(double w, double s,double e,double n, 64 int wi, int ht) throws MalformedURLException { 65 String str = baseURL; 66 if(!str.endsWith("?")) 67 str += "&"; 68 str += "bbox=" 69 + latLonFormat.format(w) + "," 70 + latLonFormat.format(s) + "," 71 + latLonFormat.format(e) + "," 72 + latLonFormat.format(n) 73 + "&width=" + wi + "&height=" + ht; 74 return new URL(str.replace(" ", "%20")); 75 } 59 public static final NumberFormat 60 latLonFormat = new DecimalFormat("###0.0000000", 61 new DecimalFormatSymbols(Locale.US)); 76 62 77 protected BufferedImage grab(URL url) throws IOException { 78 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 79 80 String contentType = conn.getHeaderField("Content-Type"); 81 if( conn.getResponseCode() != 200 82 || contentType != null && !contentType.startsWith("image") ) { 83 throw new IOException(readException(conn)); 84 } 85 86 InputStream is = new ProgressInputStream(conn, null); 87 BufferedImage img = ImageIO.read(is); 88 is.close(); 89 return img; 90 } 63 protected URL getURL(double w, double s,double e,double n, 64 int wi, int ht) throws MalformedURLException { 65 String str = baseURL; 66 if(!str.endsWith("?")) 67 str += "&"; 68 str += "bbox=" 69 + latLonFormat.format(w) + "," 70 + latLonFormat.format(s) + "," 71 + latLonFormat.format(e) + "," 72 + latLonFormat.format(n) 73 + "&width=" + wi + "&height=" + ht; 74 return new URL(str.replace(" ", "%20")); 75 } 91 76 92 protected String readException(URLConnection conn) throws IOException { 93 StringBuilder exception = new StringBuilder(); 94 InputStream in = conn.getInputStream(); 95 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 96 97 String line = null; 98 while( (line = br.readLine()) != null) { 99 exception.append(line); 100 exception.append('\n'); 101 } 102 return exception.toString(); 103 } 77 protected BufferedImage grab(URL url) throws IOException { 78 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 79 80 String contentType = conn.getHeaderField("Content-Type"); 81 if( conn.getResponseCode() != 200 82 || contentType != null && !contentType.startsWith("image") ) { 83 throw new IOException(readException(conn)); 84 } 85 86 InputStream is = new ProgressInputStream(conn, null); 87 BufferedImage img = ImageIO.read(is); 88 is.close(); 89 return img; 90 } 91 92 protected String readException(URLConnection conn) throws IOException { 93 StringBuilder exception = new StringBuilder(); 94 InputStream in = conn.getInputStream(); 95 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 96 97 String line = null; 98 while( (line = br.readLine()) != null) { 99 exception.append(line); 100 exception.append('\n'); 101 } 102 return exception.toString(); 103 } 104 104 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSInfo.java
r10767 r12778 5 5 /** 6 6 * Class that stores info about a WMS server. 7 * 7 * 8 8 * @author Frederik Ramm <frederik@remote.org> 9 9 */ 10 10 public class WMSInfo implements Comparable { 11 12 String name;13 String url;14 int prefid;15 16 public WMSInfo(String name, String url, int prefid) {17 this.name=name; this.url=url; this.prefid=prefid;18 }19 11 20 21 public void save() { 22 Main.pref.put("wmsplugin.url." + prefid + ".name", name); 23 Main.pref.put("wmsplugin.url." + prefid + ".url", url); 24 } 25 public int compareTo(Object c) 26 { 27 Integer i = 0; 28 if(c instanceof WMSInfo) 29 { 30 WMSInfo in = (WMSInfo)c; 31 i = name.compareTo(in.name); 32 if(i == 0) 33 i = url.compareTo(in.url); 34 if(i == 0) 35 i = prefid-in.prefid; 36 } 37 return i; 38 } 12 String name; 13 String url; 14 int prefid; 15 16 public WMSInfo(String name, String url, int prefid) { 17 this.name=name; this.url=url; this.prefid=prefid; 18 } 19 20 21 public void save() { 22 Main.pref.put("wmsplugin.url." + prefid + ".name", name); 23 Main.pref.put("wmsplugin.url." + prefid + ".url", url); 24 } 25 public int compareTo(Object c) 26 { 27 Integer i = 0; 28 if(c instanceof WMSInfo) 29 { 30 WMSInfo in = (WMSInfo)c; 31 i = name.compareTo(in.name); 32 if(i == 0) 33 i = url.compareTo(in.url); 34 if(i == 0) 35 i = prefid-in.prefid; 36 } 37 return i; 38 } 39 39 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
r12707 r12778 45 45 public class WMSLayer extends Layer { 46 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 } 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 47 protected static final Icon icon = 48 new ImageIcon(Toolkit.getDefaultToolkit().createImage(WMSPlugin.class.getResource("/images/wms_small.png"))); 49 50 public int messageNum = 5; //limit for messages per layer 51 protected MapView mv; 52 protected String resolution; 53 protected boolean stopAfterPaint = false; 54 protected int ImageSize = 500; 55 protected int dax = 10; 56 protected int day = 10; 57 protected int minZoom = 3; 58 protected double dx = 0.0; 59 protected double dy = 0.0; 60 protected double pixelPerDegree; 61 protected GeorefImage[][] images = new GeorefImage[dax][day]; 62 JCheckBoxMenuItem startstop = new JCheckBoxMenuItem(tr("Automatic downloading"), true); 63 64 protected String baseURL; 65 protected final int serializeFormatVersion = 4; 66 67 private ExecutorService executor; 68 69 public WMSLayer() { 70 this(tr("Blank Layer"), null); 71 initializeImages(); 72 mv = Main.map.mapView; 73 } 74 75 public WMSLayer(String name, String baseURL) { 76 super(name); 77 background = true; /* set global background variable */ 78 initializeImages(); 79 this.baseURL = baseURL; 80 mv = Main.map.mapView; 81 getPPD(); 82 83 executor = Executors.newFixedThreadPool(3); 84 } 85 86 public void getPPD(){ 87 pixelPerDegree = mv.getWidth() / (bounds().max.lon() - bounds().min.lon()); 88 } 89 90 public void initializeImages() { 91 images = new GeorefImage[dax][day]; 92 for(int x = 0; x<dax; ++x) 93 for(int y = 0; y<day; ++y) 94 images[x][y]= new GeorefImage(false); 95 } 96 97 @Override public Icon getIcon() { 98 return icon; 99 } 100 101 public String scale(){ 102 LatLon ll1 = mv.getLatLon(0,0); 103 LatLon ll2 = mv.getLatLon(100,0); 104 double dist = ll1.greatCircleDistance(ll2); 105 return dist > 1000 ? (Math.round(dist/100)/10.0)+" km" : Math.round(dist*10)/10+" m"; 106 } 107 108 @Override public String getToolTipText() { 109 if(startstop.isSelected()) 110 return tr("WMS layer ({0}), automatically downloading in zoom {1}", name, resolution); 111 else 112 return tr("WMS layer ({0}), downloading in zoom {1}", name, resolution); 113 } 114 115 @Override public boolean isMergable(Layer other) { 116 return false; 117 } 118 119 @Override public void mergeFrom(Layer from) { 120 } 121 122 private Bounds XYtoBounds (int x, int y) { 123 return new Bounds( 124 new LatLon( x * ImageSize / pixelPerDegree, 125 y * ImageSize / pixelPerDegree), 126 new LatLon((x + 1) * ImageSize / pixelPerDegree, 127 (y + 1) * ImageSize / pixelPerDegree)); 128 } 129 130 private int modulo (int a, int b) { 131 if(a%b>=0)return a%b; 132 else return a%b+b; 133 } 134 135 protected Bounds bounds(){ 136 return new Bounds( 137 mv.getLatLon(0, mv.getHeight()), 138 mv.getLatLon(mv.getWidth(), 0)); 139 } 140 141 @Override public void paint(Graphics g, final MapView mv) { 142 if(baseURL == null) return; 143 144 if( !startstop.isSelected() || (pixelPerDegree / (mv.getWidth() / (bounds().max.lon() - bounds().min.lon())) > minZoom) ){ //don't download when it's too outzoomed 145 for(int x = 0; x<dax; ++x) 146 for(int y = 0; y<day; ++y) 147 images[modulo(x,dax)][modulo(y,day)].paint(g, mv, dx, dy); 148 } else 149 downloadAndPaintVisible(g, mv); 150 } 151 152 public void displace(double dx, double dy) { 153 this.dx += dx; 154 this.dy += dy; 155 } 156 157 protected void downloadAndPaintVisible(Graphics g, final MapView mv){ 158 int bminx= (int)Math.floor ((bounds().min.lat() * pixelPerDegree ) / ImageSize ); 159 int bminy= (int)Math.floor ((bounds().min.lon() * pixelPerDegree ) / ImageSize ); 160 int bmaxx= (int)Math.ceil ((bounds().max.lat() * pixelPerDegree ) / ImageSize ); 161 int bmaxy= (int)Math.ceil ((bounds().max.lon() * pixelPerDegree ) / ImageSize ); 162 163 if((bmaxx - bminx > dax) || (bmaxy - bminy > day)){ 164 JOptionPane.showMessageDialog(Main.parent, tr("The requested area is too big. Please zoom in a little, or change resolution")); 165 return; 166 } 167 168 for(int x = bminx; x<bmaxx; ++x) 169 for(int y = bminy; y<bmaxy; ++y){ 170 GeorefImage img = images[modulo(x,dax)][modulo(y,day)]; 171 if(!img.paint(g, mv, dx, dy) && !img.downloadingStarted){ 172 img.downloadingStarted = true; 173 img.image = null; 174 Grabber gr = WMSPlugin.getGrabber(baseURL, XYtoBounds(x,y), Main.main.proj, pixelPerDegree, img, mv, this); 175 executor.submit(gr); 176 } 177 } 178 } 179 180 @Override public void visitBoundingBox(BoundingXYVisitor v) { 181 for(int x = 0; x<dax; ++x) 182 for(int y = 0; y<day; ++y) 183 if(images[x][y].image!=null){ 184 v.visit(images[x][y].min); 185 v.visit(images[x][y].max); 186 } 187 } 188 189 @Override public Object getInfoComponent() { 190 return getToolTipText(); 191 } 192 193 @Override public Component[] getMenuEntries() { 194 return new Component[]{ 195 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 196 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 197 new JSeparator(), 198 new JMenuItem(new LoadWmsAction()), 199 new JMenuItem(new SaveWmsAction()), 200 new JSeparator(), 201 startstop, 202 new JMenuItem(new changeResolutionAction()), 203 new JMenuItem(new downloadAction()), 204 new JSeparator(), 205 new JMenuItem(new LayerListPopup.InfoAction(this)) 206 }; 207 } 208 209 public GeorefImage findImage(EastNorth eastNorth) { 210 for(int x = 0; x<dax; ++x) 211 for(int y = 0; y<day; ++y) 212 if(images[x][y].image!=null && images[x][y].min!=null && images[x][y].max!=null) 213 if(images[x][y].contains(eastNorth, dx, dy)) 214 return images[x][y]; 215 return null; 216 } 217 218 public class downloadAction extends AbstractAction { 219 public downloadAction() { 220 super(tr("Download visible tiles")); 221 } 222 public void actionPerformed(ActionEvent ev) { 223 downloadAndPaintVisible(mv.getGraphics(), mv); 224 } 225 } 226 227 public class changeResolutionAction extends AbstractAction { 228 public changeResolutionAction() { 229 super(tr("Change resolution")); 230 } 231 public void actionPerformed(ActionEvent ev) { 232 initializeImages(); 233 resolution = scale(); 234 getPPD(); 235 mv.repaint(); 236 } 237 } 238 239 public class SaveWmsAction extends AbstractAction { 240 public SaveWmsAction() { 241 super(tr("Save WMS layer to file"), ImageProvider.get("save")); 242 } 243 public void actionPerformed(ActionEvent ev) { 244 File f = openFileDialog(false); 245 try 246 { 247 FileOutputStream fos = new FileOutputStream(f); 248 ObjectOutputStream oos = new ObjectOutputStream(fos); 249 oos.writeInt(serializeFormatVersion); 250 oos.writeInt(dax); 251 oos.writeInt(day); 252 oos.writeInt(ImageSize); 253 oos.writeDouble(pixelPerDegree); 254 oos.writeObject(name); 255 oos.writeObject(baseURL); 256 oos.writeObject(images); 257 oos.close(); 258 fos.close(); 259 } 260 catch (Exception ex) { 261 ex.printStackTrace(System.out); 262 } 263 } 264 } 265 266 public class LoadWmsAction extends AbstractAction { 267 public LoadWmsAction() { 268 super(tr("Load WMS layer from file"), ImageProvider.get("load")); 269 } 270 public void actionPerformed(ActionEvent ev) { 271 File f = openFileDialog(true); 272 if (f == null) return; 273 try 274 { 275 FileInputStream fis = new FileInputStream(f); 276 ObjectInputStream ois = new ObjectInputStream(fis); 277 int sfv = ois.readInt(); 278 if (sfv != serializeFormatVersion) { 279 JOptionPane.showMessageDialog(Main.parent, 280 tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion), 281 tr("File Format Error"), 282 JOptionPane.ERROR_MESSAGE); 283 return; 284 } 285 startstop.setSelected(false); 286 dax = ois.readInt(); 287 day = ois.readInt(); 288 ImageSize = ois.readInt(); 289 pixelPerDegree = ois.readDouble(); 290 name = (String) ois.readObject(); 291 baseURL = (String) ois.readObject(); 292 images = (GeorefImage[][])ois.readObject(); 293 ois.close(); 294 fis.close(); 295 mv.repaint(); 296 } 297 catch (Exception ex) { 298 // FIXME be more specific 299 ex.printStackTrace(System.out); 300 JOptionPane.showMessageDialog(Main.parent, 301 tr("Error loading file"), 302 tr("Error"), 303 JOptionPane.ERROR_MESSAGE); 304 return; 305 } 306 } 307 } 308 309 protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple) { 310 String curDir = Main.pref.get("lastDirectory"); 311 if (curDir.equals("")) 312 curDir = "."; 313 JFileChooser fc = new JFileChooser(new File(curDir)); 314 fc.setMultiSelectionEnabled(multiple); 315 for (int i = 0; i < ExtensionFileFilter.filters.length; ++i) 316 fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]); 317 fc.setAcceptAllFileFilterUsed(true); 318 319 int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent); 320 if (answer != JFileChooser.APPROVE_OPTION) 321 return null; 322 323 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) 324 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 325 326 if (!open) { 327 File file = fc.getSelectedFile(); 328 if (file == null || (file.exists() && JOptionPane.YES_OPTION != 329 JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION))) 330 return null; 331 } 332 333 return fc; 334 } 335 336 public static File openFileDialog(boolean open) { 337 JFileChooser fc = createAndOpenFileChooser(open, false); 338 if (fc == null) 339 return null; 340 341 File file = fc.getSelectedFile(); 342 343 String fn = file.getPath(); 344 if (fn.indexOf('.') == -1) { 345 FileFilter ff = fc.getFileFilter(); 346 if (ff instanceof ExtensionFileFilter) 347 fn = "." + ((ExtensionFileFilter)ff).defaultExtension; 348 else 349 fn += ".osm"; 350 file = new File(fn); 351 } 352 return file; 353 } 354 354 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPlugin.java
r12472 r12778 39 39 public class WMSPlugin extends Plugin { 40 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 else 193 194 195 196 //return new OSGBGrabber(_b, _proj, _pixelPerDegree, _images, _mv, _layer);197 198 //return new WMSGrabber(_b, _proj, _pixelPerDegree, _images, _mv, _layer);199 200 201 202 203 204 205 206 207 41 WMSLayer wmsLayer; 42 static JMenu wmsJMenu; 43 44 static ArrayList<WMSInfo> wmsList = new ArrayList<WMSInfo>(); 45 static TreeMap<String,String> wmsListDefault = new TreeMap<String,String>(); 46 47 // remember state of menu item to restore on changed preferences 48 static private boolean menuEnabled = false; 49 50 public WMSPlugin() { 51 try 52 { 53 copy("/resources/ymap.html", "ymap.html"); 54 } 55 catch(IOException e) { 56 e.printStackTrace(); 57 } 58 refreshMenu(); 59 } 60 61 // this parses the preferences settings. preferences for the wms plugin have to 62 // look like this: 63 // wmsplugin.1.name=Landsat 64 // wmsplugin.1.url=http://and.so.on/ 65 66 public void copy(String from, String to) throws FileNotFoundException, IOException 67 { 68 File pluginDir = new File(Main.pref.getPreferencesDir() + "plugins/wmsplugin/"); 69 if (!pluginDir.exists()) 70 pluginDir.mkdirs(); 71 FileOutputStream out = new FileOutputStream(Main.pref.getPreferencesDir() + "plugins/wmsplugin/" + to); 72 InputStream in = WMSPlugin.class.getResourceAsStream(from); 73 byte[] buffer = new byte[8192]; 74 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) 75 out.write(buffer, 0, len); 76 in.close(); 77 out.close(); 78 } 79 80 81 public static void refreshMenu() { 82 wmsList.clear(); 83 Map<String,String> prefs = Main.pref.getAllPrefix("wmsplugin.url."); 84 85 TreeSet<String> keys = new TreeSet<String>(prefs.keySet()); 86 int prefid = 0; 87 String name = null; 88 String url = null; 89 int lastid = -1; 90 for (String key : keys) { 91 String[] elements = key.split("\\."); 92 if (elements.length != 4) continue; 93 try { 94 prefid = Integer.parseInt(elements[2]); 95 } catch(NumberFormatException e) { 96 continue; 97 } 98 if (prefid != lastid) { 99 name = url = null; lastid = prefid; 100 } 101 if (elements[3].equals("name")) 102 name = prefs.get(key); 103 else if (elements[3].equals("url")) 104 url = prefs.get(key); 105 if (name != null && url != null) 106 wmsList.add(new WMSInfo(name, url, prefid)); 107 } 108 String source = "http://svn.openstreetmap.org/applications/editors/josm/plugins/wmsplugin/sources.cfg"; 109 try 110 { 111 MirroredInputStream s = new MirroredInputStream(source, 112 Main.pref.getPreferencesDir() + "plugins/wmsplugin/", -1); 113 InputStreamReader r; 114 try 115 { 116 r = new InputStreamReader(s, "UTF-8"); 117 } 118 catch (UnsupportedEncodingException e) 119 { 120 r = new InputStreamReader(s); 121 } 122 BufferedReader reader = new BufferedReader(r); 123 String line; 124 while((line = reader.readLine()) != null) 125 { 126 String val[] = line.split(";"); 127 if(!line.startsWith("#") && val.length == 3) 128 setDefault("true".equals(val[0]), tr(val[1]), val[2]); 129 } 130 } 131 catch (IOException e) 132 { 133 } 134 135 Collections.sort(wmsList); 136 MainMenu menu = Main.main.menu; 137 138 if (wmsJMenu == null) { 139 wmsJMenu = new JMenu(tr("WMS")); 140 menu.add(wmsJMenu, KeyEvent.VK_W, "wms"); 141 menu.add(wmsJMenu, 5); 142 } else { 143 wmsJMenu.removeAll(); 144 } 145 146 // for each configured WMSInfo, add a menu entry. 147 for (final WMSInfo u : wmsList) { 148 wmsJMenu.add(new JMenuItem(new WMSDownloadAction(u))); 149 } 150 wmsJMenu.addSeparator(); 151 wmsJMenu.add(new JMenuItem(new Map_Rectifier_WMSmenuAction())); 152 153 wmsJMenu.addSeparator(); 154 wmsJMenu.add(new JMenuItem(new 155 JosmAction(tr("Blank Layer"), "blankmenu", tr("Open a blank WMS layer to load data from a file"), null, false) { 156 public void actionPerformed(ActionEvent ev) { 157 Main.main.addLayer(new WMSLayer()); 158 } 159 })); 160 wmsJMenu.addSeparator(); 161 wmsJMenu.add(new JMenuItem(new Help_WMSmenuAction())); 162 setEnabledAll(menuEnabled); 163 } 164 165 /* add a default entry in case the URL does not yet exist */ 166 private static void setDefault(Boolean force, String name, String url) 167 { 168 String testurl = url.replaceAll("=", "_"); 169 wmsListDefault.put(name, url); 170 171 if(force && !Main.pref.getBoolean("wmsplugin.default."+testurl)) 172 { 173 Main.pref.put("wmsplugin.default."+testurl, true); 174 int id = -1; 175 for(WMSInfo i : wmsList) 176 { 177 if(url.equals(i.url)) 178 return; 179 if(i.prefid > id) 180 id = i.prefid; 181 } 182 WMSInfo newinfo = new WMSInfo(name, url, id+1); 183 newinfo.save(); 184 wmsList.add(newinfo); 185 } 186 } 187 188 public static Grabber getGrabber(String _baseURL, Bounds _b, Projection _proj, 189 double _pixelPerDegree, GeorefImage _image, MapView _mv, WMSLayer _layer){ 190 if(_baseURL.startsWith("yahoo://")) 191 return new YAHOOGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer); 192 else 193 return new WMSGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer); 194 // OSBGrabber should be rewrite for thread support first 195 //if (wmsurl.matches("(?i).*layers=npeoocmap.*") || wmsurl.matches("(?i).*layers=npe.*") ){ 196 // return new OSGBGrabber(_b, _proj, _pixelPerDegree, _images, _mv, _layer); 197 //} else { 198 // return new WMSGrabber(_b, _proj, _pixelPerDegree, _images, _mv, _layer); 199 //} 200 } 201 202 private static void setEnabledAll(boolean isEnabled) { 203 for(int i=0; i < wmsJMenu.getItemCount(); i++) { 204 JMenuItem item = wmsJMenu.getItem(i); 205 206 if(item != null) item.setEnabled(isEnabled); 207 } 208 208 menuEnabled = isEnabled; 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 209 } 210 211 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 212 if (oldFrame==null && newFrame!=null) { 213 setEnabledAll(true); 214 Main.map.addMapMode(new IconToggleButton 215 (new WMSAdjustAction(Main.map))); 216 } else if (oldFrame!=null && newFrame==null ) { 217 setEnabledAll(false); 218 } 219 } 220 221 public PreferenceSetting getPreferenceSetting() { 222 return new WMSPreferenceEditor(); 223 } 224 224 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPreferenceEditor.java
r12588 r12778 27 27 28 28 public class WMSPreferenceEditor implements PreferenceSetting { 29 30 private Map<String,String> orig;31 private DefaultTableModel model;32 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>();33 29 34 public void addGui(final PreferenceDialog gui) { 35 JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu")); 36 37 model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0); 38 final JTable list = new JTable(model); 39 JScrollPane scroll = new JScrollPane(list); 40 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 41 scroll.setPreferredSize(new Dimension(200,200)); 42 43 for (WMSInfo i : WMSPlugin.wmsList) { 44 oldValues.put(i.prefid, i); 45 model.addRow(new String[]{i.name, i.url}); 46 } 30 private Map<String,String> orig; 31 private DefaultTableModel model; 32 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>(); 47 33 48 final DefaultTableModel modeldef = new DefaultTableModel( 49 new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0); 50 final JTable listdef = new JTable(modeldef){ 51 public boolean isCellEditable(int row,int column){return false;} 52 };; 53 JScrollPane scrolldef = new JScrollPane(listdef); 54 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GBC.BOTH)); 55 scrolldef.setPreferredSize(new Dimension(200,200)); 34 public void addGui(final PreferenceDialog gui) { 35 JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu")); 56 36 57 for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) { 58 modeldef.addRow(new String[]{i.getKey(), i.getValue()}); 59 } 37 model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0); 38 final JTable list = new JTable(model); 39 JScrollPane scroll = new JScrollPane(list); 40 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 41 scroll.setPreferredSize(new Dimension(200,200)); 60 42 61 JButton add = new JButton(tr("Add")); 62 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); 63 p.add(add, GBC.std().insets(0,5,0,0)); 64 add.addActionListener(new ActionListener(){ 65 public void actionPerformed(ActionEvent e) { 66 JPanel p = new JPanel(new GridBagLayout()); 67 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0)); 68 JTextField key = new JTextField(10); 69 JTextField value = new JTextField(10); 70 p.add(key, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL)); 71 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0)); 72 p.add(value, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL)); 73 int answer = JOptionPane.showConfirmDialog(gui, p, tr("Enter a menu name and WMS URL"), JOptionPane.OK_CANCEL_OPTION); 74 if (answer == JOptionPane.OK_OPTION) { 75 model.addRow(new String[]{key.getText(), value.getText()}); 76 } 77 } 78 }); 43 for (WMSInfo i : WMSPlugin.wmsList) { 44 oldValues.put(i.prefid, i); 45 model.addRow(new String[]{i.name, i.url}); 46 } 79 47 80 JButton delete = new JButton(tr("Delete")); 81 p.add(delete, GBC.std().insets(0,5,0,0)); 82 delete.addActionListener(new ActionListener(){ 83 public void actionPerformed(ActionEvent e) { 84 if (list.getSelectedRow() == -1) 85 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete.")); 86 else 87 { 88 Integer i; 89 while ((i = list.getSelectedRow()) != -1) 90 model.removeRow(i); 91 } 92 } 93 }); 94 95 JButton copy = new JButton(tr("Copy Default")); 96 p.add(copy, GBC.std().insets(0,5,0,0)); 97 copy.addActionListener(new ActionListener(){ 98 public void actionPerformed(ActionEvent e) { 99 Integer line = listdef.getSelectedRow(); 100 if (line == -1) 101 JOptionPane.showMessageDialog(gui, tr("Please select the row to copy.")); 102 else 103 { 104 model.addRow(new String[]{modeldef.getValueAt(line, 0).toString(), 105 modeldef.getValueAt(line, 1).toString()}); 106 } 107 } 108 }); 109 } 110 111 public boolean ok() { 112 boolean change = false; 113 for (int i = 0; i < model.getRowCount(); ++i) { 114 String name = model.getValueAt(i,0).toString(); 115 String url = model.getValueAt(i,1).toString(); 48 final DefaultTableModel modeldef = new DefaultTableModel( 49 new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0); 50 final JTable listdef = new JTable(modeldef){ 51 public boolean isCellEditable(int row,int column){return false;} 52 };; 53 JScrollPane scrolldef = new JScrollPane(listdef); 54 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GBC.BOTH)); 55 scrolldef.setPreferredSize(new Dimension(200,200)); 116 56 117 WMSInfo origValue = oldValues.get(i); 118 if (origValue == null) 119 { 120 new WMSInfo(name, url, i).save(); 121 change = true; 122 } 123 else 124 { 125 if (!origValue.name.equals(name) || !origValue.url.equals(url)) 126 { 127 origValue.name = name; 128 origValue.url = url; 129 origValue.save(); 130 change = true; 131 } 132 oldValues.remove(i); 133 } 134 } 135 136 // using null values instead of empty string really deletes 137 // the preferences entry 138 for (WMSInfo i : oldValues.values()) 139 { 140 i.url = null; 141 i.name = null; 142 i.save(); 143 change = true; 144 } 57 for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) { 58 modeldef.addRow(new String[]{i.getKey(), i.getValue()}); 59 } 145 60 146 if (change) WMSPlugin.refreshMenu(); 147 return false; 148 } 149 61 JButton add = new JButton(tr("Add")); 62 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); 63 p.add(add, GBC.std().insets(0,5,0,0)); 64 add.addActionListener(new ActionListener(){ 65 public void actionPerformed(ActionEvent e) { 66 JPanel p = new JPanel(new GridBagLayout()); 67 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0)); 68 JTextField key = new JTextField(10); 69 JTextField value = new JTextField(10); 70 p.add(key, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL)); 71 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0)); 72 p.add(value, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL)); 73 int answer = JOptionPane.showConfirmDialog(gui, p, tr("Enter a menu name and WMS URL"), JOptionPane.OK_CANCEL_OPTION); 74 if (answer == JOptionPane.OK_OPTION) { 75 model.addRow(new String[]{key.getText(), value.getText()}); 76 } 77 } 78 }); 79 80 JButton delete = new JButton(tr("Delete")); 81 p.add(delete, GBC.std().insets(0,5,0,0)); 82 delete.addActionListener(new ActionListener(){ 83 public void actionPerformed(ActionEvent e) { 84 if (list.getSelectedRow() == -1) 85 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete.")); 86 else 87 { 88 Integer i; 89 while ((i = list.getSelectedRow()) != -1) 90 model.removeRow(i); 91 } 92 } 93 }); 94 95 JButton copy = new JButton(tr("Copy Default")); 96 p.add(copy, GBC.std().insets(0,5,0,0)); 97 copy.addActionListener(new ActionListener(){ 98 public void actionPerformed(ActionEvent e) { 99 Integer line = listdef.getSelectedRow(); 100 if (line == -1) 101 JOptionPane.showMessageDialog(gui, tr("Please select the row to copy.")); 102 else 103 { 104 model.addRow(new String[]{modeldef.getValueAt(line, 0).toString(), 105 modeldef.getValueAt(line, 1).toString()}); 106 } 107 } 108 }); 109 } 110 111 public boolean ok() { 112 boolean change = false; 113 for (int i = 0; i < model.getRowCount(); ++i) { 114 String name = model.getValueAt(i,0).toString(); 115 String url = model.getValueAt(i,1).toString(); 116 117 WMSInfo origValue = oldValues.get(i); 118 if (origValue == null) 119 { 120 new WMSInfo(name, url, i).save(); 121 change = true; 122 } 123 else 124 { 125 if (!origValue.name.equals(name) || !origValue.url.equals(url)) 126 { 127 origValue.name = name; 128 origValue.url = url; 129 origValue.save(); 130 change = true; 131 } 132 oldValues.remove(i); 133 } 134 } 135 136 // using null values instead of empty string really deletes 137 // the preferences entry 138 for (WMSInfo i : oldValues.values()) 139 { 140 i.url = null; 141 i.name = null; 142 i.save(); 143 change = true; 144 } 145 146 if (change) WMSPlugin.refreshMenu(); 147 return false; 148 } 149 150 150 /** 151 151 * Updates a server URL in the preferences dialog. Used by other plugins. 152 * 152 * 153 153 * @param server The server name 154 154 * @param url The server URL … … 156 156 public void setServerUrl(String server, String url) 157 157 { 158 for (int i = 0; i < model.getRowCount(); i++) 158 for (int i = 0; i < model.getRowCount(); i++) 159 159 { 160 160 if( server.equals(model.getValueAt(i,0).toString()) ) … … 175 175 public String getServerUrl(String server) 176 176 { 177 for (int i = 0; i < model.getRowCount(); i++) 177 for (int i = 0; i < model.getRowCount(); i++) 178 178 { 179 179 if( server.equals(model.getValueAt(i,0).toString()) ) -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/YAHOOGrabber.java
r11400 r12778 20 20 21 21 public class YAHOOGrabber extends WMSGrabber{ 22 22 protected String browserCmd; 23 23 24 25 26 24 YAHOOGrabber(String baseURL, Bounds b, Projection proj, 25 double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { 26 super("file:///" + Main.pref.getPreferencesDir() + "plugins/wmsplugin/ymap.html?" 27 27 // + "request=getmap&format=image/jpeg" 28 29 30 28 , b, proj, pixelPerDegree, image, mv, layer); 29 this.browserCmd = baseURL.replaceFirst("yahoo://", ""); 30 } 31 31 32 33 34 35 36 37 38 39 40 32 protected BufferedImage grab(URL url) throws IOException { 33 ArrayList<String> cmdParams = new ArrayList<String>(); 34 String urlstring = url.toExternalForm(); 35 // work around a problem in URL removing 2 slashes 36 if(!urlstring.startsWith("file:///")) 37 urlstring = urlstring.replaceFirst("file:", "file://"); 38 StringTokenizer st = new StringTokenizer(MessageFormat.format(browserCmd, urlstring)); 39 while( st.hasMoreTokens() ) 40 cmdParams.add(st.nextToken()); 41 41 42 43 42 System.out.println("WMS::Browsing YAHOO: " + cmdParams); 43 ProcessBuilder builder = new ProcessBuilder( cmdParams); 44 44 45 46 47 48 49 50 45 Process browser; 46 try { 47 browser = builder.start(); 48 } catch(IOException ioe) { 49 throw new IOException( "Could not start browser. Please check that the executable path is correct.\n" + ioe.getMessage() ); 50 } 51 51 52 53 52 return ImageIO.read(browser.getInputStream()); 53 } 54 54 }
Note:
See TracChangeset
for help on using the changeset viewer.