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

Last change on this file since 5891 was 5891, checked in by stoecker, 11 years ago

fix javadoc

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