Repository navigation
Expand file tree
/
Copy pathFunctions.cs
More file actions
205 lines (169 loc) · 6.99 KB
/
Copy pathFunctions.cs
File metadata and controls
205 lines (169 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Microsoft.AspNetCore.WebUtilities;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.ComponentModel;
namespace Geocoding_App
{
class Functions
{
public static bool StartProcess(string[] lista, BackgroundWorker worker)
{
int amountSucceded = 0;
int amountFailed = 0;
foreach (var i in lista)
{
if (!worker.CancellationPending)
{
Image image = Image.FromFile(i);
var splited = i.Split(Path.DirectorySeparatorChar);
var nameImage = splited[splited.Length - 1];
int type;
try
{
double latitude = GetCoordinateDouble(image.GetPropertyItem(2), image.GetPropertyItem(1));
double longitude = GetCoordinateDouble(image.GetPropertyItem(4), image.GetPropertyItem(3));
image.Dispose();
var resultAddress = GetAddressFromPointAsync(latitude.ToString(), longitude.ToString());
if (resultAddress.error != null)
throw new Exception();
MoveImageToDestination(resultAddress, i, nameImage);
type = 15;
amountSucceded++;
}
catch (Exception)
{
type = 14;
amountFailed++;
}
worker.ReportProgress(1, Config.languagesPhrases[type, Config.languageSelected] + "|" + nameImage + "\n");
}
else
{
break;
}
}
worker.ReportProgress(0, "\n" + Config.languagesPhrases[16, Config.languageSelected] + amountFailed + "\n");
worker.ReportProgress(0, Config.languagesPhrases[17, Config.languageSelected] + amountSucceded + "\n");
if (!worker.CancellationPending)
{
return false;
}
return true;
}
private static void MoveImageToDestination(RootObject rootObject, string pathOfImage, string nameImage)
{
if(Directory.Exists(Config.pathFrom) && Directory.Exists(Config.pathTo))
{
var newPath = Config.pathTo;
if (Config.country)
{
newPath += Path.DirectorySeparatorChar + rootObject.address.country;
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
}
if (Config.state)
{
newPath += Path.DirectorySeparatorChar + rootObject.address.state;
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
}
if (Config.city && rootObject.address.city != null)
{
newPath += Path.DirectorySeparatorChar + rootObject.address.city;
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
}
if (Config.city && rootObject.address.village != null)
{
newPath += Path.DirectorySeparatorChar + rootObject.address.village;
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
}
newPath += Path.DirectorySeparatorChar + nameImage;
File.Move(pathOfImage, newPath);
}
}
public static string[] LoadImages()
{
var extensions = new string[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp", "svg" };
var listFiles = GetFilesFrom(Config.pathFrom, extensions);
return listFiles;
}
private static RootObject GetAddressFromPointAsync(string lat, string lon)
{
HttpClient h = new HttpClient();
h.DefaultRequestHeaders.Add("User-Agent", Config.user_agent);
var query = new Dictionary<string, string>()
{
["format"] = Config.format,
["lat"] = lat.Replace(',','.'),
["lon"] = lon.Replace(',', '.'),
["zoom"] = Config.zoom,
["accept-language"] = GetLanguageByName()
};
var uri = QueryHelpers.AddQueryString(Config.url, query);
Task<string> request;
request = h.GetStringAsync(uri);
request.Wait();
var result = JsonConvert.DeserializeObject<RootObject>(request.Result);
return result;
}
private static string[] GetFilesFrom(string searchFolder, string[] filters)
{
List<string> filesFound = new List<string>();
foreach (var filter in filters)
{
filesFound.AddRange(Directory.GetFiles(searchFolder, string.Format("*.{0}", filter)));
}
return filesFound.ToArray();
}
private static string GetLanguageByName()
{
switch(Config.languageSelected)
{
case 0:
return "en-US";
case 1:
return "es-ES";
case 2:
return "da";
default:
return "en-US";
}
}
private static double GetCoordinateDouble(PropertyItem propItem, PropertyItem propItemRef)
{
uint degreesNumerator = BitConverter.ToUInt32(propItem.Value, 0);
uint degreesDenominator = BitConverter.ToUInt32(propItem.Value, 4);
double degrees = degreesNumerator / (double)degreesDenominator;
uint minutesNumerator = BitConverter.ToUInt32(propItem.Value, 8);
uint minutesDenominator = BitConverter.ToUInt32(propItem.Value, 12);
double minutes = minutesNumerator / (double)minutesDenominator;
uint secondsNumerator = BitConverter.ToUInt32(propItem.Value, 16);
uint secondsDenominator = BitConverter.ToUInt32(propItem.Value, 20);
double seconds = secondsNumerator / (double)secondsDenominator;
double coord = degrees + (minutes / 60d) + (seconds / 3600d);
string gpsRef = System.Text.Encoding.ASCII.GetString(new byte[1] { propItemRef.Value[0] });
if (gpsRef == "S" || gpsRef == "W")
{
coord *= -1;
}
return coord;
}
}
}