• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.geojson;
2 
3 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5 import org.geojson.jackson.LngLatAltDeserializer;
6 import org.geojson.jackson.LngLatAltSerializer;
7 
8 import java.io.Serializable;
9 import java.util.Arrays;
10 
11 @JsonDeserialize(using = LngLatAltDeserializer.class)
12 @JsonSerialize(using = LngLatAltSerializer.class)
13 public class LngLatAlt implements Serializable{
14 
15 	private double longitude;
16 	private double latitude;
17 	private double altitude = Double.NaN;
18 	private double[] additionalElements = new double[0];
19 
LngLatAlt()20 	public LngLatAlt() {
21 	}
22 
LngLatAlt(double longitude, double latitude)23 	public LngLatAlt(double longitude, double latitude) {
24 		this.longitude = longitude;
25 		this.latitude = latitude;
26 	}
27 
LngLatAlt(double longitude, double latitude, double altitude)28 	public LngLatAlt(double longitude, double latitude, double altitude) {
29 		this.longitude = longitude;
30 		this.latitude = latitude;
31 		this.altitude = altitude;
32 	}
33 
34 	/**
35 	 * Construct a LngLatAlt with additional elements.
36 	 * The specification allows for any number of additional elements in a position, after lng, lat, alt.
37 	 * http://geojson.org/geojson-spec.html#positions
38 	 * @param longitude The longitude.
39 	 * @param latitude The latitude.
40 	 * @param altitude The altitude.
41 	 * @param additionalElements The additional elements.
42      */
LngLatAlt(double longitude, double latitude, double altitude, double... additionalElements)43 	public LngLatAlt(double longitude, double latitude, double altitude, double... additionalElements) {
44 		this.longitude = longitude;
45 		this.latitude = latitude;
46 		this.altitude = altitude;
47 
48 		setAdditionalElements(additionalElements);
49 		checkAltitudeAndAdditionalElements();
50 	}
51 
hasAltitude()52 	public boolean hasAltitude() {
53 		return !Double.isNaN(altitude);
54 	}
55 
hasAdditionalElements()56 	public boolean hasAdditionalElements() {
57 		return additionalElements.length > 0;
58 	}
59 
getLongitude()60 	public double getLongitude() {
61 		return longitude;
62 	}
63 
setLongitude(double longitude)64 	public void setLongitude(double longitude) {
65 		this.longitude = longitude;
66 	}
67 
getLatitude()68 	public double getLatitude() {
69 		return latitude;
70 	}
71 
setLatitude(double latitude)72 	public void setLatitude(double latitude) {
73 		this.latitude = latitude;
74 	}
75 
getAltitude()76 	public double getAltitude() {
77 		return altitude;
78 	}
79 
setAltitude(double altitude)80 	public void setAltitude(double altitude) {
81 		this.altitude = altitude;
82 		checkAltitudeAndAdditionalElements();
83 	}
84 
getAdditionalElements()85 	public double[] getAdditionalElements() {
86 		return additionalElements;
87 	}
88 
setAdditionalElements(double... additionalElements)89 	public void setAdditionalElements(double... additionalElements) {
90 		if (additionalElements != null) {
91 			this.additionalElements = additionalElements;
92 		} else {
93 			this.additionalElements = new double[0];
94 		}
95 
96 		for(double element : this.additionalElements) {
97 			if (Double.isNaN(element)) {
98 				throw new IllegalArgumentException("No additional elements may be NaN.");
99 			}
100 			if (Double.isInfinite(element)) {
101 				throw new IllegalArgumentException("No additional elements may be infinite.");
102 			}
103 		}
104 
105 		checkAltitudeAndAdditionalElements();
106 	}
107 
108 	@Override
equals(Object o)109 	public boolean equals(Object o) {
110 		if (this == o) {
111 			return true;
112 		}
113 		if (!(o instanceof LngLatAlt)) {
114 			return false;
115 		}
116 		LngLatAlt lngLatAlt = (LngLatAlt)o;
117 		return Double.compare(lngLatAlt.latitude, latitude) == 0 && Double.compare(lngLatAlt.longitude, longitude) == 0
118 				&& Double.compare(lngLatAlt.altitude, altitude) == 0 &&
119 				Arrays.equals(lngLatAlt.getAdditionalElements(), additionalElements);
120 	}
121 
122 	@Override
hashCode()123 	public int hashCode() {
124 		long temp = Double.doubleToLongBits(longitude);
125 		int result = (int)(temp ^ (temp >>> 32));
126 		temp = Double.doubleToLongBits(latitude);
127 		result = 31 * result + (int)(temp ^ (temp >>> 32));
128 		temp = Double.doubleToLongBits(altitude);
129 		result = 31 * result + (int)(temp ^ (temp >>> 32));
130 		for(double element : additionalElements) {
131 			temp = Double.doubleToLongBits(element);
132 			result = 31 * result + (int) (temp ^ (temp >>> 32));
133 		}
134 		return result;
135 	}
136 
137 	@Override
toString()138 	public String toString() {
139 		String s =  "LngLatAlt{" + "longitude=" + longitude + ", latitude=" + latitude + ", altitude=" + altitude;
140 
141 		if (hasAdditionalElements()) {
142 			s += ", additionalElements=[";
143 
144 			String suffix = "";
145 			for (Double element : additionalElements) {
146 				if (element != null) {
147 					s += suffix + element;
148 					suffix = ", ";
149 				}
150 			}
151 			s += ']';
152 		}
153 
154 		s += '}';
155 
156 		return s;
157 	}
158 
checkAltitudeAndAdditionalElements()159 	private void checkAltitudeAndAdditionalElements() {
160 		if (!hasAltitude() && hasAdditionalElements()) {
161 			throw new IllegalArgumentException("Additional Elements are only valid if Altitude is also provided.");
162 		}
163 	}
164 }
165