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.ProjectionRegistry;
008
009/**
010 * The data on a single node (tags and position) that is stored in the database
011 */
012public class NodeData extends PrimitiveData implements INode {
013
014    private static final long serialVersionUID = 5626323599550908773L;
015    private static final UniqueIdGenerator idGenerator = Node.idGenerator;
016    /*
017     * we "inline" lat/lon coordinates instead of using a LatLon => reduces memory footprint
018     */
019    private double lat = Double.NaN;
020    private double lon = Double.NaN;
021
022    /**
023     * Constructs a new {@code NodeData}.
024     */
025    public NodeData() {
026        // contents can be set later with setters
027        this(idGenerator.generateUniqueId());
028    }
029
030    /**
031     * Constructs a new {@code NodeData} with given id.
032     * @param id id
033     * @since 12017
034     */
035    public NodeData(long id) {
036        super(id);
037    }
038
039    /**
040     * Constructs a new {@code NodeData}.
041     * @param data node data to copy
042     */
043    public NodeData(NodeData data) {
044        super(data);
045        setCoor(data.getCoor());
046    }
047
048    @Override
049    public double lat() {
050        return lat;
051    }
052
053    @Override
054    public double lon() {
055        return lon;
056    }
057
058    @Override
059    public boolean isLatLonKnown() {
060        return !Double.isNaN(lat) && !Double.isNaN(lon);
061    }
062
063    @Override
064    public LatLon getCoor() {
065        return isLatLonKnown() ? new LatLon(lat, lon) : null;
066    }
067
068    @Override
069    public final void setCoor(LatLon coor) {
070        if (coor == null) {
071            this.lat = Double.NaN;
072            this.lon = Double.NaN;
073        } else {
074            this.lat = coor.lat();
075            this.lon = coor.lon();
076        }
077    }
078
079    @Override
080    public void setEastNorth(EastNorth eastNorth) {
081        setCoor(ProjectionRegistry.getProjection().eastNorth2latlon(eastNorth));
082    }
083
084    @Override
085    public NodeData makeCopy() {
086        return new NodeData(this);
087    }
088
089    @Override
090    public String toString() {
091        return super.toString() + " NODE " + getCoor();
092    }
093
094    @Override
095    public OsmPrimitiveType getType() {
096        return OsmPrimitiveType.NODE;
097    }
098
099    @Override
100    public void accept(PrimitiveVisitor visitor) {
101        visitor.visit(this);
102    }
103
104    @Override
105    public BBox getBBox() {
106        return new BBox(lon, lat);
107    }
108
109    @Override
110    public boolean isReferredByWays(int n) {
111        return false;
112    }
113
114    @Override
115    public UniqueIdGenerator getIdGenerator() {
116        return idGenerator;
117    }
118}