Example #1
0
        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;
            }
        }
Example #2
0
        // 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}.");
            }
Example #3
0
        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);
                    }
                }
            });
        }