Changeset 5226 in josm for trunk/src


Ignore:
Timestamp:
2012-05-09T21:29:17+02:00 (12 years ago)
Author:
bastiK
Message:

improvements for custom projection

Location:
trunk/src/org/openstreetmap/josm
Files:
4 added
9 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r5195 r5226  
    6969import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
    7070import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    71 import org.openstreetmap.josm.gui.preferences.map.ProjectionPreference;
    7271import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
     72import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    7373import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
    7474import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
  • trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java

    r5202 r5226  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.GridBagLayout;
    7 import java.awt.event.ActionListener;
    86import java.util.ArrayList;
    9 import java.util.Collection;
    10 import java.util.Collections;
    117import java.util.HashMap;
    128import java.util.List;
     
    1410import java.util.regex.Matcher;
    1511import java.util.regex.Pattern;
    16 import javax.swing.JPanel;
    17 import javax.swing.JTextField;
    1812
    1913import org.openstreetmap.josm.data.Bounds;
     
    2115import org.openstreetmap.josm.data.projection.datum.CentricDatum;
    2216import org.openstreetmap.josm.data.projection.datum.Datum;
     17import org.openstreetmap.josm.data.projection.datum.NTV2Datum;
     18import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
    2319import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
    2420import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
     21import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
    2522import org.openstreetmap.josm.data.projection.proj.Proj;
    2623import org.openstreetmap.josm.data.projection.proj.ProjParameters;
     
    3229 * Inspired by PROJ.4 and Proj4J.
    3330 */
    34 public class CustomProjection extends AbstractProjection implements ProjectionSubPrefs {
    35 
    36     private String pref = "";
    37 
    38     public void update(String pref) {
    39         try {
     31public class CustomProjection extends AbstractProjection {
     32
     33    /**
     34     * pref String that defines the projection
     35     *
     36     * null means fall back mode (Mercator)
     37     */
     38    protected String pref = null;
     39
     40    public void update(String pref) throws ProjectionConfigurationException {
     41        if (pref == null) {
     42            this.pref = null;
     43            ellps = Ellipsoid.WGS84;
     44            datum = WGS84Datum.INSTANCE;
     45            proj = new org.openstreetmap.josm.data.projection.proj.Mercator();
     46        } else {
    4047            Map<String, String> parameters = new HashMap<String, String>();
    4148            String[] parts = pref.trim().split("\\s+");
    4249            for (int i = 0; i < parts.length; i++) {
    4350                String part = parts[i];
    44                 if (part.charAt(0) != '+')
     51                if (part.isEmpty() || part.charAt(0) != '+')
    4552                    throw new ProjectionConfigurationException(tr("Parameter must begin with a ''+'' sign (found ''{0}'')", part));
    4653                Matcher m = Pattern.compile("\\+([a-zA-Z0-9_]+)(=(.*))?").matcher(part);
     
    7481                this.k_0 = parseDouble(s, "k_0");
    7582            }
    76         } catch (ProjectionConfigurationException e) {
    77             System.err.println(e.toString()); // FIXME
    78         }
    79         this.pref = pref;
     83            this.pref = pref;
     84        }
    8085    }
    8186
     
    121126
    122127    public Datum parseDatum(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException {
     128        String nadgridsId = parameters.get("nadgrids");
     129        if (nadgridsId != null) {
     130            NTV2GridShiftFileWrapper nadgrids = Projections.getNadgrids(nadgridsId);
     131            if (nadgrids == null)
     132                throw new ProjectionConfigurationException(tr("Grid shift file ''{0}'' for option +nadgrids not supported.", nadgridsId));
     133            return new NTV2Datum(nadgridsId, null, ellps, nadgrids);
     134        }
     135
    123136        String towgs84 = parameters.get("towgs84");
    124137        if (towgs84 != null)
     
    298311    @Override
    299312    public String toCode() {
    300         return Utils.md5Hex(pref).substring(0, 10);
     313        return "proj:" + (pref == null ? "ERROR" : pref);
    301314    }
    302315
    303316    @Override
    304317    public String getCacheDirectoryName() {
    305         return toCode();
     318        return "proj-"+Utils.md5Hex(pref == null ? "" : pref).substring(0, 4);
    306319    }
    307320
     
    317330        return tr("Custom Projection (from PROJ.4 string)");
    318331    }
    319 
    320     @Override
    321     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    322         JTextField input = new JTextField(pref, 50);
    323         p.setLayout(new GridBagLayout());
    324         p.add(input);
    325     }
    326 
    327     @Override
    328     public Collection<String> getPreferences(JPanel p) {
    329         Object prefTf = p.getComponent(0);
    330         if (!(prefTf instanceof JTextField))
    331             return null;
    332         String pref = ((JTextField) prefTf).getText();
    333         return Collections.singleton(pref);
    334     }
    335 
    336     @Override
    337     public void setPreferences(Collection<String> args) {
    338         update(args.iterator().next());
    339     }
    340 
    341     @Override
    342     public String[] allCodes() {
    343         return new String[0];
    344     }
    345 
    346     @Override
    347     public Collection<String> getPreferencesFromCode(String code) {
    348         return null;
    349     }
    350 
    351332}
  • trunk/src/org/openstreetmap/josm/data/projection/GaussKrueger.java

    r5073 r5226  
    66import java.awt.GridBagLayout;
    77import java.awt.event.ActionListener;
    8 import java.io.InputStream;
    98import java.util.Collection;
    109import java.util.Collections;
     
    1413import javax.swing.JPanel;
    1514
    16 import org.openstreetmap.josm.Main;
    1715import org.openstreetmap.josm.data.Bounds;
    1816import org.openstreetmap.josm.data.coor.LatLon;
    1917import org.openstreetmap.josm.data.projection.datum.NTV2Datum;
    20 import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFile;
     18import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
    2119import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    2220import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
     
    3533    };
    3634
    37     private static NTV2GridShiftFile BETA2007 = null;
    38 
    3935    private static String[] zones = { "2", "3", "4", "5" };
    4036
     
    4440
    4541    public GaussKrueger(int zone) {
    46         if (BETA2007 == null) {
    47             try {
    48                 String gridFileName = "BETA2007.gsb";
    49                 InputStream is = Main.class.getResourceAsStream("/data/"+gridFileName);
    50                 if (is == null)
    51                     throw new RuntimeException(tr("Error: failed to open input stream for resource ''/data/{0}''.", gridFileName));
    52                 BETA2007 = new NTV2GridShiftFile();
    53                 BETA2007.loadGridShiftFile(is, false);
    54             } catch (Exception e) {
    55                 throw new RuntimeException(e);
    56             }
    57         }
    5842        updateParameters(zone);
    5943    }
     
    6246        this.zone = zone;
    6347        ellps = Ellipsoid.Bessel1841;
    64         datum = new NTV2Datum("BETA2007", null, ellps, BETA2007);
     48        datum = new NTV2Datum("BETA2007", null, ellps, NTV2GridShiftFileWrapper.BETA2007);
    6549        ////less acurrate datum (errors up to 3m):
    6650        //datum = new SevenParameterDatum(
  • trunk/src/org/openstreetmap/josm/data/projection/Lambert.java

    r5073 r5226  
    1919import org.openstreetmap.josm.data.projection.datum.NTV2Datum;
    2020import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFile;
     21import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
    2122import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
    2223import org.openstreetmap.josm.data.projection.proj.ProjParameters;
     
    7778    private int layoutZone;
    7879
    79     private static NTV2GridShiftFile ntf_rgf93Grid = null;
    80 
    81     public static NTV2GridShiftFile getNtf_rgf93Grid() {
    82         return ntf_rgf93Grid;
    83     }
    84 
    8580    public Lambert() {
    86         if (ntf_rgf93Grid == null) {
    87             try {
    88                 String gridFileName = "ntf_r93_b.gsb";
    89                 InputStream is = Main.class.getResourceAsStream("/data/"+gridFileName);
    90                 if (is == null) {
    91                     throw new RuntimeException(tr("Error: failed to open input stream for resource ''/data/{0}''. Cannot load NTF<->RGF93 grid", gridFileName));
    92                 }
    93                 ntf_rgf93Grid = new NTV2GridShiftFile();
    94                 ntf_rgf93Grid.loadGridShiftFile(is, false);
    95             } catch (Exception e) {
    96                 throw new RuntimeException(e);
    97             }
    98         }
    9981        updateParameters(DEFAULT_ZONE);
    10082    }
     
    10385        this.layoutZone = layoutZone;
    10486        ellps = Ellipsoid.clarkeIGN;
    105         datum = new NTV2Datum("ntf_rgf93Grid", null, ellps, ntf_rgf93Grid);
     87        datum = new NTV2Datum("ntf_rgf93Grid", null, ellps, NTV2GridShiftFileWrapper.ntf_rgf93);
    10688        x_0 = x_0s[layoutZone];
    10789        lon_0 = 2.0 + 20.0 / 60 + 14.025 / 3600; // 0 grade Paris
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r5072 r5226  
    1111import org.openstreetmap.josm.data.coor.LatLon;
    1212import org.openstreetmap.josm.data.projection.datum.Datum;
     13import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
    1314import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
    1415import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
     
    1617import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
    1718import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
    18 import org.openstreetmap.josm.tools.CheckParameterUtil;
    1919
    2020/**
     
    4949    static {
    5050        if (Main.pref.getBoolean("customprojection")) {
    51             addProjection(new CustomProjection());
     51            addProjection(new CustomProjectionPrefGui());
    5252        }
    5353    }
     
    8686    private static Map<String, Class<? extends Proj>> projs = new HashMap<String, Class<? extends Proj>>();
    8787    private static Map<String, Datum> datums = new HashMap<String, Datum>();
     88    private static Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<String, NTV2GridShiftFileWrapper>();
    8889
    8990    static {
     
    99100
    100101        datums.put("WGS84", WGS84Datum.INSTANCE);
     102
     103        nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
     104        nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
    101105    }
    102106
     
    122126        return datums.get(id);
    123127    }
     128
     129    public static NTV2GridShiftFileWrapper getNadgrids(String id) {
     130        return nadgrids.get(id);
     131    }
    124132}
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2Datum.java

    r5073 r5226  
    1010public class NTV2Datum extends AbstractDatum {
    1111
    12     protected NTV2GridShiftFile nadgrids;
     12    protected NTV2GridShiftFileWrapper nadgrids;
    1313
    14     public NTV2Datum(String name, String proj4Id, Ellipsoid ellps, NTV2GridShiftFile nadgrids) {
     14    public NTV2Datum(String name, String proj4Id, Ellipsoid ellps, NTV2GridShiftFileWrapper nadgrids) {
    1515        super(name, proj4Id, ellps);
    1616        this.nadgrids = nadgrids;
     
    2020    public LatLon toWGS84(LatLon ll) {
    2121        NTV2GridShift gs = new NTV2GridShift(ll);
    22         nadgrids.gridShiftForward(gs);
     22        nadgrids.getShiftFile().gridShiftForward(gs);
    2323        return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
    2424    }
     
    2727    public LatLon fromWGS84(LatLon ll) {
    2828        NTV2GridShift gs = new NTV2GridShift(ll);
    29         nadgrids.gridShiftReverse(gs);
     29        nadgrids.getShiftFile().gridShiftReverse(gs);
    3030        return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
    3131    }
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r5016 r5226  
    4343import org.openstreetmap.josm.data.projection.Projections;
    4444import org.openstreetmap.josm.gui.help.Helpful;
    45 import org.openstreetmap.josm.gui.preferences.map.ProjectionPreference;
     45import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    4646import org.openstreetmap.josm.tools.Predicate;
    4747
  • trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java

    r4987 r5226  
    3939import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    4040import org.openstreetmap.josm.gui.preferences.map.MapPreference;
    41 import org.openstreetmap.josm.gui.preferences.map.ProjectionPreference;
    4241import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
     42import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    4343import org.openstreetmap.josm.gui.preferences.shortcut.ShortcutPreference;
    4444import org.openstreetmap.josm.plugins.PluginDownloadTask;
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

    r5223 r5226  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    2 package org.openstreetmap.josm.gui.preferences.map;
     2package org.openstreetmap.josm.gui.preferences.projection;
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Component;
    67import java.awt.GridBagLayout;
    78import java.awt.event.ActionEvent;
     
    8586    private JPanel projSubPrefPanelWrapper = new JPanel(new GridBagLayout());
    8687
     88    private JLabel projectionCodeLabel;
     89    private Component projectionCodeGlue;
    8790    private JLabel projectionCode = new JLabel();
    8891    private JLabel bounds = new JLabel();
     
    122125        projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
    123126        projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
    124         projPanel.add(new JLabel(tr("Projection code")), GBC.std().insets(25,5,0,5));
    125         projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
     127        projPanel.add(projectionCodeLabel = new JLabel(tr("Projection code")), GBC.std().insets(25,5,0,5));
     128        projPanel.add(projectionCodeGlue = GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
    126129        projPanel.add(projectionCode, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
    127130        projPanel.add(new JLabel(tr("Bounds")), GBC.std().insets(25,5,0,5));
     
    152155        CoordinateFormat cf = CoordinateFormat.getDefaultFormat();
    153156        bounds.setText(b.getMin().latToString(cf)+"; "+b.getMin().lonToString(cf)+" : "+b.getMax().latToString(cf)+"; "+b.getMax().lonToString(cf));
     157        boolean hideCode = proj instanceof SubPrefsOptions && !((SubPrefsOptions) proj).showProjectionCode();
     158        projectionCodeLabel.setVisible(!hideCode);
     159        projectionCodeGlue.setVisible(!hideCode);
     160        projectionCode.setVisible(!hideCode);
    154161    }
    155162
  • trunk/src/org/openstreetmap/josm/gui/widgets/AbstractTextComponentValidator.java

    r3083 r5226  
    1919
    2020import org.openstreetmap.josm.tools.CheckParameterUtil;
     21import org.openstreetmap.josm.tools.Utils;
    2122
    2223/**
     
    4142     */
    4243    private Boolean valid = null;
     44    // remember the message
     45    private String msg;
    4346
    4447    protected void feedbackInvalid(String msg) {
    45         if (valid == null || valid) {
     48        if (valid == null || valid || !Utils.equal(msg, this.msg)) {
    4649            // only provide feedback if the validity has changed. This avoids
    4750            // unnecessary UI updates.
     
    5053            tc.setToolTipText(msg);
    5154            valid = false;
     55            this.msg = msg;
    5256        }
    5357    }
     
    5862
    5963    protected void feedbackValid(String msg) {
    60         if (valid == null || !valid) {
     64        if (valid == null || !valid || !Utils.equal(msg, this.msg)) {
    6165            // only provide feedback if the validity has changed. This avoids
    6266            // unnecessary UI updates.
     
    6569            tc.setToolTipText(msg == null ? "" : msg);
    6670            valid = true;
     71            this.msg = msg;
    6772        }
    6873    }
     
    9297     */
    9398    public AbstractTextComponentValidator(JTextComponent tc, boolean addActionListener) throws IllegalArgumentException {
     99        this(tc, true, true, addActionListener);
     100    }
     101
     102    public AbstractTextComponentValidator(JTextComponent tc, boolean addFocusListener, boolean addDocumentListener, boolean addActionListener) throws IllegalArgumentException {
    94103        CheckParameterUtil.ensureParameterNotNull(tc, "tc");
    95104        this.tc = tc;
    96         tc.addFocusListener(this);
    97         tc.getDocument().addDocumentListener(this);
     105        if (addFocusListener) {
     106            tc.addFocusListener(this);
     107        }
     108        if (addDocumentListener) {
     109            tc.getDocument().addDocumentListener(this);
     110        }
    98111        if (addActionListener) {
    99112            if (tc instanceof JTextField) {
Note: See TracChangeset for help on using the changeset viewer.