DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Trending

  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  1. DZone
  2. Data Engineering
  3. Data
  4. ASP.NET Core: Read the GPS Coordinates of a Photo

ASP.NET Core: Read the GPS Coordinates of a Photo

In this article, we take a look at how developers can extract the GPS coordinates embedded in a photo using ASP.NET Core!

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Nov. 11, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

During one of my ASP.NET Core classes I made a demo about how to read GPS coordinates from photo and display location on a map. I took my favorite photo of a beer kiosk in Krakow and displayed the location of this kiosk on a map. This blog post describes my experiment on getting GPS coordinates from EXIF data in an ASP.NET Core application.

Wikipedia defines EXIF (Exchangeable image file format) as a standard that specifies the formats for images, sound, and ancillary tags used by digital cameras (including smartphones), scanners, and other systems handling image and sound files recorded by digital cameras. Most of modern devices used to take photos save meta information in EXIF format to photos. It’s great but be aware – not all people may use this information in good purposes.

Reading EXIF Data

There are some libraries available for ASP.NET Core for reading EXIF data from photos. For this demo I decided to use a library that deals only with EXIF data. My choice was a NuGet package called ExifLib.Standard. It’s simple to use, kind of primitive and basic, but it works well and does its job.

Here is the sample how to read some EXIF fields.

using (var reader = new ExifReader("my-image.jpg"))
{
    reader.GetTagValue(ExifTags.DigitalZoomRatio, out double brightness);
    reader.GetTagValue(ExifTags.DateTimeDigitized, out DateTime photoDate);
}

Best thing about ExifLib.Standard – it works reasonably fast with bigger photos (I tried a few that are ~7 MB). Some libraries get extremely slow with photos bigger than 3 MB but ExifLib.Standard seems to work well.

Reading GPS Coordinates

Getting GPS coordinates is a little tricky, as coordinates are given back as an array of doubles containing degrees, minutes, and seconds. This is not a shortcut done by the library developer but this is how devices save coordinates.

ExifLib.Standard: Getting GPS latitude

For me this information was enough to get done with what I was up to.

Maps used on web pages usually need coordinates as real numbers. Getting components of coordinates to a real number is easy. The first number is a degree, the second one is minutes, and the third is seconds. Latitude can be transformed to a real number using the following calculation:

latitudeReal = latitude[0] + latitude[1] / 60 + latitude[2] / 3600

Based on this, I developed some extension methods to make the reading of coordinates easier.

public static class ExifLibExtensions
{
    public static double? GetLatitude(this ExifReader reader)
    {
        return reader.GetCoordinate(ExifTags.GPSLatitude);
    }

    public static double? GetLongitude(this ExifReader reader)
    {
        return reader.GetCoordinate(ExifTags.GPSLongitude);
    }

    private static double? GetCoordinate(this ExifReader reader, ExifTags type)
    {
        if (reader.GetTagValue(type, out double[] coordinates))
        {
            return ToDoubleCoordinates(coordinates);
        }

        return null;
    }

    private static double ToDoubleCoordinates(double[] coordinates)
    {
        return coordinates[0] + coordinates[1] / 60f + coordinates[2] / 3600f;
    }
}

These methods are actually simple but it’s a good task for students to work these out.

Creating a Model for Photo Coordinates

To get data to the browser we need a model. This model must carry coordinates – if available – and also error information if something went wrong when reading EXIF data. Here is the model I created.

public class PhotoCoordinatesModel
{
    public double? Lat { get; set; }
    public double? Lon { get; set; }
    public string Error { get; set; }

    public bool HasValidCoordinates()
    {
        return Lat.HasValue && Lon.HasValue;
    }
}

And here is my demo controller action that reads EXIF data.

public IActionResult Index()
{           
    var model = new PhotoCoordinatesModel();
    try
    {
        using (var reader = new ExifReader("my-photo.jpg"))
        {
            model.Lat = reader.GetLatitude();
            model.Lon = reader.GetLongitude();
        }
    }
    catch(ExifLibException exifex)
    {
        model.Error = exifex.Message;
    }

    return View(model);
}

Thanks to extension methods I created before, the code in the controller action is clean and minimal.

And here’s the end result. My favorite beer barrel in Krakow located on the map. Excellent!

My favorite beer barrel in Krakow shown on map

Wrapping Up

There are not many graphic libraries available for ASP.NET Core but if we only need EXIF data then we have some options. After trying some libraries I decided to go with ExifLib.Standard as it was minimalistic and performed well. Reading EXIF data is easy using this library. After writing some extension methods to get the latitude and longitude of the photo we got pretty clean controller actions to display tbe photo location on a map.

ASP.NET ASP.NET Core Exif Data (computing)

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook