private void DownloadImage(DownloadImage di) { try { if (!File.Exists(di.Filepath)) { HttpWebRequest req = (HttpWebRequest) WebRequest.Create(di.Url); req.UserAgent = "Mozilla"; req.Timeout = 10000; WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); Image image = Image.FromStream(stream); stream.Close(); try { image.Save(di.Filepath, System.Drawing.Imaging.ImageFormat.Png); } catch (Exception) { //log.Warn("SAVE: " + svx.Message); } resp.Close(); } else { //log.Debug("someone already downloaded the tile ..."); } } catch (Exception ) { //log.Warn("http connection " + ex.Message); } try { this.DownloadedTile( Thread.CurrentThread.ManagedThreadId ); } catch (Exception) { //log.Warn("update tile event " + ex.Message); } }
private String GetFilePathAndCreateDir(DownloadImage di) { String xpath = Utils.GetXPath(di.X, di.Z, renderer); if (!Directory.Exists(xpath)) { Directory.CreateDirectory(xpath); } String savepath = Utils.GetYPath(xpath, di.Y); return savepath; }
/// <summary> /// /// </summary> protected void DownloadImages() { DownloadImage[,] mapimages = new DownloadImage[tileMatrixSize.X, tileMatrixSize.Y]; int nCountTiles = Calculator.GetCountTiles(zoom); for (int i = xTileNumber; i <= xTileNumber + tileMatrixSize.X - 1; i++) { // correct tile number when Longitude around -180deg / 180deg int xTile = i; if (xTile < 0) { xTile = xTile + nCountTiles; } else if (xTile >= nCountTiles) { xTile = xTile - nCountTiles; } for (int j = yTileNumber; j <= yTileNumber + tileMatrixSize.Y - 1; j++) { DownloadImage di = new DownloadImage(this.GetTilesUrl(xTile, j, zoom), xTile, j, zoom ); try { mapimages[i - xTileNumber, j - yTileNumber] = di; } catch (System.IndexOutOfRangeException) { } } } DateTime dt = DateTime.Now; String zoompath = Utils.GetZoompath(this.zoom, renderer); if (!Directory.Exists(zoompath)) { Directory.CreateDirectory(zoompath); } List<DownloadImage> downloadlist = new List<DownloadImage>(); foreach (DownloadImage di in mapimages) { try { String savepath = this.GetFilePathAndCreateDir(di); Boolean needDownload = true; if (this.online) { if (File.Exists(savepath)) { needDownload = false; TimeSpan test = dt - File.GetLastWriteTime(savepath); if (test.TotalMinutes > this.cache_live) { needDownload = true; } } } di.Filepath = savepath; if (!needDownload) { this.DownloadedTile( Thread.CurrentThread.ManagedThreadId ); } else { if ((di.X == xTileNumber + 2) && (di.Y == yTileNumber + 2)) downloadlist.Insert(0, di); else downloadlist.Add(di); } } catch (Exception ) { } } try { this.FinishedCache(this,null); } catch (Exception ) { } foreach (DownloadImage di in downloadlist) { this.DownloadImage(di); } }