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