Ignore:
Timestamp:
2017-05-15T15:43:30+02:00 (7 years ago)
Author:
michael2402
Message:

See #13415: Add the ILatLon interface, unify handling of Nodes and CachedLatLon

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

Legend:

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

    r11747 r12161  
    1010import java.util.function.Consumer;
    1111
     12import org.openstreetmap.josm.data.coor.ILatLon;
    1213import org.openstreetmap.josm.data.coor.LatLon;
    1314import org.openstreetmap.josm.data.osm.BBox;
     
    1819 * This is a simple data class for "rectangular" areas of the world, given in
    1920 * lat/lon min/max values.  The values are rounded to LatLon.OSM_SERVER_PRECISION
     21 *
     22 * @see BBox to represent invalid areas.
    2023 *
    2124 * @author imi
     
    390393    /**
    391394     * Determines if the given point {@code ll} is within these bounds.
     395     * <p>
     396     * Points with unknown coordinates are always outside the coordinates.
    392397     * @param ll The lat/lon to check
    393398     * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise
    394399     */
    395400    public boolean contains(LatLon ll) {
     401        // binary compatibility
     402        return contains((ILatLon) ll);
     403    }
     404
     405    /**
     406     * Determines if the given point {@code ll} is within these bounds.
     407     * <p>
     408     * Points with unknown coordinates are always outside the coordinates.
     409     * @param ll The lat/lon to check
     410     * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise
     411     * @since xxx
     412     */
     413    public boolean contains(ILatLon ll) {
     414        if (!ll.isLatLonKnown()) {
     415            return false;
     416        }
    396417        if (ll.lat() < minLat || ll.lat() > maxLat)
    397418            return false;
  • trunk/src/org/openstreetmap/josm/data/coor/CachedLatLon.java

    r10334 r12161  
    55
    66import org.openstreetmap.josm.Main;
     7import org.openstreetmap.josm.data.projection.Projecting;
    78import org.openstreetmap.josm.data.projection.Projection;
    89
     
    2021
    2122    private EastNorth eastNorth;
    22     private transient Projection proj;
     23    private transient Object cacheKey;
    2324
    2425    /**
     
    3738    public CachedLatLon(LatLon coor) {
    3839        super(coor.lat(), coor.lon());
    39         proj = null;
     40        cacheKey = null;
    4041    }
    4142
     
    4546     */
    4647    public CachedLatLon(EastNorth eastNorth) {
    47         super(Main.getProjection().eastNorth2latlon(eastNorth));
    48         proj = Main.getProjection();
     48        this(eastNorth, Main.getProjection());
     49    }
     50
     51    private CachedLatLon(EastNorth eastNorth, Projection projection) {
     52        super(projection.eastNorth2latlon(eastNorth));
     53        cacheKey = projection.getCacheKey();
    4954        this.eastNorth = eastNorth;
    5055    }
     
    5560     * @return the internally cached east/north coordinates. null, if the globally defined projection is null
    5661     */
    57     public final EastNorth getEastNorth() {
    58         if (!Objects.equals(proj, Main.getProjection())) {
    59             proj = Main.getProjection();
    60             eastNorth = proj.latlon2eastNorth(this);
     62    @Override
     63    public final EastNorth getEastNorth(Projecting projecting) {
     64        if (!Objects.equals(cacheKey, projecting.getCacheKey())) {
     65            cacheKey = projecting.getCacheKey();
     66            eastNorth = projecting.latlon2eastNorth(this);
    6167        }
    6268        return eastNorth;
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r12131 r12161  
    4343 * @author Imi
    4444 */
    45 public class LatLon extends Coordinate {
     45public class LatLon extends Coordinate implements ILatLon {
    4646
    4747    private static final long serialVersionUID = 1L;
     
    251251    }
    252252
    253     protected LatLon(LatLon coor) {
     253    /**
     254     * Creates a new LatLon object for the given coordinate
     255     * @param coor The coordinates to copy from.
     256     */
     257    public LatLon(ILatLon coor) {
    254258        super(coor.lon(), coor.lat());
    255259    }
     
    263267    }
    264268
    265     /**
    266      * Returns the latitude, i.e., the north-south position in degrees.
    267      * @return the latitude
    268      */
     269    @Override
    269270    public double lat() {
    270271        return y;
     
    286287    }
    287288
    288     /**
    289      * Returns the longitude, i.e., the east-west position in degrees.
    290      * @return the longitude
    291      */
     289    @Override
    292290    public double lon() {
    293291        return x;
  • trunk/src/org/openstreetmap/josm/data/osm/BBox.java

    r11452 r12161  
    77
    88import org.openstreetmap.josm.data.Bounds;
     9import org.openstreetmap.josm.data.coor.ILatLon;
    910import org.openstreetmap.josm.data.coor.LatLon;
    1011import org.openstreetmap.josm.data.coor.QuadTiling;
     
    8586     */
    8687    public BBox(Way w) {
    87         w.getNodes().forEach(n -> add(n.getCoor()));
     88        w.getNodes().forEach(this::add);
    8889    }
    8990
     
    9394     */
    9495    public BBox(Node n) {
    95         if (n.isLatLonKnown()) {
    96             add(n.getCoor());
    97         }
     96        this((ILatLon) n);
     97    }
     98
     99    /**
     100     * Create BBox for a given latlon. An invalid BBox is returned if the coordinates are not known.
     101     * @param ll The lat lon position
     102     */
     103    public BBox(ILatLon ll) {
     104        add(ll);
     105    }
     106
     107    /**
     108     * Add a point to an existing BBox. Extends this bbox if necessary so that this.bounds(c) will return true
     109     * if c is a valid LatLon instance.
     110     * Kept for binary compatibility
     111     * @param c a LatLon point
     112     */
     113    public final void add(LatLon c) {
     114        add((ILatLon) c);
    98115    }
    99116
     
    103120     * @param c a LatLon point
    104121     */
    105     public final void add(LatLon c) {
    106         if (c != null && c.isValid()) {
    107             add(c.lon(), c.lat());
    108         }
     122    public final void add(ILatLon c) {
     123        add(c.lon(), c.lat());
    109124    }
    110125
  • trunk/src/org/openstreetmap/josm/data/osm/INode.java

    r9460 r12161  
    33
    44import org.openstreetmap.josm.data.coor.EastNorth;
     5import org.openstreetmap.josm.data.coor.ILatLon;
    56import org.openstreetmap.josm.data.coor.LatLon;
    67
     
    910 * @since 4098
    1011 */
    11 public interface INode extends IPrimitive {
     12public interface INode extends IPrimitive, ILatLon {
    1213
    1314    /**
     
    2425
    2526    /**
    26      * Returns east/north coordinates of this node.
    27      * @return east/north coordinates of this node
    28      */
    29     EastNorth getEastNorth();
    30 
    31     /**
    3227     * Sets east/north coordinates of this node.
    3328     * @param eastNorth east/north coordinates of this node
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r12031 r12161  
    1515import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    1616import org.openstreetmap.josm.data.osm.visitor.Visitor;
    17 import org.openstreetmap.josm.data.projection.Projection;
     17import org.openstreetmap.josm.data.projection.Projecting;
    1818import org.openstreetmap.josm.data.projection.Projections;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    4848     * @since 7828
    4949     */
     50    @Override
    5051    public boolean isLatLonKnown() {
     52        // We cannot use default implementation - if we remove this implementation, we will break binary compatibility.
    5153        return !Double.isNaN(lat) && !Double.isNaN(lon);
    5254    }
     
    8183    @Override
    8284    public LatLon getCoor() {
    83         if (!isLatLonKnown()) return null;
    84         return new LatLon(lat, lon);
    85     }
    86 
    87     /**
    88      * <p>Replies the projected east/north coordinates.</p>
    89      *
    90      * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
    91      * Internally caches the projected coordinates.</p>
    92      *
    93      * <p>Replies {@code null} if this node doesn't know lat/lon-coordinates, i.e. because it is an incomplete node.
    94      *
    95      * @return the east north coordinates or {@code null}
    96      * @see #invalidateEastNorthCache()
    97      *
    98      */
    99     @Override
    100     public EastNorth getEastNorth() {
    101         return getEastNorth(Main.getProjection());
    102     }
    103 
    104     /**
    105      * Replies the projected east/north coordinates.
    106      * <p>
    107      * The result of the last conversion is cached. The cache object is used as cache key.
    108      * @param projection The projection to use.
    109      * @return The projected east/north coordinates
    110      * @since 10827
    111      */
    112     public EastNorth getEastNorth(Projection projection) {
     85        if (!isLatLonKnown()) {
     86            return null;
     87        } else {
     88            return new LatLon(lat, lon);
     89        }
     90    }
     91
     92    @Override
     93    public double lat() {
     94        return lat;
     95    }
     96
     97    @Override
     98    public double lon() {
     99        return lon;
     100    }
     101
     102    @Override
     103    public EastNorth getEastNorth(Projecting projection) {
    113104        if (!isLatLonKnown()) return null;
    114105
  • trunk/src/org/openstreetmap/josm/data/osm/NodeData.java

    r12017 r12161  
    4141    }
    4242
    43     private boolean isLatLonKnown() {
     43    @Override
     44    public double lat() {
     45        return lat;
     46    }
     47
     48    @Override
     49    public double lon() {
     50        return lon;
     51    }
     52
     53    @Override
     54    public boolean isLatLonKnown() {
    4455        return !Double.isNaN(lat) && !Double.isNaN(lon);
    4556    }
  • trunk/src/org/openstreetmap/josm/data/osm/NodePositionComparator.java

    r9880 r12161  
    2020            return 0;
    2121
    22         int dLat = Double.compare(n1.getCoor().lat(), n2.getCoor().lat());
    23         return dLat != 0 ? dLat : Double.compare(n1.getCoor().lon(), n2.getCoor().lon());
     22        int dLat = Double.compare(n1.lat(), n2.lat());
     23        return dLat != 0 ? dLat : Double.compare(n1.lon(), n2.lon());
    2424    }
    2525}
  • trunk/src/org/openstreetmap/josm/data/projection/AbstractProjection.java

    r12149 r12161  
    1010import org.openstreetmap.josm.data.ProjectionBounds;
    1111import org.openstreetmap.josm.data.coor.EastNorth;
     12import org.openstreetmap.josm.data.coor.ILatLon;
    1213import org.openstreetmap.josm.data.coor.LatLon;
    1314import org.openstreetmap.josm.data.projection.datum.Datum;
     
    114115
    115116    @Override
    116     public EastNorth latlon2eastNorth(LatLon ll) {
    117         ll = datum.fromWGS84(ll);
     117    public EastNorth latlon2eastNorth(ILatLon toConvert) {
     118        // TODO: Use ILatLon in datum, so we don't need to wrap it here.
     119        LatLon ll = datum.fromWGS84(new LatLon(toConvert));
    118120        double[] en = proj.project(Utils.toRadians(ll.lat()), Utils.toRadians(LatLon.normalizeLon(ll.lon() - lon0 - pm)));
    119121        return new EastNorth((ellps.a * k0 * en[0] + x0) / toMeter, (ellps.a * k0 * en[1] + y0) / toMeter);
     
    122124    @Override
    123125    public LatLon eastNorth2latlon(EastNorth en) {
     126        // We know it is a latlon. Nice would be to change this method return type to ILatLon
    124127        return eastNorth2latlon(en, LatLon::normalizeLon);
    125128    }
     
    127130    @Override
    128131    public LatLon eastNorth2latlonClamped(EastNorth en) {
    129         LatLon ll = eastNorth2latlon(en, lon -> Utils.clamp(lon, -180, 180));
     132        ILatLon ll = eastNorth2latlon(en, lon -> Utils.clamp(lon, -180, 180));
    130133        Bounds bounds = getWorldBoundsLatLon();
    131134        return new LatLon(Utils.clamp(ll.lat(), bounds.getMinLat(), bounds.getMaxLat()),
  • trunk/src/org/openstreetmap/josm/data/projection/Ellipsoid.java

    r12013 r12161  
    77package org.openstreetmap.josm.data.projection;
    88
     9import org.openstreetmap.josm.data.coor.ILatLon;
    910import org.openstreetmap.josm.data.coor.LatLon;
    1011import org.openstreetmap.josm.tools.Utils;
  • trunk/src/org/openstreetmap/josm/data/projection/Projecting.java

    r10805 r12161  
    66import org.openstreetmap.josm.data.ProjectionBounds;
    77import org.openstreetmap.josm.data.coor.EastNorth;
     8import org.openstreetmap.josm.data.coor.ILatLon;
    89import org.openstreetmap.josm.data.coor.LatLon;
    910
     
    1920    /**
    2021     * Convert from lat/lon to easting/northing.
     22     * <p>
     23     * This method exists to not break binary compatibility with old plugins
    2124     *
    2225     * @param ll the geographical point to convert (in WGS84 lat/lon)
    2326     * @return the corresponding east/north coordinates
    2427     */
    25     EastNorth latlon2eastNorth(LatLon ll);
     28    default EastNorth latlon2eastNorth(LatLon ll) {
     29        return latlon2eastNorth((ILatLon) ll);
     30    }
     31
     32    /**
     33     * Convert from lat/lon to easting/northing. This method uses the newer {@link ILatLon} interface.
     34     *
     35     * @param ll the geographical point to convert (in WGS84 lat/lon)
     36     * @return the corresponding east/north coordinates
     37     * @since xxx
     38     */
     39    EastNorth latlon2eastNorth(ILatLon ll);
    2640
    2741    /**
     
    4862     */
    4963    Map<ProjectionBounds, Projecting> getProjectingsForArea(ProjectionBounds area);
     64
     65    /**
     66     * Gets the object used as cache identifier when caching results of this projection.
     67     * @return The object to use as cache key
     68     * @since 10827
     69     */
     70    default Object getCacheKey() {
     71        return this;
     72    }
    5073}
  • trunk/src/org/openstreetmap/josm/data/projection/Projection.java

    r11858 r12161  
    119119     */
    120120    boolean switchXY();
    121 
    122     /**
    123      * Gets the object used as cache identifier when caching results of this projection.
    124      * @return The object to use as cache key
    125      * @since 10827
    126      */
    127     default Object getCacheKey() {
    128         return this;
    129     }
    130121}
  • trunk/src/org/openstreetmap/josm/data/projection/ShiftedProjecting.java

    r10836 r12161  
    77import org.openstreetmap.josm.data.ProjectionBounds;
    88import org.openstreetmap.josm.data.coor.EastNorth;
     9import org.openstreetmap.josm.data.coor.ILatLon;
    910import org.openstreetmap.josm.data.coor.LatLon;
    1011
     
    2930
    3031    @Override
    31     public EastNorth latlon2eastNorth(LatLon ll) {
     32    public EastNorth latlon2eastNorth(ILatLon ll) {
    3233        return base.latlon2eastNorth(ll).add(offset);
    3334    }
  • trunk/src/org/openstreetmap/josm/data/validation/PaintVisitor.java

    r12131 r12161  
    33
    44import java.awt.Color;
    5 import java.awt.Graphics;
     5import java.awt.Graphics2D;
    66import java.awt.Point;
    77import java.util.HashSet;
     
    1818import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    1919import org.openstreetmap.josm.gui.MapView;
     20import org.openstreetmap.josm.gui.draw.MapViewPath;
     21import org.openstreetmap.josm.gui.draw.SymbolShape;
    2022import org.openstreetmap.josm.tools.Utils;
    2123
     
    2729public class PaintVisitor extends AbstractVisitor implements ValidatorVisitor {
    2830    /** The graphics */
    29     private final Graphics g;
     31    private final Graphics2D g;
    3032    /** The MapView */
    3133    private final MapView mv;
     
    4446     * @param mv The Mapview
    4547     */
    46     public PaintVisitor(Graphics g, MapView mv) {
     48    public PaintVisitor(Graphics2D g, MapView mv) {
    4749        this.g = g;
    4850        this.mv = mv;
     
    121123
    122124        if (!paintedPoints.contains(pp)) {
    123             Point p = mv.getPoint(n);
     125            MapViewPath circle = new MapViewPath(mv.getState()).shapeAround(n, SymbolShape.CIRCLE, 10);
    124126
    125127            if (selected) {
    126128                g.setColor(getHighlightColor(color));
    127                 g.fillOval(p.x - 5, p.y - 5, 10, 10);
     129                g.fill(circle);
    128130            }
    129131            g.setColor(color);
    130             g.drawOval(p.x - 5, p.y - 5, 10, 10);
     132            g.draw(circle);
    131133            paintedPoints.add(pp);
    132134        }
Note: See TracChangeset for help on using the changeset viewer.