public static bool GetFromTabDelimitedPairFile(string path, out List <GisFromToPair> pairList, out string explanation) { explanation = ""; pairList = new List <GisFromToPair>(); if (!File.Exists(path)) { explanation = $"File={path} does not exist."; return(false); } try { string[] lines = File.ReadAllLines(path); int nn = 0; foreach (string line in lines) { string[] tokens = line.Split('\t'); if (tokens.Length != 3) { explanation = $"Line={nn} Text={line}. Expected 3 tokens, found={tokens.Length}"; return(false); } GisLocation fromLoc = GisLocation.ParseFromString(tokens[1], out explanation); if (fromLoc == null) { explanation = $"Line={nn}. Cannot Parse Origin={tokens[1]}"; return(false); } GisLocation toLoc = GisLocation.ParseFromString(tokens[2], out explanation); if (toLoc == null) { explanation = $"Line={nn}. Cannot Parse Destination={tokens[2]}"; return(false); } GisFromToPair pair = new GisFromToPair(fromLoc, toLoc); if (pair == null) { explanation = $"Line={nn}. Text={line}. Cannot Create Pair."; return(false); } pairList.Add(pair); nn++; } return(true); } catch (Exception ex) { explanation = $"Path={path} Err={ex.Message}"; return(false); } }
public static bool CreatePairedData(List <GisLocation> locationList, int count, out List <GisFromToPair> pairList, out string explanation) { explanation = ""; pairList = null; if (locationList == null || !locationList.Any()) { explanation = $"LocationList is empty."; return(false); } int nn = locationList.Count; int maxCount = nn * nn - nn; if (count > maxCount) { explanation = $"Count={count} exceed maximum={maxCount}"; return(false); } try { Random rnd = new Random(); Dictionary <int, GisFromToPair> pairDict = new Dictionary <int, GisFromToPair>(); while (pairDict.Count < count) { int iiFrom = rnd.Next(0, locationList.Count); int iiTo = -1; do { iiTo = rnd.Next(0, locationList.Count); } while (iiTo == iiFrom); GisFromToPair pair = new GisFromToPair(locationList[iiFrom], locationList[iiTo]); if (!pairDict.ContainsKey(pair.Key)) { pairDict.Add(pair.Key, pair); } } pairList = pairDict.Values.ToList(); return(true); } catch (Exception ex) { explanation = $" Err={ex.Message}"; return(false); } }