source: osm/applications/editors/josm/plugins/ywms/src/org/openstreetmap/josm/plugins/ywms/YWMSPlugin.java@ 3508

Last change on this file since 3508 was 3508, checked in by imi, 18 years ago
  • updated keys.pot
  • removed landsat (outdated, superseeded by wmsplugin)
  • fixed a warning that caused problems in xgettext in ywmsplugin
File size: 5.4 KB
Line 
1package org.openstreetmap.josm.plugins.ywms;
2
3import java.io.IOException;
4
5import org.openstreetmap.josm.Main;
6import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
7import org.openstreetmap.josm.plugins.Plugin;
8import org.openstreetmap.josm.plugins.PluginProxy;
9
10/**
11 *
12 * YWMS server
13 * <p>
14 * Emulates a primitive WMS server (only GetMap requests) that serves Yahoo!
15 * satellite images.
16 * <p>
17 * This plugin is heavily based on Frederik Ramm <frederik@remote.org> YWMS
18 * server in perl (see
19 * http://lists.openstreetmap.org/pipermail/dev/2007-January/002814.html), so
20 * most of the calculations and documentation is taken from his code. However,
21 * this plugin does not need any X server to run, because of a Firefox feature
22 * that dumps all loaded pages to PPM files, activated through the environment
23 * variable MOZ_FORCE_PAINT_AFTER_ONLOAD, pointing to the directory and prefix
24 * of the generated image files.
25 * <p>
26 * For each incoming request:
27 * <ul>
28 * <li>starts a firefox instance to render the web page
29 * <li>converts the resulting image to jpeg and returns it
30 * </ul>
31 * <p>
32 * This method can theoretically be used to display maps from any web site where
33 * the API supports selecting a geographic region AND includes a call to find
34 * out the region actually displayed.
35 * <p>
36 * This is required because most such services have discrete zoom levels, while
37 * the WMS request issued by josm expects to receive EXACTLY the coordinates
38 * requested in an image of the requested pixel size.
39 * <p>
40 * The HTML/Javscript used by this server uses the Yahoo! API to display an
41 * image depicting the selected area in the best available zoom level and then
42 * inquires about the area actually displayed, which will always be larger than
43 * what was requested. This information is then written to stdout using the
44 * "dump" Javascript command. From there it is read by this server, and used to
45 * stretch and cut the resulting browser image to the size requested in the WMS
46 * request.
47 * <p>
48 * To illustrate: <lu>
49 * <li>josm says "I want the area (48.5,7.0)-(48.8,7.1) as a 1000x800 pixel
50 * image"
51 * <li>we ask Yahoo about the best zoom level to display this area on 1000x800
52 * and request the image
53 * <li>we ask Yahoo about the extents actually displayed and receive the
54 * answer: (48.45,6.95)-(48.85,7.15)
55 * <li>This is .4 degrees high (we requested .3 degrees high) and .2 degrees
56 * wide (we requested .1 wide).
57 * <li>We scale the image to 1333x1600 and cut a 1000x800 section from the
58 * middle, knowing that this will now be exactly .3 degrees by .1 degrees (minus
59 * projection errors of course!) <lu>
60 * <p>
61 * <br>
62 * <b>Implementation note:</b> <lu>
63 * <li>Some information is passed from Javascript to Java, so Firefox must be
64 * configured with the method "dump" to work. To allow this method in firefox,
65 * create or modify the option "browser.dom.window.dump.enabled=true" in
66 * "about:config"
67 * <p>
68 * <li>Also, as firefox must be started and killed once and again, it is
69 * recommended to create a profile with the option
70 * "browser.sessionstore.resume_from_crash" to false and set other profile to
71 * default, so no nag screens are shown. </lu>
72 *
73 * @author Francisco R. Santos <frsantos@gmail.com>
74 * @author Frederik Ramm <frederik@remote.org>
75 * @version 0.2 03/10/2007
76 */
77public class YWMSPlugin extends Plugin
78{
79 static HTTPServer server;
80
81 /**
82 * Creates the plugin, and starts the HTTP server
83 */
84 public YWMSPlugin()
85 {
86 WARNING. This compilation error is made on purpose.
87 We are not allowed to use this plugin at present.
88 Please, don not use it until all legal issues are resolved.
89
90 try
91 {
92 copy("/resources/ymap.html", "ymap.html");
93 copy("/resources/config.html", "config.html");
94 restartServer();
95 }
96 catch(IOException e)
97 {
98 e.printStackTrace();
99 }
100 }
101
102 @Override
103 public PreferenceSetting getPreferenceSetting()
104 {
105 return new YWMSPreferenceSetting();
106 }
107
108 /**
109 * Starts or restarts the HTTP server
110 *
111 */
112 public void restartServer()
113 {
114 try
115 {
116 if( server != null )
117 server.stopServer();
118
119 int port;
120 String strPort = Main.pref.get("ywms.port");
121 try
122 {
123 port = Integer.parseInt( strPort );
124 }
125 catch(Exception e)
126 {
127 System.out.println("YWMS::Invalid port '" + strPort + "'. Using default " + HTTPServer.DEFAULT_PORT);
128 port = HTTPServer.DEFAULT_PORT;
129 }
130 server = new HTTPServer(port);
131 server.start();
132 }
133 catch(IOException ioe)
134 {
135 ioe.printStackTrace();
136 }
137 }
138
139 /**
140 * Returns the plugin's directory
141 * <p>
142 * Utility method for classes that can't acces the plugin object
143 *
144 * @return The directory of the plugin
145 */
146 public static String getStaticPluginDir()
147 {
148 YWMSPlugin plugin = getPlugin();
149 return ( plugin != null ) ? plugin.getPluginDir() : null;
150 }
151
152 /**
153 * Utility method to retrieve the plugin for classes that can't access to the plugin object directly.
154 *
155 * @return The YWMS plugin
156 */
157 public static YWMSPlugin getPlugin()
158 {
159 for (PluginProxy plugin : Main.plugins)
160 {
161 if( plugin.info.className.equals(YWMSPlugin.class.getName()) )
162 {
163 return (YWMSPlugin)plugin.plugin;
164 }
165 }
166
167 return null;
168 }
169}
Note: See TracBrowser for help on using the repository browser.