[8132] | 1 | /*
|
---|
[13061] | 2 | * Copyright 2002-2017 Drew Noakes
|
---|
[8132] | 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 | * More information about this project is available at:
|
---|
| 17 | *
|
---|
| 18 | * https://drewnoakes.com/code/exif/
|
---|
| 19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
| 20 | */
|
---|
| 21 | package com.drew.metadata.exif;
|
---|
| 22 |
|
---|
| 23 | import com.drew.lang.GeoLocation;
|
---|
| 24 | import com.drew.lang.Rational;
|
---|
| 25 | import com.drew.lang.annotations.NotNull;
|
---|
| 26 | import com.drew.lang.annotations.Nullable;
|
---|
| 27 |
|
---|
[10862] | 28 | import java.text.DateFormat;
|
---|
| 29 | import java.text.ParseException;
|
---|
| 30 | import java.text.SimpleDateFormat;
|
---|
| 31 | import java.util.Date;
|
---|
[8132] | 32 | import java.util.HashMap;
|
---|
[10862] | 33 | import java.util.Locale;
|
---|
[8132] | 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Describes Exif tags that contain Global Positioning System (GPS) data.
|
---|
| 37 | *
|
---|
| 38 | * @author Drew Noakes https://drewnoakes.com
|
---|
| 39 | */
|
---|
[13061] | 40 | @SuppressWarnings("WeakerAccess")
|
---|
[8243] | 41 | public class GpsDirectory extends ExifDirectoryBase
|
---|
[8132] | 42 | {
|
---|
| 43 | /** GPS tag version GPSVersionID 0 0 BYTE 4 */
|
---|
| 44 | public static final int TAG_VERSION_ID = 0x0000;
|
---|
| 45 | /** North or South Latitude GPSLatitudeRef 1 1 ASCII 2 */
|
---|
| 46 | public static final int TAG_LATITUDE_REF = 0x0001;
|
---|
| 47 | /** Latitude GPSLatitude 2 2 RATIONAL 3 */
|
---|
| 48 | public static final int TAG_LATITUDE = 0x0002;
|
---|
| 49 | /** East or West Longitude GPSLongitudeRef 3 3 ASCII 2 */
|
---|
| 50 | public static final int TAG_LONGITUDE_REF = 0x0003;
|
---|
| 51 | /** Longitude GPSLongitude 4 4 RATIONAL 3 */
|
---|
| 52 | public static final int TAG_LONGITUDE = 0x0004;
|
---|
| 53 | /** Altitude reference GPSAltitudeRef 5 5 BYTE 1 */
|
---|
| 54 | public static final int TAG_ALTITUDE_REF = 0x0005;
|
---|
| 55 | /** Altitude GPSAltitude 6 6 RATIONAL 1 */
|
---|
| 56 | public static final int TAG_ALTITUDE = 0x0006;
|
---|
| 57 | /** GPS time (atomic clock) GPSTimeStamp 7 7 RATIONAL 3 */
|
---|
| 58 | public static final int TAG_TIME_STAMP = 0x0007;
|
---|
| 59 | /** GPS satellites used for measurement GPSSatellites 8 8 ASCII Any */
|
---|
| 60 | public static final int TAG_SATELLITES = 0x0008;
|
---|
| 61 | /** GPS receiver status GPSStatus 9 9 ASCII 2 */
|
---|
| 62 | public static final int TAG_STATUS = 0x0009;
|
---|
| 63 | /** GPS measurement mode GPSMeasureMode 10 A ASCII 2 */
|
---|
| 64 | public static final int TAG_MEASURE_MODE = 0x000A;
|
---|
| 65 | /** Measurement precision GPSDOP 11 B RATIONAL 1 */
|
---|
| 66 | public static final int TAG_DOP = 0x000B;
|
---|
| 67 | /** Speed unit GPSSpeedRef 12 C ASCII 2 */
|
---|
| 68 | public static final int TAG_SPEED_REF = 0x000C;
|
---|
| 69 | /** Speed of GPS receiver GPSSpeed 13 D RATIONAL 1 */
|
---|
| 70 | public static final int TAG_SPEED = 0x000D;
|
---|
| 71 | /** Reference for direction of movement GPSTrackRef 14 E ASCII 2 */
|
---|
| 72 | public static final int TAG_TRACK_REF = 0x000E;
|
---|
| 73 | /** Direction of movement GPSTrack 15 F RATIONAL 1 */
|
---|
| 74 | public static final int TAG_TRACK = 0x000F;
|
---|
| 75 | /** Reference for direction of image GPSImgDirectionRef 16 10 ASCII 2 */
|
---|
| 76 | public static final int TAG_IMG_DIRECTION_REF = 0x0010;
|
---|
| 77 | /** Direction of image GPSImgDirection 17 11 RATIONAL 1 */
|
---|
| 78 | public static final int TAG_IMG_DIRECTION = 0x0011;
|
---|
| 79 | /** Geodetic survey data used GPSMapDatum 18 12 ASCII Any */
|
---|
| 80 | public static final int TAG_MAP_DATUM = 0x0012;
|
---|
| 81 | /** Reference for latitude of destination GPSDestLatitudeRef 19 13 ASCII 2 */
|
---|
| 82 | public static final int TAG_DEST_LATITUDE_REF = 0x0013;
|
---|
| 83 | /** Latitude of destination GPSDestLatitude 20 14 RATIONAL 3 */
|
---|
| 84 | public static final int TAG_DEST_LATITUDE = 0x0014;
|
---|
| 85 | /** Reference for longitude of destination GPSDestLongitudeRef 21 15 ASCII 2 */
|
---|
| 86 | public static final int TAG_DEST_LONGITUDE_REF = 0x0015;
|
---|
| 87 | /** Longitude of destination GPSDestLongitude 22 16 RATIONAL 3 */
|
---|
| 88 | public static final int TAG_DEST_LONGITUDE = 0x0016;
|
---|
| 89 | /** Reference for bearing of destination GPSDestBearingRef 23 17 ASCII 2 */
|
---|
| 90 | public static final int TAG_DEST_BEARING_REF = 0x0017;
|
---|
| 91 | /** Bearing of destination GPSDestBearing 24 18 RATIONAL 1 */
|
---|
| 92 | public static final int TAG_DEST_BEARING = 0x0018;
|
---|
| 93 | /** Reference for distance to destination GPSDestDistanceRef 25 19 ASCII 2 */
|
---|
| 94 | public static final int TAG_DEST_DISTANCE_REF = 0x0019;
|
---|
| 95 | /** Distance to destination GPSDestDistance 26 1A RATIONAL 1 */
|
---|
| 96 | public static final int TAG_DEST_DISTANCE = 0x001A;
|
---|
| 97 |
|
---|
| 98 | /** Values of "GPS", "CELLID", "WLAN" or "MANUAL" by the EXIF spec. */
|
---|
| 99 | public static final int TAG_PROCESSING_METHOD = 0x001B;
|
---|
| 100 | public static final int TAG_AREA_INFORMATION = 0x001C;
|
---|
| 101 | public static final int TAG_DATE_STAMP = 0x001D;
|
---|
| 102 | public static final int TAG_DIFFERENTIAL = 0x001E;
|
---|
| 103 |
|
---|
| 104 | @NotNull
|
---|
| 105 | protected static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>();
|
---|
| 106 |
|
---|
| 107 | static
|
---|
| 108 | {
|
---|
[8243] | 109 | addExifTagNames(_tagNameMap);
|
---|
| 110 |
|
---|
[8132] | 111 | _tagNameMap.put(TAG_VERSION_ID, "GPS Version ID");
|
---|
| 112 | _tagNameMap.put(TAG_LATITUDE_REF, "GPS Latitude Ref");
|
---|
| 113 | _tagNameMap.put(TAG_LATITUDE, "GPS Latitude");
|
---|
| 114 | _tagNameMap.put(TAG_LONGITUDE_REF, "GPS Longitude Ref");
|
---|
| 115 | _tagNameMap.put(TAG_LONGITUDE, "GPS Longitude");
|
---|
| 116 | _tagNameMap.put(TAG_ALTITUDE_REF, "GPS Altitude Ref");
|
---|
| 117 | _tagNameMap.put(TAG_ALTITUDE, "GPS Altitude");
|
---|
| 118 | _tagNameMap.put(TAG_TIME_STAMP, "GPS Time-Stamp");
|
---|
| 119 | _tagNameMap.put(TAG_SATELLITES, "GPS Satellites");
|
---|
| 120 | _tagNameMap.put(TAG_STATUS, "GPS Status");
|
---|
| 121 | _tagNameMap.put(TAG_MEASURE_MODE, "GPS Measure Mode");
|
---|
| 122 | _tagNameMap.put(TAG_DOP, "GPS DOP");
|
---|
| 123 | _tagNameMap.put(TAG_SPEED_REF, "GPS Speed Ref");
|
---|
| 124 | _tagNameMap.put(TAG_SPEED, "GPS Speed");
|
---|
| 125 | _tagNameMap.put(TAG_TRACK_REF, "GPS Track Ref");
|
---|
| 126 | _tagNameMap.put(TAG_TRACK, "GPS Track");
|
---|
| 127 | _tagNameMap.put(TAG_IMG_DIRECTION_REF, "GPS Img Direction Ref");
|
---|
| 128 | _tagNameMap.put(TAG_IMG_DIRECTION, "GPS Img Direction");
|
---|
| 129 | _tagNameMap.put(TAG_MAP_DATUM, "GPS Map Datum");
|
---|
| 130 | _tagNameMap.put(TAG_DEST_LATITUDE_REF, "GPS Dest Latitude Ref");
|
---|
| 131 | _tagNameMap.put(TAG_DEST_LATITUDE, "GPS Dest Latitude");
|
---|
| 132 | _tagNameMap.put(TAG_DEST_LONGITUDE_REF, "GPS Dest Longitude Ref");
|
---|
| 133 | _tagNameMap.put(TAG_DEST_LONGITUDE, "GPS Dest Longitude");
|
---|
| 134 | _tagNameMap.put(TAG_DEST_BEARING_REF, "GPS Dest Bearing Ref");
|
---|
| 135 | _tagNameMap.put(TAG_DEST_BEARING, "GPS Dest Bearing");
|
---|
| 136 | _tagNameMap.put(TAG_DEST_DISTANCE_REF, "GPS Dest Distance Ref");
|
---|
| 137 | _tagNameMap.put(TAG_DEST_DISTANCE, "GPS Dest Distance");
|
---|
| 138 | _tagNameMap.put(TAG_PROCESSING_METHOD, "GPS Processing Method");
|
---|
| 139 | _tagNameMap.put(TAG_AREA_INFORMATION, "GPS Area Information");
|
---|
| 140 | _tagNameMap.put(TAG_DATE_STAMP, "GPS Date Stamp");
|
---|
| 141 | _tagNameMap.put(TAG_DIFFERENTIAL, "GPS Differential");
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public GpsDirectory()
|
---|
| 145 | {
|
---|
| 146 | this.setDescriptor(new GpsDescriptor(this));
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | @Override
|
---|
| 150 | @NotNull
|
---|
| 151 | public String getName()
|
---|
| 152 | {
|
---|
| 153 | return "GPS";
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | @Override
|
---|
| 157 | @NotNull
|
---|
| 158 | protected HashMap<Integer, String> getTagNameMap()
|
---|
| 159 | {
|
---|
| 160 | return _tagNameMap;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Parses various tags in an attempt to obtain a single object representing the latitude and longitude
|
---|
| 165 | * at which this image was captured.
|
---|
| 166 | *
|
---|
| 167 | * @return The geographical location of this image, if possible, otherwise null
|
---|
| 168 | */
|
---|
| 169 | @Nullable
|
---|
| 170 | public GeoLocation getGeoLocation()
|
---|
| 171 | {
|
---|
[10862] | 172 | Rational[] latitudes = getRationalArray(TAG_LATITUDE);
|
---|
| 173 | Rational[] longitudes = getRationalArray(TAG_LONGITUDE);
|
---|
| 174 | String latitudeRef = getString(TAG_LATITUDE_REF);
|
---|
| 175 | String longitudeRef = getString(TAG_LONGITUDE_REF);
|
---|
[8132] | 176 |
|
---|
| 177 | // Make sure we have the required values
|
---|
| 178 | if (latitudes == null || latitudes.length != 3)
|
---|
| 179 | return null;
|
---|
| 180 | if (longitudes == null || longitudes.length != 3)
|
---|
| 181 | return null;
|
---|
| 182 | if (latitudeRef == null || longitudeRef == null)
|
---|
| 183 | return null;
|
---|
| 184 |
|
---|
| 185 | Double lat = GeoLocation.degreesMinutesSecondsToDecimal(latitudes[0], latitudes[1], latitudes[2], latitudeRef.equalsIgnoreCase("S"));
|
---|
| 186 | Double lon = GeoLocation.degreesMinutesSecondsToDecimal(longitudes[0], longitudes[1], longitudes[2], longitudeRef.equalsIgnoreCase("W"));
|
---|
| 187 |
|
---|
| 188 | // This can return null, in cases where the conversion was not possible
|
---|
| 189 | if (lat == null || lon == null)
|
---|
| 190 | return null;
|
---|
| 191 |
|
---|
| 192 | return new GeoLocation(lat, lon);
|
---|
| 193 | }
|
---|
[10862] | 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Parses the date stamp tag and the time stamp tag to obtain a single Date object representing the
|
---|
| 197 | * date and time when this image was captured.
|
---|
| 198 | *
|
---|
| 199 | * @return A Date object representing when this image was captured, if possible, otherwise null
|
---|
| 200 | */
|
---|
| 201 | @Nullable
|
---|
| 202 | public Date getGpsDate()
|
---|
| 203 | {
|
---|
| 204 | String date = getString(TAG_DATE_STAMP);
|
---|
| 205 | Rational[] timeComponents = getRationalArray(TAG_TIME_STAMP);
|
---|
| 206 |
|
---|
| 207 | // Make sure we have the required values
|
---|
| 208 | if (date == null)
|
---|
| 209 | return null;
|
---|
| 210 | if (timeComponents == null || timeComponents.length != 3)
|
---|
| 211 | return null;
|
---|
| 212 |
|
---|
| 213 | String dateTime = String.format(Locale.US, "%s %02d:%02d:%02.3f UTC",
|
---|
| 214 | date, timeComponents[0].intValue(), timeComponents[1].intValue(), timeComponents[2].doubleValue());
|
---|
| 215 | try {
|
---|
| 216 | DateFormat parser = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss.S z");
|
---|
| 217 | return parser.parse(dateTime);
|
---|
| 218 | } catch (ParseException e) {
|
---|
| 219 | return null;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
[8132] | 222 | }
|
---|