001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import org.openstreetmap.josm.data.coor.EastNorth;
005import org.openstreetmap.josm.data.coor.LatLon;
006import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
007import org.openstreetmap.josm.data.projection.Projections;
008
009public class NodeData extends PrimitiveData implements INode {
010
011    private static final long serialVersionUID = 5626323599550908773L;
012    /*
013     * we "inline" lat/lon coordinates instead of using a LatLon => reduces memory footprint
014     */
015    private double lat = Double.NaN;
016    private double lon = Double.NaN;
017
018    /**
019     * Constructs a new {@code NodeData}.
020     */
021    public NodeData() {
022        // contents can be set later with setters
023    }
024
025    /**
026     * Constructs a new {@code NodeData}.
027     * @param data node data to copy
028     */
029    public NodeData(NodeData data) {
030        super(data);
031        setCoor(data.getCoor());
032    }
033
034    private boolean isLatLonKnown() {
035        return !Double.isNaN(lat) && !Double.isNaN(lon);
036    }
037
038    @Override
039    public LatLon getCoor() {
040        return isLatLonKnown() ? new LatLon(lat, lon) : null;
041    }
042
043    @Override
044    public final void setCoor(LatLon coor) {
045        if (coor == null) {
046            this.lat = Double.NaN;
047            this.lon = Double.NaN;
048        } else {
049            this.lat = coor.lat();
050            this.lon = coor.lon();
051        }
052    }
053
054    @Override
055    public EastNorth getEastNorth() {
056        // No internal caching of projected coordinates needed. In contrast to getEastNorth()
057        // on Node, this method is rarely used. Caching would be overkill.
058        return Projections.project(getCoor());
059    }
060
061    @Override
062    public void setEastNorth(EastNorth eastNorth) {
063        setCoor(Projections.inverseProject(eastNorth));
064    }
065
066    @Override
067    public NodeData makeCopy() {
068        return new NodeData(this);
069    }
070
071    @Override
072    public String toString() {
073        return super.toString() + " NODE " + getCoor();
074    }
075
076    @Override
077    public OsmPrimitiveType getType() {
078        return OsmPrimitiveType.NODE;
079    }
080
081    @Override
082    public void accept(PrimitiveVisitor visitor) {
083        visitor.visit(this);
084    }
085}