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