Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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!

· Web Dev Zone ·
Free Resource

Learn how to add document editing and viewing to your web app on .Net (C#), Node.JS, Java, PHP, Ruby, etc.

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.

Extend your web service functionality with docx, xlsx and pptx editing. Check out ONLYOFFICE document editors for integration.

Topics:
asp.net core ,web dev ,exifinterface ,tutorial ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}