Advertisement
Guest User

Untitled

a guest
Apr 8th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. using Npgsql;
  7. using NpgsqlTypes;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Data.SqlTypes;
  11. using System.Threading;
  12. using System.Xml.Linq;
  13. using System.Net;
  14.  
  15. namespace P3Parser
  16. {
  17. class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. doStuffs();
  22. }
  23.  
  24. public static void doStuffs()
  25. {
  26. parseMisdaden();
  27. }
  28.  
  29. public static void parseMisdaden()
  30. {
  31. timsParser tp1 = new timsParser();
  32. int[] i1 = { 0,5,8,10,9,11,14,19,22};
  33. List<List<string>> misdaden1 = timsParser.parseCSV(@"D:\AlleCsv\misdaden\Straatroof-2011.csv", i1, ';');
  34. string[] misdaadnamen = { "casenummer", "dag", "begindatum", "begintijd", "einddatum", "eindtijd", "plaats", "straat", "postcode"};
  35. List<int> removeIndex = new List<int>();
  36. //Throw out the empty strings.
  37. for (int i = 0; i < misdaden1.Count; i++)
  38. {
  39. for (int j = 0; j < misdaden1[0].Count; j++)
  40. {
  41. Console.ForegroundColor = ConsoleColor.White;
  42. Console.Write('"' + misdaden1[i][j] + '"' + " ");
  43. if (misdaden1[i][j].Replace(" ", "") == "")
  44. {
  45. Console.ForegroundColor = ConsoleColor.Red;
  46. Console.Write("REMOVED");
  47. removeIndex.Add(i);
  48. }
  49. }
  50. Console.WriteLine();
  51. }
  52. for (int i = 0; i < removeIndex.Count; i++)
  53. {
  54. misdaden1.RemoveAt(removeIndex[i]-i);
  55. }
  56. Console.WriteLine();
  57. for (int i = 0; i < misdaden1.Count; i++)
  58. {
  59. for (int j = 0; j < misdaden1[0].Count; j++)
  60. {
  61. Console.ForegroundColor = ConsoleColor.White;
  62. Console.Write('"' + misdaden1[i][j] + '"' + " ");
  63. if (misdaden1[i][j].Replace(" ", "") == "")
  64. {
  65. Console.ForegroundColor = ConsoleColor.Red;
  66. Console.WriteLine("REMOVED");
  67. Console.ForegroundColor = ConsoleColor.White;
  68. removeIndex.Add(i);
  69. }
  70. }
  71. Console.WriteLine(" " + i.ToString());
  72. }
  73. /*
  74. List<List<string>> misdaden2 = new List<List<string>>();
  75. for (int i = 0; i < misdaden1.Count; i++)
  76. {
  77. latlng p;
  78. List<string> l = new List<string>();
  79. p = timsParser.addressToLatLng(misdaden1[i][8]);
  80. if (p.x != 0 && p.y != 0)
  81. {
  82. l.AddRange(misdaden1[i]);
  83. l.Add(p.x.ToString());
  84. l.Add(p.y.ToString());
  85. misdaden2.Add(l);
  86. }
  87. else
  88. {
  89. p = timsParser.addressToLatLng(misdaden1[i][6] + " " + misdaden1[i][7]);
  90. if (p.x != 0 && p.y != 0)
  91. {
  92. l.AddRange(misdaden1[i]);
  93. l.Add(p.x.ToString());
  94. l.Add(p.y.ToString());
  95. misdaden2.Add(l);
  96. }
  97. else
  98. {
  99. Console.WriteLine();
  100. }
  101. }
  102. } */
  103. for (int i = 0; i < misdaden1.Count; i++)
  104. {
  105. for (int j = 0; j < misdaden1[0].Count; j++)
  106. {
  107. if (misdaden1[i][j].Contains("'"))
  108. {
  109. misdaden1[i][j] = misdaden1[i][j].Replace("'", "");
  110. }
  111. Console.Write(misdaden1[i][j] + ", ");
  112. }
  113. Console.WriteLine();
  114. }
  115. SQLconnector.InsertIntoTable("misdaden", misdaadnamen.ToList(), misdaden1);
  116. Console.Read();
  117. }
  118. }
  119.  
  120. class timsParser
  121. {
  122. public timsParser()
  123. {
  124.  
  125. }
  126.  
  127. public static List<List<string>> parseCSV(string filelocation, int[] rows, char seperator)
  128. {
  129. List<List<string>> endresult = new List<List<string>>();
  130. using (StreamReader R = new StreamReader(filelocation))
  131. {
  132. int i = 0;
  133. string line;
  134. while (true)
  135. {
  136. i++;
  137. line = R.ReadLine();
  138. try
  139. {
  140. Thread.Sleep(1);
  141. List<string> s = line.Split(seperator).ToList();
  142. List<string> s2 = new List<string>();
  143. if (line.Replace(" ", "") != "")
  144. {
  145. for (int j = 0; j < s.Count; j++)
  146. {
  147. if (rows.Contains(j))
  148. {
  149. s2.Add(s[j]);
  150. }
  151. }
  152. if (s2.Count == rows.Length)
  153. {
  154. endresult.Add(s2);
  155. }
  156. }
  157. }
  158. catch
  159. {
  160. return endresult;
  161. }
  162. }
  163. }
  164. }
  165.  
  166. public static latlng addressToLatLng(string address)
  167. {
  168. int attempt = 0;
  169. Console.ForegroundColor = ConsoleColor.White;
  170. Console.WriteLine("INPUT: " + address);
  171. string requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(address));
  172. tryagain:
  173. Thread.Sleep(2000);
  174. try
  175. {
  176. WebRequest request = WebRequest.Create(requestUri);
  177. WebResponse response = request.GetResponse();
  178. XDocument xdoc = XDocument.Load(response.GetResponseStream());
  179. XElement result = xdoc.Element("GeocodeResponse").Element("result");
  180. XElement locationElement = result.Element("geometry").Element("location");
  181. XElement lat = locationElement.Element("lat");
  182. XElement lng = locationElement.Element("lng");
  183.  
  184. Console.ForegroundColor = ConsoleColor.Green;
  185. Console.WriteLine("SUCCES!");
  186.  
  187. Console.WriteLine("LAT: " + lat.Value.ToString());
  188. Console.WriteLine("LNG: " + lng.Value.ToString());
  189. Console.ForegroundColor = ConsoleColor.White;
  190. return (new latlng(double.Parse(lat.Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat), double.Parse(lng.Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat)));
  191. }
  192. catch (Exception e)
  193. {
  194. if (attempt < 4)
  195. {
  196. attempt++;
  197. goto tryagain;
  198. }
  199. else
  200. {
  201. Console.ForegroundColor = ConsoleColor.Red;
  202. Console.WriteLine("Didn't manage to get the latitude and longitude");
  203. Console.WriteLine(e);
  204. Console.ForegroundColor = ConsoleColor.White;
  205. return new latlng(0, 0);
  206. }
  207. }
  208. }
  209.  
  210. }
  211.  
  212. public class SQLconnector
  213. {
  214. public static NpgsqlConnection Con = new NpgsqlConnection("Host=localhost;Username=postgres;Password=Test;Database=Project3Solo");
  215.  
  216. public static void InsertIntoTable(string tablename, List<string> Names, List<List<string>> Values)
  217. {
  218. NpgsqlCommand COM = new NpgsqlCommand();
  219. Con.Open();
  220. COM.Connection = Con;
  221. for (int i = 0; i < Values.Count; i++)
  222. {
  223. COM.CommandText = "INSERT INTO " + tablename + "(";
  224. for (int j = 0; j < Names.Count; j++)
  225. {
  226. COM.CommandText = COM.CommandText + Names[j];
  227. if (j != Names.Count-1)
  228. {
  229. COM.CommandText = COM.CommandText + ",";
  230. }
  231. }
  232. COM.CommandText = COM.CommandText + ") VALUES (";
  233. for (int j = 0; j < Values[i].Count; j++)
  234. {
  235. COM.CommandText = COM.CommandText + "'" + Values[i][j] + "'";
  236. if (j != Values[0].Count-1)
  237. {
  238. COM.CommandText = COM.CommandText + ",";
  239. }
  240. }
  241. COM.CommandText = COM.CommandText + ");";
  242. Thread.Sleep(10);
  243. Console.WriteLine(COM.CommandText);
  244. try
  245. {
  246. COM.ExecuteNonQuery();
  247. }
  248. catch (Exception e)
  249. {
  250. Console.WriteLine(e.ToString());
  251. }
  252. }
  253. Con.Close();
  254. }
  255.  
  256. }
  257.  
  258. public class latlng
  259. {
  260. public double x;
  261. public double y;
  262. public latlng(double x, double y)
  263. {
  264. this.x = x;
  265. this.y = y;
  266. }
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement