public GisFromToPair(GisLocation fromLoc, GisLocation toLoc) { this.FromLocation = fromLoc; this.ToLocation = toLoc; Key = $"{fromLoc}@@{toLoc}".GetHashCode(); }
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); } }
/// <summary> /// Format is address, city state zip /// Note that the city contain spaces, and that the only comma in the line follows the address. /// </summary> /// <param name="encoded"></param> /// <param name="explanation"></param> /// <returns></returns> public static GisLocation ParseFromString(string encoded, out string explanation) { explanation = ""; try { string[] tokens = encoded.Split(','); if (tokens.Count() != 2) { explanation = $"Encoded={encoded} Expected a comma following the address"; return(null); } GisLocation loc = new GisLocation(); loc.address = tokens[0]; string[] fragments = tokens[1].Trim().Split(' '); List <string> tokenList = new List <string>(); foreach (string frag in fragments) { if (frag.Trim() != "") { tokenList.Add(frag); } } // The last token is the zip, next to last is the state, and all others are space delimited city int count = tokenList.Count; if (count < 3) { explanation = $"Encoded={encoded}. Expected city, state, and zip"; return(null); } loc.zip = tokenList[count - 1]; loc.state = tokenList[count - 2]; StringBuilder sb = new StringBuilder(); for (int ii = 0; ii < tokenList.Count - 3; ii++) { sb.Append(tokenList[ii] + " "); } loc.city = sb.ToString().Trim(); return(loc); } catch (Exception ex) { explanation = $"Encoded={encoded}. Err={ex.Message}"; return(null); } }