public void Reproject_DifferentCoordinateSystems_ReprojectImageAndMetaData() { // Setup var mapArgs = new MapArgs(new Rectangle(0, 0, 722, 349), new Extent(520981.864447542, 6853700.54100246, 709995.365081098, 6945065.79269375)); ProjectionInfo sourceProjection = ProjectionInfo.FromEpsgCode(25831); ProjectionInfo targetProjection = KnownCoordinateSystems.Projected.World.WebMercator; var projector = new TileReprojector(mapArgs, sourceProjection, targetProjection); var sourceReference = new WorldFile(140, 0.0, 0.0, -140, 641716.59261121, 5825498); Bitmap sourceTile = Resources.source; WorldFile targetReference; Bitmap targetTile; // Call projector.Reproject(sourceReference, sourceTile, out targetReference, out targetTile); // Assert // Note: These ground truth values have been defined using https://github.com/FObermaier/DotSpatial.Plugins/blob/master/DotSpatial.Plugins.BruTileLayer/Reprojection/TileReprojector.cs Assert.AreEqual(261.791552124038, targetReference.A11, 1e-8); Assert.AreEqual(0.0, targetReference.A21); Assert.AreEqual(0.0, targetReference.A12); Assert.AreEqual(-261.79155212403651, targetReference.A22, 1e-8); Assert.AreEqual(564962.84520438069, targetReference.B1, 1e-8); Assert.AreEqual(6902131.9781454066, targetReference.B2, 1e-8); TestHelper.AssertImagesAreEqual(Resources.target, targetTile); }
/// <summary> /// Reprojects a <see cref="Bitmap"/> and corresponding georeferenced metadata to /// a new coordinate system. /// </summary> /// <param name="sourceReference">The georeferenced metadata of <paramref name="sourceTile"/>.</param> /// <param name="sourceTile">The tile image.</param> /// <param name="targetReference">Output: The reprojected georeferenced metadata.</param> /// <param name="targetTile">Output: The reprojected tile image.</param> public void Reproject(WorldFile sourceReference, Bitmap sourceTile, out WorldFile targetReference, out Bitmap targetTile) { if (source == null || target == null || source.Equals(target)) { targetReference = sourceReference; targetTile = sourceTile; return; } Rectangle targetTileExtentInPixelCoordinates = GetTargetExtentInPixelCoordinates(sourceReference, sourceTile); // Get the intersection with the current viewport targetTileExtentInPixelCoordinates.Intersect(mapArgs.ImageRectangle); // Is it empty, don't return anything if (targetTileExtentInPixelCoordinates.Width == 0 || targetTileExtentInPixelCoordinates.Height == 0) { targetTile = null; targetReference = null; return; } int offsetX = targetTileExtentInPixelCoordinates.X; int offsetY = targetTileExtentInPixelCoordinates.Y; // Prepare the result tile targetTile = new Bitmap(targetTileExtentInPixelCoordinates.Size.Width, targetTileExtentInPixelCoordinates.Size.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(targetTile)) { g.Clear(Color.Transparent); } ColorAccess sourceTileColorAccess = ColorAccess.Create(sourceTile); ColorAccess targetTileColorAccess = ColorAccess.Create(targetTile); // Copy values to output buffer Size sourceTileSize = sourceTile.Size; for (var i = 0; i < targetTile.Height; i++) { foreach (Tuple <Point, Point> ppair in GetValidPoints(offsetY + i, offsetY, offsetX, offsetX + targetTile.Width, sourceReference, sourceTileSize)) { Color c = sourceTileColorAccess[ppair.Item1.X, ppair.Item1.Y]; targetTileColorAccess[ppair.Item2.X, ppair.Item2.Y] = c; } } // Copy to output tile targetTileColorAccess.SetBufferToImageAtOriginalLocation(targetTile); // Compute the reference Extent outExtent = mapArgs.PixelToProj(targetTileExtentInPixelCoordinates); targetReference = new WorldFile( outExtent.Width / targetTileExtentInPixelCoordinates.Width, 0, 0, -outExtent.Height / targetTileExtentInPixelCoordinates.Height, outExtent.X, outExtent.Y); }
private IEnumerable <Tuple <Point, Point> > GetValidPoints(int y, int offsetY, int x1, int x2, WorldFile sourceReference, Size sourceTileSize) { int len = x2 - x1; var xy = new double[len * 2]; var i = 0; for (int x = x1; x < x2; x++) { Coordinate c = mapArgs.PixelToProj(new Point(x, y)); xy[i] = c.X; xy[i + 1] = c.Y; i = i + 2; } DotSpatialReproject.ReprojectPoints(xy, null, target, source, 0, len); i = 0; y -= offsetY; for (var x = 0; x < len; x++) { var coord = new Coordinate(xy[i++], xy[i++]); Point sourcePixelLocation = sourceReference.ToScreenCoordinates(coord); if (IsSourcePointInsideArea(sourceTileSize, sourcePixelLocation)) { yield return(Tuple.Create(sourcePixelLocation, new Point(x, y))); } } }
private Rectangle GetTargetExtentInPixelCoordinates(WorldFile sourceReference, Bitmap sourceTile) { IPolygon sourceTileCircumference = sourceReference.BoundingOrdinatesToWorldCoordinates(sourceTile.Width, sourceTile.Height); ILinearRing targetTileCircumference = sourceTileCircumference.Shell.Reproject(source, target); Extent targetTileExtent = targetTileCircumference.EnvelopeInternal.ToExtent(); return(mapArgs.ProjToPixel(targetTileExtent)); }
public void Reproject_SameProjection_ReturnSourceMaterial() { // Setup ProjectionInfo projection = KnownCoordinateSystems.Projected.NationalGrids.Rijksdriehoekstelsel; var mapArgs = new MapArgs(new Rectangle(), new Extent()); var projector = new TileReprojector(mapArgs, projection, projection); var sourceReference = new WorldFile(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); Bitmap sourceTile = Resources.testImage; WorldFile targetReference; Bitmap targetTile; // Call projector.Reproject(sourceReference, sourceTile, out targetReference, out targetTile); // Assert Assert.AreSame(sourceReference, targetReference); Assert.AreSame(sourceTile, targetTile); }
public void Reproject_TargetTileWouldNotBeVisibleInViewport_ReturnNull() { // Setup ProjectionInfo sourceProjection = KnownCoordinateSystems.Projected.NationalGrids.Rijksdriehoekstelsel; ProjectionInfo targetProjection = KnownCoordinateSystems.Projected.World.WebMercator; var mapArgs = new MapArgs(new Rectangle(0, 0, 10, 10), new Extent(5, 50, 10, 100)); var projector = new TileReprojector(mapArgs, sourceProjection, targetProjection); var sourceReference = new WorldFile(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); Bitmap sourceTile = Resources.testImage; WorldFile targetReference; Bitmap targetTile; // Call projector.Reproject(sourceReference, sourceTile, out targetReference, out targetTile); // Assert Assert.IsNull(targetReference); Assert.IsNull(targetTile); }