blob: e02e2cf05c017f339b522d84ad5d3dfecaa60e42 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json.readers;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.json.TokenBuffer;
import static com.yahoo.document.json.readers.JsonParserHelpers.*;
/**
* @author arnej
*/
public class GeoPositionReader {
static void fillGeoPosition(TokenBuffer buffer, FieldValue positionFieldValue) {
Double latitude = null;
Double longitude = null;
expectObjectStart(buffer.current());
int initNesting = buffer.nesting();
for (buffer.next(); buffer.nesting() >= initNesting; buffer.next()) {
String curName = buffer.currentName();
if ("lat".equals(curName) || "latitude".equals(curName)) {
latitude = readDouble(buffer) * 1.0e6;
} else if ("lng".equals(curName) || "longitude".equals(curName)) {
longitude = readDouble(buffer) * 1.0e6;
} else if ("x".equals(curName)) {
longitude = readDouble(buffer);
} else if ("y".equals(curName)) {
latitude = readDouble(buffer);
} else {
throw new IllegalArgumentException("Unexpected attribute "+curName+" in geo position field");
}
}
expectObjectEnd(buffer.current());
if (latitude == null) {
throw new IllegalArgumentException("Missing 'lat' attribute in geo position field");
}
if (longitude == null) {
throw new IllegalArgumentException("Missing 'lng' attribute in geo position field");
}
int y = (int) Math.round(latitude);
int x = (int) Math.round(longitude);
var geopos = PositionDataType.valueOf(x, y);
positionFieldValue.assign(geopos);
}
private static double readDouble(TokenBuffer buffer) {
try {
return Double.parseDouble(buffer.currentText());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Expected a number but got '" + buffer.currentText());
}
}
}
|