source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePlugin.java@ 29101

Last change on this file since 29101 was 29101, checked in by pieren, 12 years ago

#8229 patch from Don-vip going directly to projection preference window if grab action is called in incompatible cadastre proj.

  • Property svn:eol-style set to native
File size: 23.4 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package cadastre_fr;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.awt.event.KeyEvent;
11import java.io.File;
12import java.util.Arrays;
13import java.util.HashMap;
14import java.util.Map;
15
16import javax.swing.JCheckBoxMenuItem;
17import javax.swing.JDialog;
18import javax.swing.JMenu;
19import javax.swing.JMenuItem;
20import javax.swing.JOptionPane;
21import javax.swing.KeyStroke;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.JosmAction;
25import org.openstreetmap.josm.actions.UploadAction;
26import org.openstreetmap.josm.data.projection.AbstractProjection;
27import org.openstreetmap.josm.data.projection.Projection;
28import org.openstreetmap.josm.gui.MainMenu;
29import org.openstreetmap.josm.gui.MapFrame;
30import org.openstreetmap.josm.gui.IconToggleButton;
31import org.openstreetmap.josm.gui.layer.Layer;
32import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
33import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
34import org.openstreetmap.josm.gui.preferences.map.MapPreference;
35import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
36import org.openstreetmap.josm.plugins.Plugin;
37import org.openstreetmap.josm.plugins.PluginInformation;
38
39/**
40 *
41 * Plugin to access the French Cadastre WMS server at www.cadastre.gouv.fr This
42 * WMS server requires some specific handling like retrieving a cookie for a
43 * limitation in case of no activity, or the request to the server shall provide
44 * a city/town/village code.
45 *
46 * @author Pieren <pieren3@gmail.com>,
47 * @author <matthieu.lochegnies@gmail.com> for the extension to codeCommune
48 * @version 0.8
49 * History:
50 * 0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
51 * 0.2 22-Jun-2008 first stable version
52 * 0.3 24-Jun-2008 add code departement
53 * 0.4 06-Jul-2008 - add images scales, icons, menu items disabling
54 * - remove dependencies of wmsplugin
55 * - add option to force a Lambert zone (for median locations)
56 * - add auto-sourcing
57 * 0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
58 * - no overlapping of grabbed images if transparency is enabled
59 * - set one layer per location
60 * - use utf-8 charset in POST request to server
61 * - improve the preferences setting dialog
62 * - cancel the current download is now possible
63 * - add automatic images caching and load on request (+ manage cache directory size)
64 * - enable auto-sourcing only if a WMS layer is used
65 * 0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
66 * - process the overlapping images when cache is loaded from disk
67 * - save the last 'new location request' text again in preferences
68 * - avoid duplicate layers with same name
69 * - set text input for new locations in upper case
70 * - the cache directory is configurable in "cadastrewms.cacheDir"
71 * - improve configuration change updates
72 * 0.7 24-Aug-2008 - mask images only if transparency enabled
73 * - validate projection name by Lambert.toString() method
74 * 0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
75 * - new possible grab factor of 100 square meters fixed size
76 * - minor fixes due to changes in JOSM core classes
77 * - first draft of raster image support
78 * 0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
79 * and simplify
80 * 1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
81 * - increase PNG picture size requested to WMS (800x1000)
82 * - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
83 * 1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
84 * - propose to use shortcut F11 for grabbing
85 * 1.2 16-Aug-2009 - implementation of raster image grabbing, cropping and georeferencing (not the
86 * overview rasters (Tableau d'assemblage) but directly small units (Feuille)
87 * 1.3 23-Aug-2009 - improve georeferencing action cancellation
88 * - fixed bug of raster image loaded from cache not working on Java1.6
89 * - improve mouse click bounce detection during georeferencing process
90 * 1.4 25-Oct-2009 - add support for new Lambert CC 9 Zones projection
91 * - add optional crosspieces display on raster image layers
92 * - add automatic raster images georeferencing when WMS provides data
93 * - re-implement manual adjustment mode in raster image layer
94 * 1.5 21-Nov-2009 - major changes in projection in core : no magical zone prediction anymore for
95 * Lambert 4 and 9 zones; grid translation implemented for Lambert 4 zones;
96 * support of subprojections in preferences for zones setting and UTM20N
97 * - removed autosourcing of empty new nodes
98 * 1.6 28-Nov-2009 - Fix minor issues if Grab is called without layer (possible since projection rework)
99 * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
100 * 1.8 11-Mar-2010 - filter the mouse button 1 during georeferencing
101 * - retry if getting a new cookie failed (10 times during 30 seconds)
102 * - cookie expiration automatically detected and renewed (after 30 minutes)
103 * - proper WMS layer cleanup at destruction (workaround for memory leak)
104 * - new cache format (v3) storing original image and cropped image bbox + angle
105 * - new cache format (v4) storing original image size for later rotation
106 * - cache files read compatible with previous formats
107 * - raster image rotation issues fixed, now using shift+ctrl key instead of ctrl
108 * - raster image adjustment using default system menu modifier (ctrl for windows) for Mac support
109 * - image resolution configurable (high, medium, low) like the online interface
110 * - layer selection configurable for vectorized images
111 * - improved download cancellation
112 * - from Erik Amzallag:
113 * - possibility to modify the auto-sourcing text just before upload
114 * - from Clément Ménier:
115 * - new option allowing an auto-selection of the first cadastre layer for grab
116 * - non-modal JDialog in MenuActionGrabPlanImage
117 * - new options in the image filter (bilinear, bicubic)
118 * 1.9 05-Apr-2010 - added a scroll bar in preferences
119 * - download cancellation improved
120 * - last deployment for Java1.5 compatibility
121 * 2.0 07-Jul-2010 - update projection for "La Reunion" departement to RGR92, UTM40S.
122 * - add 'departement' as option in the municipality selection
123 * - fixed bug in cache directory size control (and disabled by default)
124 * - add map mode for addressing
125 * - from Nicolas Dumoulin:
126 * - add "tableau d'assemblage" in raster images for georeferencing (as option)
127 * 2.1 14-Jan-2011 - add GrabThread moving the grab to a separate thread
128 * - the divided BBox mode starts from the central square and loads the next in a spiral
129 * - move the grabber from CadastrPlugin singleton to each wmsLayer instance to allow grabbing
130 * of multiple municipalities in parallel.
131 * 2.2 01-Jul-2011 - replace deprecated Main.proj by newest Main.getProjection()
132 * - fix list of raster images (Feuilles) parsing failing after a Cadastre server change/maintenance
133 */
134public class CadastrePlugin extends Plugin {
135 static String VERSION = "2.1";
136
137 static JMenu cadastreJMenu;
138
139 public static String source = "";
140
141 // true if the checkbox "auto-sourcing" is set in the plugin menu
142 public static boolean autoSourcing = false;
143
144 // true when the plugin is first used, e.g. grab from WMS or download cache file
145 public static boolean pluginUsed = false;
146
147 public static String cacheDir = null;
148
149 public static boolean alterColors = false;
150
151 public static boolean backgroundTransparent = false;
152
153 public static float transparency = 1.0f;
154
155 public static boolean drawBoundaries = false;
156
157 public static int imageWidth, imageHeight;
158
159 public static String grabLayers, grabStyles = null;
160
161 static private boolean menuEnabled = false;
162
163 private static String LAYER_BULDINGS = "CDIF:LS2";
164 private static String STYLE_BUILDING = "LS2_90";
165 private static String LAYER_WATER = "CDIF:LS3";
166 private static String STYLE_WATER = "LS3_90";
167 private static String LAYER_SYMBOL = "CDIF:LS1";
168 private static String STYLE_SYMBOL = "LS1_90";
169 private static String LAYER_PARCELS = "CDIF:PARCELLE";
170 private static String STYLE_PARCELS = "PARCELLE_90";
171 private static String LAYER_NUMERO = "CDIF:NUMERO";
172 private static String STYLE_NUMERO = "NUMERO_90";
173 private static String LAYER_LABEL = "CDIF:PT3,CDIF:PT2,CDIF:PT1";
174 private static String STYLE_LABEL = "PT3_90,PT2_90,PT1_90";
175 private static String LAYER_LIEUDIT = "CDIF:LIEUDIT";
176 private static String STYLE_LIEUDIT = "LIEUDIT_90";
177 private static String LAYER_SECTION = "CDIF:SUBSECTION,CDIF:SECTION";
178 private static String STYLE_SECTION = "SUBSECTION_90,SECTION_90";
179 private static String LAYER_COMMUNE = "CDIF:COMMUNE";
180 private static String STYLE_COMMUNE = "COMMUNE_90";
181
182 /**
183 * Creates the plugin and setup the default settings if necessary
184 *
185 * @throws Exception
186 */
187 public CadastrePlugin(PluginInformation info) throws Exception {
188 super(info);
189 System.out.println("Pluging cadastre-fr v"+VERSION+" started...");
190 if (Main.pref.get("cadastrewms.cacheDir").equals(""))
191 cacheDir = Main.pref.getPreferencesDir()+"plugins"+File.separatorChar+"cadastrewms"+File.separatorChar;
192 else {
193 cacheDir = Main.pref.get("cadastrewms.cacheDir");
194 if (cacheDir.charAt(cacheDir.length()-1) != File.separatorChar )
195 cacheDir += File.separatorChar;
196 }
197 System.out.println("current cache directory: "+cacheDir);
198
199 refreshConfiguration();
200
201 UploadAction.registerUploadHook(new CheckSourceUploadHook());
202
203 }
204
205 public static void refreshMenu() {
206 MainMenu menu = Main.main.menu;
207
208 if (cadastreJMenu == null) {
209 cadastreJMenu = menu.addMenu(marktr("Cadastre"), KeyEvent.VK_C, menu.defaultMenuPos, ht("/Plugin/CadastreFr"));
210 JosmAction grab = new MenuActionGrab();
211 JMenuItem menuGrab = new JMenuItem(grab);
212 KeyStroke ks = grab.getShortcut().getKeyStroke();
213 if (ks != null) {
214 menuGrab.setAccelerator(ks);
215 }
216 JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
217 JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
218 final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
219 menuSource.setSelected(autoSourcing);
220 menuSource.addActionListener(new ActionListener() {
221 public void actionPerformed(ActionEvent ev) {
222 Main.pref.put("cadastrewms.autosourcing", menuSource.isSelected());
223 autoSourcing = menuSource.isSelected();
224 }
225 });
226
227 //JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
228 //JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
229 JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
230 // temporary disabled:
231 //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
232 //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
233
234 cadastreJMenu.add(menuGrab);
235 cadastreJMenu.add(menuActionGrabPlanImage);
236 cadastreJMenu.add(menuSettings);
237 cadastreJMenu.add(menuSource);
238 //cadastreJMenu.add(menuResetCookie); not required any more
239 //cadastreJMenu.add(menuLambertZone);
240 //if (Main.pref.getBoolean("cadastrewms.buildingsMenu", false))
241 // cadastreJMenu.add(menuActionBuildings);
242 cadastreJMenu.add(menuLoadFromCache);
243 // all SVG features disabled until official WMS is released
244 //cadastreJMenu.add(menuActionBoundaries);
245 }
246 setEnabledAll(menuEnabled);
247 }
248
249 public static void refreshConfiguration() {
250 source = checkSourceMillesime();
251 autoSourcing = Main.pref.getBoolean("cadastrewms.autosourcing", true);
252 alterColors = Main.pref.getBoolean("cadastrewms.alterColors");
253 drawBoundaries = Main.pref.getBoolean("cadastrewms.drawBoundaries", false);
254 if (alterColors) {
255 backgroundTransparent = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
256 transparency = Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"));
257 } else {
258 backgroundTransparent = false;
259 transparency = 1.0f;
260 }
261 String currentResolution = Main.pref.get("cadastrewms.resolution", "high");
262 if (currentResolution.equals("high")) {
263 imageWidth = 1000; imageHeight = 800;
264 } else if (currentResolution.equals("medium")){
265 imageWidth = 800; imageHeight = 600;
266 } else {
267 imageWidth = 600; imageHeight = 400;
268 }
269 refreshLayersURL();
270
271 /* TODO: remove in july 2012 */
272 if(!Main.pref.getBoolean("cadastregrab.shortcut.warn", false))
273 {
274 Main.pref.put("cadastregrab.shortcut.warn", true);
275 JOptionPane.showMessageDialog(Main.parent,
276 tr("Plugin cadastre-fr used the key shortcut F11 for grabbing,\n"+
277 "which is now allocated for full-screen switch.\n"+
278 "The new grabbing key is F10, but you can change the key\n" +
279 "in the shortcut settings if you want."));
280 }
281 refreshMenu();
282 }
283
284 private static void refreshLayersURL() {
285 grabLayers = "";
286 grabStyles = "";
287 int countLayers = 0;
288 if (Main.pref.getBoolean("cadastrewms.layerWater", true)) {
289 grabLayers += LAYER_WATER + ",";
290 grabStyles += STYLE_WATER + ",";
291 countLayers++;
292 }
293 if (Main.pref.getBoolean("cadastrewms.layerBuilding", true)) {
294 grabLayers += LAYER_BULDINGS + ",";
295 grabStyles += STYLE_BUILDING + ",";
296 countLayers++;
297 }
298 if (Main.pref.getBoolean("cadastrewms.layerSymbol", true)) {
299 grabLayers += LAYER_SYMBOL + ",";
300 grabStyles += STYLE_SYMBOL + ",";
301 countLayers++;
302 }
303 if (Main.pref.getBoolean("cadastrewms.layerParcel", true)) {
304 grabLayers += LAYER_PARCELS + ",";
305 grabStyles += STYLE_PARCELS + ",";
306 countLayers++;
307 }
308 if (Main.pref.getBoolean("cadastrewms.layerNumero", true)) {
309 grabLayers += LAYER_NUMERO + ",";
310 grabStyles += STYLE_NUMERO + ",";
311 countLayers++;
312 }
313 if (Main.pref.getBoolean("cadastrewms.layerLabel", true)) {
314 grabLayers += LAYER_LABEL + ",";
315 grabStyles += STYLE_LABEL + ",";
316 countLayers++;
317 }
318 if (Main.pref.getBoolean("cadastrewms.layerLieudit", true)) {
319 grabLayers += LAYER_LIEUDIT + ",";
320 grabStyles += STYLE_LIEUDIT + ",";
321 countLayers++;
322 }
323 if (Main.pref.getBoolean("cadastrewms.layerSection", true)) {
324 grabLayers += LAYER_SECTION + ",";
325 grabStyles += STYLE_SECTION + ",";
326 countLayers++;
327 }
328 if (Main.pref.getBoolean("cadastrewms.layerCommune", true)) {
329 grabLayers += LAYER_COMMUNE + ",";
330 grabStyles += STYLE_COMMUNE + ",";
331 countLayers++;
332 }
333 if (countLayers > 2) { // remove the last ','
334 grabLayers = grabLayers.substring(0, grabLayers.length()-1);
335 grabStyles = grabStyles.substring(0, grabStyles.length()-1);
336 } else {
337 JOptionPane.showMessageDialog(Main.parent,tr("Please enable at least two WMS layers in the cadastre-fr "
338 + "plugin configuration.\nLayers ''Building'' and ''Parcel'' added by default."));
339 Main.pref.put("cadastrewms.layerBuilding", true);
340 Main.pref.put("cadastrewms.layerParcel", true);
341 grabLayers += LAYER_BULDINGS + "," + LAYER_PARCELS;
342 grabStyles += STYLE_BUILDING + "," + STYLE_PARCELS;
343 }
344 }
345
346 @Override
347 public PreferenceSetting getPreferenceSetting() {
348 return new CadastrePreferenceSetting();
349 }
350
351 private static void setEnabledAll(boolean isEnabled) {
352 for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
353 JMenuItem item = cadastreJMenu.getItem(i);
354 if (item != null)
355 if (item.getText().equals(MenuActionGrabPlanImage.name) /*||
356 item.getText().equals(MenuActionGrab.name) ||
357 item.getText().equals(MenuActionBoundaries.name) ||
358 item.getText().equals(MenuActionBuildings.name)*/) {
359 item.setEnabled(isEnabled);
360 }
361 }
362 menuEnabled = isEnabled;
363 }
364
365 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
366 if (cadastreJMenu != null) {
367 if (oldFrame == null && newFrame != null) {
368 setEnabledAll(true);
369 Main.map.addMapMode(new IconToggleButton(new WMSAdjustAction(Main.map)));
370 Main.map.addMapMode(new IconToggleButton(new Address(Main.map)));
371 } else if (oldFrame != null && newFrame == null) {
372 setEnabledAll(false);
373 //Lambert.layoutZone = -1;
374 //LambertCC9Zones.layoutZone = -1;
375 }
376 }
377 }
378
379 public static boolean isLambert() {
380 String code = Main.getProjection().toCode();
381 return Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code);
382 }
383
384 public static boolean isUtm_france_dom() {
385 String code = Main.getProjection().toCode();
386 return Arrays.asList(ProjectionPreference.utm_france_dom.allCodes()).contains(code);
387 }
388
389 public static boolean isLambert_cc9() {
390 String code = Main.getProjection().toCode();
391 return Arrays.asList(ProjectionPreference.lambert_cc9.allCodes()).contains(code);
392 }
393
394 public static boolean isCadastreProjection() {
395 return isLambert() || isUtm_france_dom() || isLambert_cc9();
396 }
397
398 public static int getCadastreProjectionLayoutZone() {
399 int zone = -1;
400 Projection proj = Main.getProjection();
401 if (proj instanceof AbstractProjection) {
402 Integer code = ((AbstractProjection) proj).getEpsgCode();
403 if (code != null) {
404 if (code >= 3942 && code <= 3950) { // LambertCC9Zones
405 zone = code - 3942;
406 } else if (code >= 27561 && 27564 <= code) { // Lambert
407 zone = code - 27561;
408 } else { // UTM_France_DOM
409 Map<Integer, Integer> utmfr = new HashMap<Integer, Integer>();
410 utmfr.put(2969, 0);
411 utmfr.put(2970, 1);
412 utmfr.put(2973, 2);
413 utmfr.put(2975, 3);
414 utmfr.put(2972, 4);
415 if (utmfr.containsKey(code)) {
416 zone = utmfr.get(code);
417 }
418 }
419 }
420 }
421 return zone;
422 }
423
424 public static void safeSleep(long milliseconds) {
425 try {
426 Thread.sleep(milliseconds);
427 } catch (InterruptedException e) {}
428 }
429
430 // See OptionPaneUtil
431 // FIXME: this is a temporary solution.
432 public static void prepareDialog(JDialog dialog) {
433 if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
434 try {
435 dialog.setAlwaysOnTop(true);
436 } catch(SecurityException e) {
437 System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
438 }
439 }
440 dialog.setModal(true);
441 dialog.toFront();
442 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
443 }
444
445 /**
446 * Adds the WMSLayer following this rule:<br/>
447 * - if a WMSLayer exists place this new layer just before this layer<br/>
448 * - Otherwise place it at the bottom
449 * @param wmsLayer the wmsLayer to add
450 */
451 public static void addWMSLayer(WMSLayer wmsLayer) {
452 if (Main.map != null && Main.map.mapView != null) {
453 int wmsNewLayerPos = Main.map.mapView.getAllLayers().size();
454 for(Layer l : Main.map.mapView.getLayersOfType(WMSLayer.class)) {
455 int wmsPos = Main.map.mapView.getLayerPos(l);
456 if (wmsPos < wmsNewLayerPos) wmsNewLayerPos = wmsPos;
457 }
458 Main.main.addLayer(wmsLayer);
459 // Move the layer to its new position
460 Main.map.mapView.moveLayer(wmsLayer, wmsNewLayerPos);
461 } else
462 Main.main.addLayer(wmsLayer);
463 }
464
465 private static String checkSourceMillesime() {
466 java.util.Calendar calendar = java.util.Calendar.getInstance();
467 int currentYear = calendar.get(java.util.Calendar.YEAR);
468 String src = Main.pref.get("cadastrewms.source",
469 "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre. Mise \u00e0 jour : AAAA");
470 String srcYear = src.substring(src.lastIndexOf(" ")+1);
471 Integer year = null;
472 try {
473 year = Integer.decode(srcYear);
474 } catch (NumberFormatException e) {}
475 if (srcYear.equals("AAAA") || (year != null && year < currentYear)) {
476 System.out.println("Replace source year "+srcYear+" by current year "+currentYear);
477 src = src.substring(0, src.lastIndexOf(" ")+1)+currentYear;
478 Main.pref.put("cadastrewms.source", src);
479 }
480 return src;
481 }
482
483 public static void askToChangeProjection() {
484 if (JOptionPane.showConfirmDialog(Main.parent,
485 tr("To enable the cadastre WMS plugin, change\n"
486 + "the current projection to one of the cadastre\n"
487 + "projections and retry"),
488 tr("Change the current projection"), JOptionPane.OK_CANCEL_OPTION)
489 == JOptionPane.OK_OPTION) {
490 PreferenceDialog p = new PreferenceDialog(Main.parent);
491 p.selectPreferencesTabByClass(MapPreference.class);
492 p.getTabbedPane().getSetting(ProjectionPreference.class).selectProjection(ProjectionPreference.lambert_cc9);
493 p.setVisible(true);
494 }
495 }
496}
Note: See TracBrowser for help on using the repository browser.