Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //Polygon string
  6. string polygonRoute = @"POLYGON((-47.819807 58.544817,-47.818974 58.544876,-45.414636 58.69179,-45.41381 58.691833,-42.992289 58.793575,-42.991466 58.793601,-40.558929 58.849725,-40.558109 58.849736,-38.120913 58.859992,-38.120094 58.859987,-35.684683 58.82433,-35.683862 58.824311,-33.256645 58.742899,-33.25582 58.742864,-30.843063 58.616056,-30.842233 58.616005,-28.44995 58.444353,-28.449114 58.444285,-26.082975 58.228522,-26.080714 58.228256,-26.07847 58.227872,-26.07625 58.227371,-26.074059 58.226755,-26.071903 58.226025,-26.069788 58.225183,-26.06772 58.224231,-26.065705 58.223173,-26.063748 58.22201,-26.061854 58.220747,-26.060029 58.219386,-26.058278 58.217932,-26.056606 58.216388,-26.055016 58.214759,-26.053514 58.213048,-26.052104 58.211262,-26.050789 58.209404,-26.049573 58.20748,-26.048459 58.205494,-26.047451 58.203453,-26.046551 58.201363,-26.045762 58.199228,-26.045085 58.197054,-26.044523 58.194848,-26.044078 58.192616,-26.04375 58.190364,-26.04354 58.188097,-26.043449 58.185823,-26.043477 58.183547,-26.043624 58.181275,-26.04389 58.179015,-26.044274 58.176771,-26.044775 58.17455,-26.045391 58.172359,-26.046122 58.170203,-26.046964 58.168088,-26.047915 58.166021,-26.048974 58.164005,-26.050136 58.162048,-26.051399 58.160155,-26.05276 58.15833,-26.054214 58.156579,-26.055758 58.154906,-26.057388 58.153317,-26.059098 58.151815,-26.060885 58.150404,-26.062743 58.149089,-26.064667 58.147873,-26.066652 58.14676,-26.068693 58.145752,-26.070784 58.144852,-26.072919 58.144062,-26.075092 58.143386,-26.077298 58.142824,-26.07953 58.142378,-26.081783 58.14205,-26.084049 58.14184,-26.086324 58.141749,-26.0886 58.141778,-26.090871 58.141925,-28.456592 58.35765,-30.848042 58.529241,-33.259972 58.656005,-35.686366 58.737389,-38.120957 58.773034,-40.557333 58.762782,-42.989049 58.706678,-45.409746 58.60497,-47.813254 58.458107,-50.194121 58.266687,-50.196394 58.266564,-50.198671 58.26656,-50.200944 58.266676,-50.203208 58.26691,-50.205457 58.267262,-50.207684 58.267732,-50.209884 58.268317,-50.21205 58.269017,-50.214176 58.269829,-50.216257 58.270751,-50.218287 58.271781,-50.220261 58.272916,-50.222172 58.274153,-50.224015 58.275487,-50.225787 58.276917,-50.227481 58.278437,-50.229093 58.280044,-50.230619 58.281733,-50.232055 58.2835,-50.233396 58.285339,-50.234639 58.287246,-50.23578 58.289215,-50.236817 58.291242,-50.237746 58.293319,-50.238566 58.295443,-50.239273 58.297607,-50.239866 58.299804,-50.240343 58.30203,-50.240702 58.304278,-50.240944 58.306541,-50.241067 58.308814,-50.241071 58.31109,-50.240956 58.313364,-50.240722 58.315628,-50.240369 58.317877,-50.2399 58.320104,-50.239314 58.322304,-50.238615 58.32447,-50.237802 58.326596,-50.23688 58.328677,-50.23585 58.330707,-50.234715 58.33268,-50.233479 58.334591,-50.232144 58.336435,-50.230714 58.338207,-50.229194 58.339901,-50.227587 58.341513,-50.225898 58.343039,-50.224132 58.344475,-50.222293 58.345816,-50.220386 58.347059,-50.218416 58.3482,-50.21639 58.349237,-50.214312 58.350166,-50.212188 58.350985,-50.210025 58.351692,-50.207827 58.352285,-50.205601 58.352762,-50.203354 58.353122,-50.20109 58.353364,-47.819807 58.544817))";
  7.  
  8. //Get the raw LatLng string values
  9. System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"(?:POLYGON\(\()(?<a>.*?)\)\)");
  10. var latlongstring = regex.Matches(polygonRoute)[0].Groups["a"].Value;
  11.  
  12. //Separate each lat lng by splitting on a comma
  13. List<string> splits = latlongstring.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  14.  
  15. //Create strongly typed LatLng values
  16. var latlngSplints = splits.Select(p => new LatLng(p));
  17.  
  18. //Find similar values using an equality comparer with a tolerance so that we can identify the edges
  19. var testListGroup = latlngSplints.ToList().GroupBy(ele => ele.Lat, new DoubleEqualityComparer(0.05)).Select(group => new { metric = group.Key, Count = group.Count(), Values = group }).ToList();
  20.  
  21. //Get the two higest groups to get the start and finish
  22. var roundedges = testListGroup.OrderByDescending(p => p.Count).Take(2).ToList();
  23.  
  24. //Foreach round edge (2)
  25. foreach (var edge in roundedges)
  26. {
  27. //Take the 2nd and penultimate values and remove them from the original split values
  28. foreach (var pt in edge.Values.Skip(1).Take(edge.Values.Count() - 2))
  29. {
  30. splits.Remove(pt.ToString());
  31. }
  32. }
  33. //ReJoin the split values
  34. var latlngStringOutput = string.Join(",", splits);
  35.  
  36. var polygonRouteOutput = $@"POLYGON(({latlngStringOutput}))";
  37. }
  38. }
  39.  
  40. public class LatLng
  41. {
  42. public double Lat { get; set; }
  43. public double Lng { get; set; }
  44. public LatLng(string value)
  45. {
  46. var o = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  47. Lat = Convert.ToDouble(o[0]);
  48. Lng = Convert.ToDouble(o[1]);
  49. }
  50.  
  51. public override string ToString()
  52. {
  53. return Lat + " " + Lng;
  54. }
  55. }
  56.  
  57. public class DoubleEqualityComparer : IEqualityComparer<double>
  58. {
  59. private double tol = 0;
  60.  
  61. public DoubleEqualityComparer(double Tol)
  62. {
  63. tol = Tol;
  64. }
  65.  
  66. public bool Equals(double d1, double d2)
  67. {
  68. return EQ(d1, d2, tol);
  69. }
  70.  
  71. public int GetHashCode(double d)
  72. {
  73. return Math.Round(d).GetHashCode();
  74. }
  75. public bool EQ(double dbl, double compareDbl, double tolerance)
  76. {
  77. return Math.Abs(dbl - compareDbl) < tolerance;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement