public void ParseGpsDirectory_ValidDirectory_LatLongMetadata() { var gpsDirectory = new GpsDirectory(); var photo = new Photo(""); var gpsReference = "N"; var gpsRationalArr = new Rational[3] { new Rational(1, 1), new Rational(60, 1), new Rational(3600, 1) }; gpsDirectory.Set(GpsDirectory.TagLatitudeRef, gpsReference); gpsDirectory.Set(GpsDirectory.TagLatitude, gpsRationalArr); gpsDirectory.Set(GpsDirectory.TagLongitudeRef, gpsReference); gpsDirectory.Set(GpsDirectory.TagLongitude, gpsRationalArr); gpsDirectory.Parse(photo); var actualLatitude = photo.Latitude; var actualLongitude = photo.Longitude; var expectedLatitude = MetadataConverter.DegreesMinutesSecondsToDecimalDegrees(gpsRationalArr, gpsReference); var expectedLongitude = MetadataConverter.DegreesMinutesSecondsToDecimalDegrees(gpsRationalArr, gpsReference); Assert.Equal(expectedLatitude, actualLatitude); Assert.Equal(expectedLongitude, actualLongitude); }
/// <summary>Parses <see cref="GpsDirectory" /> metadata and saves it to the <see cref="Photo" />.</summary> /// <param name="directory">Directory containing the GPS metadata.</param> /// <param name="photo">Photo object used for storing metadata.</param> public static void Parse(this GpsDirectory directory, Photo photo) { if (directory is null || photo is null) { return; } var gpsLatRef = directory.GetString(GpsDirectory.TagLatitudeRef); var gpsLat = directory.GetRationalArray(GpsDirectory.TagLatitude); var gpsLonRef = directory.GetString(GpsDirectory.TagLongitudeRef); var gpsLon = directory.GetRationalArray(GpsDirectory.TagLongitude); var latitude = MetadataConverter.DegreesMinutesSecondsToDecimalDegrees(gpsLat, gpsLatRef); var longitude = MetadataConverter.DegreesMinutesSecondsToDecimalDegrees(gpsLon, gpsLonRef); photo.Latitude = latitude; photo.Longitude = longitude; // if we cannot get both the altitude and its reference, do not save anything if (directory.TryGetByte(GpsDirectory.TagAltitudeRef, out var gpsAltBit) && directory.TryGetInt16(GpsDirectory.TagAltitude, out var gpsAlt)) { photo.AltitudeReference = gpsAltBit == 0 ? "Sea level" : "Below sea level"; photo.Altitude = gpsAlt; } }
public void ParseGpsDirectory_NullDirectory_NoMetadata() { GpsDirectory gpsDirectory = null; var photo = new Photo(""); gpsDirectory.Parse(photo); Assert.Null(photo.Latitude); Assert.Null(photo.Longitude); Assert.Null(photo.AltitudeReference); Assert.Null(photo.Altitude); }
public void ParseGpsDirectory_ValidDirectory_AltMetadata() { var gpsDirectory = new GpsDirectory(); var photo = new Photo(""); var gpsAltBit = 0; short gpsAlt = 50; gpsDirectory.Set(GpsDirectory.TagAltitudeRef, gpsAltBit); gpsDirectory.Set(GpsDirectory.TagAltitude, gpsAlt); gpsDirectory.Parse(photo); var actualAltitudeRef = photo.AltitudeReference; var actualAltitude = photo.Altitude; var expectedAltitudeRef = "Sea level"; Assert.Equal(expectedAltitudeRef, actualAltitudeRef); Assert.Equal(gpsAlt, actualAltitude); }
private void SetGPSData(GpsDirectory gpsDirectory, ref Metadata myMetadata) { // GPS RAW Altitude: 96 double altitude; if (gpsDirectory.TryGetDouble(GpsDirectory.TagAltitude, out altitude)) { myMetadata.Altitude = altitude; uint index; if (gpsDirectory.TryGetUInt32(GpsDirectory.TagAltitudeRef, out index)) { myMetadata.Altitude = myMetadata.Altitude * (index == 1 ? -1 : 1); } } // GPS Location: 50,5323033300363, 30,4931270299872 myMetadata.Location = gpsDirectory.GetGeoLocation(); if (myMetadata.Location != null && myMetadata.Location.IsZero) { myMetadata.Location = null; } }
// Insert logic for processing found files here. public static void ProcessFile(string galleryName , string path , string webPath , string strThumbnailsFolder , List <MyObjectInJson> picsJson , List <MyObjectInJson> thumbsJson , bool resizeImages , Log log , string subdirectoryName , bool isAll = false) { if (!isAll) { if (!System.IO.Directory.Exists(strThumbnailsFolder)) { System.IO.Directory.CreateDirectory(strThumbnailsFolder); } } ImagesProcessing imagesProcessing = new ImagesProcessing(); if (resizeImages) { imagesProcessing.ResizeImage(path, Path.Combine(strThumbnailsFolder, Path.GetFileName(path)), 200, 200); } try { IReadOnlyList <MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(path); GpsDirectory gps = directories.OfType <GpsDirectory>().FirstOrDefault(); GeoLocation location = gps?.GetGeoLocation(); if (!(location is null)) { int indexOfGalleryNameInPath = path.IndexOf(galleryName); indexOfGalleryNameInPath = indexOfGalleryNameInPath + galleryName.Length; string relativeSysPathFileName = path.Substring(indexOfGalleryNameInPath, path.Length - indexOfGalleryNameInPath); relativeSysPathFileName = relativeSysPathFileName.Substring(0, relativeSysPathFileName.LastIndexOf(@"\") + 1); string picsPath = relativeSysPathFileName.Replace(@"\", "/"); string thumbsPath = string.Empty; if (picsPath.IndexOf("pics") > 0) { thumbsPath = picsPath.Substring(0, picsPath.IndexOf("pics")) + "thumbs/"; } string jsonPicsPath = $"..{picsPath}{Path.GetFileName(path)}"; string jsonThumbsPath = $"..{thumbsPath}{Path.GetFileName(path)}"; if (!webPath.EndsWith("/")) { webPath = webPath + '/'; } if (!string.IsNullOrWhiteSpace(subdirectoryName)) { if (!subdirectoryName.EndsWith("/")) { subdirectoryName = subdirectoryName + '/'; } webPath = webPath + subdirectoryName; } picsJson.Add(new MyObjectInJson { FileName = isAll ? path : jsonPicsPath, Latitude = location.Latitude, Longitude = location.Longitude }); thumbsJson.Add(new MyObjectInJson { FileName = jsonThumbsPath, Latitude = location.Latitude, Longitude = location.Longitude }); } else { log.WriteLog($"Location is null."); } log.WriteLog($"Processed file '{path}', count: {picsJson.Count}."); }
static void Main(string[] args) { MRArguments argument = UsageChecker.checkAndBuildArgument(args); var r = new ReverseGeoCode <ExtendedGeoName>(GeoFileReader.ReadExtendedGeoNames(@argument.cityDbFileInfo.FullName)); Parallel.ForEach(argument.targetDirs, (dir) => { if (dir.Name.Contains("@")) { Console.WriteLine("skipping {0}.already renamed. maybe.. it`s name contains @ character", dir.Name); return; } FileInfo[] files = dir.GetFiles(); if (files != null && files.Length > 0) { Dictionary <String, int> cCodeCounter = new Dictionary <String, int>(); Dictionary <String, int> cityCounter = new Dictionary <String, int>(); foreach (FileInfo file in files) { try { IEnumerable <MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(file.FullName); GpsDirectory gpsDirectory = directories.OfType <GpsDirectory>().First(); if (gpsDirectory != null && gpsDirectory.GetGeoLocation() != null) { double lat = gpsDirectory.GetGeoLocation().Latitude; double lon = gpsDirectory.GetGeoLocation().Longitude; ExtendedGeoName result = r.NearestNeighbourSearch(r.CreateFromLatLong(lat, lon), 1).First(); String city = result.Name; if (cityCounter.TryGetValue(city, out int tvalue)) { cityCounter[city] = tvalue + 1; } else { cityCounter.Add(city, 1); } String ccode = result.CountryCode; if (cCodeCounter.TryGetValue(ccode, out int cvalue)) { cCodeCounter[ccode] = cvalue + 1; } else { cCodeCounter.Add(ccode, 1); } } } catch (Exception) { } } if (cCodeCounter.Count > 0) { int total = 0; String countryName = ""; int maxCountryCount = 0; foreach (String key in cCodeCounter.Keys) { int tempCCount = cCodeCounter[key]; total += tempCCount; if (tempCCount > maxCountryCount) { countryName = key; } } String cityName = ""; List <Tuple <String, int> > cities = new List <Tuple <String, int> >(); foreach (String key in cityCounter.Keys) { cities.Add(new Tuple <string, int>(key, cityCounter[key])); } Tuple <String, int>[] citiesArray = cities.OrderBy(tup => tup.Item2).ToArray(); String destination = dir.Name + " @" + countryName; if (citiesArray != null && citiesArray.Length > 0) { destination += (", " + citiesArray[0].Item1); cityName = citiesArray[0].Item1; if (citiesArray.Length > 1) { Console.WriteLine("second candidate citi name : {0}, count:{1}", citiesArray[1].Item1, citiesArray[1].Item2); int c1val = citiesArray[0].Item2; int c2val = citiesArray[1].Item2; if (c2val / (float)c1val > 0.4) { destination += ("," + citiesArray[1].Item1 + "... "); } } } bool moved = false; try { System.IO.Directory.Move(dir.FullName, dir.Parent.FullName + Path.DirectorySeparatorChar + destination); moved = true; } catch (Exception) { } Console.WriteLine("{0} has {2}/{1} gps data and almost file located [{3}@{4}]. then renaming it to {5} : {6}", dir.Name, files.Length, total, cityName, countryName, destination, moved ? "success" : "failed"); } else { Console.WriteLine("{0}`s {1} file has no gps data", dir.Name, files.Length); } } }); }