/// <summary> /// Determines the coordinate values that corresponds to the closest coordinate point in the NLDAS grid. /// </summary> /// <param name="errorMsg"></param> /// <param name="point">ICoordinate</param> /// <returns>[Latitude, Longitude]</returns> public double[] DetermineReturnCoordinates(out string errorMsg, IPointCoordinate point) { errorMsg = ""; double[] coord = new double[2]; double step = step = 0.25; double x = (point.Longitude + 179.8750) / step; coord[1] = (Math.Round(x, MidpointRounding.AwayFromZero) * step) - 179.8750; double y = (point.Latitude + 59.8750) / step; coord[0] = (Math.Round(y, MidpointRounding.AwayFromZero) * step) - 59.8750; return(coord); }
/// <summary> /// Determines the coordinate values that corresponds to the closest coordinate point in the NLDAS grid. /// </summary> /// <param name="errorMsg"></param> /// <param name="point">ICoordinate</param> /// <returns>[Latitude, Longitude]</returns> public double[] DetermineReturnCoordinates(out string errorMsg, IPointCoordinate point) { errorMsg = ""; double[] coord = new double[2]; double step = 0.125; double x = (point.Longitude + 124.9375) / step; coord[1] = (Math.Round(x, MidpointRounding.AwayFromZero) * step) - 124.9375; double y = (point.Latitude - 25.0625) / step; coord[0] = (Math.Round(y, MidpointRounding.AwayFromZero) * step) + 25.0625; return(coord); }
/// <summary> /// Gets timezone details, timezone name and offset, based on the coordinates in point. /// Function first attempts getting timezone details from google earth engine hms api, if it fails reverts to google maps api. /// </summary> /// <param name="errorMsg"></param> /// <param name="point">IPointCoordinate containing a latitude and longitude value.</param> /// <returns></returns> public ITimezone GetTimezone(out string errorMsg, IPointCoordinate point) { errorMsg = ""; //Dictionary<string, string> urls = (Dictionary<string, string>)HttpContext.Current.Application["urlList"]; //string url = urls["TIMEZONE_GEE_INT"]; string url = "https://134.67.114.1/hms/rest/timezone/"; string queryString = "latitude=" + point.Latitude.ToString() + "&longitude=" + point.Longitude.ToString(); string completeUrl = url + queryString; try { WebClient wc = new WebClient(); byte[] buffer = wc.DownloadData(completeUrl); string resultString = Encoding.UTF8.GetString(buffer); Dictionary <string, string> tz = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(resultString); return(new Timezone() { Name = tz["tzName"], Offset = Convert.ToDouble(tz["tzOffset"]), DLS = false }); } catch { string key = "AIzaSyDUdVJFt_SUwqfNTfziXXUFK7gkHxTnRIE"; // Personal google api key, to be replaced by project key string baseUrl = "https://maps.googleapis.com/maps/api/timezone/json?"; string location = "location=" + point.Latitude.ToString() + "," + point.Longitude.ToString(); string timeStamp = "timestamp=1331161200"; completeUrl = baseUrl + location + "&" + timeStamp + "&key=" + key; try { WebClient wc = new WebClient(); byte[] buffer = wc.DownloadData(completeUrl); string resultString = Encoding.UTF8.GetString(buffer); Dictionary <string, string> tz = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(resultString); return(new Timezone() { Name = tz["timeZoneId"], Offset = Convert.ToDouble(tz["rawOffset"]) / 3600, DLS = false }); } catch (Exception ex) { errorMsg = "ERROR: " + ex.Message; return(new Timezone()); } } }
/// <summary> /// Validate geometry Point input. /// </summary> /// <param name="errors"></param> /// <param name="point"></param> /// <returns></returns> private static bool ValidatePoint(out List <string> errors, IPointCoordinate point) { errors = new List <string>(); bool validPoint = true; if (point == null) { errors.Add("ERROR: Valid geometry point input not found."); return(false); } if (point.Latitude > 90.0 || point.Latitude < -90.0) { errors.Add("ERROR: Latitude value is not valid. Latitude must be between -90 and 90. Latitude: " + point.Latitude.ToString()); validPoint = false; } if (point.Longitude > 180.0 || point.Longitude < -180.0) { errors.Add("ERROR: Longitude value is not valid. Longitude must be between -180 and 180. Longitude: " + point.Longitude.ToString()); validPoint = false; } return(validPoint); }
/// <summary> /// Converts latitude/longitude to X/Y values for NLDAS location. /// </summary> /// <param name="errorMsg"></param> /// <param name="point">ICoordinate</param> /// <returns></returns> private static string[] GetXYCoordinate(out string errorMsg, IPointCoordinate point) { errorMsg = ""; double xMax = 463.0; double yMax = 223.0; double x, y = 0.0; string[] results = new string[2]; x = (point.Longitude + 124.9375) / 0.125; y = (point.Latitude - 25.0625) / 0.125; if (x > xMax || x < 0) { errorMsg = "ERROR: Longitude value outside accepted range for NLDAS or is invalid. Provided longitude: " + point.Longitude.ToString() + "\n"; return(null); } if (y > yMax || y < 0) { errorMsg = "ERROR: Latitude value outside accepted range for NLDAS or is invalid. Provided latitude: " + point.Latitude.ToString() + "\n"; return(null); } results[0] = Convert.ToString(Math.Round(x, MidpointRounding.AwayFromZero)); results[1] = Convert.ToString(Math.Round(y, MidpointRounding.AwayFromZero)); return(results); }