Example #1
0
        public byte[] GenerateTile(int x, int y, int zoom)
        {
            byte[] tileBytes = null;
            var    font      = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 12);

            var corner        = TileUtils.TileXYToPixelXY(x, y);
            var latLong       = TileUtils.PixelXYToLatLong(corner.X, corner.Y, zoom);
            var latLongOffset = TileUtils.PixelXYToLatLong(corner.X + TileSize, corner.Y + TileSize, zoom);



            var graticules = graticuleService.DrawCore(latLong, latLongOffset);


            using (Image <Rgba32> outputImage = new Image <Rgba32>(this.TileSize, this.TileSize))
            {
                string tileText = $"{x}/{y}/{zoom}{Environment.NewLine}";
                outputImage.Mutate(o => o
                                   .Fill(Rgba32.White)
                                   .DrawText(tileText, font, Rgba32.Black, new PointF(10, 10))
                                   );
                outputImage.Mutate(o => DrawGraticules(o, graticules, corner, zoom));



                // Test
                if (DebugPoint != null)
                {
                    var testPixel = TileUtils.LatLongToPixelXY(DebugPoint.Latitude, DebugPoint.Longitude, zoom);
                    var testTile  = TileUtils.PixelXYToTileXY(testPixel.X, testPixel.Y);

                    // Draw test pixel
                    if (testTile.X == x && testTile.Y == y)
                    {
                        var basex = TileUtils.TileXYToPixelXY(x, y);
                        var ptLoc = new PointF(testPixel.X - basex.X, testPixel.Y - basex.Y);
                        outputImage.Mutate(o =>
                                           o.DrawLines(Rgba32.Blue, 1f,
                                                       new PointF[] { new PointF(ptLoc.X - 10, ptLoc.Y - 10), new PointF(ptLoc.X + 10, ptLoc.Y + 10) })
                                           .DrawLines(Rgba32.Blue, 1f,
                                                      new PointF[] { new PointF(ptLoc.X - 10, ptLoc.Y + 10), new PointF(ptLoc.X + 10, ptLoc.Y - 10) }));
                    }
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    outputImage.SaveAsPng(ms);
                    tileBytes = ms.ToArray();
                }
            }

            return(tileBytes);
        }