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

fixed #14734 - Handling imagery offsets when reprojecting

Location:
trunk/src/org/openstreetmap/josm/data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.