GPS coordinate from jpeg file metadata

22 views (last 30 days)
I'm working on a geo location progect and I need to plot some gps coordinate obtained from Jpeg pictures taken with a smartphone. Using info = imfinfo(filename) I get the information from the jpeg file but they are only ''visual'' information, so i would like to know if there's function to utilize the info i obtain from imfinfo to write a gps variable. To be clear:
imglatitude=0
info=imfinfo('file.jpg')
info =
scalar structure containing the fields:
Filename = C:\Users\...\file.jpg
FileModDate = 28-Apr-2015 17:20:16
FileSize = 3129532
Format = JPEG
GPSInfo =
scalar structure containing the fields:
GPSLatitudeRef = N
GPSLongitudeRef = E
GPSAltitude = 44
GPSLatitude =
xx.xxx
xx.xxx
xx.xxx
GPSLongitude =
xx.xxx
xx.xxx
xx.xxx
imglatitude=*GPSLatitude obtanined from imfinfo*
I've got no clue about how to get this gps info out from the imfinfo function! Thanks in advance for your help

Accepted Answer

Amy Haskins
Amy Haskins on 18 May 2015
The data returned from imfinfo is a structure and the fields can be accessed individually. If you have access to the Mapping Toolbox then with a little bit of effort you can use the wmmarker function display your image on a map.
Example: Use a sample image from the Image Processing Toolbox which contains geographic data.
handsInfo = imfinfo('hands1.jpg');
Display the GPS information
handsInfo.GPSInfo
ans =
GPSLatitudeRef: 'N'
GPSLatitude: [42 17 57.0600]
GPSLongitudeRef: 'W'
GPSLongitude: [71 21 5.8200]
GPSAltitudeRef: 0
GPSAltitude: 69.7069
GPSTimeStamp: [16 48 35]
GPSImgDirectionRef: 'T'
GPSImgDirection: 291.9459
GPSDateStamp: '2014:03:11'
Note that the latitude and longitude are given in Degrees, Minutes, and Seconds (DMS). We will need to convert them to decimal degrees.
lat = dms2degrees(handsInfo.GPSInfo.GPSLatitude);
lon = dms2degrees(handsInfo.GPSInfo.GPSLongitude);
The GPSLatitudeRef and GPSLongitudeRef fields determine if the lat and lon should be positive or negative.
if strcmp(handsInfo.GPSInfo.GPSLatitudeRef,'S')
lat = -1 * lat;
end
if strcmp(handsInfo.GPSInfo.GPSLongitudeRef,'W')
lon = -1 * lon;
end
Display the photo on a webmap to put it in context. This particualar image is small, but for a larger image you might want to use imresize to create a smaller thumbnail.
wmmarker(lat, lon, 'OverlayName', 'Hands1', 'Description', ...
'Photo taken at The Mathworks, Inc', 'Icon', 'hands1.jpg')
  4 Comments
Jesus Garcia
Jesus Garcia on 30 Oct 2017
Just put [] to group the 3 vectors and it will work:
lat = dms2degrees([handsInfo.GPSInfo.GPSLatitude]);
lon = dms2degrees([handsInfo.GPSInfo.GPSLongitude])
Adrian
Adam Danz
Adam Danz on 23 Mar 2024
Thanks for this great demo @Amy Haskins!
I found that some of my image files contained GPS data that had a value of 60 in seconds or minutes which causes an error in dms2degrees where minutes and seconds must be <60.
To get around this, I wrapped dms2degrees in a helper function that corrected the coordinates containing 60. I'll share it here in case someone else (or my future self) needs it.
lat = dms2degreesCorrection(handsInfo.GPSInfo.GPSLatitude);
lon = dms2degreesCorrection(handsInfo.GPSInfo.GPSLongitude);
function dg = dms2degreesCorrection(dms)
% Some image files can have values of 60 for seconds and minutes but this
% errors in MATLAB's dms2degrees. It's corrected here. dms is a 1x3
% vector. dg is a scalar.
if dms(3) >= 60
dms(2) = dms(2) + floor(dms(3)/60);
dms(3) = mod(dms(3),60);
end
if dms(2) >= 60
dms(1) = dms(1) + floor(dms(2)/60);
dms(2) = mod(dms(2),60);
end
dg = dms2degrees(dms);
end

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!