source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java@ 12735

Last change on this file since 12735 was 12735, checked in by bastiK, 7 years ago

see #15229 - move CoordinateFormat code out of LatLon class

  • Property svn:eol-style set to native
File size: 22.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.projection;
3
4import static org.openstreetmap.josm.data.SystemOfMeasurement.ALL_SYSTEMS;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Component;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionListener;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17import javax.swing.BorderFactory;
18import javax.swing.JButton;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.JSeparator;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.actions.ExpertToggleAction;
26import org.openstreetmap.josm.data.Bounds;
27import org.openstreetmap.josm.data.SystemOfMeasurement;
28import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
29import org.openstreetmap.josm.data.coor.conversion.ICoordinateFormat;
30import org.openstreetmap.josm.data.preferences.CollectionProperty;
31import org.openstreetmap.josm.data.preferences.StringProperty;
32import org.openstreetmap.josm.data.projection.CustomProjection;
33import org.openstreetmap.josm.data.projection.Projection;
34import org.openstreetmap.josm.gui.ExtendedDialog;
35import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
36import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
37import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
38import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
39import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
40import org.openstreetmap.josm.gui.widgets.JosmComboBox;
41import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
42import org.openstreetmap.josm.tools.GBC;
43import org.openstreetmap.josm.tools.JosmRuntimeException;
44
45/**
46 * Projection preferences.
47 *
48 * How to add new Projections:
49 * - Find EPSG code for the projection.
50 * - Look up the parameter string for Proj4, e.g. on http://spatialreference.org/
51 * and add it to the file 'data/projection/epsg' in JOSM trunk
52 * - Search for official references and verify the parameter values. These
53 * documents are often available in the local language only.
54 * - Use {@link #registerProjectionChoice}, to make the entry known to JOSM.
55 *
56 * In case there is no EPSG code:
57 * - override {@link AbstractProjectionChoice#getProjection()} and provide
58 * a manual implementation of the projection. Use {@link CustomProjection}
59 * if possible.
60 */
61public class ProjectionPreference implements SubPreferenceSetting {
62
63 /**
64 * Factory used to create a new {@code ProjectionPreference}.
65 */
66 public static class Factory implements PreferenceSettingFactory {
67 @Override
68 public PreferenceSetting createPreferenceSetting() {
69 return new ProjectionPreference();
70 }
71 }
72
73 private static final List<ProjectionChoice> projectionChoices = new ArrayList<>();
74 private static final Map<String, ProjectionChoice> projectionChoicesById = new HashMap<>();
75
76 /**
77 * WGS84: Directly use latitude / longitude values as x/y.
78 */
79 public static final ProjectionChoice wgs84 = registerProjectionChoice(tr("WGS84 Geographic"), "core:wgs84", 4326, "epsg4326");
80
81 /**
82 * Mercator Projection.
83 *
84 * The center of the mercator projection is always the 0 grad coordinate.
85 *
86 * See also USGS Bulletin 1532 (http://pubs.usgs.gov/bul/1532/report.pdf)
87 * initially EPSG used 3785 but that has been superseded by 3857, see https://www.epsg-registry.org/
88 */
89 public static final ProjectionChoice mercator = registerProjectionChoice(tr("Mercator"), "core:mercator", 3857);
90
91 /**
92 * Lambert conic conform 4 zones using the French geodetic system NTF.
93 *
94 * This newer version uses the grid translation NTF&lt;-&gt;RGF93 provided by IGN for a submillimetric accuracy.
95 * (RGF93 is the French geodetic system similar to WGS84 but not mathematically equal)
96 *
97 * Source: http://geodesie.ign.fr/contenu/fichiers/Changement_systeme_geodesique.pdf
98 */
99 public static final ProjectionChoice lambert = new LambertProjectionChoice();
100
101 /**
102 * French departements in the Caribbean Sea and Indian Ocean.
103 *
104 * Using the UTM transvers Mercator projection and specific geodesic settings.
105 */
106 public static final ProjectionChoice utm_france_dom = new UTMFranceDOMProjectionChoice();
107
108 /**
109 * Lambert Conic Conform 9 Zones projection.
110 *
111 * As specified by the IGN in this document
112 * http://geodesie.ign.fr/contenu/fichiers/documentation/rgf93/cc9zones.pdf
113 */
114 public static final ProjectionChoice lambert_cc9 = new LambertCC9ZonesProjectionChoice();
115
116 static {
117
118 /************************
119 * Global projections.
120 */
121
122 /**
123 * UTM.
124 */
125 registerProjectionChoice(new UTMProjectionChoice());
126
127 /************************
128 * Regional - alphabetical order by country code.
129 */
130
131 /**
132 * Belgian Lambert 72 projection.
133 *
134 * As specified by the Belgian IGN in this document:
135 * http://www.ngi.be/Common/Lambert2008/Transformation_Geographic_Lambert_FR.pdf
136 *
137 * @author Don-vip
138 */
139 registerProjectionChoice(tr("Belgian Lambert 1972"), "core:belgianLambert1972", 31370); // BE
140
141 /**
142 * Belgian Lambert 2008 projection.
143 *
144 * As specified by the Belgian IGN in this document:
145 * http://www.ngi.be/Common/Lambert2008/Transformation_Geographic_Lambert_FR.pdf
146 *
147 * @author Don-vip
148 */
149 registerProjectionChoice(tr("Belgian Lambert 2008"), "core:belgianLambert2008", 3812); // BE
150
151 /**
152 * SwissGrid CH1903 / L03, see https://en.wikipedia.org/wiki/Swiss_coordinate_system.
153 *
154 * Actually, what we have here, is CH1903+ (EPSG:2056), but without
155 * the additional false easting of 2000km and false northing 1000 km.
156 *
157 * To get to CH1903, a shift file is required. So currently, there are errors
158 * up to 1.6m (depending on the location).
159 */
160 registerProjectionChoice(new SwissGridProjectionChoice()); // CH
161
162 registerProjectionChoice(new GaussKruegerProjectionChoice()); // DE
163
164 /**
165 * Estonian Coordinate System of 1997.
166 *
167 * Thanks to Johan Montagnat and its geoconv java converter application
168 * (https://www.i3s.unice.fr/~johan/gps/ , published under GPL license)
169 * from which some code and constants have been reused here.
170 */
171 registerProjectionChoice(tr("Lambert Zone (Estonia)"), "core:lambertest", 3301); // EE
172
173 /**
174 * Lambert conic conform 4 zones using the French geodetic system NTF.
175 *
176 * This newer version uses the grid translation NTF<->RGF93 provided by IGN for a submillimetric accuracy.
177 * (RGF93 is the French geodetic system similar to WGS84 but not mathematically equal)
178 *
179 * Source: http://geodesie.ign.fr/contenu/fichiers/Changement_systeme_geodesique.pdf
180 * @author Pieren
181 */
182 registerProjectionChoice(lambert); // FR
183
184 /**
185 * Lambert 93 projection.
186 *
187 * As specified by the IGN in this document
188 * http://geodesie.ign.fr/contenu/fichiers/documentation/rgf93/Lambert-93.pdf
189 * @author Don-vip
190 */
191 registerProjectionChoice(tr("Lambert 93 (France)"), "core:lambert93", 2154); // FR
192
193 /**
194 * Lambert Conic Conform 9 Zones projection.
195 *
196 * As specified by the IGN in this document
197 * http://geodesie.ign.fr/contenu/fichiers/documentation/rgf93/cc9zones.pdf
198 * @author Pieren
199 */
200 registerProjectionChoice(lambert_cc9); // FR
201
202 /**
203 * French departements in the Caribbean Sea and Indian Ocean.
204 *
205 * Using the UTM transvers Mercator projection and specific geodesic settings.
206 */
207 registerProjectionChoice(utm_france_dom); // FR
208
209 /**
210 * LKS-92/ Latvia TM projection.
211 *
212 * Based on data from spatialreference.org.
213 * http://spatialreference.org/ref/epsg/3059/
214 *
215 * @author Viesturs Zarins
216 */
217 registerProjectionChoice(tr("LKS-92 (Latvia TM)"), "core:tmerclv", 3059); // LV
218
219 /**
220 * Netherlands RD projection
221 *
222 * @author vholten
223 */
224 registerProjectionChoice(tr("Rijksdriehoekscoördinaten (Netherlands)"), "core:dutchrd", 28992); // NL
225
226 /**
227 * PUWG 1992 and 2000 are the official cordinate systems in Poland.
228 *
229 * They use the same math as UTM only with different constants.
230 *
231 * @author steelman
232 */
233 registerProjectionChoice(new PuwgProjectionChoice()); // PL
234
235 /**
236 * SWEREF99 13 30 projection. Based on data from spatialreference.org.
237 * http://spatialreference.org/ref/epsg/3008/
238 *
239 * @author Hanno Hecker
240 */
241 registerProjectionChoice(tr("SWEREF99 13 30 / EPSG:3008 (Sweden)"), "core:sweref99", 3008); // SE
242
243 /************************
244 * Projection by Code.
245 */
246 registerProjectionChoice(new CodeProjectionChoice());
247
248 /************************
249 * Custom projection.
250 */
251 registerProjectionChoice(new CustomProjectionChoice());
252 }
253
254 public static void registerProjectionChoice(ProjectionChoice c) {
255 projectionChoices.add(c);
256 projectionChoicesById.put(c.getId(), c);
257 }
258
259 public static ProjectionChoice registerProjectionChoice(String name, String id, Integer epsg, String cacheDir) {
260 ProjectionChoice pc = new SingleProjectionChoice(name, id, "EPSG:"+epsg, cacheDir);
261 registerProjectionChoice(pc);
262 return pc;
263 }
264
265 private static ProjectionChoice registerProjectionChoice(String name, String id, Integer epsg) {
266 ProjectionChoice pc = new SingleProjectionChoice(name, id, "EPSG:"+epsg);
267 registerProjectionChoice(pc);
268 return pc;
269 }
270
271 public static List<ProjectionChoice> getProjectionChoices() {
272 return Collections.unmodifiableList(projectionChoices);
273 }
274
275 private static String projectionChoice;
276
277 private static final StringProperty PROP_PROJECTION_DEFAULT = new StringProperty("projection.default", mercator.getId());
278 private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
279 private static final CollectionProperty PROP_SUB_PROJECTION_DEFAULT = new CollectionProperty("projection.default.sub", null);
280 private static final String[] unitsValues = ALL_SYSTEMS.keySet().toArray(new String[ALL_SYSTEMS.size()]);
281 private static final String[] unitsValuesTr = new String[unitsValues.length];
282 static {
283 for (int i = 0; i < unitsValues.length; ++i) {
284 unitsValuesTr[i] = tr(unitsValues[i]);
285 }
286 }
287
288 /**
289 * Combobox with all projections available
290 */
291 private final JosmComboBox<ProjectionChoice> projectionCombo;
292
293 /**
294 * Combobox with all coordinate display possibilities
295 */
296 private final JosmComboBox<ICoordinateFormat> coordinatesCombo;
297
298 private final JosmComboBox<String> unitsCombo = new JosmComboBox<>(unitsValuesTr);
299
300 /**
301 * This variable holds the JPanel with the projection's preferences. If the
302 * selected projection does not implement this, it will be set to an empty
303 * Panel.
304 */
305 private JPanel projSubPrefPanel;
306 private final JPanel projSubPrefPanelWrapper = new JPanel(new GridBagLayout());
307
308 private final JLabel projectionCodeLabel = new JLabel(tr("Projection code"));
309 private final Component projectionCodeGlue = GBC.glue(5, 0);
310 private final JLabel projectionCode = new JLabel();
311 private final JLabel projectionNameLabel = new JLabel(tr("Projection name"));
312 private final Component projectionNameGlue = GBC.glue(5, 0);
313 private final JLabel projectionName = new JLabel();
314 private final JLabel bounds = new JLabel();
315
316 /**
317 * This is the panel holding all projection preferences
318 */
319 private final VerticallyScrollablePanel projPanel = new VerticallyScrollablePanel(new GridBagLayout());
320
321 /**
322 * The GridBagConstraints for the Panel containing the ProjectionSubPrefs.
323 * This is required twice in the code, creating it here keeps both occurrences
324 * in sync
325 */
326 private static final GBC projSubPrefPanelGBC = GBC.std().fill(GBC.BOTH).weight(1.0, 1.0);
327
328 public ProjectionPreference() {
329 this.projectionCombo = new JosmComboBox<>(
330 projectionChoices.toArray(new ProjectionChoice[projectionChoices.size()]));
331 this.coordinatesCombo = new JosmComboBox<>(
332 CoordinateFormatManager.getCoordinateFormats().toArray(new ICoordinateFormat[0]));
333 }
334
335 @Override
336 public void addGui(PreferenceTabbedPane gui) {
337 final ProjectionChoice pc = setupProjectionCombo();
338
339 for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
340 if (coordinatesCombo.getItemAt(i).getId().equals(PROP_COORDINATES.get())) {
341 coordinatesCombo.setSelectedIndex(i);
342 break;
343 }
344 }
345
346 for (int i = 0; i < unitsValues.length; ++i) {
347 if (unitsValues[i].equals(SystemOfMeasurement.PROP_SYSTEM_OF_MEASUREMENT.get())) {
348 unitsCombo.setSelectedIndex(i);
349 break;
350 }
351 }
352
353 projPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
354 projPanel.add(new JLabel(tr("Projection method")), GBC.std().insets(5, 5, 0, 5));
355 projPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
356 projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
357 projPanel.add(projectionCodeLabel, GBC.std().insets(25, 5, 0, 5));
358 projPanel.add(projectionCodeGlue, GBC.std().fill(GBC.HORIZONTAL));
359 projPanel.add(projectionCode, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
360 projPanel.add(projectionNameLabel, GBC.std().insets(25, 5, 0, 5));
361 projPanel.add(projectionNameGlue, GBC.std().fill(GBC.HORIZONTAL));
362 projPanel.add(projectionName, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
363 projPanel.add(new JLabel(tr("Bounds")), GBC.std().insets(25, 5, 0, 5));
364 projPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
365 projPanel.add(bounds, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
366 projPanel.add(projSubPrefPanelWrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(20, 5, 5, 5));
367
368 projectionCodeLabel.setLabelFor(projectionCode);
369 projectionNameLabel.setLabelFor(projectionName);
370
371 JButton btnSetAsDefault = new JButton(tr("Set as default"));
372 projPanel.add(btnSetAsDefault, GBC.eol().insets(5, 10, 5, 5));
373 btnSetAsDefault.addActionListener(e -> {
374 ProjectionChoice pc2 = (ProjectionChoice) projectionCombo.getSelectedItem();
375 String id = pc2.getId();
376 Collection<String> prefs = pc2.getPreferences(projSubPrefPanel);
377 setProjection(id, prefs, true);
378 pc2.setPreferences(prefs);
379 Projection proj = pc2.getProjection();
380 new ExtendedDialog(gui, tr("Default projection"), tr("OK"))
381 .setButtonIcons("ok")
382 .setIcon(JOptionPane.INFORMATION_MESSAGE)
383 .setContent(tr("Default projection has been set to ''{0}''", proj.toCode()))
384 .showDialog();
385 });
386 ExpertToggleAction.addVisibilitySwitcher(btnSetAsDefault);
387
388 projPanel.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 10));
389 projPanel.add(new JLabel(tr("Display coordinates as")), GBC.std().insets(5, 5, 0, 5));
390 projPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
391 projPanel.add(coordinatesCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
392 projPanel.add(new JLabel(tr("System of measurement")), GBC.std().insets(5, 5, 0, 5));
393 projPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
394 projPanel.add(unitsCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0, 5, 5, 5));
395 projPanel.add(GBC.glue(1, 1), GBC.std().fill(GBC.HORIZONTAL).weight(1.0, 1.0));
396
397 gui.getMapPreference().addSubTab(this, tr("Map Projection"), projPanel.getVerticalScrollPane());
398
399 selectedProjectionChanged(pc);
400 }
401
402 private void updateMeta(ProjectionChoice pc) {
403 pc.setPreferences(pc.getPreferences(projSubPrefPanel));
404 Projection proj = pc.getProjection();
405 projectionCode.setText(proj.toCode());
406 projectionName.setText(proj.toString());
407 Bounds b = proj.getWorldBoundsLatLon();
408 ICoordinateFormat cf = CoordinateFormatManager.getDefaultFormat();
409 bounds.setText(cf.lonToString(b.getMin()) + ", " + cf.latToString(b.getMin()) + " : " +
410 cf.lonToString(b.getMax()) + ", " + cf.latToString(b.getMax()));
411 boolean showCode = true;
412 boolean showName = false;
413 if (pc instanceof SubPrefsOptions) {
414 showCode = ((SubPrefsOptions) pc).showProjectionCode();
415 showName = ((SubPrefsOptions) pc).showProjectionName();
416 }
417 projectionCodeLabel.setVisible(showCode);
418 projectionCodeGlue.setVisible(showCode);
419 projectionCode.setVisible(showCode);
420 projectionNameLabel.setVisible(showName);
421 projectionNameGlue.setVisible(showName);
422 projectionName.setVisible(showName);
423 }
424
425 @Override
426 public boolean ok() {
427 ProjectionChoice pc = (ProjectionChoice) projectionCombo.getSelectedItem();
428
429 String id = pc.getId();
430 Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
431
432 setProjection(id, prefs, false);
433
434 if (PROP_COORDINATES.put(((ICoordinateFormat) coordinatesCombo.getSelectedItem()).getId())) {
435 CoordinateFormatManager.setCoordinateFormat((ICoordinateFormat) coordinatesCombo.getSelectedItem());
436 }
437
438 int i = unitsCombo.getSelectedIndex();
439 SystemOfMeasurement.setSystemOfMeasurement(unitsValues[i]);
440
441 return false;
442 }
443
444 public static void setProjection() {
445 setProjection(PROP_PROJECTION_DEFAULT.get(), PROP_SUB_PROJECTION_DEFAULT.get(), false);
446 }
447
448 /**
449 * Set projection.
450 * @param id id of the selected projection choice
451 * @param pref the configuration for the selected projection choice
452 * @param makeDefault true, if it is to be set as permanent default
453 * false, if it is to be set for the current session
454 * @since 12306
455 */
456 public static void setProjection(String id, Collection<String> pref, boolean makeDefault) {
457 ProjectionChoice pc = projectionChoicesById.get(id);
458
459 if (pc == null) {
460 JOptionPane.showMessageDialog(
461 Main.parent,
462 tr("The projection {0} could not be activated. Using Mercator", id),
463 tr("Error"),
464 JOptionPane.ERROR_MESSAGE
465 );
466 pref = null;
467 pc = mercator;
468 }
469 id = pc.getId();
470 Main.pref.putCollection("projection.sub."+id, pref);
471 if (makeDefault) {
472 PROP_PROJECTION_DEFAULT.put(id);
473 PROP_SUB_PROJECTION_DEFAULT.put(pref);
474 } else {
475 projectionChoice = id;
476 }
477 pc.setPreferences(pref);
478 Projection proj = pc.getProjection();
479 Main.setProjection(proj);
480 }
481
482 /**
483 * Handles all the work related to update the projection-specific
484 * preferences
485 * @param pc the choice class representing user selection
486 */
487 private void selectedProjectionChanged(final ProjectionChoice pc) {
488 // Don't try to update if we're still starting up
489 int size = projPanel.getComponentCount();
490 if (size < 1)
491 return;
492
493 final ActionListener listener = e -> updateMeta(pc);
494
495 // Replace old panel with new one
496 projSubPrefPanelWrapper.removeAll();
497 projSubPrefPanel = pc.getPreferencePanel(listener);
498 projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
499 projPanel.revalidate();
500 projSubPrefPanel.repaint();
501 updateMeta(pc);
502 }
503
504 /**
505 * Sets up projection combobox with default values and action listener
506 * @return the choice class for user selection
507 */
508 private ProjectionChoice setupProjectionCombo() {
509 String pcId = getCurrentProjectionChoiceId();
510 ProjectionChoice pc = null;
511 for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
512 ProjectionChoice pc1 = projectionCombo.getItemAt(i);
513 pc1.setPreferences(getSubprojectionPreference(pc1.getId()));
514 if (pc1.getId().equals(pcId)) {
515 projectionCombo.setSelectedIndex(i);
516 selectedProjectionChanged(pc1);
517 pc = pc1;
518 }
519 }
520 // If the ProjectionChoice from the preferences is not available, it
521 // should have been set to Mercator at JOSM start.
522 if (pc == null)
523 throw new JosmRuntimeException("Couldn't find the current projection in the list of available projections!");
524
525 projectionCombo.addActionListener(e -> {
526 ProjectionChoice pc1 = (ProjectionChoice) projectionCombo.getSelectedItem();
527 selectedProjectionChanged(pc1);
528 });
529 return pc;
530 }
531
532 /**
533 * Get the id of the projection choice that is currently set.
534 * @return id of the projection choice that is currently set
535 */
536 public static String getCurrentProjectionChoiceId() {
537 return projectionChoice != null ? projectionChoice : PROP_PROJECTION_DEFAULT.get();
538 }
539
540 /**
541 * Get the preferences that have been selected the last time for the given
542 * projection choice.
543 * @param pcId id of the projection choice
544 * @return projection choice parameters that have been selected by the user
545 * the last time; null if user has never selected the given projection choice
546 */
547 public static Collection<String> getSubprojectionPreference(String pcId) {
548 return Main.pref.getCollection("projection.sub."+pcId, null);
549 }
550
551 @Override
552 public boolean isExpert() {
553 return false;
554 }
555
556 @Override
557 public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
558 return gui.getMapPreference();
559 }
560
561 /**
562 * Selects the given projection.
563 * @param projection The projection to select.
564 * @since 5604
565 */
566 public void selectProjection(ProjectionChoice projection) {
567 if (projectionCombo != null && projection != null) {
568 projectionCombo.setSelectedItem(projection);
569 }
570 }
571}
Note: See TracBrowser for help on using the repository browser.