-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoname.cs
93 lines (82 loc) · 3.12 KB
/
Geoname.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HFLabsGeonames
{
/// <summary>
/// шаблон табл геонеймса
/// </summary>
class Geoname
{
/// <summary>
/// чтение шаблона
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static List<string> ReadTemplate(string fileName)
{
char separator = ':';
List<string> templList = new List<string>();
using (StreamReader sr = File.OpenText(fileName))
{
string s = String.Empty;
while ((s = sr.ReadLine()) != null)
{
templList.Add(s.Split(separator).First().Trim());
}
}
return templList;
}
/// <summary>
/// загрузка данных с предварит выборкой по стране
/// </summary>
/// <param name="fileName"></param>
/// <param name="contry"></param>
/// <returns></returns>
public static List<string[]> ReadGeoData(string fileName, string country = "BY")
{
char separator = '\t';
List<string[]> geoList = new List<string[]>();
var start = DateTime.Now;
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
string s;
while ((s = sr.ReadLine()) != null)
{
var geoLine = s.Split(separator);
if (geoLine.Contains(country))
geoList.Add(geoLine);
}
}
var end = DateTime.Now;
GC.Collect();
return geoList;
}
/// <summary>
/// сопоставить данные и шаблон
/// </summary>
/// <param name="geoData"></param>
/// <param name="geoTemplate"></param>
/// <returns></returns>
internal static List<Dictionary<string, string>> Collate(List<string[]> geoData, List<string> geoTemplate)
{
if (geoData == null || geoTemplate == null || geoData.Count == 0 || geoTemplate.Count == 0)
throw new MissingFieldException("Отсутствуют данные или шаблон данных.");
if (geoData.First().Count() != geoTemplate.Count)
throw new MissingFieldException("Данные не соотв шаблону.");
List<Dictionary<string, string>> geoMapList = new List<Dictionary<string, string>>();
foreach (var geo in geoData)
{
var dic = geoTemplate.Zip(geo, (k, v) => new { key = k, value = v }).ToDictionary(x => x.key, x => x.value);
geoMapList.Add(dic);
}
return geoMapList;
}
}
}