Changeset 3477 in josm


Ignore:
Timestamp:
2010-08-29T14:07:46+02:00 (14 years ago)
Author:
stoecker
Message:

apply patch by Gnonthgol - fix #5330 - UTM issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/UTM.java

    r3083 r3477  
    55
    66import java.awt.GridBagLayout;
     7import java.util.Arrays;
    78import java.util.Collection;
    8 import java.util.Collections;
    9 
     9
     10import javax.swing.ButtonGroup;
     11import javax.swing.JCheckBox;
    1012import javax.swing.JComboBox;
    1113import javax.swing.JLabel;
    1214import javax.swing.JPanel;
     15import javax.swing.JRadioButton;
    1316
    1417import org.openstreetmap.josm.data.Bounds;
     
    2528public class UTM implements Projection, ProjectionSubPrefs {
    2629
    27     public static final int DEFAULT_ZONE = 30;
     30    private static final int DEFAULT_ZONE = 30;
    2831    private int zone = DEFAULT_ZONE;
    2932
    30     final private static double UTMScaleFactor = 0.9996;
     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    private final static double UTMScaleFactor = 0.9996;
    3140
    3241    /* Ellipsoid model constants (WGS84) - TODO Use Elliposid class here too */
     
    339348    public EastNorth latlon2eastNorth(LatLon p) {
    340349        EastNorth a = mapLatLonToXY(Math.toRadians(p.lat()), Math.toRadians(p.lon()), UTMCentralMeridian(getzone()));
    341         return new EastNorth(a.east() * UTMScaleFactor + 3500000.0, a.north() * UTMScaleFactor);
     350        return new EastNorth(a.east() * UTMScaleFactor + getEastOffset(), a.north() * UTMScaleFactor + getNorthOffset());
    342351    }
    343352
    344353    public LatLon eastNorth2latlon(EastNorth p) {
    345         return mapXYToLatLon((p.east()-3500000.0)/UTMScaleFactor, p.north()/UTMScaleFactor, UTMCentralMeridian(getzone()));
     354        return mapXYToLatLon((p.east()-getEastOffset())/UTMScaleFactor, (p.north()-getNorthOffset())/UTMScaleFactor, UTMCentralMeridian(getzone()));
    346355    }
    347356
     
    355364    }
    356365
     366    private double getEastOffset() {
     367        return 500000 + (offset?3000000:0);
     368    }
     369
     370    private double getNorthOffset() {
     371        if (hemisphere == Hemisphere.North)
     372            return 0;
     373        else
     374            return 10000000;
     375    }
     376
     377    private int epsgCode() {
     378        return ((offset?325800:32600) + getzone() + (hemisphere == Hemisphere.South?100:0));
     379    }
     380
    357381    public String toCode() {
    358         return "EPSG:"+ (325800 + getzone());
     382        return "EPSG:"+ epsgCode();
    359383    }
    360384
    361385    @Override
    362386    public int hashCode() {
    363         return getClass().getName().hashCode()+zone; // our only real variable
     387        return toCode().hashCode();
    364388    }
    365389
    366390    public String getCacheDirectoryName() {
    367         return "epsg"+ (325800 + getzone());
     391        return "epsg"+ epsgCode();
    368392    }
    369393
    370394    public Bounds getWorldBoundsLatLon()
    371395    {
    372         return new Bounds(
    373                 new LatLon(-85.0, UTMCentralMeridianDeg(getzone())-5.0),
    374                 new LatLon(85.0, UTMCentralMeridianDeg(getzone())+5.0));
     396        if (hemisphere == Hemisphere.North)
     397            return new Bounds(
     398                    new LatLon(-5.0, UTMCentralMeridianDeg(getzone())-5.0),
     399                    new LatLon(85.0, UTMCentralMeridianDeg(getzone())+5.0));
     400        else
     401            return new Bounds(
     402                    new LatLon(-85.0, UTMCentralMeridianDeg(getzone())-5.0),
     403                    new LatLon(5.0, UTMCentralMeridianDeg(getzone())+5.0));
    375404    }
    376405
     
    381410
    382411    public void setupPreferencePanel(JPanel p) {
    383         JComboBox prefcb = new JComboBox();
     412        //Zone
     413        JComboBox zonecb = new JComboBox();
    384414        for(int i = 1; i <= 60; i++) {
    385             prefcb.addItem(i);
     415            zonecb.addItem(i);
    386416        }
    387417
    388         prefcb.setSelectedIndex(zone - 1);
     418        zonecb.setSelectedIndex(zone - 1);
    389419        p.setLayout(new GridBagLayout());
    390420        p.add(new JLabel(tr("UTM Zone")), GBC.std().insets(5,5,0,5));
    391421        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    392422        /* Note: we use component position 2 below to find this again */
    393         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
     423        p.add(zonecb, GBC.eop().fill(GBC.HORIZONTAL));
    394424        p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
     425
     426        //Hemisphere
     427        JRadioButton north = new JRadioButton();
     428        north.setSelected(hemisphere == Hemisphere.North);
     429        JRadioButton south = new JRadioButton();
     430        south.setSelected(hemisphere == Hemisphere.South);
     431
     432        ButtonGroup group = new ButtonGroup();
     433        group.add(north);
     434        group.add(south);
     435
     436        JPanel bPanel = new JPanel();
     437        bPanel.setLayout(new GridBagLayout());
     438
     439        bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
     440        bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
     441        bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
     442        bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
     443        bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
     444        bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
     445
     446        p.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5,5,0,5));
     447        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
     448        p.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
     449        p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
     450
     451        //Offset
     452        JCheckBox offsetBox = new JCheckBox();
     453        offsetBox.setSelected(offset);
     454
     455        p.add(new JLabel(tr("Offset 3.000.000m east")), GBC.std().insets(5,5,0,5));
     456        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
     457        /* Note: we use component position 2 below to find this again */
     458        p.add(offsetBox, GBC.eop().fill(GBC.HORIZONTAL));
     459        p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    395460    }
    396461
    397462    public Collection<String> getPreferences(JPanel p) {
    398         Object prefcb = p.getComponent(2);
    399         if(!(prefcb instanceof JComboBox))
    400             return null;
    401         int zone = ((JComboBox)prefcb).getSelectedIndex() + 1;
    402         return Collections.singleton(Integer.toString(zone));
     463        int zone = DEFAULT_ZONE;
     464        Hemisphere hemisphere = DEFAULT_HEMISPHERE;
     465        boolean offset = false;
     466
     467        Object zonecb = p.getComponent(2);
     468        if (zonecb instanceof JComboBox) {
     469            zone = ((JComboBox)zonecb).getSelectedIndex() + 1;
     470        }
     471
     472        Object bPanel = p.getComponent(6);
     473        if (bPanel instanceof JPanel) {
     474            Object south = ((JPanel)bPanel).getComponent(4);
     475            if (south instanceof JRadioButton) {
     476                hemisphere = ((JRadioButton)south).isSelected()?Hemisphere.South:Hemisphere.North;
     477            }
     478        }
     479
     480        Object offsetBox = p.getComponent(10);
     481        if (offsetBox instanceof JCheckBox) {
     482            offset = ((JCheckBox) offsetBox).isSelected();
     483        }
     484
     485        return Arrays.asList(Integer.toString(zone), hemisphere.toString(), (offset?"offset":"standard"));
    403486    }
    404487
     
    406489    {
    407490        zone = DEFAULT_ZONE;
     491        hemisphere = DEFAULT_HEMISPHERE;
     492        offset = true; //Default in previous versions
     493
    408494        if(args != null)
    409495        {
     496            String[] array = args.toArray(new String[0]);
    410497            try {
    411                 for(String s : args)
    412                 {
    413                     zone = Integer.parseInt(s);
    414                     if(zone <= 0 || zone > 60) {
    415                         zone = DEFAULT_ZONE;
    416                     }
    417                     break;
     498                zone = Integer.parseInt(array[0]);
     499                if(zone <= 0 || zone > 60) {
     500                    zone = DEFAULT_ZONE;
    418501                }
    419502            } catch(NumberFormatException e) {}
     503
     504            if (array.length > 1) {
     505                hemisphere = Hemisphere.valueOf(array[1]);
     506            }
     507
     508            if (array.length > 2) {
     509                offset = array[2].equals("offset");
     510            }
    420511        }
    421512    }
     
    423514    public Collection<String> getPreferencesFromCode(String code)
    424515    {
    425         if(code.startsWith("EPSG:3258"))
     516        boolean offset = code.startsWith("EPSG:3258") || code.startsWith("EPSG:3259");
     517
     518        if(code.startsWith("EPSG:326") || code.startsWith("EPSG:327") || offset)
    426519        {
    427520            try {
    428                 String zonestring = code.substring(9);
     521                Hemisphere hemisphere;
     522                String zonestring;
     523                if (offset) {
     524                    hemisphere = code.charAt(8)=='8'?Hemisphere.North:Hemisphere.South;
     525                    zonestring = code.substring(9);
     526                } else {
     527                    hemisphere = code.charAt(7)=='6'?Hemisphere.North:Hemisphere.South;
     528                    zonestring = code.substring(8);
     529                }
     530
    429531                int zoneval = Integer.parseInt(zonestring);
    430532                if(zoneval > 0 && zoneval <= 60)
    431                     return Collections.singleton(zonestring);
     533                    return Arrays.asList(zonestring, hemisphere.toString(), (offset?"offset":"standard"));
    432534            } catch(NumberFormatException e) {}
    433535        }
Note: See TracChangeset for help on using the changeset viewer.