private void DrawTile(MapArgs args, TileInfo info, Resolution resolution, byte[] buffer, TileReprojector tr = null) { if (buffer == null || buffer.Length == 0) { return; } tr = tr ?? new TileReprojector(args, _projection, _targetProjection); using (var bitmap = (Bitmap)Image.FromStream(new MemoryStream(buffer))) { var inWorldFile = new Reprojection.WorldFile(resolution.UnitsPerPixel, 0, 0, -resolution.UnitsPerPixel, info.Extent.MinX, info.Extent.MaxY); Reprojection.WorldFile outWorldFile; Bitmap outBitmap; tr.Reproject(inWorldFile, bitmap, out outWorldFile, out outBitmap); if (outWorldFile == null) { return; } var lt = args.ProjToPixel(outWorldFile.ToGround(0, 0)); var rb = args.ProjToPixel(outWorldFile.ToGround(outBitmap.Width, outBitmap.Height)); var rect = new Rectangle(lt, Size.Subtract(new Size(rb), new Size(lt))); args.Device.DrawImage(outBitmap, rect, 0, 0, outBitmap.Width, outBitmap.Height, GraphicsUnit.Pixel, _imageAttributes); if (outBitmap != bitmap) { outBitmap.Dispose(); } if (FrameTile) { if (FramePen != null) { args.Device.DrawRectangle(FramePen, rect); } } } }
public void Reproject(WorldFile inReference, Bitmap inTile, out WorldFile outReference, out Bitmap outTile) { // Shortcut when no projections have been assigned or they are of the same reference if (_source == null || _target == null || _source == _target) { outReference = inReference; outTile = inTile; return; } // Bounding polygon on the ground var ps = inReference.ToGroundBounds(inTile.Width, inTile.Height); // Bounding polygon on the ground in target projection var pt = ps.Shell.Reproject(_source, _target); // The target extent var ptExtent = pt.EnvelopeInternal.ToExtent(); // The target extent projected to the current viewport var ptRect = _mapArgs.ProjToPixel(ptExtent); // Get the intersection with the current viewport ptRect.Intersect(_mapArgs.ImageRectangle); // Is it empty, don't return anything if (ptRect.Width == 0 || ptRect.Height == 0) { outTile = null; outReference = null; return; } var offX = ptRect.X; var offY = ptRect.Y; // Prepare the result tile outTile = new Bitmap(ptRect.Size.Width, ptRect.Size.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(outTile)) { g.Clear(Color.Transparent); } var caIn = ColorAccess.Create(inTile); // not needed anymore var inSize = inTile.Size; inTile.Dispose(); var caOut = ColorAccess.Create(outTile); // Copy values to output buffer for (var i = 0; i < outTile.Height; i++) { foreach (Tuple <Point, Point> ppair in GetValidPoints(offY + i, offY, offX, offX + outTile.Width, inReference, inSize)) { var c = caIn[ppair.Item1.X, ppair.Item1.Y]; caOut[ppair.Item2.X, ppair.Item2.Y] = c; } } // Copy to output tile SetBitmapBuffer(outTile, caOut.Buffer); // Compute the reference var outExtent = _mapArgs.PixelToProj(ptRect); outReference = new WorldFile( outExtent.Width / ptRect.Width, 0, 0, -outExtent.Height / ptRect.Height, outExtent.X, outExtent.Y); }
private IEnumerable <Tuple <Point, Point> > GetValidPoints(int y, int y1, int x1, int x2, WorldFile inReference, Size checkSize) { var len = (x2 - x1); var len2 = len * 2; var xy = new double[len2]; var i = 0; for (var x = x1; x < x2; x++) { var c = _mapArgs.PixelToProj(new Point(x, y)); xy[i++] = c.X; xy[i++] = c.Y; } Projections.Reproject.ReprojectPoints(xy, null, _target, _source, 0, len); //Projections.Reproject.ReprojectPoints(xy, null, _target, KnownCoordinateSystems.Geographic.World.WGS1984, 0, len); //ClipWGS84(xy); //Projections.Reproject.ReprojectPoints(xy, null, KnownCoordinateSystems.Geographic.World.WGS1984, _source, 0, len); i = 0; y -= y1; x2 -= x1; for (var x = 0; x < x2; x++) { var coord = new Coordinate(xy[i++], xy[i++]); var inPoint = inReference.ToRaster(coord); if (Verbose) { var tmp1 = _mapArgs.PixelToProj(new Point(x + x1, y + y1)); var tmp2 = new [] { tmp1.X, tmp1.Y }; //Projections.Reproject.ReprojectPoints(tmp2, null, _target, _source, 0, 1); Console.WriteLine("{0} -> [{1},{2}] -> [{3},{4}] -> {5}", new Point(x + x1, y + y1), tmp2[0].ToString(NumberFormatInfo.InvariantInfo), tmp2[1].ToString(NumberFormatInfo.InvariantInfo), coord.X.ToString(NumberFormatInfo.InvariantInfo), coord.Y.ToString(NumberFormatInfo.InvariantInfo), inPoint); } //if (!IsValid(checkSize, inPoint)) //{ // coord.X *= -1; // inPoint = inReference.ToRaster(coord); //} if (IsValid(checkSize, inPoint)) { yield return(Tuple.Create(inPoint, new Point(x, y))); } } }
private void DrawTile(MapArgs args, TileInfo info, Resolution resolution, byte[] buffer, TileReprojector tr = null) { if (buffer == null || buffer.Length == 0) return; tr = tr ?? new TileReprojector(args, _projection, _targetProjection); using (var bitmap = (Bitmap)Image.FromStream(new MemoryStream(buffer))) { var inWorldFile = new Reprojection.WorldFile(resolution.UnitsPerPixel, 0, 0, -resolution.UnitsPerPixel, info.Extent.MinX, info.Extent.MaxY); Reprojection.WorldFile outWorldFile; Bitmap outBitmap; tr.Reproject(inWorldFile, bitmap, out outWorldFile, out outBitmap); if (outWorldFile == null) return; var lt = args.ProjToPixel(outWorldFile.ToGround(0, 0)); var rb = args.ProjToPixel(outWorldFile.ToGround(outBitmap.Width, outBitmap.Height)); var rect = new Rectangle(lt, Size.Subtract(new Size(rb), new Size(lt))); args.Device.DrawImage(outBitmap, rect, 0, 0, outBitmap.Width, outBitmap.Height, GraphicsUnit.Pixel, _imageAttributes); if (outBitmap != bitmap) outBitmap.Dispose(); if (FrameTile) { if (FramePen != null) args.Device.DrawRectangle(FramePen, rect); } } }