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

Last change on this file since 4234 was 3779, checked in by Upliner, 13 years ago

Identify projections in offset bookmarks by EPSG codes, bugfixes in getPreferencesFromCode() functions as they're critical now.

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