/*--------------------------------------------------------- * Method: findCoords * * Purpose: Finds the averaged coordinates of a city * using the .csv file containing coordinates * for various zip codes. * * Parameters: * city - a string containing the name of the city * statecode - the 2-letter abbreviation for the state * * Returns: A Location object. * Returns '-1, -1' as coords if the city * is not found. *--------------------------------------------------------*/ public static Location findCoords(string city, string statecode) { double latSum = 0.0; double longSum = 0.0; int count = 0; // read through the .csv file // and sum the coordinates where the // city and statenames match string url = "http://theochem.mercer.edu/csc330/data/zip_codes_states.csv"; WebReader reader = new WebReader(url); string line = reader.getLine(); while (line != null) { string[] columns = line.Split(','); if (columns[3].Contains(city) && columns[4].Contains(statecode)) { try { latSum += Double.Parse(columns[1]); longSum += Double.Parse(columns[2]); count++; } catch (FormatException) {} } line = reader.getLine(); } if (count == 0) { return(new Location(-1.0, -1.0, city, statecode)); } return(new Location((latSum / count), (longSum / count), city, statecode)); }
/*--------------------------------------------------------- * Method: findNearestAirport * * Purpose: Finds the nearest usable airport to some location * * Returns: Location of the nearest usable airport *--------------------------------------------------------*/ public static Airport findNearestAirport(Location target) { Location l = new Location(-1, -1, target.city, target.state); Airport result = new Airport("", l, false); result.position.city = target.city; result.position.state = target.state; double mindist = 5000; WebReader reader = new WebReader("http://theochem.mercer.edu/csc330/data/airports.csv"); String line = reader.getLine(); while (line != null) { Regex regex = new Regex("\".*?\""); line = regex.Replace(line, m => m.Value.Replace(',', ' ')); line = line.Replace("\"", ""); String[] columns = line.Split(','); if (columns[2].Contains("airport")) { double lat = Double.Parse(columns[4]); double lon = Double.Parse(columns[5]); double dist = getDistance(target.latitude, target.longitude, lat, lon); if (dist < mindist) { mindist = dist; result.position.latitude = lat; result.position.longitude = lon; result.name = columns[3]; } } line = reader.getLine(); } return(result); }