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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Teradata Performance and Skew Prevention Tips
  • Java 23 Features: A Deep Dive Into the Newest Enhancements
  • A Guide to Container Runtimes
  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.1K 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, DZone MVB. 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!