|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.mp4.boxes; |
| 18 | + |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +/** |
| 23 | + * Parses ISO 6709 location strings as written in QuickTime/MP4 metadata: the udta |
| 24 | + * "©xyz" box and the {@code com.apple.quicktime.location.ISO6709} value. Returns the |
| 25 | + * latitude, longitude and optional altitude. |
| 26 | + * <p> |
| 27 | + * Both the decimal-degree form (e.g. {@code +12.3456-098.7654+010.500/}) and the ISO 6709 |
| 28 | + * sexagesimal compact forms ({@code ±DDMM.MMMM} / {@code ±DDMMSS.SSSS} for latitude and the |
| 29 | + * {@code ±DDD...} equivalents for longitude) are handled. The form is chosen by the |
| 30 | + * integer-digit count of each angle, which is unambiguous for valid coordinates: a decimal |
| 31 | + * latitude ({@code |lat| <= 90}) has at most 2 integer digits and a decimal longitude |
| 32 | + * ({@code |lon| <= 180}) at most 3, so neither can collide with the 4/6 (latitude) or 5/7 |
| 33 | + * (longitude) integer-digit counts of the minute/second forms. Any other digit count falls |
| 34 | + * back to a lenient decimal parse. |
| 35 | + */ |
| 36 | +public final class ISO6709 { |
| 37 | + |
| 38 | + //latitude, longitude and an optional altitude, each a signed number; any trailing |
| 39 | + //CRS designator and the "/" terminator are ignored |
| 40 | + private static final Pattern PATTERN = Pattern.compile( |
| 41 | + "([+-]\\d+(?:\\.\\d+)?)([+-]\\d+(?:\\.\\d+)?)([+-]\\d+(?:\\.\\d+)?)?"); |
| 42 | + |
| 43 | + private ISO6709() { |
| 44 | + } |
| 45 | + |
| 46 | + public static final class Location { |
| 47 | + public final double latitude; |
| 48 | + public final double longitude; |
| 49 | + //null when the string carries no altitude component |
| 50 | + public final Double altitude; |
| 51 | + |
| 52 | + Location(double latitude, double longitude, Double altitude) { |
| 53 | + this.latitude = latitude; |
| 54 | + this.longitude = longitude; |
| 55 | + this.altitude = altitude; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param s an ISO 6709 location string, or null |
| 61 | + * @return the parsed location, or null if {@code s} is null or contains no location |
| 62 | + */ |
| 63 | + public static Location parse(String s) { |
| 64 | + if (s == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + Matcher matcher = PATTERN.matcher(s); |
| 68 | + if (!matcher.find()) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + double latitude = decodeAngle(matcher.group(1), 2); //latitude: 2 degree digits |
| 72 | + double longitude = decodeAngle(matcher.group(2), 3); //longitude: 3 degree digits |
| 73 | + Double altitude = matcher.group(3) == null ? null : Double.parseDouble(matcher.group(3)); |
| 74 | + return new Location(latitude, longitude, altitude); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Decodes one signed angle. {@code degreeDigits} is the number of integer digits the |
| 79 | + * degrees field occupies in the ISO 6709 compact forms: 2 for latitude, 3 for longitude. |
| 80 | + */ |
| 81 | + private static double decodeAngle(String token, int degreeDigits) { |
| 82 | + char sign = token.charAt(0); |
| 83 | + int dot = token.indexOf('.'); |
| 84 | + String intPart = dot < 0 ? token.substring(1) : token.substring(1, dot); |
| 85 | + String frac = dot < 0 ? "" : token.substring(dot); //includes the '.' |
| 86 | + int digits = intPart.length(); |
| 87 | + double value; |
| 88 | + if (digits == degreeDigits + 2) { |
| 89 | + //±DDMM.MMMM (or ±DDDMM.MMMM): degrees + minutes/60 |
| 90 | + double degrees = Integer.parseInt(intPart.substring(0, degreeDigits)); |
| 91 | + double minutes = Double.parseDouble(intPart.substring(degreeDigits) + frac); |
| 92 | + value = degrees + minutes / 60.0; |
| 93 | + } else if (digits == degreeDigits + 4) { |
| 94 | + //±DDMMSS.SSSS (or ±DDDMMSS.SSSS): degrees + minutes/60 + seconds/3600 |
| 95 | + double degrees = Integer.parseInt(intPart.substring(0, degreeDigits)); |
| 96 | + double minutes = Integer.parseInt(intPart.substring(degreeDigits, degreeDigits + 2)); |
| 97 | + double seconds = Double.parseDouble(intPart.substring(degreeDigits + 2) + frac); |
| 98 | + value = degrees + minutes / 60.0 + seconds / 3600.0; |
| 99 | + } else { |
| 100 | + //decimal degrees (the common case) or a non-conformant count: lenient fallback |
| 101 | + value = Double.parseDouble(intPart + frac); |
| 102 | + } |
| 103 | + return sign == '-' ? -value : value; |
| 104 | + } |
| 105 | +} |
0 commit comments