- Timestamp:
- 2012-05-10T16:12:42+02:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/projection
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java
r5226 r5227 5 5 6 6 import java.util.ArrayList; 7 import java.util.Arrays; 7 8 import java.util.HashMap; 9 import java.util.HashSet; 8 10 import java.util.List; 9 11 import java.util.Map; 12 import java.util.Set; 10 13 import java.util.regex.Matcher; 11 14 import java.util.regex.Pattern; … … 13 16 import org.openstreetmap.josm.data.Bounds; 14 17 import org.openstreetmap.josm.data.coor.LatLon; 18 import org.openstreetmap.josm.data.projection.CustomProjection.Param; 15 19 import org.openstreetmap.josm.data.projection.datum.CentricDatum; 16 20 import org.openstreetmap.josm.data.projection.datum.Datum; … … 38 42 protected String pref = null; 39 43 44 protected static class Param { 45 public String key; 46 public boolean hasValue; 47 48 public final static Param x_0 = new Param("x_0", true); 49 public final static Param y_0 = new Param("y_0", true); 50 public final static Param lon_0 = new Param("lon_0", true); 51 public final static Param k_0 = new Param("k_0", true); 52 public final static Param ellps = new Param("ellps", true); 53 public final static Param a = new Param("a", true); 54 public final static Param es = new Param("es", true); 55 public final static Param rf = new Param("rf", true); 56 public final static Param f = new Param("f", true); 57 public final static Param b = new Param("b", true); 58 public final static Param datum = new Param("datum", true); 59 public final static Param towgs84 = new Param("towgs84", true); 60 public final static Param nadgrids = new Param("nadgrids", true); 61 public final static Param proj = new Param("proj", true); 62 public final static Param lat_0 = new Param("lat_0", true); 63 public final static Param lat_1 = new Param("lat_1", true); 64 public final static Param lat_2 = new Param("lat_2", true); 65 66 public final static Set<Param> params = new HashSet<Param>(Arrays.asList( 67 x_0, y_0, lon_0, k_0, ellps, a, es, rf, f, b, datum, towgs84, 68 nadgrids, proj, lat_0, lat_1, lat_2 69 )); 70 71 public final static Map<String, Param> paramsByKey = new HashMap<String, Param>(); 72 static { 73 for (Param p : params) { 74 paramsByKey.put(p.key, p); 75 } 76 } 77 78 public Param(String key, boolean hasValue) { 79 this.key = key; 80 this.hasValue = hasValue; 81 } 82 } 83 40 84 public void update(String pref) throws ProjectionConfigurationException { 41 85 if (pref == null) { … … 47 91 Map<String, String> parameters = new HashMap<String, String>(); 48 92 String[] parts = pref.trim().split("\\s+"); 93 if (pref.trim().isEmpty()) { 94 parts = new String[0]; 95 } 49 96 for (int i = 0; i < parts.length; i++) { 50 97 String part = parts[i]; … … 58 105 value = m.group(3); 59 106 } 107 if (!Param.paramsByKey.containsKey(key)) 108 throw new ProjectionConfigurationException(tr("Unkown parameter: ''{0}''.", key)); 109 if (Param.paramsByKey.get(key).hasValue && value == null) 110 throw new ProjectionConfigurationException(tr("Value expected for parameter ''{0}''.", key)); 111 if (!Param.paramsByKey.get(key).hasValue && value != null) 112 throw new ProjectionConfigurationException(tr("No value expected for parameter ''{0}''.", key)); 60 113 parameters.put(key, value); 61 114 } else … … 65 118 datum = parseDatum(parameters, ellps); 66 119 proj = parseProjection(parameters, ellps); 67 String s = parameters.get( "x_0");120 String s = parameters.get(Param.x_0.key); 68 121 if (s != null) { 69 this.x_0 = parseDouble(s, "x_0");70 } 71 s = parameters.get( "y_0");122 this.x_0 = parseDouble(s, Param.x_0.key); 123 } 124 s = parameters.get(Param.y_0.key); 72 125 if (s != null) { 73 this.y_0 = parseDouble(s, "y_0");74 } 75 s = parameters.get( "lon_0");126 this.y_0 = parseDouble(s, Param.x_0.key); 127 } 128 s = parameters.get(Param.lon_0.key); 76 129 if (s != null) { 77 this.lon_0 = parseAngle(s, "lon_0");78 } 79 s = parameters.get( "k_0");130 this.lon_0 = parseAngle(s, Param.x_0.key); 131 } 132 s = parameters.get(Param.k_0.key); 80 133 if (s != null) { 81 this.k_0 = parseDouble(s, "k_0");134 this.k_0 = parseDouble(s, Param.x_0.key); 82 135 } 83 136 this.pref = pref; … … 86 139 87 140 public Ellipsoid parseEllipsoid(Map<String, String> parameters) throws ProjectionConfigurationException { 88 String code = parameters.get( "ellps");141 String code = parameters.get(Param.ellps.key); 89 142 if (code != null) { 90 143 Ellipsoid ellipsoid = Projections.getEllipsoid(code); … … 95 148 } 96 149 } 97 String s = parameters.get( "a");150 String s = parameters.get(Param.a.key); 98 151 if (s != null) { 99 double a = parseDouble(s, "a");100 if (parameters.get( "es") != null) {101 double es = parseDouble(parameters, "es");152 double a = parseDouble(s, Param.a.key); 153 if (parameters.get(Param.es.key) != null) { 154 double es = parseDouble(parameters, Param.es.key); 102 155 return Ellipsoid.create_a_es(a, es); 103 156 } 104 if (parameters.get( "rf") != null) {105 double rf = parseDouble(parameters, "rf");157 if (parameters.get(Param.rf.key) != null) { 158 double rf = parseDouble(parameters, Param.rf.key); 106 159 return Ellipsoid.create_a_rf(a, rf); 107 160 } 108 if (parameters.get( "f") != null) {109 double f = parseDouble(parameters, "f");161 if (parameters.get(Param.f.key) != null) { 162 double f = parseDouble(parameters, Param.f.key); 110 163 return Ellipsoid.create_a_f(a, f); 111 164 } 112 if (parameters.get( "b") != null) {113 double b = parseDouble(parameters, "b");165 if (parameters.get(Param.b.key) != null) { 166 double b = parseDouble(parameters, Param.b.key); 114 167 return Ellipsoid.create_a_b(a, b); 115 168 } 116 169 } 117 if (parameters.containsKey( "a") ||118 parameters.containsKey( "es") ||119 parameters.containsKey( "rf") ||120 parameters.containsKey( "f") ||121 parameters.containsKey( "b"))170 if (parameters.containsKey(Param.a.key) || 171 parameters.containsKey(Param.es.key) || 172 parameters.containsKey(Param.rf.key) || 173 parameters.containsKey(Param.f.key) || 174 parameters.containsKey(Param.b.key)) 122 175 throw new ProjectionConfigurationException(tr("Combination of ellipsoid parameters is not supported.")); 123 176 // nothing specified, use WGS84 as default … … 126 179 127 180 public Datum parseDatum(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException { 128 String nadgridsId = parameters.get( "nadgrids");181 String nadgridsId = parameters.get(Param.nadgrids.key); 129 182 if (nadgridsId != null) { 130 183 NTV2GridShiftFileWrapper nadgrids = Projections.getNadgrids(nadgridsId); … … 134 187 } 135 188 136 String towgs84 = parameters.get( "towgs84");189 String towgs84 = parameters.get(Param.towgs84.key); 137 190 if (towgs84 != null) 138 191 return parseToWGS84(towgs84, ellps); 139 192 140 String datumId = parameters.get( "datum");193 String datumId = parameters.get(Param.datum.key); 141 194 if (datumId != null) { 142 195 Datum datum = Projections.getDatum(datumId); … … 189 242 190 243 public Proj parseProjection(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException { 191 String id = (String) parameters.get( "proj");244 String id = (String) parameters.get(Param.proj.key); 192 245 if (id == null) throw new ProjectionConfigurationException(tr("Projection required (+proj=*)")); 193 246 194 Proj proj = Projections.get Projection(id);247 Proj proj = Projections.getBaseProjection(id); 195 248 if (proj == null) throw new ProjectionConfigurationException(tr("Unkown projection identifier: ''{0}''", id)); 196 249 … … 200 253 201 254 String s; 202 s = parameters.get( "lat_0");255 s = parameters.get(Param.lat_0.key); 203 256 if (s != null) { 204 projParams.lat_0 = parseAngle(s, "lat_0");205 } 206 s = parameters.get( "lat_1");257 projParams.lat_0 = parseAngle(s, Param.lat_0.key); 258 } 259 s = parameters.get(Param.lat_1.key); 207 260 if (s != null) { 208 projParams.lat_1 = parseAngle(s, "lat_1");209 } 210 s = parameters.get( "lat_2");261 projParams.lat_1 = parseAngle(s, Param.lat_1.key); 262 } 263 s = parameters.get(Param.lat_2.key); 211 264 if (s != null) { 212 projParams.lat_2 = parseAngle(s, "lat_2");265 projParams.lat_2 = parseAngle(s, Param.lat_2.key); 213 266 } 214 267 proj.initialize(projParams); … … 328 381 @Override 329 382 public String toString() { 330 return tr("Custom Projection (from PROJ.4 string)");383 return tr("Custom Projection"); 331 384 } 332 385 } -
trunk/src/org/openstreetmap/josm/data/projection/CustomProjectionPrefGui.java
r5226 r5227 9 9 import java.awt.event.ActionEvent; 10 10 import java.awt.event.ActionListener; 11 import java.util.ArrayList; 11 12 import java.util.Collection; 12 13 import java.util.Collections; 13 import java.util. logging.Level;14 import java.util. logging.Logger;14 import java.util.List; 15 import java.util.Map; 15 16 16 17 import javax.swing.JButton; 18 import javax.swing.JComponent; 17 19 import javax.swing.JLabel; 18 20 import javax.swing.JPanel; 19 21 import javax.swing.JTextField; 20 22 23 import org.openstreetmap.josm.gui.ExtendedDialog; 21 24 import org.openstreetmap.josm.gui.preferences.projection.SubPrefsOptions; 22 25 import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator; … … 24 27 import org.openstreetmap.josm.tools.GBC; 25 28 import org.openstreetmap.josm.tools.ImageProvider; 29 import org.openstreetmap.josm.tools.Utils; 26 30 27 31 public class CustomProjectionPrefGui extends CustomProjection implements ProjectionSubPrefs, SubPrefsOptions { … … 29 33 @Override 30 34 public void setupPreferencePanel(JPanel p, ActionListener listener) { 31 final JTextField input = new JTextField(pref == null ? "" : pref, 30); 32 final HtmlPanel errorsPanel = new HtmlPanel(); 33 errorsPanel.setVisible(false); 34 final JLabel valStatus = new JLabel(); 35 valStatus.setVisible(false); 36 37 final AbstractTextComponentValidator val = new AbstractTextComponentValidator(input, false, false, false) { 38 39 private String error; 40 41 @Override 42 public void validate() { 43 if (!isValid()) { 44 feedbackInvalid(tr("Invalid projection configuration: {0}",error)); 45 } else { 46 feedbackValid(tr("Projection configuration is valid.")); 47 } 48 } 49 50 @Override 51 public boolean isValid() { 52 try { 53 CustomProjection test = new CustomProjection(); 54 test.update(input.getText()); 55 } catch (ProjectionConfigurationException ex) { 56 error = ex.getMessage(); 57 valStatus.setIcon(ImageProvider.get("data", "error.png")); 35 JPanel inner = new PreferencePanel(); 36 p.setLayout(new GridBagLayout()); 37 p.add(inner, GBC.std().fill(GBC.HORIZONTAL)); 38 } 39 40 private class PreferencePanel extends JPanel { 41 42 public final JTextField input; 43 44 public PreferencePanel() { 45 input = new JTextField(pref == null ? "" : pref, 30); 46 build(); 47 } 48 49 private void build() { 50 final HtmlPanel errorsPanel = new HtmlPanel(); 51 errorsPanel.setVisible(false); 52 final JLabel valStatus = new JLabel(); 53 valStatus.setVisible(false); 54 55 final AbstractTextComponentValidator val = new AbstractTextComponentValidator(input, false, false, false) { 56 57 private String error; 58 59 @Override 60 public void validate() { 61 if (!isValid()) { 62 feedbackInvalid(tr("Invalid projection configuration: {0}",error)); 63 } else { 64 feedbackValid(tr("Projection configuration is valid.")); 65 } 66 } 67 68 @Override 69 public boolean isValid() { 70 try { 71 CustomProjection test = new CustomProjection(); 72 test.update(input.getText()); 73 } catch (ProjectionConfigurationException ex) { 74 error = ex.getMessage(); 75 valStatus.setIcon(ImageProvider.get("data", "error.png")); 76 valStatus.setVisible(true); 77 errorsPanel.setText(error); 78 errorsPanel.setVisible(true); 79 return false; 80 } 81 errorsPanel.setVisible(false); 82 valStatus.setIcon(ImageProvider.get("misc", "green_check.png")); 58 83 valStatus.setVisible(true); 59 errorsPanel.setText(error); 60 errorsPanel.setVisible(true); 61 return false; 62 } 63 errorsPanel.setVisible(false); 64 valStatus.setIcon(ImageProvider.get("misc", "green_check.png")); 65 valStatus.setVisible(true); 66 return true; 67 } 68 69 }; 70 71 JButton btnCheck = new JButton(tr("Validate")); 72 btnCheck.addActionListener(new ActionListener() { 73 @Override 74 public void actionPerformed(ActionEvent e) { 75 val.validate(); 76 } 77 }); 78 btnCheck.setLayout(new BorderLayout()); 79 btnCheck.setMargin(new Insets(-1,0,-1,0)); 80 81 p.setLayout(new GridBagLayout()); 82 p.add(input, GBC.std().fill(GBC.HORIZONTAL).insets(0, 0, 5, 5)); 83 p.add(btnCheck, GBC.eol()); 84 JPanel p2 = new JPanel(new GridBagLayout()); 85 p2.add(valStatus, GBC.std().anchor(GBC.WEST).weight(0.0001, 0)); 86 p2.add(errorsPanel, GBC.eol().fill(GBC.HORIZONTAL)); 87 p.add(p2, GBC.eol().fill(GBC.HORIZONTAL)); 84 return true; 85 } 86 87 }; 88 89 JButton btnCheck = new JButton(tr("Validate")); 90 btnCheck.addActionListener(new ActionListener() { 91 @Override 92 public void actionPerformed(ActionEvent e) { 93 val.validate(); 94 } 95 }); 96 btnCheck.setLayout(new BorderLayout()); 97 btnCheck.setMargin(new Insets(-1,0,-1,0)); 98 99 JButton btnInfo = new JButton(tr("Parameter information...")); 100 btnInfo.addActionListener(new ActionListener() { 101 @Override 102 public void actionPerformed(ActionEvent e) { 103 ParameterInfoDialog dlg = new ParameterInfoDialog(); 104 dlg.showDialog(); 105 dlg.toFront(); 106 } 107 }); 108 109 this.setLayout(new GridBagLayout()); 110 JPanel p2 = new JPanel(new GridBagLayout()); 111 p2.add(input, GBC.std().fill(GBC.HORIZONTAL).insets(0, 20, 5, 5)); 112 p2.add(btnCheck, GBC.eol().insets(0, 20, 0, 5)); 113 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL)); 114 p2 = new JPanel(new GridBagLayout()); 115 p2.add(valStatus, GBC.std().anchor(GBC.WEST).weight(0.0001, 0)); 116 p2.add(errorsPanel, GBC.eol().fill(GBC.HORIZONTAL)); 117 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL)); 118 p2 = new JPanel(new GridBagLayout()); 119 p2.add(btnInfo, GBC.std().insets(0, 20, 0, 0)); 120 p2.add(GBC.glue(1, 0), GBC.eol().fill(GBC.HORIZONTAL)); 121 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL)); 122 } 123 } 124 125 public class ParameterInfoDialog extends ExtendedDialog { 126 127 public ParameterInfoDialog() { 128 super(null, tr("Parameter information"), new String[] { tr("Close") }, false); 129 setContent(build()); 130 } 131 132 private JComponent build() { 133 StringBuilder s = new StringBuilder(); 134 s.append("<b>+proj=...</b> - <i>"+tr("Projection name")+"</i><br>"); 135 s.append(" "+tr("Supported values:")+" "); 136 s.append(listKeys(Projections.projs)+"<br>"); 137 s.append("<b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>"+tr("Projection parameters")+"</i><br>"); 138 s.append("<b>+x_0=..., +y_0=...</b> - <i>"+tr("False easting and false northing")+"</i><br>"); 139 s.append("<b>+lon_0=...</b> - <i>"+tr("Central meridian")+"</i><br>"); 140 s.append("<b>+k_0=...</b> - <i>"+tr("Scaling factor")+"</i><br>"); 141 s.append("<b>+ellps=...</b> - <i>"+tr("Ellipsoid name")+"</i><br>"); 142 s.append(" "+tr("Supported values:")+" "); 143 s.append(listKeys(Projections.ellipsoids)+"<br>"); 144 s.append("<b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>"+tr("Ellipsoid parameters")+"</i><br>"); 145 s.append("<b>+datum=...</b> - <i>"+tr("Datum name")+"</i><br>"); 146 s.append(" "+tr("Supported values:")+" "); 147 s.append(listKeys(Projections.datums)+"<br>"); 148 s.append("<b>+towgs84=...</b> - <i>"+tr("3 or 7 term datum transform parameters")+"</i><br>"); 149 s.append("<b>+nadgrids=...</b> - <i>"+tr("NTv2 grid file")+"</i><br>"); 150 s.append(" "+tr("Build-in:")+" "); 151 s.append(listKeys(Projections.nadgrids)+"<br>"); 152 153 HtmlPanel info = new HtmlPanel(s.toString()); 154 return info; 155 } 156 157 private String listKeys(Map<String, ?> map) { 158 List<String> keys = new ArrayList<String>(map.keySet()); 159 Collections.sort(keys); 160 return Utils.join(", ", keys); 161 } 88 162 } 89 163 90 164 @Override 91 165 public Collection<String> getPreferences(JPanel p) { 92 Object prefTf = p.getComponent(0); 93 if (!(prefTf instanceof JTextField)) 94 return null; 95 String pref = ((JTextField) prefTf).getText(); 166 PreferencePanel prefPanel = (PreferencePanel) p.getComponent(0); 167 String pref = prefPanel.input.getText(); 96 168 return Collections.singleton(pref); 97 169 } … … 100 172 public void setPreferences(Collection<String> args) { 101 173 try { 174 if (args == null || args.isEmpty()) throw new ProjectionConfigurationException(); 102 175 update(args.iterator().next()); 103 176 } catch (ProjectionConfigurationException ex) { -
trunk/src/org/openstreetmap/josm/data/projection/Projections.java
r5226 r5227 13 13 import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper; 14 14 import org.openstreetmap.josm.data.projection.datum.WGS84Datum; 15 import org.openstreetmap.josm.data.projection.proj.ClassProjFactory; 15 16 import org.openstreetmap.josm.data.projection.proj.LambertConformalConic; 17 import org.openstreetmap.josm.data.projection.proj.LonLat; 16 18 import org.openstreetmap.josm.data.projection.proj.Proj; 19 import org.openstreetmap.josm.data.projection.proj.ProjFactory; 17 20 import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator; 18 21 import org.openstreetmap.josm.data.projection.proj.TransverseMercator; … … 45 48 new Puwg(), // PL 46 49 new Epsg3008(), // SE 50 new CustomProjectionPrefGui() 47 51 })); 48 49 static {50 if (Main.pref.getBoolean("customprojection")) {51 addProjection(new CustomProjectionPrefGui());52 }53 }54 52 55 53 public static ArrayList<Projection> getProjections() { … … 82 80 * should be compatible to PROJ.4 83 81 */ 84 85 private static Map<String, Ellipsoid> ellipsoids = new HashMap<String, Ellipsoid>(); 86 private static Map<String, Class<? extends Proj>> projs = new HashMap<String, Class<? extends Proj>>(); 87 private static Map<String, Datum> datums = new HashMap<String, Datum>(); 88 private static Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<String, NTV2GridShiftFileWrapper>(); 82 public static Map<String, Ellipsoid> ellipsoids = new HashMap<String, Ellipsoid>(); 83 public static Map<String, ProjFactory> projs = new HashMap<String, ProjFactory>(); 84 public static Map<String, Datum> datums = new HashMap<String, Datum>(); 85 public static Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<String, NTV2GridShiftFileWrapper>(); 89 86 90 87 static { 88 registerBaseProjection("lonlat", LonLat.class, "core"); 89 registerBaseProjection("merc", org.openstreetmap.josm.data.projection.proj.Mercator.class, "core"); 90 registerBaseProjection("lcc", LambertConformalConic.class, "core"); 91 registerBaseProjection("somerc", SwissObliqueMercator.class, "core"); 92 registerBaseProjection("tmerc", TransverseMercator.class, "core"); 93 91 94 ellipsoids.put("intl", Ellipsoid.hayford); 92 95 ellipsoids.put("GRS80", Ellipsoid.GRS80); 93 96 ellipsoids.put("WGS84", Ellipsoid.WGS84); 94 97 ellipsoids.put("bessel", Ellipsoid.Bessel1841); 95 96 projs.put("merc", org.openstreetmap.josm.data.projection.proj.Mercator.class);97 projs.put("lcc", LambertConformalConic.class);98 projs.put("somerc", SwissObliqueMercator.class);99 projs.put("tmerc", TransverseMercator.class);100 98 101 99 datums.put("WGS84", WGS84Datum.INSTANCE); … … 105 103 } 106 104 105 /** 106 * Plugins can register additional base projections. 107 * 108 * @param id The "official" PROJ.4 id. In case the projection is not supported 109 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj. 110 * @param fac The base projection factory. 111 * @param origin Multiple plugins may implement the same base projection. 112 * Provide plugin name or similar string, so it be differentiated. 113 */ 114 public static void registerBaseProjection(String id, ProjFactory fac, String origin) { 115 projs.put(id, fac); 116 } 117 118 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) { 119 registerBaseProjection(id, new ClassProjFactory(projClass), origin); 120 } 121 122 public static Proj getBaseProjection(String id) { 123 ProjFactory fac = projs.get(id); 124 if (fac == null) return null; 125 return fac.createInstance(); 126 } 127 107 128 public static Ellipsoid getEllipsoid(String id) { 108 129 return ellipsoids.get(id); 109 }110 111 public static Proj getProjection(String id) {112 Class<? extends Proj> projClass = projs.get(id);113 if (projClass == null) return null;114 Proj proj = null;115 try {116 proj = projClass.newInstance();117 } catch (InstantiationException e) {118 throw new RuntimeException(e);119 } catch (IllegalAccessException e) {120 throw new RuntimeException(e);121 }122 return proj;123 130 } 124 131
Note:
See TracChangeset
for help on using the changeset viewer.