source: josm/trunk/src/org/openstreetmap/josm/data/projection/Puwg.java@ 3689

Last change on this file since 3689 was 3480, checked in by bastiK, 14 years ago

fixed some issues with world bounds; add basic projection tests

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2// 2009 by Łukasz Stelmach
3package org.openstreetmap.josm.data.projection;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.GridBagLayout;
8import java.text.DecimalFormat;
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.data.Bounds;
17import org.openstreetmap.josm.data.coor.EastNorth;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.tools.GBC;
20
21/**
22 * PUWG 1992 and 2000 are the official cordinate systems in Poland.
23 * They use the same math as UTM only with different constants.
24 *
25 * @author steelman
26 */
27public class Puwg extends UTM implements Projection,ProjectionSubPrefs {
28 public static final int DEFAULT_ZONE = 0;
29 private int zone = DEFAULT_ZONE;
30
31 static PuwgData[] Zones = new PuwgData[]{
32 new Epsg2180(),
33 new Epsg2176(),
34 new Epsg2177(),
35 new Epsg2178(),
36 new Epsg2179()
37 };
38
39 private static DecimalFormat decFormatter = new DecimalFormat("###0");
40
41 @Override
42 public EastNorth latlon2eastNorth(LatLon p) {
43 PuwgData z = Zones[zone];
44 double easting = z.getPuwgFalseEasting();
45 double northing = z.getPuwgFalseNorthing();
46 double scale = z.getPuwgScaleFactor();
47 double center = z.getPuwgCentralMeridian(); /* in radians */
48 EastNorth a = mapLatLonToXY(Math.toRadians(p.lat()), Math.toRadians(p.lon()), center);
49 return new EastNorth(a.east() * scale + easting, a.north() * scale + northing);
50 }
51
52 @Override
53 public LatLon eastNorth2latlon(EastNorth p) {
54 PuwgData z = Zones[zone];
55 double easting = z.getPuwgFalseEasting();
56 double northing = z.getPuwgFalseNorthing();
57 double scale = z.getPuwgScaleFactor();
58 double center = z.getPuwgCentralMeridian(); /* in radians */
59 return mapXYToLatLon((p.east() - easting)/scale, (p.north() - northing)/scale, center);
60 }
61
62 @Override public String toString() {
63 return tr("PUWG (Poland)");
64 }
65
66 @Override
67 public String toCode() {
68 return Zones[zone].toCode();
69 }
70
71 @Override
72 public int hashCode() {
73 return getClass().getName().hashCode()+zone; // our only real variable
74 }
75
76 @Override
77 public String getCacheDirectoryName() {
78 return Zones[zone].getCacheDirectoryName();
79 }
80
81 @Override
82 public Bounds getWorldBoundsLatLon() {
83 return Zones[zone].getWorldBoundsLatLon();
84 }
85
86 @Override
87 public double getDefaultZoomInPPD() {
88 // This will set the scale bar to about 100 km
89 return 0.009;
90 }
91
92 public String eastToString(EastNorth p) {
93 return decFormatter.format(p.east());
94 }
95
96 public String northToString(EastNorth p) {
97 return decFormatter.format(p.north());
98 }
99
100 @Override
101 public void setupPreferencePanel(JPanel p) {
102 JComboBox prefcb = new JComboBox(Puwg.Zones);
103
104 prefcb.setSelectedIndex(zone);
105 p.setLayout(new GridBagLayout());
106 p.add(new JLabel(tr("PUWG Zone")), GBC.std().insets(5,5,0,5));
107 p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
108 /* Note: we use component position 2 below to find this again */
109 p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
110 p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
111 }
112
113 @Override
114 public Collection<String> getPreferences(JPanel p) {
115 Object prefcb = p.getComponent(2);
116 if(!(prefcb instanceof JComboBox))
117 return null;
118 int zone = ((JComboBox)prefcb).getSelectedIndex();
119 return Collections.singleton((Puwg.Zones[zone]).toCode());
120 }
121
122 @Override
123 public Collection<String> getPreferencesFromCode(String code)
124 {
125 for (PuwgData p : Puwg.Zones)
126 {
127 if(code.equals(p.toCode()))
128 return Collections.singleton(code);
129 }
130 return null;
131 }
132
133 @Override
134 public void setPreferences(Collection<String> args)
135 {
136 zone = DEFAULT_ZONE;
137 if(args != null)
138 {
139 try {
140 for(String s : args)
141 {
142 for (int i=0; i < Puwg.Zones.length; ++i)
143 if(s.equals(Zones[i].toCode())) {
144 zone = i;
145 }
146 break;
147 }
148 } catch (NullPointerException e) {}
149 }
150 }
151}
152
153interface PuwgData {
154 double getPuwgCentralMeridianDeg();
155 double getPuwgCentralMeridian();
156 double getPuwgFalseEasting();
157 double getPuwgFalseNorthing();
158 double getPuwgScaleFactor();
159
160 // Projection methods
161 String toCode();
162 String getCacheDirectoryName();
163 Bounds getWorldBoundsLatLon();
164}
165
166class Epsg2180 implements PuwgData {
167
168 private static final double Epsg2180FalseEasting = 500000.0; /* y */
169 private static final double Epsg2180FalseNorthing = -5300000.0; /* x */
170 private static final double Epsg2180ScaleFactor = 0.9993;
171 private static final double Epsg2180CentralMeridian = 19.0;
172
173 @Override public String toString() {
174 return tr("PUWG 1992 (Poland)");
175 }
176
177 public String toCode() {
178 return "EPSG:2180";
179 }
180
181 public String getCacheDirectoryName() {
182 return "epsg2180";
183 }
184
185 public Bounds getWorldBoundsLatLon()
186 {
187 return new Bounds(
188 new LatLon(49.00, 14.12),
189 new LatLon(54.84, 24.15));
190 }
191
192 public double getPuwgCentralMeridianDeg() { return Epsg2180CentralMeridian; }
193 public double getPuwgCentralMeridian() { return Math.toRadians(Epsg2180CentralMeridian); }
194 public double getPuwgFalseEasting() { return Epsg2180FalseEasting; }
195 public double getPuwgFalseNorthing() { return Epsg2180FalseNorthing; }
196 public double getPuwgScaleFactor() { return Epsg2180ScaleFactor; }
197}
198
199abstract class Puwg2000 implements PuwgData {
200
201 private static final double PuwgFalseEasting = 500000.0;
202 private static final double PuwgFalseNorthing = 0;
203 private static final double PuwgScaleFactor = 0.999923;
204 //final private double[] Puwg2000CentralMeridian = {15.0, 18.0, 21.0, 24.0};
205 final private String[] Puwg2000Code = { "EPSG:2176", "EPSG:2177", "EPSG:2178", "EPSG:2179"};
206 final private String[] Puwg2000CDName = { "epsg2176", "epsg2177", "epsg2178", "epsg2179"};
207
208 @Override public String toString() {
209 return tr("PUWG 2000 Zone {0} (Poland)", Integer.toString(getZone()));
210 }
211
212 public String toCode() {
213 return Puwg2000Code[getZoneIndex()];
214 }
215
216 public String getCacheDirectoryName() {
217 return Puwg2000CDName[getZoneIndex()];
218 }
219
220 public Bounds getWorldBoundsLatLon()
221 {
222 return new Bounds(
223 new LatLon(49.00, (3 * getZone()) - 1.5),
224 new LatLon(54.84, (3 * getZone()) + 1.5));
225 }
226
227 public double getPuwgCentralMeridianDeg() { return getZone() * 3.0; }
228 public double getPuwgCentralMeridian() { return Math.toRadians(getZone() * 3.0); }
229 public double getPuwgFalseNorthing() { return PuwgFalseNorthing;}
230 public double getPuwgFalseEasting() { return 1e6 * getZone() + PuwgFalseEasting; }
231 public double getPuwgScaleFactor() { return PuwgScaleFactor; }
232 public abstract int getZone();
233
234 public int getZoneIndex() { return getZone() - 5; }
235
236}
237
238class Epsg2176 extends Puwg2000 {
239 private static final int PuwgZone = 5;
240
241 @Override
242 public int getZone() { return PuwgZone; }
243}
244
245class Epsg2177 extends Puwg2000 {
246 private static final int PuwgZone = 6;
247
248 @Override
249 public int getZone() { return PuwgZone; }
250}
251
252class Epsg2178 extends Puwg2000 {
253 private static final int PuwgZone = 7;
254
255 @Override
256 public int getZone() { return PuwgZone; }
257}
258
259class Epsg2179 extends Puwg2000 {
260 private static final int PuwgZone = 8;
261
262 @Override
263 public int getZone() { return PuwgZone; }
264}
Note: See TracBrowser for help on using the repository browser.