Changeset 12093 in josm


Ignore:
Timestamp:
2017-05-10T12:22:20+02:00 (7 years ago)
Author:
bastiK
Message:

fixed #14734 - Handling imagery offsets when reprojecting

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

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

    r11713 r12093  
    2626import org.openstreetmap.josm.actions.mapmode.MapMode;
    2727import org.openstreetmap.josm.data.coor.EastNorth;
     28import org.openstreetmap.josm.data.coor.LatLon;
    2829import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    2930import org.openstreetmap.josm.gui.ExtendedDialog;
     
    4344    private static Cursor cursor = ImageProvider.getCursor("normal", "move");
    4445
    45     private EastNorth old;
     46    private OffsetBookmark old;
     47    private OffsetBookmark tempOffset;
    4648    private EastNorth prevEastNorth;
    4749    private transient AbstractTileSourceLayer<?> layer;
     
    6668            layer.setVisible(true);
    6769        }
    68         old = layer.getDisplaySettings().getDisplacement();
     70        old = layer.getDisplaySettings().getOffsetBookmark();
     71        EastNorth curOff = old == null ? EastNorth.ZERO : old.getDisplacement(Main.getProjection());
     72        LatLon center;
     73        if (Main.isDisplayingMapView()) {
     74            center = Main.getProjection().eastNorth2latlon(Main.map.mapView.getCenter());
     75        } else {
     76            center = LatLon.ZERO;
     77        }
     78        tempOffset = new OffsetBookmark(
     79                Main.getProjection().toCode(),
     80                layer.getInfo().getName(),
     81                null,
     82                curOff.east(), curOff.north(), center.lon(), center.lat());
     83        layer.getDisplaySettings().setOffsetBookmark(tempOffset);
    6984        addListeners();
    7085        showOffsetDialog(new ImageryOffsetDialog());
     
    96111        if (offsetDialog != null) {
    97112            if (layer != null) {
    98                 layer.getDisplaySettings().setDisplacement(old);
     113                layer.getDisplaySettings().setOffsetBookmark(old);
    99114            }
    100115            hideOffsetDialog();
     
    136151        if (dx != 0 || dy != 0) {
    137152            double ppd = layer.getPPD();
    138             layer.getDisplaySettings().addDisplacement(new EastNorth(dx / ppd, dy / ppd));
     153            EastNorth d = tempOffset.getDisplacement().add(new EastNorth(dx / ppd, dy / ppd));
     154            tempOffset.setDisplacement(d);
     155            layer.getDisplaySettings().setOffsetBookmark(tempOffset);
    139156            if (offsetDialog != null) {
    140157                offsetDialog.updateOffset();
     
    163180        if (layer == null || prevEastNorth == null) return;
    164181        EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
    165         EastNorth d = layer.getDisplaySettings().getDisplacement().add(eastNorth).subtract(prevEastNorth);
    166         layer.getDisplaySettings().setDisplacement(d);
     182        EastNorth d = tempOffset.getDisplacement().add(eastNorth).subtract(prevEastNorth);
     183        tempOffset.setDisplacement(d);
     184        layer.getDisplaySettings().setOffsetBookmark(tempOffset);
    167185        if (offsetDialog != null) {
    168186            offsetDialog.updateOffset();
     
    239257                    double dx = Double.parseDouble(easting);
    240258                    double dy = Double.parseDouble(northing);
    241                     layer.getDisplaySettings().setDisplacement(new EastNorth(dx, dy));
     259                    tempOffset.setDisplacement(new EastNorth(dx, dy));
     260                    layer.getDisplaySettings().setOffsetBookmark(tempOffset);
    242261                } catch (NumberFormatException nfe) {
    243262                    // we repaint offset numbers in any case
     
    305324            if (layer != null) {
    306325                if (getValue() != 1) {
    307                     layer.getDisplaySettings().setDisplacement(old);
     326                    layer.getDisplaySettings().setOffsetBookmark(old);
    308327                } else if (tBookmarkName.getText() != null && !tBookmarkName.getText().isEmpty()) {
    309328                    OffsetBookmark.bookmarkOffset(tBookmarkName.getText(), layer);
  • trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java

    r11844 r12093  
    1515
    1616    private static final long serialVersionUID = 1L;
     17
     18    public static final EastNorth ZERO = new EastNorth(0, 0);
    1719
    1820    /**
  • trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java

    r12084 r12093  
    1212import org.openstreetmap.josm.Main;
    1313import org.openstreetmap.josm.data.Preferences.pref;
     14import org.openstreetmap.josm.data.Preferences.writeExplicitly;
    1415import org.openstreetmap.josm.data.coor.EastNorth;
    1516import org.openstreetmap.josm.data.coor.LatLon;
     17import org.openstreetmap.josm.data.projection.Projection;
     18import org.openstreetmap.josm.data.projection.Projections;
    1619import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    1720import org.openstreetmap.josm.gui.layer.ImageryLayer;
    1821
     22/**
     23 * Class to save a displacement of background imagery as a bookmark.
     24 *
     25 * Known offset bookmarks will be stored in the preferences and can be
     26 * restored by the user in later sessions.
     27 */
    1928public class OffsetBookmark {
    2029    private static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
     
    2332    @pref private String imagery_name;
    2433    @pref private String name;
    25     @pref private double dx, dy;
     34    @pref @writeExplicitly private double dx, dy;
    2635    @pref private double center_lon, center_lat;
    2736
    2837    public boolean isUsable(ImageryLayer layer) {
    2938        if (projection_code == null) return false;
    30         if (!Main.getProjection().toCode().equals(projection_code)) return false;
     39        if (!Main.getProjection().toCode().equals(projection_code) && !hasCenter()) return false;
    3140        return layer.getInfo().getName().equals(imagery_name);
    3241    }
     
    8392    }
    8493
    85     public EastNorth getOffset() {
     94    /**
     95     * Get displacement in EastNorth coordinates of the original projection.
     96     *
     97     * @see #getProjectionCode()
     98     * @return the displacement
     99     */
     100    public EastNorth getDisplacement() {
    86101        return new EastNorth(dx, dy);
    87102    }
    88103
     104    /**
     105     * Get displacement in EastNorth coordinates of a given projection.
     106     *
     107     * Displacement will be converted to the given projection, with respect to the
     108     * center (reference point) of this bookmark.
     109     * @param proj the projection
     110     * @return the displacement, converted to that projection
     111     */
     112    public EastNorth getDisplacement(Projection proj) {
     113        if (proj.toCode().equals(projection_code)) {
     114            return getDisplacement();
     115        }
     116        LatLon center = getCenter();
     117        Projection offsetProj = Projections.getProjectionByCode(projection_code);
     118        EastNorth centerEN = offsetProj.latlon2eastNorth(center);
     119        EastNorth shiftedEN = centerEN.add(getDisplacement());
     120        LatLon shifted = offsetProj.eastNorth2latlon(shiftedEN);
     121        EastNorth centerEN2 = proj.latlon2eastNorth(center);
     122        EastNorth shiftedEN2 = proj.latlon2eastNorth(shifted);
     123        return shiftedEN2.subtract(centerEN2);
     124    }
     125
     126    /**
     127     * Get center/reference point of the bookmark.
     128     *
     129     * Basically this is the place where it was created and is valid.
     130     * The center may be unrecorded (see {@link #hasCenter()), in which
     131     * case a dummy center (0,0) will be returned.
     132     * @return the center
     133     */
    89134    public LatLon getCenter() {
    90135        return new LatLon(center_lat, center_lon);
    91136    }
    92137
     138    /**
     139     * Check if bookmark has a valid center.
     140     * @return true if bookmark has a valid center
     141     */
     142    public boolean hasCenter() {
     143        return center_lat != 0 || center_lon != 0;
     144    }
     145
    93146    public void setProjectionCode(String projectionCode) {
    94147        this.projection_code = projectionCode;
     
    103156    }
    104157
    105     public void setOffset(EastNorth offset) {
    106         this.dx = offset.east();
    107         this.dy = offset.north();
     158    public void setDisplacement(EastNorth displacement) {
     159        this.dx = displacement.east();
     160        this.dy = displacement.north();
    108161    }
    109162
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r12028 r12093  
    8181import org.openstreetmap.josm.data.coor.LatLon;
    8282import org.openstreetmap.josm.data.imagery.ImageryInfo;
     83import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    8384import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
    8485import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
     
    334335    @Override
    335336    @Deprecated
    336     public void displace(double dx, double dy) {
    337         getDisplaySettings().addDisplacement(new EastNorth(dx, dy));
    338     }
    339 
    340     /**
    341      * {@inheritDoc}
    342      * @deprecated Use {@link TileSourceDisplaySettings}
    343      */
    344     @Override
    345     @Deprecated
    346     public void setOffset(double dx, double dy) {
    347         getDisplaySettings().setDisplacement(new EastNorth(dx, dy));
     337    public void setOffset(OffsetBookmark offset) {
     338        getDisplaySettings().setOffsetBookmark(offset);
    348339    }
    349340
     
    18561847    public void projectionChanged(Projection oldValue, Projection newValue) {
    18571848        super.projectionChanged(oldValue, newValue);
     1849        displaySettings.setOffsetBookmark(displaySettings.getOffsetBookmark());
    18581850        if (tileCache != null) {
    18591851            tileCache.clear();
  • trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java

    r12085 r12093  
    3131import org.openstreetmap.josm.Main;
    3232import org.openstreetmap.josm.data.ProjectionBounds;
     33import org.openstreetmap.josm.data.coor.EastNorth;
    3334import org.openstreetmap.josm.data.imagery.ImageryInfo;
    3435import org.openstreetmap.josm.data.imagery.OffsetBookmark;
     
    108109     * Sets the displacement offset of this layer. The layer is automatically invalidated.
    109110     * To be removed end of 2016
    110      * @param dx The x offset
    111      * @param dy The y offset
     111     * @param offset the offset bookmark
    112112     * @deprecated Use {@link TileSourceDisplaySettings}
    113113     */
    114114    @Deprecated
    115     public void setOffset(double dx, double dy) {
    116         // moved to AbstractTileSourceLayer/TileSourceDisplaySettings. Remains until all actions migrate.
    117     }
    118 
    119     /**
    120      * To be removed end of 2016
    121      * @param dx deprecated
    122      * @param dy deprecated
    123      * @deprecated Use {@link TileSourceDisplaySettings}
    124      */
    125     @Deprecated
    126     public void displace(double dx, double dy) {
     115    public void setOffset(OffsetBookmark offset) {
    127116        // moved to AbstractTileSourceLayer/TileSourceDisplaySettings. Remains until all actions migrate.
    128117    }
     
    210199        @Override
    211200        public void actionPerformed(ActionEvent ev) {
    212             setOffset(b.getOffset().east(), b.getOffset().north());
     201            setOffset(b);
    213202            Main.main.menu.imageryMenu.refreshOffsetMenu();
    214203            Main.map.repaint();
     
    253242            }
    254243            JCheckBoxMenuItem item = new JCheckBoxMenuItem(new ApplyOffsetAction(b));
    255             if (Utils.equalsEpsilon(b.getOffset().east(), getDx()) &&
    256                     Utils.equalsEpsilon(b.getOffset().north(), getDy())) {
     244            EastNorth offset = b.getDisplacement(Main.getProjection());
     245            if (Utils.equalsEpsilon(offset.east(), getDx()) && Utils.equalsEpsilon(offset.north(), getDy())) {
    257246                item.setSelected(true);
    258247            }
  • trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileSourceDisplaySettings.java

    r11893 r12093  
    88import org.openstreetmap.josm.Main;
    99import org.openstreetmap.josm.data.coor.EastNorth;
     10import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    1011import org.openstreetmap.josm.data.preferences.BooleanProperty;
    1112import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
     
    6263    private boolean showErrors;
    6364
    64     /**
    65      * The displacement
    66      */
    67     private EastNorth displacement = new EastNorth(0, 0);
     65    private OffsetBookmark offsetBookmark = null;
     66    /**
     67     * the displacement (basically caches the displacement from the offsetBookmark
     68     * in the current projection)
     69     */
     70    private EastNorth displacement = EastNorth.ZERO;
    6871
    6972    private final CopyOnWriteArrayList<DisplaySettingsChangeListener> listeners = new CopyOnWriteArrayList<>();
     
    189192
    190193    /**
    191      * Set the displacement
    192      * @param displacement The new displacement
    193      * @since 10571
    194      */
    195     public void setDisplacement(EastNorth displacement) {
     194     * Sets an offset bookmark to use.
     195     *
     196     * @param offsetBookmark the offset bookmark, may be null
     197     */
     198    public void setOffsetBookmark(OffsetBookmark offsetBookmark) {
     199        this.offsetBookmark = offsetBookmark;
     200        if (offsetBookmark == null) {
     201            setDisplacement(EastNorth.ZERO);
     202        } else {
     203            setDisplacement(offsetBookmark.getDisplacement(Main.getProjection()));
     204        }
     205    }
     206
     207    /**
     208     * Gets the offset bookmark in use.
     209     * @return the offset bookmark, may be null
     210     */
     211    public OffsetBookmark getOffsetBookmark() {
     212        return this.offsetBookmark;
     213    }
     214
     215    private void setDisplacement(EastNorth displacement) {
    196216        CheckParameterUtil.ensureValidCoordinates(displacement, "displacement");
    197217        this.displacement = displacement;
    198218        fireSettingsChange(DISPLACEMENT);
    199     }
    200 
    201     /**
    202      * Adds the given value to the displacement.
    203      * @param displacement The value to add.
    204      * @since 10571
    205      */
    206     public void addDisplacement(EastNorth displacement) {
    207         CheckParameterUtil.ensureValidCoordinates(displacement, "displacement");
    208         setDisplacement(this.displacement.add(displacement));
    209219    }
    210220
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r12084 r12093  
    912912                    return info.getName();
    913913                case 3:
    914                     return info.getOffset().east();
     914                    return info.getDisplacement().east();
    915915                case 4:
    916                     return info.getOffset().north();
     916                    return info.getDisplacement().north();
    917917                default:
    918918                    throw new ArrayIndexOutOfBoundsException();
     
    932932                case 3:
    933933                    double dx = Double.parseDouble((String) o);
    934                     info.setOffset(new EastNorth(dx, info.getOffset().north()));
     934                    info.setDisplacement(new EastNorth(dx, info.getDisplacement().north()));
    935935                    break;
    936936                case 4:
    937937                    double dy = Double.parseDouble((String) o);
    938                     info.setOffset(new EastNorth(info.getOffset().east(), dy));
     938                    info.setDisplacement(new EastNorth(info.getDisplacement().east(), dy));
    939939                    break;
    940940                default:
Note: See TracChangeset for help on using the changeset viewer.