async private Task <byte[]> GetTile(IServiceRequestContext context, TileServiceMetadata metadata, int epsg, double scale, int row, int col, string format, GridOrientation orientation) { if (!metadata.EPSGCodes.Contains(epsg)) { throw new ArgumentException("Wrong epsg argument"); } //if (!metadata.Scales.Contains(scale)) // throw new ArgumentException("Wrong scale argument"); scale = metadata.Scales.GetScale(scale); if (scale <= 0.0) { throw new ArgumentException("Wrong scale argument"); } //IEnvelope bounds = metadata.GetEPSGEnvelope(epsg); //if (bounds == null || bounds.Width == 0.0 || bounds.Height == 0.0) // throw new Exception("No bounds defined for EPSG:" + epsg); format = format.ToLower(); if (format != ".png" && format != ".jpg") { throw new Exception("Unsupported image format"); } if (format == ".png" && metadata.FormatPng == false) { throw new Exception("Format image/png not supported"); } if (format == ".jpg" && metadata.FormatJpg == false) { throw new Exception("Format image/jpeg no supported"); } string path = _mapServer.TileCachePath + @"/" + MapName(context) + @"/_alllayers/" + TileServiceMetadata.TilePath(orientation, epsg, scale, row, col) + format; if ((orientation == GridOrientation.UpperLeft && metadata.UpperLeftCacheTiles) || (orientation == GridOrientation.LowerLeft && metadata.LowerLeftCacheTiles)) { FileInfo fi = new FileInfo(path); if (fi.Exists) { //context.ServiceRequest.Response = fi.FullName; using (FileStream fs = File.Open(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) //new FileStream(bundleFilename, FileMode.Open, FileAccess.Read)) { byte[] data = new byte[fi.Length]; await fs.ReadAsync(data, 0, data.Length); return(data); } } } return(null); }
private void GetTile(IServiceRequestContext context, TileServiceMetadata metadata, int epsg, double scale, int row, int col, string format, GridOrientation orientation, bool renderOnTheFly) { if (!metadata.EPSGCodes.Contains(epsg)) { throw new ArgumentException("Wrong epsg argument"); } //if (!metadata.Scales.Contains(scale)) // throw new ArgumentException("Wrong scale argument"); scale = metadata.Scales.GetScale(scale); if (scale <= 0.0) { throw new ArgumentException("Wrong scale argument"); } //IEnvelope bounds = metadata.GetEPSGEnvelope(epsg); //if (bounds == null || bounds.Width == 0.0 || bounds.Height == 0.0) // throw new Exception("No bounds defined for EPSG:" + epsg); format = format.ToLower(); if (format != ".png" && format != ".jpg") { throw new Exception("Unsupported image format"); } if (format == ".png" && metadata.FormatPng == false) { throw new Exception("Format image/png not supported"); } if (format == ".jpg" && metadata.FormatJpg == false) { throw new Exception("Format image/jpeg no supported"); } string path = _mapServer.TileCachePath + @"\" + context.ServiceMap.Name + @"\_alllayers\" + TileServiceMetadata.TilePath(orientation, epsg, scale, row, col) + format; if ((orientation == GridOrientation.UpperLeft && metadata.UpperLeftCacheTiles) || (orientation == GridOrientation.LowerLeft && metadata.LowerLeftCacheTiles)) { FileInfo fi = new FileInfo(path); if (fi.Exists) { context.ServiceRequest.Response = fi.FullName; return; } else if (!renderOnTheFly && !metadata.RenderTilesOnTheFly) { return; // Empty } if (!fi.Directory.Exists) { fi.Directory.Create(); } } else { path = _mapServer.OutputPath + @"\tile_" + Guid.NewGuid().ToString("N").ToLower() + format; } ISpatialReference sRef = SpatialReference.FromID("epsg:" + epsg); using (IServiceMap map = context.ServiceMap) { map.Display.SpatialReference = sRef; map.Display.dpi = metadata.Dpi; map.Display.iWidth = metadata.TileWidth; map.Display.iHeight = metadata.TileHeight; double res = (double)scale / (metadata.Dpi / 0.0254); if (map.Display.MapUnits != GeoUnits.Meters) { GeoUnitConverter converter = new GeoUnitConverter(); res = converter.Convert(res, GeoUnits.Meters, map.Display.MapUnits); } var origin = orientation == GridOrientation.UpperLeft ? metadata.GetOriginUpperLeft(epsg) : metadata.GetOriginLowerLeft(epsg); double H = metadata.TileHeight * res; double y = (orientation == GridOrientation.UpperLeft ? origin.Y - H * (row + 1) : origin.Y + H * row); double W = metadata.TileWidth * res; //if (map.Display.MapUnits == GeoUnits.DecimalDegrees) //{ // double phi = (2 * y + H) / 2.0; // W /= Math.Cos(phi / 180.0 * Math.PI); //} double x = origin.X + W * col; map.Display.ZoomTo(new Envelope(x, y, x + W, y + H)); map.Render(); bool maketrans = map.Display.MakeTransparent; map.Display.MakeTransparent = true; map.SaveImage(path, format == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png); map.Display.MakeTransparent = maketrans; context.ServiceRequest.Response = path; _mapServer.Log("CreateTile:", loggingMethod.request_detail, path); } }