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

Last change on this file since 20247 was 20247, checked in by pieren, 15 years ago

layer selection configurable for vectorized images

  • Property svn:eol-style set to native
File size: 18.5 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;
11
12import javax.swing.JCheckBoxMenuItem;
13import javax.swing.JDialog;
14import javax.swing.JMenu;
15import javax.swing.JMenuItem;
16import javax.swing.JOptionPane;
17import javax.swing.KeyStroke;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.JosmAction;
21import org.openstreetmap.josm.actions.UploadAction;
22import org.openstreetmap.josm.gui.MainMenu;
23import org.openstreetmap.josm.gui.MapFrame;
24import org.openstreetmap.josm.gui.IconToggleButton;
25import org.openstreetmap.josm.gui.layer.Layer;
26import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
27import org.openstreetmap.josm.plugins.Plugin;
28import org.openstreetmap.josm.plugins.PluginInformation;
29import org.openstreetmap.josm.data.projection.*;
30
31/**
32 *
33 * Plugin to access the French Cadastre WMS server at www.cadastre.gouv.fr This
34 * WMS server requires some specific handling like retrieving a cookie for a
35 * limitation in case of no activity, or the request to the server shall provide
36 * a city/town/village code.
37 *
38 * @author Pieren <pieren3@gmail.com>,
39 * @author <matthieu.lochegnies@gmail.com> for the extension to codeCommune
40 * @version 0.8
41 * History:
42 * 0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
43 * 0.2 22-Jun-2008 first stable version
44 * 0.3 24-Jun-2008 add code departement
45 * 0.4 06-Jul-2008 - add images scales, icons, menu items disabling
46 * - remove dependencies of wmsplugin
47 * - add option to force a Lambert zone (for median locations)
48 * - add auto-sourcing
49 * 0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
50 * - no overlapping of grabbed images if transparency is enabled
51 * - set one layer per location
52 * - use utf-8 charset in POST request to server
53 * - improve the preferences setting dialog
54 * - cancel the current download is now possible
55 * - add automatic images caching and load on request (+ manage cache directory size)
56 * - enable auto-sourcing only if a WMS layer is used
57 * 0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
58 * - process the overlapping images when cache is loaded from disk
59 * - save the last 'new location request' text again in preferences
60 * - avoid duplicate layers with same name
61 * - set text input for new locations in upper case
62 * - the cache directory is configurable in "cadastrewms.cacheDir"
63 * - improve configuration change updates
64 * 0.7 24-Aug-2008 - mask images only if transparency enabled
65 * - validate projection name by Lambert.toString() method
66 * 0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
67 * - new possible grab factor of 100 square meters fixed size
68 * - minor fixes due to changes in JOSM core classes
69 * - first draft of raster image support
70 * 0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
71 * and simplify
72 * 1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
73 * - increase PNG picture size requested to WMS (800x1000)
74 * - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
75 * 1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
76 * - propose to use shortcut F11 for grabbing
77 * 1.2 16-Aug-2009 - implementation of raster image grabbing, cropping and georeferencing (not the
78 * overview rasters (Tableau d'assemblage) but directly small units (Feuille)
79 * 1.3 23-Aug-2009 - improve georeferencing action cancellation
80 * - fixed bug of raster image loaded from cache not working on Java1.6
81 * - improve mouse click bounce detection during georeferencing process
82 * 1.4 25-Oct-2009 - add support for new Lambert CC 9 Zones projection
83 * - add optional crosspieces display on raster image layers
84 * - add automatic raster images georeferencing when WMS provides data
85 * - re-implement manual adjustment mode in raster image layer
86 * 1.5 21-Nov-2009 - major changes in projection in core : no magical zone prediction anymore for
87 * Lambert 4 and 9 zones; grid translation implemented for Lambert 4 zones;
88 * support of subprojections in preferences for zones setting and UTM20N
89 * - removed autosourcing of empty new nodes
90 * 1.6 28-Nov-2009 - Fix minor issues if Grab is called without layer (possible since projection rework)
91 * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
92 * 1.8 xxx - filter the mouse button 1 during georeferencing
93 * - retry if getting a new cookie failed (10 times during 30 seconds)
94 * - cookie expiration automatically detected and renewed (after 30 minutes)
95 * - proper WMS layer cleanup at destruction (workaround for memory leak)
96 * - new cache format (v3) storing original image and cropped image bbox + angle
97 * - new cache format (v4) storing original image size for later rotation
98 * - cache files read compatible with previous formats
99 * - raster image rotation issues fixed, now using shift+ctrl key instead of ctrl
100 * - raster image adjustment using default system menu modifier (ctrl for windows) for Mac support
101 * - image resolution configurable (high, medium, low) like the online interface
102 * - layer selection configurable for vectorized images
103 * - from Erik Amzallag:
104 * - possibility to modify the auto-sourcing text just before upload
105 * - from Clément Ménier:
106 * - new option allowing an auto-selection of the first cadastre layer for grab
107 * - non-modal JDialog in MenuActionGrabPlanImage
108 */
109public class CadastrePlugin extends Plugin {
110 static String VERSION = "1.8";
111
112 static JMenu cadastreJMenu;
113
114 public static CadastreGrabber cadastreGrabber = new CadastreGrabber();
115
116 public static String source = "";
117
118 // true if the checkbox "auto-sourcing" is set in the plugin menu
119 public static boolean autoSourcing = false;
120
121 // true when the plugin is first used, e.g. grab from WMS or download cache file
122 public static boolean pluginUsed = false;
123
124 public static String cacheDir = null;
125
126 public static boolean alterColors = false;
127
128 public static boolean backgroundTransparent = false;
129
130 public static float transparency = 1.0f;
131
132 public static boolean drawBoundaries = false;
133
134 public static int imageWidth, imageHeight;
135
136 public static String grabLayers, grabStyles = null;
137
138 static private boolean menuEnabled = false;
139
140 /**
141 * Creates the plugin and setup the default settings if necessary
142 *
143 * @throws Exception
144 */
145 public CadastrePlugin(PluginInformation info) throws Exception {
146 super(info);
147 System.out.println("Pluging cadastre-fr v"+VERSION+" started...");
148 if (Main.pref.get("cadastrewms.cacheDir").equals(""))
149 cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
150 else {
151 cacheDir = Main.pref.get("cadastrewms.cacheDir");
152 if (cacheDir.charAt(cacheDir.length()-1) != '\\' )
153 cacheDir += '\\';
154 }
155 System.out.println("current cache directory: "+cacheDir);
156
157 refreshConfiguration();
158
159 UploadAction.registerUploadHook(new CheckSourceUploadHook());
160
161 }
162
163 public static void refreshMenu() {
164 MainMenu menu = Main.main.menu;
165
166 if (cadastreJMenu == null) {
167 cadastreJMenu = menu.addMenu(marktr("Cadastre"), KeyEvent.VK_C, menu.defaultMenuPos, ht("/Plugin/CadastreFr"));
168 JosmAction grab = new MenuActionGrab();
169 JMenuItem menuGrab = new JMenuItem(grab);
170 KeyStroke ks = grab.getShortcut().getKeyStroke();
171 if (ks != null) {
172 menuGrab.setAccelerator(ks);
173 }
174 JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
175 JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
176 final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
177 menuSource.setSelected(autoSourcing);
178 menuSource.addActionListener(new ActionListener() {
179 public void actionPerformed(ActionEvent ev) {
180 Main.pref.put("cadastrewms.autosourcing", menuSource.isSelected());
181 autoSourcing = menuSource.isSelected();
182 }
183 });
184
185 //JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
186 //JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
187 JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
188 // temporary disabled:
189 //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
190 //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
191
192 cadastreJMenu.add(menuGrab);
193 cadastreJMenu.add(menuActionGrabPlanImage);
194 cadastreJMenu.add(menuSettings);
195 cadastreJMenu.add(menuSource);
196 //cadastreJMenu.add(menuResetCookie); not required any more
197 //cadastreJMenu.add(menuLambertZone);
198 cadastreJMenu.add(menuLoadFromCache);
199 // all SVG features disabled until official WMS is released
200 //cadastreJMenu.add(menuActionBoundaries);
201 //cadastreJMenu.add(menuActionBuildings);
202 }
203 setEnabledAll(menuEnabled);
204 }
205
206 public static void refreshConfiguration() {
207 source = checkSourceMillesime();
208 autoSourcing = Main.pref.getBoolean("cadastrewms.autosourcing", true);
209 alterColors = Main.pref.getBoolean("cadastrewms.alterColors");
210 drawBoundaries = Main.pref.getBoolean("cadastrewms.drawBoundaries", false);
211 if (alterColors) {
212 backgroundTransparent = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
213 transparency = Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"));
214 } else {
215 backgroundTransparent = false;
216 transparency = 1.0f;
217 }
218 String currentResolution = Main.pref.get("cadastrewms.resolution", "high");
219 if (currentResolution.equals("high")) {
220 imageWidth = 1000; imageHeight = 800;
221 } else if (currentResolution.equals("medium")){
222 imageWidth = 800; imageHeight = 600;
223 } else {
224 imageWidth = 600; imageHeight = 400;
225 }
226 grabLayers = "";
227 grabStyles = "";
228 if (Main.pref.getBoolean("cadastrewms.layerWater", true)) {
229 grabLayers += "CDIF:LS3,";
230 grabStyles += "LS3_90,";
231 }
232 if (Main.pref.getBoolean("cadastrewms.layerBuilding", true)) {
233 grabLayers += "CDIF:LS2,";
234 grabStyles += "LS2_90,";
235 }
236 if (Main.pref.getBoolean("cadastrewms.layerSymbol", true)) {
237 grabLayers += "CDIF:LS1,";
238 grabStyles += "LS1_90,";
239 }
240 if (Main.pref.getBoolean("cadastrewms.layerParcel", true)) {
241 grabLayers += "CDIF:PARCELLE,";
242 grabStyles += "PARCELLE_90,";
243 }
244 if (Main.pref.getBoolean("cadastrewms.layerNumero", true)) {
245 grabLayers += "CDIF:NUMERO,";
246 grabStyles += "NUMERO_90,";
247 }
248 if (Main.pref.getBoolean("cadastrewms.layerLabel", true)) {
249 grabLayers += "CDIF:PT3,CDIF:PT2,CDIF:PT1,";
250 grabStyles += "PT3_90,PT2_90,PT1_90,";
251 }
252 if (Main.pref.getBoolean("cadastrewms.layerLieudit", true)) {
253 grabLayers += "CDIF:LIEUDIT,";
254 grabStyles += "NUMERO_90,";
255 }
256 if (Main.pref.getBoolean("cadastrewms.layerSection", true)) {
257 grabLayers += "CDIF:SUBSECTION,CDIF:SECTION,";
258 grabStyles += "SUBSECTION_90,SECTION_90,";
259 }
260 if (Main.pref.getBoolean("cadastrewms.layerCommune", true)) {
261 grabLayers += "CDIF:COMMUNE,";
262 grabStyles += "COMMUNE_90,";
263 }
264 if (grabLayers.length() > 0) { // remove the last ','
265 grabLayers = grabLayers.substring(0, grabLayers.length()-1);
266 grabStyles = grabStyles.substring(0, grabStyles.length()-1);
267 }
268
269 // overwrite F11 shortcut used from the beginning by this plugin and recently used
270 // for full-screen switch in JOSM core
271 int i = 0;
272 String p = Main.pref.get("shortcut.shortcut."+i, null);
273 boolean alreadyRedefined = false;
274 while (p != null) {
275 String[] s = p.split(";");
276 alreadyRedefined = alreadyRedefined || s[0].equals("menu:view:fullscreen");
277 i++;
278 p = Main.pref.get("shortcut.shortcut."+i, null);
279 }
280 if (!alreadyRedefined) {
281 int reply = JOptionPane.showConfirmDialog(null,
282 tr("Plugin cadastre-fr used traditionaly for grabbing the key shortcut F11\n"+
283 "which is currently allocated for full-screen switch by default\n"+
284 "Would you like to restore F11 for grab action ?"),
285 tr("Restore grab shortcut F11"),
286 JOptionPane.YES_NO_OPTION);
287 if (reply == JOptionPane.OK_OPTION) {
288 System.out.println("redefine fullscreen shortcut F11 to shift+F11");
289 Main.pref.put("shortcut.shortcut."+i, "menu:view:fullscreen;Toggle Full Screen view;122;5;122;64;false;true");
290 JOptionPane.showMessageDialog(Main.parent,tr("JOSM is stopped for the change to take effect."));
291 System.exit(0);
292 }
293 } else
294 System.out.println("shortcut F11 already redefined; do not change");
295
296 refreshMenu();
297 }
298
299 @Override
300 public PreferenceSetting getPreferenceSetting() {
301 return new CadastrePreferenceSetting();
302 }
303
304 private static void setEnabledAll(boolean isEnabled) {
305 for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
306 JMenuItem item = cadastreJMenu.getItem(i);
307 if (item != null)
308 if (item.getText().equals(MenuActionGrabPlanImage.name) /*||
309 item.getText().equals(MenuActionGrab.name) ||
310 item.getText().equals(MenuActionBoundaries.name) ||
311 item.getText().equals(MenuActionBuildings.name)*/) {
312 item.setEnabled(isEnabled);
313 }
314 }
315 menuEnabled = isEnabled;
316 }
317
318 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
319 if (cadastreJMenu != null) {
320 if (oldFrame == null && newFrame != null) {
321 setEnabledAll(true);
322 Main.map.addMapMode(new IconToggleButton
323 (new WMSAdjustAction(Main.map)));
324 } else if (oldFrame != null && newFrame == null) {
325 setEnabledAll(false);
326 //Lambert.layoutZone = -1;
327 //LambertCC9Zones.layoutZone = -1;
328 }
329 }
330 }
331
332 public static boolean isCadastreProjection() {
333 return Main.proj.toString().equals(new Lambert().toString())
334 || Main.proj.toString().equals(new UTM_20N_France_DOM().toString())
335 || Main.proj.toString().equals(new GaussLaborde_Reunion().toString())
336 || Main.proj.toString().equals(new LambertCC9Zones().toString());
337 }
338
339 public static void safeSleep(long milliseconds) {
340 try {
341 Thread.sleep(milliseconds);
342 } catch (InterruptedException e) {}
343 }
344
345 // See OptionPaneUtil
346 // FIXME: this is a temporary solution.
347 public static void prepareDialog(JDialog dialog) {
348 if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
349 try {
350 dialog.setAlwaysOnTop(true);
351 } catch(SecurityException e) {
352 System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
353 }
354 }
355 dialog.setModal(true);
356 dialog.toFront();
357 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
358 }
359
360 /**
361 * Adds the WMSLayer following this rule:<br/>
362 * - if a WMSLayer exists place this new layer just before this layer<br/>
363 * - Otherwise place it at the bottom
364 * @param wmsLayer the wmsLayer to add
365 */
366 public static void addWMSLayer(WMSLayer wmsLayer) {
367 if (Main.map != null && Main.map.mapView != null) {
368 int wmsNewLayerPos = Main.map.mapView.getAllLayers().size();
369 for(Layer l : Main.map.mapView.getLayersOfType(WMSLayer.class)) {
370 int wmsPos = Main.map.mapView.getLayerPos(l);
371 if (wmsPos < wmsNewLayerPos) wmsNewLayerPos = wmsPos;
372 }
373 Main.main.addLayer(wmsLayer);
374 // Move the layer to its new position
375 Main.map.mapView.moveLayer(wmsLayer, wmsNewLayerPos);
376 } else
377 Main.main.addLayer(wmsLayer);
378 }
379
380 private static String checkSourceMillesime() {
381 java.util.Calendar calendar = java.util.Calendar.getInstance();
382 int currentYear = calendar.get(java.util.Calendar.YEAR);
383 String src = Main.pref.get("cadastrewms.source",
384 "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre. Mise \u00e0 jour : AAAA");
385 String srcYear = src.substring(src.lastIndexOf(" ")+1);
386 Integer year = null;
387 try {
388 year = Integer.decode(srcYear);
389 } catch (NumberFormatException e) {}
390 if (srcYear.equals("AAAA") || (year != null && year < currentYear)) {
391 System.out.println("Replace source year "+srcYear+" by current year "+currentYear);
392 src = src.substring(0, src.lastIndexOf(" ")+1)+currentYear;
393 Main.pref.put("cadastrewms.source", src);
394 }
395 return src;
396 }
397
398}
Note: See TracBrowser for help on using the repository browser.