source: josm/trunk/src/org/openstreetmap/josm/data/projection/GaussKrueger.java@ 5072

Last change on this file since 5072 was 5066, checked in by bastiK, 12 years ago

Proj parameter refactoring (see #7495)

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.event.ActionListener;
8import java.io.InputStream;
9import java.util.Collection;
10import java.util.Collections;
11
12import javax.swing.JComboBox;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.projection.proj.ProjParameters;
20import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
21import org.openstreetmap.josm.tools.GBC;
22
23public class GaussKrueger extends AbstractProjection implements ProjectionSubPrefs {
24
25 public static final int DEFAULT_ZONE = 2;
26 private int zone;
27
28 private static Bounds[] bounds = {
29 new Bounds(new LatLon(-5, 3.5), new LatLon(85, 8.5)),
30 new Bounds(new LatLon(-5, 6.5), new LatLon(85, 11.5)),
31 new Bounds(new LatLon(-5, 9.5), new LatLon(85, 14.5)),
32 new Bounds(new LatLon(-5, 12.5), new LatLon(85, 17.5)),
33 };
34
35 private static NTV2GridShiftFile BETA2007 = null;
36
37 private static String[] zones = { "2", "3", "4", "5" };
38
39 public GaussKrueger() {
40 this(DEFAULT_ZONE);
41 }
42
43 public GaussKrueger(int zone) {
44 if (BETA2007 == null) {
45 try {
46 String gridFileName = "BETA2007.gsb";
47 InputStream is = Main.class.getResourceAsStream("/data/"+gridFileName);
48 if (is == null)
49 throw new RuntimeException(tr("Error: failed to open input stream for resource ''/data/{0}''.", gridFileName));
50 BETA2007 = new NTV2GridShiftFile();
51 BETA2007.loadGridShiftFile(is, false);
52 } catch (Exception e) {
53 throw new RuntimeException(e);
54 }
55 }
56 updateParameters(zone);
57 }
58
59 private void updateParameters(int zone) {
60 this.zone = zone;
61 ellps = Ellipsoid.Bessel1841;
62 nadgrids = BETA2007;
63 ////less acurrate datum (errors up to 3m):
64 //datum = new SevenParameterDatum(
65 // tr("Deutsches Hauptdreiecksnetz"), null, ellps,
66 // 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.70);
67 proj = new TransverseMercator();
68 try {
69 proj.initialize(new ProjParameters() {{ ellps = GaussKrueger.this.ellps; }});
70 } catch (ProjectionConfigurationException e) {
71 throw new RuntimeException(e);
72 }
73 x_0 = 1000000 * zone + 500000;
74 lon_0 = 3 * zone;
75 }
76
77 @Override
78 public String toString() {
79 return tr("Gau\u00DF-Kr\u00FCger");
80 }
81
82 @Override
83 public Integer getEpsgCode() {
84 return 31464 + zone;
85 }
86
87 @Override
88 public String getCacheDirectoryName() {
89 return "gausskrueger"+zone;
90 }
91
92 @Override
93 public Bounds getWorldBoundsLatLon() {
94 return bounds[zone-2];
95 }
96
97 @Override
98 public void setupPreferencePanel(JPanel p, ActionListener listener) {
99 JComboBox prefcb = new JComboBox(zones);
100
101 prefcb.setSelectedIndex(zone-2);
102 p.setLayout(new GridBagLayout());
103 p.add(new JLabel(tr("GK Zone")), GBC.std().insets(5,5,0,5));
104 p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
105 /* Note: we use component position 2 below to find this again */
106 p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
107 p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
108
109 if (listener != null) {
110 prefcb.addActionListener(listener);
111 }
112 }
113
114 @Override
115 public Collection<String> getPreferences(JPanel p) {
116 Object prefcb = p.getComponent(2);
117 if(!(prefcb instanceof JComboBox))
118 return null;
119 int zone = ((JComboBox)prefcb).getSelectedIndex();
120 return Collections.singleton(Integer.toString(zone+2));
121 }
122
123 @Override
124 public void setPreferences(Collection<String> args) {
125 int zone = DEFAULT_ZONE;
126 if (args != null) {
127 try {
128 for(String s : args)
129 {
130 zone = Integer.parseInt(s);
131 if(zone < 2 || zone > 5) {
132 zone = DEFAULT_ZONE;
133 }
134 break;
135 }
136 } catch(NumberFormatException e) {}
137 }
138 updateParameters(zone);
139 }
140
141 @Override
142 public String[] allCodes() {
143 String[] zones = new String[4];
144 for (int zone = 2; zone <= 5; zone++) {
145 zones[zone-2] = "EPSG:" + (31464 + zone);
146 }
147 return zones;
148 }
149
150 @Override
151 public Collection<String> getPreferencesFromCode(String code)
152 {
153 //zone 2 = EPSG:31466 up to zone 5 = EPSG:31469
154 for (int zone = 2; zone <= 5; zone++) {
155 String epsg = "EPSG:" + (31464 + zone);
156 if (epsg.equals(code))
157 return Collections.singleton(String.valueOf(zone));
158 }
159 return null;
160 }
161
162}
Note: See TracBrowser for help on using the repository browser.