File tree 5 files changed +41
-2
lines changed
5 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,28 @@ public void GeoLocation()
52
52
Assert . Equal ( - 1.9141666666666666 , geoLocation ! . Longitude ) ;
53
53
}
54
54
55
+ [ Fact ]
56
+ public void GeoLocationWithAltitude ( )
57
+ {
58
+ var metadata = ImageMetadataReader . ReadMetadata ( "Data/ABOVE_SEA_LEVEL.jpg" ) ;
59
+ var gpsDirectory = metadata . OfType < GpsDirectory > ( ) . First ( ) ;
60
+ var geoLocation = gpsDirectory . GetGeoLocation ( ) ;
61
+ Assert . NotNull ( geoLocation ) ;
62
+ Assert . True ( geoLocation . Altitude > 1 ) ;
63
+ }
64
+
65
+ [ Fact ]
66
+ public void GetLocationWithAltitudeBelowSeaLevel ( )
67
+ {
68
+ var metadata = ImageMetadataReader . ReadMetadata ( "Data/BELOW_SEA_LEVEL.JPG" ) ;
69
+ var gpsDirectory = metadata . OfType < GpsDirectory > ( ) . First ( ) ;
70
+ var geoLocation = gpsDirectory . GetGeoLocation ( ) ;
71
+
72
+ Assert . NotNull ( geoLocation ) ;
73
+
74
+ Assert . True ( geoLocation . Altitude < - 1 ) ;
75
+ }
76
+
55
77
[ Fact ]
56
78
public void GpsDate ( )
57
79
{
Original file line number Diff line number Diff line change @@ -163,8 +163,10 @@ public GpsDirectory() : base(_tagNameMap)
163
163
{
164
164
var latitudes = this . GetRationalArray ( TagLatitude ) ;
165
165
var longitudes = this . GetRationalArray ( TagLongitude ) ;
166
+ bool hasAltitude = this . TryGetRational ( TagAltitude , out var altitude ) ;
166
167
var latitudeRef = this . GetString ( TagLatitudeRef ) ;
167
168
var longitudeRef = this . GetString ( TagLongitudeRef ) ;
169
+ bool hasAltitudeRef = this . TryGetByte ( TagAltitudeRef , out var altitudeRef ) ;
168
170
169
171
// Make sure we have the required values
170
172
if ( latitudes is null || latitudes . Length != 3 )
@@ -183,7 +185,17 @@ public GpsDirectory() : base(_tagNameMap)
183
185
if ( lat == null || lon == null )
184
186
return null ;
185
187
186
- return new GeoLocation ( ( double ) lat , ( double ) lon ) ;
188
+ double ? alt = null ;
189
+
190
+ if ( hasAltitude )
191
+ {
192
+ alt = altitude . ToDouble ( ) ;
193
+
194
+ if ( hasAltitudeRef && altitudeRef == 0x01 ) // Invert value when ref i 1, indicates that the value is below sea level
195
+ alt = - 1.0 * alt ;
196
+ }
197
+
198
+ return new GeoLocation ( ( double ) lat , ( double ) lon , alt ) ;
187
199
}
188
200
189
201
/// <summary>
Original file line number Diff line number Diff line change @@ -19,10 +19,12 @@ public sealed class GeoLocation
19
19
/// </summary>
20
20
/// <param name="latitude">the latitude, in degrees</param>
21
21
/// <param name="longitude">the longitude, in degrees</param>
22
- public GeoLocation ( double latitude , double longitude )
22
+ /// <param name="altitude">the altitude, in meters</param>
23
+ public GeoLocation ( double latitude , double longitude , double ? altitude )
23
24
{
24
25
Latitude = latitude ;
25
26
Longitude = longitude ;
27
+ Altitude = altitude ;
26
28
}
27
29
28
30
/// <value>the latitudinal angle of this location, in degrees.</value>
@@ -31,6 +33,9 @@ public GeoLocation(double latitude, double longitude)
31
33
/// <value>the longitudinal angle of this location, in degrees.</value>
32
34
public double Longitude { get ; }
33
35
36
+ /// <value>the altitude of this location, in meters.</value>
37
+ public double ? Altitude { get ; }
38
+
34
39
/// <value>true, if both latitude and longitude are equal to zero</value>
35
40
public bool IsZero => Latitude == 0 && Longitude == 0 ;
36
41
You can’t perform that action at this time.
0 commit comments