Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- //Polygon string
- 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))";
- //Get the raw LatLng string values
- System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"(?:POLYGON\(\()(?<a>.*?)\)\)");
- var latlongstring = regex.Matches(polygonRoute)[0].Groups["a"].Value;
- //Separate each lat lng by splitting on a comma
- List<string> splits = latlongstring.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
- //Create strongly typed LatLng values
- var latlngSplints = splits.Select(p => new LatLng(p));
- //Find similar values using an equality comparer with a tolerance so that we can identify the edges
- var testListGroup = latlngSplints.ToList().GroupBy(ele => ele.Lat, new DoubleEqualityComparer(0.05)).Select(group => new { metric = group.Key, Count = group.Count(), Values = group }).ToList();
- //Get the two higest groups to get the start and finish
- var roundedges = testListGroup.OrderByDescending(p => p.Count).Take(2).ToList();
- //Foreach round edge (2)
- foreach (var edge in roundedges)
- {
- //Take the 2nd and penultimate values and remove them from the original split values
- foreach (var pt in edge.Values.Skip(1).Take(edge.Values.Count() - 2))
- {
- splits.Remove(pt.ToString());
- }
- }
- //ReJoin the split values
- var latlngStringOutput = string.Join(",", splits);
- var polygonRouteOutput = $@"POLYGON(({latlngStringOutput}))";
- }
- }
- public class LatLng
- {
- public double Lat { get; set; }
- public double Lng { get; set; }
- public LatLng(string value)
- {
- var o = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- Lat = Convert.ToDouble(o[0]);
- Lng = Convert.ToDouble(o[1]);
- }
- public override string ToString()
- {
- return Lat + " " + Lng;
- }
- }
- public class DoubleEqualityComparer : IEqualityComparer<double>
- {
- private double tol = 0;
- public DoubleEqualityComparer(double Tol)
- {
- tol = Tol;
- }
- public bool Equals(double d1, double d2)
- {
- return EQ(d1, d2, tol);
- }
- public int GetHashCode(double d)
- {
- return Math.Round(d).GetHashCode();
- }
- public bool EQ(double dbl, double compareDbl, double tolerance)
- {
- return Math.Abs(dbl - compareDbl) < tolerance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement