public void WriteOut(string fname) { try { QresFinder.EnsureExistenceOfDirectory(fname); File.WriteAllLines(fname, lines); } catch (Exception ex) { Debug.LogError(ex.ToString()); } }
public (bool, string) GetElevdataFromQresFinder(QresFinder qrf) { if (!qrf.Exists()) { return(false, "Qrf could not find data"); } var text = qrf.GetText(); var textar = text.Split('\n'); var df = new SimpleDf(); df.ReadCsv(textar); var rv = ProcessDf(df); return(rv); }
async Task <bool> GetQuadkeyAsy(string qkname, string ppath, string scenename) { Debug.Log("GetQuadKeyAsy" + scenename + " lod:" + levelOfDetail + " qk:" + qkname); // https://t1.ssl.ak.dynamic.tiles.virtualearth.net/comp/ch/021230030212230?mkt=en&it=A,G,L,LA&og=30&n=z // //var uri = "https://t3.ssl.ak.dynamic.tiles.virtualearth.net/comp/ch/" + qkname + "?mkt=en&it=A,G,L,LA&og=30&n=z"; var uri = GetUri(qkname); using (var webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. webRequest.SendWebRequest(); while (!webRequest.isDone) { //yield return new WaitForSeconds(1000); await Task.Delay(TimeSpan.FromSeconds(0.05f)); Debug.Log(" back from Task.Delay"); } string[] pages = uri.Split('/'); int lastpage = pages.Length - 1; if (webRequest.isNetworkError) { Debug.Log(pages[lastpage] + " - Error: " + webRequest.error); return(false); } else { Debug.Log(pages[lastpage] + " - Received " + webRequest.downloadHandler.data.Length + " bytes"); //var dname = GetFullQkSubDir(scenename); var dname = ppath; var bytes = webRequest.downloadHandler.data; var fname = dname + qkname + ".png"; QresFinder.EnsureExistenceOfDirectory(fname); File.WriteAllBytes(fname, bytes); Debug.Log("Wrote " + fname + " bytes:" + bytes.Length); return(true); } } }
public async Task <(bool, int)> RetrieveElevations(bool execute = true, bool forceload = false) { var ok = true; string errmsg = ""; var nretrieved = 0; if (heights.Count > 0) { return(ok, nretrieved); } var efname = "eledata.csv"; var efpath = "qkmaps/" + GetEleCsvSubDir(scenename, mapprov); qrf = new QresFinder(efpath, efname); (ok, errmsg) = GetElevdataFromQresFinder(qrf); var ppath = qrf.PersistentPathName(); var tpath = qrf.TempPathName(); if (forceload || !ok) { Debug.LogWarning(errmsg); Debug.LogWarning("Trying to retrieve heights from web"); //(ok,errmsg) = GetElevdataSync(); (ok, errmsg, nretrieved) = await GetWwwElevDataAsync(tpath, ppath, execute); if (!ok) { Debug.LogError(errmsg); } else { qrf.Reload(); (ok, errmsg) = GetElevdataFromQresFinder(qrf);// try again if (!ok) { Debug.LogError(errmsg); } } } return(ok, nretrieved); }
async Task <(bool, string, int)> GetWwwElevDataAsync(string tpath, string ppath, bool execute = true) { Debug.Log("GetElevDataAsy - llb:" + llb.ToString() + " nrow:" + nrow + " ncol:" + ncol); var ok = true; var errmsg = ""; ElevCsvMaker csvmaker = new ElevCsvMaker(ncol, decpt: 1); int maxElevationsPerRequest = 1024; // https://docs.microsoft.com/en-us/bingmaps/rest-services/elevations/get-elevations int maxrowinblk = (maxElevationsPerRequest / ncol); // floor is what happens if (maxrowinblk <= 0) { errmsg = $"Error ncol:{ncol} great than Bing Elevation Api Max Request:{maxElevationsPerRequest}"; return(false, errmsg, 0); } int nblk = nrow / maxrowinblk; if (nrow % maxrowinblk != 0) { nblk += 1; // get the leftovers } Debug.Log("Stats maxrowinblk:" + maxrowinblk + " nblk:" + nblk); int nretrieved = 0; for (int iblk = 0; iblk < nblk; iblk++) { //var uri = String.Format(url, llb.latmin, llb.lngmin, llb.latmax, llb.lngmax, nrow, ncol, bingKey); (var uriok, var uri, var blklatmin, var blklatmax) = GetWwwUri(llb, nrow, ncol, iblk, maxrowinblk); if (!uriok) { break; } if (execute) { var rfname = GetElevReqName(tpath, iblk); QresFinder.EnsureExistenceOfDirectory(rfname); File.WriteAllText(rfname, uri); Debug.Log("Wrote " + rfname + " bytes:" + uri.Length); using (var webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. var wr = webRequest.SendWebRequest(); while (!webRequest.isDone) { await Task.Delay(TimeSpan.FromSeconds(0.05f)); Debug.Log(" back from Task.Delay"); } string[] uriarray = uri.Split('/'); int urilast = uriarray.Length - 1; if (webRequest.isNetworkError) { errmsg = uriarray[urilast] + " - Error: " + webRequest.error; return(false, errmsg, nretrieved); } Debug.Log(uriarray[urilast] + " - Received " + webRequest.downloadHandler.data.Length + " bytes"); var fname = GetEleBlkPathFileName(tpath, iblk); QresFinder.EnsureExistenceOfDirectory(fname); var bytes = webRequest.downloadHandler.data; var str = System.Text.Encoding.Default.GetString(bytes); ok = str.Contains("\"statusCode\":200"); if (ok) { csvmaker.AddStr(iblk, blklatmin, blklatmax, str); } else { errmsg = "See " + fname + " for error message"; Debug.LogError(errmsg); } File.WriteAllBytes(fname, bytes); Debug.Log("Wrote " + fname + " bytes:" + bytes.Length + " ok:" + ok); } } nretrieved++; } //var csvfname = GetElevCsvFullName(); var csvfname = GetElevCsvFullName(ppath); QresFinder.EnsureExistenceOfDirectory(csvfname); csvmaker.WriteOut(csvfname); return(ok, errmsg, nretrieved); }