1 | package cadastre_fr;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 |
|
---|
10 | import javax.swing.JCheckBoxMenuItem;
|
---|
11 | import javax.swing.JDialog;
|
---|
12 | import javax.swing.JMenu;
|
---|
13 | import javax.swing.JMenuItem;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.KeyStroke;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
19 | import org.openstreetmap.josm.actions.UploadAction;
|
---|
20 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
21 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
22 | import org.openstreetmap.josm.gui.IconToggleButton;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
24 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
25 | import org.openstreetmap.josm.data.projection.*;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * Plugin to access the French Cadastre WMS server at www.cadastre.gouv.fr This
|
---|
30 | * WMS server requires some specific handling like retrieving a cookie for a
|
---|
31 | * limitation in case of no activity, or the request to the server shall provide
|
---|
32 | * a city/town/village code.
|
---|
33 | *
|
---|
34 | * @author Pieren <pieren3@gmail.com>,
|
---|
35 | * @author <matthieu.lochegnies@gmail.com> for the extension to codeCommune
|
---|
36 | * @version 0.8
|
---|
37 | * History:
|
---|
38 | * 0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
|
---|
39 | * 0.2 22-Jun-2008 first stable version
|
---|
40 | * 0.3 24-Jun-2008 add code departement
|
---|
41 | * 0.4 06-Jul-2008 - add images scales, icons, menu items disabling
|
---|
42 | * - remove dependencies of wmsplugin
|
---|
43 | * - add option to force a Lambert zone (for median locations)
|
---|
44 | * - add auto-sourcing
|
---|
45 | * 0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
|
---|
46 | * - no overlapping of grabbed images if transparency is enabled
|
---|
47 | * - set one layer per location
|
---|
48 | * - use utf-8 charset in POST request to server
|
---|
49 | * - improve the preferences setting dialog
|
---|
50 | * - cancel the current download is now possible
|
---|
51 | * - add automatic images caching and load on request (+ manage cache directory size)
|
---|
52 | * - enable auto-sourcing only if a WMS layer is used
|
---|
53 | * 0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
|
---|
54 | * - process the overlapping images when cache is loaded from disk
|
---|
55 | * - save the last 'new location request' text again in preferences
|
---|
56 | * - avoid duplicate layers with same name
|
---|
57 | * - set text input for new locations in upper case
|
---|
58 | * - the cache directory is configurable in "cadastrewms.cacheDir"
|
---|
59 | * - improve configuration change updates
|
---|
60 | * 0.7 24-Aug-2008 - mask images only if transparency enabled
|
---|
61 | * - validate projection name by Lambert.toString() method
|
---|
62 | * 0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
|
---|
63 | * - new possible grab factor of 100 square meters fixed size
|
---|
64 | * - minor fixes due to changes in JOSM core classes
|
---|
65 | * - first draft of raster image support
|
---|
66 | * 0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
|
---|
67 | * and simplify
|
---|
68 | * 1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
|
---|
69 | * - increase PNG picture size requested to WMS (800x1000)
|
---|
70 | * - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
|
---|
71 | * 1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
|
---|
72 | * - propose to use shortcut F11 for grabbing
|
---|
73 | * 1.2 16-Aug-2009 - implementation of raster image grabbing, cropping and georeferencing (not the
|
---|
74 | * overview rasters (Tableau d'assemblage) but directly small units (Feuille)
|
---|
75 | * 1.3 23-Aug-2009 - improve georeferencing action cancellation
|
---|
76 | * - fixed bug of raster image loaded from cache not working on Java1.6
|
---|
77 | * - improve mouse click bounce detection during georeferencing process
|
---|
78 | * 1.4 25-Oct-2009 - add support for new Lambert CC 9 Zones projection
|
---|
79 | * - add optional crosspieces display on raster image layers
|
---|
80 | * - add automatic raster images georeferencing when WMS provides data
|
---|
81 | * - re-implement manual adjustment mode in raster image layer
|
---|
82 | */
|
---|
83 | public class CadastrePlugin extends Plugin {
|
---|
84 | static String VERSION = "1.4";
|
---|
85 |
|
---|
86 | static JMenu cadastreJMenu;
|
---|
87 |
|
---|
88 | public static CadastreGrabber cadastreGrabber = new CadastreGrabber();
|
---|
89 |
|
---|
90 | public static String source = "";
|
---|
91 |
|
---|
92 | // true if the checkbox "auto-sourcing" is set in the plugin menu
|
---|
93 | public static boolean autoSourcing = false;
|
---|
94 |
|
---|
95 | // true when the plugin is first used, e.g. grab from WMS or download cache file
|
---|
96 | public static boolean pluginUsed = false;
|
---|
97 |
|
---|
98 | public static String cacheDir = null;
|
---|
99 |
|
---|
100 | public static boolean alterColors = false;
|
---|
101 |
|
---|
102 | public static boolean backgroundTransparent = false;
|
---|
103 |
|
---|
104 | public static float transparency = 1.0f;
|
---|
105 |
|
---|
106 | public static boolean drawBoundaries = false;
|
---|
107 |
|
---|
108 | static private boolean menuEnabled = false;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Creates the plugin and setup the default settings if necessary
|
---|
112 | *
|
---|
113 | * @throws Exception
|
---|
114 | */
|
---|
115 | public CadastrePlugin() throws Exception {
|
---|
116 | System.out.println("Pluging \"cadastre-fr\" started...");
|
---|
117 | if (Main.pref.get("cadastrewms.cacheDir").equals(""))
|
---|
118 | cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
|
---|
119 | else {
|
---|
120 | cacheDir = Main.pref.get("cadastrewms.cacheDir");
|
---|
121 | if (cacheDir.charAt(cacheDir.length()-1) != '\\' )
|
---|
122 | cacheDir += '\\';
|
---|
123 | }
|
---|
124 | System.out.println("current cache directory: "+cacheDir);
|
---|
125 |
|
---|
126 | refreshConfiguration();
|
---|
127 |
|
---|
128 | UploadAction.registerUploadHook(new CheckSourceUploadHook());
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static void refreshMenu() {
|
---|
132 | MainMenu menu = Main.main.menu;
|
---|
133 |
|
---|
134 | if (cadastreJMenu == null) {
|
---|
135 | cadastreJMenu = menu.addMenu(marktr("Cadastre"), KeyEvent.VK_C, menu.defaultMenuPos);
|
---|
136 | JosmAction grab = new MenuActionGrab();
|
---|
137 | JMenuItem menuGrab = new JMenuItem(grab);
|
---|
138 | KeyStroke ks = grab.getShortcut().getKeyStroke();
|
---|
139 | if (ks != null) {
|
---|
140 | menuGrab.setAccelerator(ks);
|
---|
141 | }
|
---|
142 | JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
|
---|
143 | JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
|
---|
144 | final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
|
---|
145 | menuSource.setSelected(autoSourcing);
|
---|
146 | menuSource.addActionListener(new ActionListener() {
|
---|
147 | public void actionPerformed(ActionEvent ev) {
|
---|
148 | Main.pref.put("cadastrewms.autosourcing", menuSource.isSelected());
|
---|
149 | autoSourcing = menuSource.isSelected();
|
---|
150 | }
|
---|
151 | });
|
---|
152 |
|
---|
153 | JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
|
---|
154 | JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
|
---|
155 | JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
|
---|
156 | //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
|
---|
157 | //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
|
---|
158 |
|
---|
159 | cadastreJMenu.add(menuGrab);
|
---|
160 | cadastreJMenu.add(menuActionGrabPlanImage);
|
---|
161 | cadastreJMenu.add(menuSettings);
|
---|
162 | cadastreJMenu.add(menuSource);
|
---|
163 | cadastreJMenu.add(menuResetCookie);
|
---|
164 | cadastreJMenu.add(menuLambertZone);
|
---|
165 | cadastreJMenu.add(menuLoadFromCache);
|
---|
166 | // all SVG features disabled until official WMS is released
|
---|
167 | //cadastreJMenu.add(menuActionBoundaries);
|
---|
168 | //cadastreJMenu.add(menuActionBuildings);
|
---|
169 | }
|
---|
170 | setEnabledAll(menuEnabled);
|
---|
171 | }
|
---|
172 |
|
---|
173 | public static void refreshConfiguration() {
|
---|
174 | source = Main.pref.get("cadastrewms.source",
|
---|
175 | "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre ; mise \u00e0 jour : AAAA");
|
---|
176 | autoSourcing = Main.pref.getBoolean("cadastrewms.autosourcing", true);
|
---|
177 | alterColors = Main.pref.getBoolean("cadastrewms.alterColors");
|
---|
178 | drawBoundaries = Main.pref.getBoolean("cadastrewms.drawBoundaries", false);
|
---|
179 | if (alterColors) {
|
---|
180 | backgroundTransparent = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
|
---|
181 | transparency = Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"));
|
---|
182 | } else {
|
---|
183 | backgroundTransparent = false;
|
---|
184 | transparency = 1.0f;
|
---|
185 | }
|
---|
186 | // overwrite F11 shortcut used from the beginning by this plugin and recently used
|
---|
187 | // for full-screen switch in JOSM core
|
---|
188 | int i = 0;
|
---|
189 | String p = Main.pref.get("shortcut.shortcut."+i, null);
|
---|
190 | boolean alreadyRedefined = false;
|
---|
191 | while (p != null) {
|
---|
192 | String[] s = p.split(";");
|
---|
193 | alreadyRedefined = alreadyRedefined || s[0].equals("menu:view:fullscreen");
|
---|
194 | i++;
|
---|
195 | p = Main.pref.get("shortcut.shortcut."+i, null);
|
---|
196 | }
|
---|
197 | if (!alreadyRedefined) {
|
---|
198 | int reply = JOptionPane.showConfirmDialog(null,
|
---|
199 | tr("Plugin cadastre-fr used traditionaly for grabbing the key shortcut F11\n"+
|
---|
200 | "which is currently allocated for full-screen switch by default\n"+
|
---|
201 | "Would you like to restore F11 for grab action ?"),
|
---|
202 | tr("Restore grab shortcut F11"),
|
---|
203 | JOptionPane.YES_NO_OPTION);
|
---|
204 | if (reply == JOptionPane.OK_OPTION) {
|
---|
205 | System.out.println("redefine fullscreen shortcut F11 to shift+F11");
|
---|
206 | Main.pref.put("shortcut.shortcut."+i, "menu:view:fullscreen;Toggle Full Screen view;122;5;122;64;false;true");
|
---|
207 | JOptionPane.showMessageDialog(Main.parent,tr("JOSM is stopped for the change to take effect."));
|
---|
208 | System.exit(0);
|
---|
209 | }
|
---|
210 | } else
|
---|
211 | System.out.println("shortcut F11 already redefined; do not change");
|
---|
212 |
|
---|
213 | refreshMenu();
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public PreferenceSetting getPreferenceSetting() {
|
---|
218 | return new CadastrePreferenceSetting();
|
---|
219 | }
|
---|
220 |
|
---|
221 | private static void setEnabledAll(boolean isEnabled) {
|
---|
222 | for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
|
---|
223 | JMenuItem item = cadastreJMenu.getItem(i);
|
---|
224 | if (item != null)
|
---|
225 | if (item.getText().equals(MenuActionGrabPlanImage.name) /*||
|
---|
226 | item.getText().equals(MenuActionGrab.name) ||
|
---|
227 | item.getText().equals(MenuActionBoundaries.name) ||
|
---|
228 | item.getText().equals(MenuActionBuildings.name)*/) {
|
---|
229 | item.setEnabled(isEnabled);
|
---|
230 | } else if (item.getText().equals(MenuActionLambertZone.name)) {
|
---|
231 | item.setEnabled(!isEnabled);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | menuEnabled = isEnabled;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
238 | if (cadastreJMenu != null) {
|
---|
239 | if (oldFrame == null && newFrame != null) {
|
---|
240 | setEnabledAll(true);
|
---|
241 | Main.map.addMapMode(new IconToggleButton
|
---|
242 | (new WMSAdjustAction(Main.map)));
|
---|
243 | } else if (oldFrame != null && newFrame == null) {
|
---|
244 | setEnabledAll(false);
|
---|
245 | Lambert.layoutZone = -1;
|
---|
246 | LambertCC9Zones.layoutZone = -1;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | public static boolean isCadastreProjection() {
|
---|
252 | return Main.proj.toString().equals(new Lambert().toString())
|
---|
253 | || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Fort_Marigot().toString())
|
---|
254 | || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Ste_Anne().toString())
|
---|
255 | || Main.proj.toString().equals(new UTM_20N_Martinique_Fort_Desaix().toString())
|
---|
256 | || Main.proj.toString().equals(new GaussLaborde_Reunion().toString())
|
---|
257 | || Main.proj.toString().equals(new LambertCC9Zones().toString());
|
---|
258 | }
|
---|
259 |
|
---|
260 | public static void safeSleep(long milliseconds) {
|
---|
261 | try {
|
---|
262 | Thread.sleep(milliseconds);
|
---|
263 | } catch (InterruptedException e) {}
|
---|
264 | }
|
---|
265 |
|
---|
266 | // See OptionPaneUtil
|
---|
267 | // FIXME: this is a temporary solution.
|
---|
268 | public static void prepareDialog(JDialog dialog) {
|
---|
269 | if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
|
---|
270 | try {
|
---|
271 | dialog.setAlwaysOnTop(true);
|
---|
272 | } catch(SecurityException e) {
|
---|
273 | System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
|
---|
274 | }
|
---|
275 | }
|
---|
276 | dialog.setModal(true);
|
---|
277 | dialog.toFront();
|
---|
278 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
279 | }
|
---|
280 | }
|
---|