Completed basic support for NFC / NDEF formats applicable to 2D barcodes. Not yet...
[zxing.git] / core / src / com / google / zxing / client / result / GeoParsedResult.java
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 import com.google.zxing.Result;
20
21 /**
22  * Represents a "geo:" URI result, which specifices a location on the surface of
23  * the Earth as well as an optional altitude above the surface. See
24  * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
25  * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
26  *
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class GeoParsedResult extends ParsedReaderResult {
30
31   private final String geoURI;
32   private final float latitude;
33   private final float longitude;
34   private final float altitude;
35
36   private GeoParsedResult(String geoURI, float latitude, float longitude, float altitude) {
37     super(ParsedReaderResultType.GEO);
38     this.geoURI = geoURI;
39     this.latitude = latitude;
40     this.longitude = longitude;
41     this.altitude = altitude;
42   }
43
44   public static GeoParsedResult parse(Result result) {
45     String rawText = result.getText();
46     if (rawText == null || !rawText.startsWith("geo:")) {
47       return null;
48     }
49     // Drop geo, query portion
50     int queryStart = rawText.indexOf('?', 4);
51     if (queryStart < 0) {
52       rawText = rawText.substring(4);
53     } else {
54       rawText = rawText.substring(4, queryStart);
55     }
56     int latitudeEnd = rawText.indexOf(',');
57     if (latitudeEnd < 0) {
58       return null;
59     }
60     float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
61     int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
62     float longitude;
63     float altitude; // in meters
64     if (longitudeEnd < 0) {
65       longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
66       altitude = 0.0f;
67     } else {
68       longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1, longitudeEnd));
69       altitude = Float.parseFloat(rawText.substring(longitudeEnd + 1));
70     }
71     return new GeoParsedResult(rawText, latitude, longitude, altitude);
72   }
73
74   public String getGeoURI() {
75     return geoURI;
76   }
77
78   /**
79    * @return latitude in degrees
80    */
81   public float getLatitude() {
82     return latitude;
83   }
84
85   /**
86    * @return longitude in degrees
87    */
88   public float getLongitude() {
89     return longitude;
90   }
91
92   /**
93    * @return altitude in meters. If not specified, in the geo URI, returns 0.0
94    */
95   public float getAltitude() {
96     return altitude;
97   }
98
99   public String getDisplayResult() {
100     StringBuffer result = new StringBuffer(50);
101     result.append(latitude);
102     result.append(" deg N, ");
103     result.append(longitude);
104     result.append(" deg E");
105     if (altitude > 0.0f) {
106       result.append(", ");
107       result.append(altitude);
108       result.append('m');
109     }
110     return result.toString();
111   }
112
113   /**
114    * @return a URI link to Google Maps which display the point on the Earth described
115    *  by this instance, and sets the zoom level in a way that roughly reflects the
116    *  altitude, if specified
117    */
118   /*
119   public String getGoogleMapsURI() {
120     StringBuffer result = new StringBuffer(50);
121     result.append("http://maps.google.com/?ll=");
122     result.append(latitude);
123     result.append(',');
124     result.append(longitude);
125     if (altitude > 0.0f) {
126       // Map altitude to zoom level, cleverly. Roughly, zoom level 19 is like a
127       // view from 1000ft, 18 is like 2000ft, 17 like 4000ft, and so on.
128       float altitudeInFeet = altitude * 3.28f;
129       int altitudeInKFeet = (int) (altitudeInFeet / 1000.0f);
130       // No Math.log() available here, so compute log base 2 the old fashioned way
131       // Here logBaseTwo will take on a value between 0 and 18 actually
132       int logBaseTwo = 0;
133       while (altitudeInKFeet > 1 && logBaseTwo < 18) {
134         altitudeInKFeet >>= 1;
135         logBaseTwo++;
136       }
137       int zoom = 19 - logBaseTwo;
138       result.append("&z=");
139       result.append(zoom);
140     }
141     return result.toString();
142   }
143    */
144
145 }