aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/geo/BoundingBoxParser.java
blob: 17ba536b4739ab49318747c7c0984622fea18ca9 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.geo;

/**
 * Class for parsing a bounding box in text format:
 * "n=37.44899,s=37.3323,e=-121.98241,w=-122.06566"
 *
 * <pre>
 * Input from:
 * http://gws.maps.yahoo.com/findlocation?q=sunnyvale,ca&amp;amp;flags=X
 * which gives this format:
 * &lt;boundingbox&gt;
 * &lt;north&gt;37.44899&lt;/north&gt;&lt;south&gt;37.3323&lt;/south&gt;&lt;east&gt;-121.98241&lt;/east&gt;&lt;west&gt;-122.06566&lt;/west&gt;
 * &lt;/boundingbox&gt;
 * it's also easy to use the geoplanet bounding box
 * &lt;boundingBox&gt;  
 * &lt;southWest&gt;  
 * &lt;latitude&gt;40.183868&lt;/latitude&gt;  
 * &lt;longitude&gt;-74.819519&lt;/longitude&gt;  
 * &lt;/southWest&gt;  
 * &lt;northEast&gt;  
 * &lt;latitude&gt;40.248291&lt;/latitude&gt;  
 * &lt;longitude&gt;-74.728798&lt;/longitude&gt;  
 * &lt;/northEast&gt;  
 * &lt;/boundingBox&gt;  
 * can be input as:
 * s=40.183868,w=-74.819519,n=40.248291,e=-74.728798
 * </pre>
 *
 * @author arnej27959
 */
public class BoundingBoxParser {

    // return variables
    public double n = 0.0;
    public double s = 0.0;
    public double e = 0.0;
    public double w = 0.0;

    /**
     * parse the given string as a bounding box and return a parser object with parsed coordinates in member variables
     * @throws IllegalArgumentException if the input is malformed in any way
     */
    public BoundingBoxParser(String bb) {
        this.parseString = bb;
        this.len = bb.length();
        parse();
    }

    private final String parseString;
    private final int len;
    private int pos = 0;

    private char getNextChar() throws IllegalArgumentException {
        if (pos == len) {
            pos++;
            return 0;
        } else if (pos > len) {
            throw new IllegalArgumentException("position after end of string");
        } else {
            return parseString.charAt(pos++);
        }
    }

    private boolean isCompassDirection(char ch) {
        return (ch == 'N' || ch == 'S' || ch == 'E' || ch == 'W' ||
                ch == 'n' || ch == 's' || ch == 'e' || ch == 'w');
    }

    private int lastNumStartPos = 0;

    private char nsew = 0;
    private boolean doneN = false;
    private boolean doneS = false;
    private boolean doneE = false;
    private boolean doneW = false;

    private void parse() {
        do {
            char ch = getNextChar();
            if (isCompassDirection(ch) && nsew == 0) {
                if (ch == 'n' || ch =='N') {
                    nsew = 'n';
                } else if (ch == 's' || ch == 'S') {
                    nsew = 's';
                } else if (ch == 'e' || ch == 'E') {
                    nsew = 'e';
                } else if (ch == 'w' || ch == 'W') {
                    nsew = 'w';
                }
                lastNumStartPos = 0;
            }
            if (ch == '=' || ch == ':') {
                if (nsew != 0) {
                    lastNumStartPos = pos;
                }
            }
            if (ch == ',' || ch == 0 || ch == ' ') {
                if (nsew != 0 && lastNumStartPos > 0) {
                    String sub = parseString.substring(lastNumStartPos, pos-1);
                    try {
                        double v = Double.parseDouble(sub);
                        if (nsew == 'n') {
                            if (doneN) {
                                throw new IllegalArgumentException("multiple limits for 'n' boundary");
                            }
                            n = v;
                            doneN = true;
                        } else if (nsew == 's') {
                            if (doneS) {
                                throw new IllegalArgumentException("multiple limits for 's' boundary");
                            }
                            s = v;
                            doneS = true;
                        } else if (nsew == 'e') {
                            if (doneE) {
                                throw new IllegalArgumentException("multiple limits for 'e' boundary");
                            }
                            e = v;
                            doneE = true;
                        } else if (nsew == 'w') {
                            if (doneW) {
                                throw new IllegalArgumentException("multiple limits for 'w' boundary");
                            }
                            w = v;
                            doneW = true;
                        }
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException("Could not parse "+nsew+" limit '"+sub+"' as a number");
                    }
                    nsew = 0;
                }
            }
        } while (pos <= len);

        if (doneN && doneS && doneE && doneW) {
            return;
        } else {
            throw new IllegalArgumentException("Missing bounding box limits, n="+doneN+" s="+doneS+" e="+doneE+" w="+doneW);
        }
    }

}