Advertisement
Guest User

Country Coordinate Finder Update

a guest
Nov 1st, 2012
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.IO;
  7. using System.Xml.Linq;
  8.  
  9. namespace CountryTest
  10. {
  11. public class Kml
  12. {
  13. public List<Country> Countries { get; private set; }
  14.  
  15. public Kml(String kml)
  16. {
  17. this.Countries = new List<Country>();
  18.  
  19. XElement xElement = XElement.Parse(kml);
  20.  
  21. var placemarks = xElement.Elements().First().Elements(XName.Get("{http://earth.google.com/kml/2.2}Placemark"));
  22.  
  23. foreach (var p in placemarks)
  24. {
  25. var extendedData = p.Element(XName.Get("{http://earth.google.com/kml/2.2}ExtendedData")).Elements();
  26.  
  27. string name = "";
  28.  
  29. foreach (var dataElement in extendedData)
  30. {
  31. var a = dataElement.Attribute(XName.Get("name")).Value;
  32. if (a == "NAME")
  33. {
  34. name = dataElement.Element(XName.Get("{http://earth.google.com/kml/2.2}value")).Value.Replace("<![CDATA[", "").Replace("]]>", "");
  35. }
  36. }
  37.  
  38. var multiGeometry = p.Element(XName.Get("{http://earth.google.com/kml/2.2}MultiGeometry")).Elements().ToList();
  39.  
  40. List<Polygon> outerPolygons = new List<Polygon>();
  41. List<Polygon> innerPolygons = new List<Polygon>();
  42.  
  43. var n = multiGeometry.First().Name;
  44.  
  45. if (multiGeometry.First().Name == XName.Get("{http://earth.google.com/kml/2.2}Polygon"))
  46. {
  47. var polygonString = multiGeometry.Elements().First().Elements().First().Elements().First().Value.Replace("\n","");
  48. outerPolygons.Add(new Polygon(polygonString));
  49. }
  50. else
  51. {
  52. var polygons = multiGeometry.Elements();
  53.  
  54. foreach (var polygon in polygons)
  55. {
  56. var type = polygon.Elements().First().Name;
  57. if (type == XName.Get("{http://earth.google.com/kml/2.2}outerBoundaryIs"))
  58. {
  59. var polygonString = polygon.Elements().First().Value.Replace("\n", "");
  60. outerPolygons.Add(new Polygon(polygonString));
  61. }
  62. else if (type == XName.Get("{http://earth.google.com/kml/2.2}innerBoundaryIs"))
  63. {
  64. var polygonString = polygon.Elements().First().Value.Replace("\n", "");
  65. innerPolygons.Add(new Polygon(polygonString));
  66. }
  67. }
  68. }
  69.  
  70. Countries.Add(new Country(name, outerPolygons, innerPolygons));
  71. }
  72. }
  73.  
  74. public String GetCountryByCoordinates(double x, double y)
  75. {
  76. foreach (Country c in Countries)
  77. if (c.ContainsPoint(x, y))
  78. return c.Name;
  79.  
  80. return "";
  81. }
  82. }
  83.  
  84. public class Country
  85. {
  86. public String Name { get; private set; }
  87.  
  88. List<Polygon> _outerPolygons;
  89. List<Polygon> _innerPolygons;
  90.  
  91. public Country(String name, List<Polygon> outerPolygons, List<Polygon> innerPolygons)
  92. {
  93. this.Name = name;
  94. _outerPolygons = outerPolygons;
  95. _innerPolygons = innerPolygons;
  96. }
  97.  
  98. public bool ContainsPoint(double x, double y)
  99. {
  100. foreach (Polygon inner in _innerPolygons)
  101. if (inner.ContainsPoint(x, y))
  102. return false;
  103.  
  104. foreach (Polygon outer in _outerPolygons)
  105. if (outer.ContainsPoint(x, y))
  106. return true;
  107.  
  108. return false;
  109. }
  110. }
  111.  
  112. public class Polygon
  113. {
  114. List<Tuple<double, double>> _coordinates;
  115. public Polygon(String kmlPolygonString)
  116. {
  117. _coordinates = new List<Tuple<double, double>>();
  118.  
  119. string[] points = kmlPolygonString.Split(' ');
  120.  
  121. foreach (string s in points)
  122. {
  123. string[] parts = s.Split(',');
  124. double x = double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture);
  125. double y = double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture);
  126.  
  127. _coordinates.Add(new Tuple<double, double>(x, y));
  128. }
  129. }
  130.  
  131. //modified from source: http://stackoverflow.com/a/2922778/1020861
  132. public bool ContainsPoint(double x, double y)
  133. {
  134. int i, j;
  135. bool c = false;
  136. for (i = 0, j = _coordinates.Count - 1; i < _coordinates.Count; j = i++)
  137. {
  138. if (((_coordinates[i].Item2 > y) != (_coordinates[j].Item2 > y)) &&
  139. (x < (_coordinates[j].Item1 - _coordinates[i].Item1) * (y - _coordinates[i].Item2) / (_coordinates[j].Item2 - _coordinates[i].Item2) + _coordinates[i].Item1))
  140. c = !c;
  141. }
  142. return c;
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement