public void AddGpsDataToImage(Guid imageId, ImageGpsData gpsData) { var image = this.images.GetById(imageId); if (image != null && gpsData?.Latitude != null && gpsData.Longitude.HasValue) { image.ImageGpsData = gpsData; this.Update(image); var lowFile = this.appEnvironment.WebRootPath + Constants.MainContentFolder + "/" + image.AlbumId + "/" + Constants.ImageFolderLow + "/" + image.FileName; var middleFile = this.appEnvironment.WebRootPath + Constants.MainContentFolder + "/" + image.AlbumId + "/" + Constants.ImageFolderMiddle + "/" + image.FileName; var highFile = this.appEnvironment.WebRootPath + Constants.MainContentFolder + "/" + image.AlbumId + "/" + Constants.ImageFolderOriginal + "/" + image.FileName; using (var imageMagick = new MagickImage(lowFile)) { var exif = imageMagick.GetExifProfile() ?? new ExifProfile(); exif.Parts = ExifParts.All; exif.SetValue(ExifTag.GPSLatitude, ExifDoubleToGps(gpsData.Latitude.Value)); exif.SetValue(ExifTag.GPSLongitude, ExifDoubleToGps(gpsData.Longitude.Value)); imageMagick.AddProfile(exif, true); imageMagick.Write(lowFile); } using (var imageMagick = new MagickImage(middleFile)) { var exif = imageMagick.GetExifProfile() ?? new ExifProfile(); exif.Parts = ExifParts.All; exif.SetValue(ExifTag.GPSLatitude, ExifDoubleToGps(gpsData.Latitude.Value)); exif.SetValue(ExifTag.GPSLongitude, ExifDoubleToGps(gpsData.Longitude.Value)); imageMagick.AddProfile(exif, true); imageMagick.Write(middleFile); } using (var imageMagick = new MagickImage(highFile)) { var exif = imageMagick.GetExifProfile() ?? new ExifProfile(); exif.Parts = ExifParts.All; exif.SetValue(ExifTag.GPSLatitude, ExifDoubleToGps(gpsData.Latitude.Value)); exif.SetValue(ExifTag.GPSLongitude, ExifDoubleToGps(gpsData.Longitude.Value)); imageMagick.AddProfile(exif, true); imageMagick.Write(highFile); } } this.memoryCache.Remove(CacheKeys.AlbumsServiceCacheKey); this.memoryCache.Remove(CacheKeys.ImageServiceCacheKey); this.memoryCache.Remove(CacheKeys.FileServiceCacheKey); }
public async Task <ImageGpsData> GetGpsDataOriginal(double latitude, double longitude) { var result = new ImageGpsData { Id = Guid.NewGuid(), CreatedOn = DateTime.Now }; var dbElement = this.gpsDbData.All() .FirstOrDefault(x => x.Latitude == latitude && x.Longitude == longitude); if (dbElement != null) { return(dbElement); } var httpClient = new HttpClient(); var result1 = await httpClient.GetStringAsync( "https://maps.googleapis.com/maps/api/geocode/xml?latlng=" + longitude.ToString(CultureInfo.InvariantCulture) + "," + latitude.ToString(CultureInfo.InvariantCulture) + "&key=AIzaSyAJOGz_xyAi_2CdRPW4HX-g5E1WcTwQMSY"); var xmlElm = XElement.Parse(result1); var status = (from elm in xmlElm.Descendants() where elm.Name == "status" select elm).FirstOrDefault(); if (status.Value.ToLower() == "ok") { var results = from elm in xmlElm.Elements() where elm.Name == "result" && (elm.Elements().First().Value == "locality" || elm.Elements().First().Value == "political") select elm; var res = (from elm in results.Descendants() where elm.Name == "formatted_address" select elm) .FirstOrDefault(); if (res != null) { result.Latitude = latitude; result.Longitude = longitude; result.LocationName = res.Value; return(result); } else { return(null); } } else { return(null); } }
public async Task <ImageGpsData> GetGpsData(string location) { var gpsData = this.gpsDbData.All().FirstOrDefault(x => x.LocationName == location); if (gpsData != null) { return(gpsData); } var result = new ImageGpsData { Id = Guid.NewGuid(), CreatedOn = DateTime.Now }; if (!string.IsNullOrEmpty(location)) { var httpClient = new HttpClient(); var result1 = await httpClient.GetStringAsync( "https://maps.googleapis.com/maps/api/geocode/xml?address=" + location + "&key=AIzaSyAJOGz_xyAi_2CdRPW4HX-g5E1WcTwQMSY"); var xmlElm = XElement.Parse(result1); var status = (from elm in xmlElm.Descendants() where elm.Name == "status" select elm).FirstOrDefault(); if (status.Value.ToLower() == "ok") { var lat = (from elm in xmlElm.Descendants() where elm.Name == "lat" select elm).FirstOrDefault(); var lng = (from elm in xmlElm.Descendants() where elm.Name == "lng" select elm).FirstOrDefault(); if (lat != null && lng != null) { result.Latitude = double.Parse(lat.Value, CultureInfo.InvariantCulture); result.Longitude = double.Parse(lng.Value, CultureInfo.InvariantCulture); result.LocationName = location; return(result); } else { return(null); } } else { return(null); } } return(null); }