Exemplo n.º 1
0
        /// <summary>Returns a Tile for the specified area.</summary>
        /// <param name="zoom">The zoom level of the desired tile.</param>
        /// <param name="x">Tile index along the X axis.</param>
        /// <param name="y">Tile index along the Y axis.</param>
        /// <returns>
        /// If any of the indexes are outside the valid range of tile numbers for the specified zoom level,
        /// null will be returned.
        /// </returns>
        internal static BitmapImage GetTileImage(int zoom, int x, int y)
        {
            //For now we move the tiles a little

            if (string.IsNullOrEmpty(CacheFolder))
            {
                throw new InvalidOperationException("Must set the CacheFolder before calling GetTileImage.");
            }
            double xOffset     = worldScale.NormalizeX(xTileOffset);
            double yOffset     = worldScale.NormalizeY(yTileOffset);
            double inverseZoom = 18 - zoom;
            double xNormalized = worldScale.NormalizeX(x * Math.Pow(2, inverseZoom)) + xOffset;
            double yNormalized = worldScale.NormalizeY(y * Math.Pow(2, inverseZoom)) + yOffset;

            double tileCount = Math.Pow(2, zoom) - 1;

            int tilex = (int)Math.Floor(tileCount * xNormalized);
            int tiley = (int)Math.Floor(tileCount * yNormalized);

            if (tilex < 0 || tiley < 0 || tilex > tileCount || tiley > tileCount) // Bounds check
            {
                DebugUtil.LogWithLocation(String.Format("Request was outside of bounds zoom: {0} x: {1} y:{2} maxtile:{3}", zoom, x, y, tileCount));
                return(null);
            }

            Uri uri = new Uri(string.Format(CultureInfo.InvariantCulture, TileFormat, zoom, tilex, tiley));

            //log4net.LogManager.GetLogger(typeof(TileService)).DebugFormat("Fetching Image from URI: {0}", uri);
            return(BitmapStore.GetImage(uri));
        }
Exemplo n.º 2
0
        public void UpdateDebugLabels()
        {
            if (!IsDebugEnabled)
            {
                return;
            }
            double n                = Math.Pow(2, TileZoom);
            double inverseZoom      = 18 - tileZoom;
            double newTileX         = TileX * Math.Pow(2, inverseZoom);
            double newTileY         = tileY * Math.Pow(2, inverseZoom);
            double NormalizedX      = (scale.NormalizeX(newTileX) + scale.NormalizeX(TileService.xTileOffset));
            double NormalizedY      = (scale.NormalizeY(newTileY) + scale.NormalizeY(TileService.yTileOffset));
            double osmTileXPreFloor = NormalizedX * n;
            double osmTileYPreFloor = NormalizedY * n;

            int osmTileX = (int)Math.Floor(osmTileXPreFloor);
            int osmTileY = (int)Math.Floor(osmTileYPreFloor);

            //Clear any previous
            this.Children.Clear();
            //Generate a unique tile color
            Random r     = new Random(Guid.NewGuid().GetHashCode());
            Color  color = Color.FromArgb(100, (byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255));
            //Create a small border for tile
            double            borderSize = Width * 0.05; // 5%
            RectangleVisual3D LeftEdge   = new RectangleVisual3D();
            RectangleVisual3D RightEdge  = new RectangleVisual3D();
            RectangleVisual3D TopEdge    = new RectangleVisual3D();
            RectangleVisual3D BottomEdge = new RectangleVisual3D();
            double            offset     = Width / 2;

            //LeftEdge
            LeftEdge.Origin = new Point3D(borderSize / 2 - offset, Width / 2 - offset, 0.1);
            LeftEdge.Width  = Width;
            LeftEdge.Length = borderSize;
            LeftEdge.Fill   = new SolidColorBrush(color);

            //Right Edge
            RightEdge.Origin = new Point3D(Width - borderSize / 2 - offset, Width / 2 - offset, 0.1);
            RightEdge.Length = borderSize;
            RightEdge.Width  = Width;
            RightEdge.Fill   = new SolidColorBrush(color);
            //TopEdge
            TopEdge.Origin = new Point3D(Width / 2 - offset, borderSize / 2 - offset, 0.1);
            TopEdge.Width  = borderSize;
            TopEdge.Length = Width;
            TopEdge.Fill   = new SolidColorBrush(color);
            //BottomEdge
            BottomEdge.Origin = new Point3D(Width / 2 - offset, Width - borderSize / 2 - offset, 0.1);
            BottomEdge.Width  = borderSize;
            BottomEdge.Length = Width;
            BottomEdge.Fill   = new SolidColorBrush(color);
            //Create text billboard for x / y values
            BillboardTextVisual3D tilenumBilboard = new BillboardTextVisual3D();

            tilenumBilboard.Position   = new Point3D(0, 0, 8 + inverseZoom * 4);
            tilenumBilboard.Background = Brushes.LightSalmon;
            tilenumBilboard.Text       = $"TXY: {TileX}/{TileY} NTXY: {newTileX}/{newTileY} \n OSMTXY: {osmTileXPreFloor}/{osmTileYPreFloor} \n OSMFTXY: {osmTileX - 1}/{osmTileY}";

            Children.Add(LeftEdge);
            Children.Add(RightEdge);
            Children.Add(TopEdge);
            Children.Add(BottomEdge);
            Children.Add(tilenumBilboard);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Pans the Grid Tiles the given amount (only supports one square for now)
        /// </summary>
        /// <param name="panAmountX"></param>
        /// <param name="panAmountY"></param>
        private void PanGrid(int panAmountX, int panAmountY)
        {
            PerfStopwatch ps = PerfStopwatch.StartNew("Pan Grid");

            if (panAmountX == 0 && panAmountY == 0)
            {
                return;
            }

            //If positive move tiles from left edge to right edge
            int xTileIndexToFind = GetXTileEdge(panAmountX);
            int yTileIndexToFind = GetYTileEdge(panAmountY);

            List <MapTileVisual> tilesToMove = new List <MapTileVisual>();

            if (panAmountX != 0)
            {
                tilesToMove.AddRange(Tiles.Where(t => t.TileX == xTileIndexToFind));
            }
            if (panAmountY != 0)
            {
                tilesToMove.AddRange(Tiles.Where(t => t.TileY == yTileIndexToFind));
            }

            OSMWorldScale scale = new OSMWorldScale();

            double mapCenterNormalizedX = scale.NormalizeX(TileService.xTileOffset);
            double mapCenterNormalizedY = scale.NormalizeY(TileService.yTileOffset);

            double tileTranslateX = scale.GetTileXTranslateFix(mapCenterNormalizedX, mapZoom, TileSize); //TileSize * fracX;
            double tileTranslateY = scale.GetTileYTranslateFix(mapCenterNormalizedY, mapZoom, TileSize); //TileSize * fracY;

            foreach (MapTileVisual tile in tilesToMove)
            {
                if (tile.TileX == xTileIndexToFind)
                {
                    tile.TileX += GridSize * panAmountX;
                }
                if (tile.TileY == yTileIndexToFind)
                {
                    tile.TileY += GridSize * panAmountY;
                }

                TranslateTransform3D transform = tile.Transform as TranslateTransform3D;
                //Apply this as a transformation as well
                if (transform != null)
                {
                    transform.OffsetX = ((tile.TileX * TileSize * -1) - TileSize / 2) - tileTranslateX;
                    transform.OffsetY = ((tile.TileY * TileSize) - TileSize / 2) - tileTranslateY;
                }

                tile.UpdateDebugLabels();

                //Send of a request to update the tile
                TileGenerator.LoadTileImageAsync(tile,
                                                 tile.TileX,
                                                 tile.TileY,
                                                 MapZoomLevel);
            }

            currentTileX += panAmountX;
            currentTileY += panAmountY;
            ps.Stop();
        }
Exemplo n.º 4
0
        public void InitGrid()
        {
            int midNum = (int)Math.Ceiling(GridSize / 2d);

            MapCenter = new Point3D(0d, 0d, 0d);
            Random ran = new Random();

            currentTileX = 0;
            currentTileY = 0;

            OSMWorldScale scale = new OSMWorldScale();

            double mapCenterNormalizedX = scale.NormalizeX(TileService.xTileOffset);
            double mapCenterNormalizedY = scale.NormalizeY(TileService.yTileOffset);

            double maxTiles = Math.Pow(2, mapZoom);
            double tileNumX = mapCenterNormalizedX * maxTiles;
            double tileNumY = mapCenterNormalizedY * maxTiles;

            //X and y are flipped
            double fracX = tileNumX - Math.Floor(tileNumX);
            double fracY = tileNumY - Math.Floor(tileNumY);

            double posFracX = fracX;
            double posFracY = fracY;

            if (posFracX <= 0.5)
            {
                posFracX += 1;
            }

            if (posFracY >= 0.5)
            {
                posFracY -= 1;
            }
            if (mapZoom == 15 || mapZoom == 12)
            {
                posFracY -= 1;
            }

            double tileTranslateX = scale.GetTileXTranslateFix(mapCenterNormalizedX, mapZoom, TileSize); //TileSize * fracX;
            double tileTranslateY = scale.GetTileYTranslateFix(mapCenterNormalizedY, mapZoom, TileSize); //TileSize * fracY;

            Debug.WriteLine($"TranslateFixes: {tileTranslateX} / {tileTranslateY} TileSize {TileSize}");
            Debug.WriteLine($"TranslateFixesWOLessTHanFixes: {TileSize * fracX} / {TileSize * fracY}");
            Debug.WriteLine($"TranslateFixesPositive: {posFracX} / {posFracY}");

            //double fooTest2 =
            //double fooTest2 = scale.GetTileXTranslateFix(mapCenterNormalizedX, mapZoom, TileSize);

            BoundingBoxWireFrameVisual3D box = new BoundingBoxWireFrameVisual3D();

            box.Color = Colors.Yellow;
            double bSize = 1 * TileSize / 32;

            box.Thickness   = 1;
            box.BoundingBox = new Rect3D((tileTranslateX) - bSize / 2, tileTranslateY - bSize / 2, 0, bSize, bSize, bSize);
            this.Children.Add(box);

            BoundingBoxWireFrameVisual3D box2 = new BoundingBoxWireFrameVisual3D();

            box2.Color = Colors.LimeGreen;
            double b2Size = 1 * TileSize / 32;

            box2.Thickness   = 1;
            box2.BoundingBox = new Rect3D((posFracX * TileSize * -1) - b2Size / 2, (posFracY * TileSize) - b2Size / 2, 0, b2Size, b2Size, b2Size);
            this.Children.Add(box2);

            for (int x = 0; x < GridSize; x++)
            {
                for (int y = 0; y < GridSize; y++)
                {
                    int gridPosX = ((x + 1) - midNum);
                    int gridPosY = ((y + 1) - midNum);

                    MapTileVisual tile = new MapTileVisual();

                    tile.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)ran.Next(30, 255), (byte)ran.Next(32, 255), (byte)ran.Next(28, 255)));

                    TranslateTransform3D position = new TranslateTransform3D();

                    position.OffsetX = (gridPosX * TileSize) - TileSize / 2;
                    position.OffsetY = (gridPosY * TileSize) - TileSize / 2;
                    if (TranslateOnZoom)
                    {
                        position.OffsetX -= tileTranslateX;
                        position.OffsetY -= tileTranslateY;
                    }

                    tile.Transform = position;

                    tile.Width  = TileSize;
                    tile.Length = TileSize;

                    tile.TileX    = gridPosX * -1;
                    tile.TileY    = gridPosY;
                    tile.TileZoom = MapZoomLevel;

                    this.Children.Add(tile);
                    Tiles.Add(tile);

                    if (TileGenerator != null)
                    {
                        //log.DebugFormat("Requesting Image for x/y/zoom {0} / {1} / {2}", tile.TileX, tile.TileY, mapZoom);
                        TileGenerator.LoadTileImageAsync(tile,
                                                         tile.TileX,
                                                         tile.TileY,
                                                         MapZoomLevel);
                    }
                    else
                    {
                        Aegir.Util.DebugUtil.LogWithLocation("Could not load image, TileGeneratorWas Null");
                    }
                    tile.UpdateDebugLabels();
                }
            }
        }