Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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, _sourceProjection, _targetProjection);

            using (var bitmap = (Bitmap)Image.FromStream(new MemoryStream(buffer)))
            {
                var inWorldFile = new WorldFile(resolution.UnitsPerPixel, 0,
                                                0, -resolution.UnitsPerPixel,
                                                info.Extent.MinX, info.Extent.MaxY);

                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);
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// This draws content from the specified geographic regions onto the specified graphics
        /// object specified by MapArgs.
        /// </summary>
        public void DrawRegions(MapArgs args, List <DsExtent> regions)
        {
            // If this layer is not marked visible, exit
            if (!IsVisible)
            {
                return;
            }

            _stopwatch.Reset();

            var region = regions.FirstOrDefault() ?? args.GeographicExtents;

            if (!Monitor.TryEnter(_drawLock))
            {
                return;
            }

            LogManager.DefaultLogManager.LogMessage("MAP   : " + region, DialogResult.OK);

            // If we have a target projection, so project extent to providers extent
            var geoExtent = _targetProjection == null
                                    ? region
                                    : region.Intersection(Extent).Reproject(_targetProjection, _sourceProjection);

            LogManager.DefaultLogManager.LogMessage("SOURCE: " + geoExtent, DialogResult.OK);

            if (geoExtent.IsEmpty())
            {
                LogManager.DefaultLogManager.LogMessage("Skipping because extent is empty!", DialogResult.OK);
                Monitor.Exit(_drawLock);
                return;
            }

            BtExtent extent;

            try
            {
                extent = ToBrutileExtent(geoExtent);
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogManager.Exception(ex);
                Monitor.Exit(_drawLock);
                return;
            }

            if (double.IsNaN(extent.Area))
            {
                LogManager.DefaultLogManager.LogMessage("Skipping because extent is empty!", DialogResult.OK);
                Monitor.Exit(_drawLock);
                return;
            }

            var pixelSize = extent.Width / args.ImageRectangle.Width;

            var tileSource = _configuration.TileSource;
            var schema     = tileSource.Schema;
            var level      = _level = Utilities.GetNearestLevel(schema.Resolutions, pixelSize);

            _tileFetcher.Clear();
            var tiles       = new List <TileInfo>(Sort(schema.GetTileInfos(extent, level), geoExtent.Center));
            var waitHandles = new List <WaitHandle>();
            var tilesNotImmediatelyDrawn = new List <TileInfo>();

            LogManager.DefaultLogManager.LogMessage(string.Format("Trying to get #{0} tiles: ", tiles.Count),
                                                    DialogResult.OK);

            // Set up Tile reprojector
            var tr = new TileReprojector(args, _sourceProjection, _targetProjection);


            var sw = new Stopwatch();

            sw.Start();

            // Store the current transformation
            var transform  = args.Device.Transform;
            var resolution = schema.Resolutions[level];

            foreach (var info in tiles)
            {
                var are       = _tileFetcher.AsyncMode ? null : new AutoResetEvent(false);
                var imageData = _tileFetcher.GetTile(info, are);
                if (imageData != null)
                {
                    //DrawTile
                    DrawTile(args, info, resolution, imageData, tr);
                    continue;
                }
                if (are == null)
                {
                    continue;
                }

                waitHandles.Add(are);
                tilesNotImmediatelyDrawn.Add(info);
            }

            //Wait for tiles
            foreach (var handle in waitHandles)
            {
                handle.WaitOne();
            }

            //Draw the tiles that were not present at the moment requested
            foreach (var tileInfo in tilesNotImmediatelyDrawn)
            {
                DrawTile(args, tileInfo, resolution, _tileFetcher.GetTile(tileInfo, null), tr);
            }

            //Restore the transform
            args.Device.Transform = transform;

            sw.Stop();

            Debug.WriteLine("{0} ms", sw.ElapsedMilliseconds);
            Debug.Write(string.Format("Trying to render #{0} tiles: ", tiles.Count));

            LogManager.DefaultLogManager.LogMessage(string.Format("{0} ms", sw.ElapsedMilliseconds), DialogResult.OK);
            //if (InvalidRegion != null)
            //    MapFrame.Invalidate();

            //_stopwatch.Restart();
            Monitor.Exit(_drawLock);
        }