source: josm/trunk/src/org/openstreetmap/josm/data/projection/UTM.java@ 5066

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

Proj parameter refactoring (see #7495)

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11
12import javax.swing.ButtonGroup;
13import javax.swing.JCheckBox;
14import javax.swing.JComboBox;
15import javax.swing.JLabel;
16import javax.swing.JPanel;
17import javax.swing.JRadioButton;
18
19import org.openstreetmap.josm.data.Bounds;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
22import org.openstreetmap.josm.data.projection.proj.ProjParameters;
23import org.openstreetmap.josm.tools.GBC;
24
25/**
26 *
27 * @author Dirk Stöcker
28 * code based on JavaScript from Chuck Taylor
29 *
30 */
31public class UTM extends AbstractProjection implements ProjectionSubPrefs {
32
33 private static final int DEFAULT_ZONE = 30;
34 private int zone;
35
36 public enum Hemisphere { North, South }
37 private static final Hemisphere DEFAULT_HEMISPHERE = Hemisphere.North;
38 private Hemisphere hemisphere;
39
40 /**
41 * Applies an additional false easting of 3000000 m if true.
42 */
43 private boolean offset;
44
45 public UTM() {
46 this(DEFAULT_ZONE, DEFAULT_HEMISPHERE, false);
47 }
48
49 public UTM(int zone, Hemisphere hemisphere, boolean offset) {
50 ellps = Ellipsoid.GRS80;
51 proj = new org.openstreetmap.josm.data.projection.proj.TransverseMercator();
52 try {
53 proj.initialize(new ProjParameters() {{ ellps = UTM.this.ellps; }});
54 } catch (ProjectionConfigurationException e) {
55 throw new RuntimeException(e);
56 }
57 datum = GRS80Datum.INSTANCE;
58 updateParameters(zone, hemisphere, offset);
59 }
60
61 public void updateParameters(int zone, Hemisphere hemisphere, boolean offset) {
62 this.zone = zone;
63 this.hemisphere = hemisphere;
64 this.offset = offset;
65 x_0 = 500000 + (offset ? 3000000 : 0);
66 y_0 = hemisphere == Hemisphere.North ? 0 : 10000000;
67 lon_0 = getUtmCentralMeridianDeg(zone);
68 k_0 = 0.9996;
69 }
70
71 /*
72 * UTMCentralMeridian
73 *
74 * Determines the central meridian for the given UTM zone.
75 *
76 * Inputs:
77 * zone - An integer value designating the UTM zone, range [1,60].
78 *
79 * Returns:
80 * The central meridian for the given UTM zone, in radians, or zero
81 * if the UTM zone parameter is outside the range [1,60].
82 * Range of the central meridian is the radian equivalent of [-177,+177].
83 *
84 */
85 private double getUtmCentralMeridianDeg(int zone)
86 {
87 return -183.0 + (zone * 6.0);
88 }
89
90 public int getzone() {
91 return zone;
92 }
93
94 @Override
95 public String toString() {
96 return tr("UTM");
97 }
98
99 @Override
100 public Integer getEpsgCode() {
101 return ((offset?325800:32600) + getzone() + (hemisphere == Hemisphere.South?100:0));
102 }
103
104 @Override
105 public int hashCode() {
106 return toCode().hashCode();
107 }
108
109 @Override
110 public String getCacheDirectoryName() {
111 return "epsg"+ getEpsgCode();
112 }
113
114 @Override
115 public Bounds getWorldBoundsLatLon()
116 {
117 if (hemisphere == Hemisphere.North)
118 return new Bounds(
119 new LatLon(-5.0, getUtmCentralMeridianDeg(getzone())-5.0),
120 new LatLon(85.0, getUtmCentralMeridianDeg(getzone())+5.0));
121 else
122 return new Bounds(
123 new LatLon(-85.0, getUtmCentralMeridianDeg(getzone())-5.0),
124 new LatLon(5.0, getUtmCentralMeridianDeg(getzone())+5.0));
125 }
126
127 @Override
128 public void setupPreferencePanel(JPanel p, ActionListener listener) {
129 //Zone
130 JComboBox zonecb = new JComboBox();
131 for(int i = 1; i <= 60; i++) {
132 zonecb.addItem(i);
133 }
134
135 zonecb.setSelectedIndex(zone - 1);
136 p.setLayout(new GridBagLayout());
137 p.add(new JLabel(tr("UTM Zone")), GBC.std().insets(5,5,0,5));
138 p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
139 /* Note: we use component position 2 below to find this again */
140 p.add(zonecb, GBC.eop().fill(GBC.HORIZONTAL));
141 p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
142
143 //Hemisphere
144 JRadioButton north = new JRadioButton();
145 north.setSelected(hemisphere == Hemisphere.North);
146 JRadioButton south = new JRadioButton();
147 south.setSelected(hemisphere == Hemisphere.South);
148
149 ButtonGroup group = new ButtonGroup();
150 group.add(north);
151 group.add(south);
152
153 JPanel bPanel = new JPanel();
154 bPanel.setLayout(new GridBagLayout());
155
156 bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
157 bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
158 bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
159 bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
160 bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
161 bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
162
163 p.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5,5,0,5));
164 p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
165 p.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
166 p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
167
168 //Offset
169 JCheckBox offsetBox = new JCheckBox();
170 offsetBox.setSelected(offset);
171
172 p.add(new JLabel(tr("Offset 3.000.000m east")), GBC.std().insets(5,5,0,5));
173 p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
174 /* Note: we use component position 2 below to find this again */
175 p.add(offsetBox, GBC.eop().fill(GBC.HORIZONTAL));
176 p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
177
178 if (listener != null) {
179 north.addActionListener(listener);
180 south.addActionListener(listener);
181 zonecb.addActionListener(listener);
182 offsetBox.addActionListener(listener);
183 }
184 }
185
186 @Override
187 public Collection<String> getPreferences(JPanel p) {
188 int zone = DEFAULT_ZONE;
189 Hemisphere hemisphere = DEFAULT_HEMISPHERE;
190 boolean offset = false;
191
192 Object zonecb = p.getComponent(2);
193 if (zonecb instanceof JComboBox) {
194 zone = ((JComboBox)zonecb).getSelectedIndex() + 1;
195 }
196
197 Object bPanel = p.getComponent(6);
198 if (bPanel instanceof JPanel) {
199 Object south = ((JPanel)bPanel).getComponent(4);
200 if (south instanceof JRadioButton) {
201 hemisphere = ((JRadioButton)south).isSelected()?Hemisphere.South:Hemisphere.North;
202 }
203 }
204
205 Object offsetBox = p.getComponent(10);
206 if (offsetBox instanceof JCheckBox) {
207 offset = ((JCheckBox) offsetBox).isSelected();
208 }
209
210 return Arrays.asList(Integer.toString(zone), hemisphere.toString(), (offset?"offset":"standard"));
211 }
212
213 @Override
214 public void setPreferences(Collection<String> args) {
215 int zone = DEFAULT_ZONE;
216 Hemisphere hemisphere = DEFAULT_HEMISPHERE;
217 boolean offset = false;
218
219 if(args != null)
220 {
221 String[] array = args.toArray(new String[0]);
222 try {
223 zone = Integer.parseInt(array[0]);
224 if(zone <= 0 || zone > 60) {
225 zone = DEFAULT_ZONE;
226 }
227 } catch(NumberFormatException e) {}
228
229 if (array.length > 1) {
230 hemisphere = Hemisphere.valueOf(array[1]);
231 }
232
233 if (array.length > 2) {
234 offset = array[2].equals("offset");
235 }
236 }
237 updateParameters(zone, hemisphere, offset);
238 }
239
240 @Override
241 public String[] allCodes() {
242 ArrayList<String> projections = new ArrayList<String>(60*4);
243 for (int zone = 1;zone <= 60; zone++) {
244 for (boolean offset : new boolean[] { false, true }) {
245 for (Hemisphere hemisphere : Hemisphere.values()) {
246 projections.add("EPSG:" + ((offset?325800:32600) + zone + (hemisphere == Hemisphere.South?100:0)));
247 }
248 }
249 }
250 return projections.toArray(new String[0]);
251 }
252
253 @Override
254 public Collection<String> getPreferencesFromCode(String code) {
255
256 boolean offset = code.startsWith("EPSG:3258") || code.startsWith("EPSG:3259");
257
258 if(code.startsWith("EPSG:326") || code.startsWith("EPSG:327") || offset)
259 {
260 try {
261 Hemisphere hemisphere;
262 String zonestring;
263 if (offset) {
264 hemisphere = code.charAt(8)=='8'?Hemisphere.North:Hemisphere.South;
265 zonestring = code.substring(9);
266 } else {
267 hemisphere = code.charAt(7)=='6'?Hemisphere.North:Hemisphere.South;
268 zonestring = code.substring(8);
269 }
270
271 int zoneval = Integer.parseInt(zonestring);
272 if(zoneval > 0 && zoneval <= 60)
273 return Arrays.asList(zonestring, hemisphere.toString(), (offset?"offset":"standard"));
274 } catch(NumberFormatException e) {}
275 }
276 return null;
277 }
278}
Note: See TracBrowser for help on using the repository browser.