aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/internal/GeoPosType.java
blob: 2999f7506ee243651c97a66f5e37c059223e9c6d (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.document.internal;

import com.yahoo.document.DataType;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.Field;
import com.yahoo.document.StructDataType;
import com.yahoo.document.datatypes.Struct;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * @author arnej
 **/
public final class GeoPosType extends StructDataType {

    private final boolean useV8json;
    private static final Field F_X = new Field("x", DataType.INT);
    private static final Field F_Y = new Field("y", DataType.INT);

    public GeoPosType(int vespaVersion) {
        super("position");
        this.useV8json = (vespaVersion == 8);
        assert(vespaVersion > 6);
        assert(vespaVersion < 9);
        addField(F_X);
        addField(F_Y);
    }

    public boolean renderJsonAsVespa8() {
        return this.useV8json;
    }

    public double getLatitude(Struct pos) {
        assert(pos.getDataType() == this);
        double ns = PositionDataType.getYValue(pos).getInteger() * 1.0e-6;
        return ns;
    }

    public double getLongitude(Struct pos) {
        assert(pos.getDataType() == this);
        double ew = PositionDataType.getXValue(pos).getInteger() * 1.0e-6;
        return ew;
    }

    private static final DecimalFormat degreeFmt;

    static {
        degreeFmt = new DecimalFormat("0.0#####", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        degreeFmt.setMinimumIntegerDigits(1);
        degreeFmt.setMinimumFractionDigits(1);
        degreeFmt.setMaximumFractionDigits(6);
    }

    static String fmtD(double degrees) {
        return degreeFmt.format(degrees);
    }

    public String fmtLatitude(Struct pos) {
        assert(pos.getDataType() == this);
        double ns = PositionDataType.getYValue(pos).getInteger() * 1.0e-6;
        return fmtD(ns);
    }

    public String fmtLongitude(Struct pos) {
        assert(pos.getDataType() == this);
        double ew = PositionDataType.getXValue(pos).getInteger() * 1.0e-6;
        return fmtD(ew);
    }

}