Esempio n. 1
0
 public RasterTile(RasterSource source, RasterTileIdentifier identifier)
 {
     _level = source.Levels[identifier.Level];
     _identifier = identifier;
     _west = identifier.X * Level.LongitudePostsPerTile;
     _east = Math.Min(_west + Level.LongitudePostsPerTile, Level.LongitudePosts) - 1;
     _south = identifier.Y * Level.LatitudePostsPerTile;
     _north = Math.Min(_south + Level.LatitudePostsPerTile, Level.LatitudePosts) - 1;
 }
Esempio n. 2
0
        public RasterTile GetTile(RasterTileIdentifier identifier)
        {
            RasterTile tile;
            if (m_activeTiles.TryGetValue(identifier, out tile))
            {
                return tile;
            }

            // New tiles are not initially active.  They become active when loaded.
            tile = new RasterTile(this, identifier);
            return tile;
        }
Esempio n. 3
0
        public override Texture2D LoadTileTexture(RasterTileIdentifier identifier)
        {
            int level = identifier.Level;
            int longitudeIndex = identifier.X;
            int latitudeIndex = identifier.Y;

            string cachePath = "esri";
            cachePath = Path.Combine(cachePath, level.ToString());
            cachePath = Path.Combine(cachePath, latitudeIndex.ToString());
            string cacheFilename = Path.Combine(cachePath, longitudeIndex.ToString() + ".jpg");

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            if (!File.Exists(cacheFilename))
            {
                // Esri tiles are numbered from the northwest instead of from the southwest.

                StringBuilder query = new StringBuilder(_baseUri.AbsoluteUri);
                query.Append(level);
                query.Append('/');
                query.Append((1 << level) - latitudeIndex - 1);
                query.Append('/');
                query.Append(longitudeIndex);

                string queryString = query.ToString();
                ++_tilesLoaded;
                Console.WriteLine("(" + _tilesLoaded + ") Downloading " + queryString);

                WebRequest request = WebRequest.Create(queryString);
                using (WebResponse response = request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (FileStream file = new FileStream(cacheFilename, FileMode.Create, FileAccess.Write))
                {
                    const int bufferSize = 4096;
                    byte[] buffer = new byte[bufferSize];

                    int bytesRead = stream.Read(buffer, 0, bufferSize);
                    while (bytesRead > 0)
                    {
                        file.Write(buffer, 0, bytesRead);
                        bytesRead = stream.Read(buffer, 0, bufferSize);
                    }
                }
            }

            Bitmap bitmap = new Bitmap(cacheFilename);
            return Device.CreateTexture2DRectangle(bitmap, TextureFormat.RedGreenBlue8);
        }
Esempio n. 4
0
 public abstract Texture2D LoadTileTexture(RasterTileIdentifier identifier);
        public override Texture2D LoadTileTexture(RasterTileIdentifier identifier)
        {
            string cachePath = identifier.Level.ToString();
            cachePath = Path.Combine(cachePath, identifier.X.ToString());
            string cacheFilename = Path.Combine(cachePath, identifier.Y.ToString() + ".bil");

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            int heightsToRead = TileWidth * TileHeight;
            float[] result = new float[heightsToRead];
            byte[] data = null;

            if (File.Exists(cacheFilename))
            {
                data = File.ReadAllBytes(cacheFilename);
            }

            if (data == null || data.Length != heightsToRead * sizeof(short))
            {
                double divisor = Math.Pow(2.0, identifier.Level);
                double longitudeResolution = LevelZeroDeltaLongitudeDegrees / divisor;
                double latitudeResolution = LevelZeroDeltaLatitudeDegrees / divisor;

                double west = -180.0 + longitudeResolution * identifier.X;
                double east = -180.0 + longitudeResolution * (identifier.X + 1);
                double south = -90.0 + latitudeResolution * identifier.Y;
                double north = -90.0 + latitudeResolution * (identifier.Y + 1);

                StringBuilder query = new StringBuilder(_baseUri.AbsoluteUri);
                query.Append("&bbox=");
                query.Append(west.ToString("0.###########"));
                query.Append(',');
                query.Append(south.ToString("0.###########"));
                query.Append(',');
                query.Append(east.ToString("0.###########"));
                query.Append(',');
                query.Append(north.ToString("0.###########"));
                query.Append('&');

                string queryString = query.ToString();
                ++_tilesLoaded;
                Console.WriteLine("(" + _tilesLoaded + ") Downloading " + queryString);

                int bytesToRead = heightsToRead * 2;

                WebRequest request = WebRequest.Create(queryString);

                data = new byte[bytesToRead];

                using (WebResponse response = request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (FileStream file = new FileStream(cacheFilename, FileMode.Create, FileAccess.Write))
                {
                    int bytesRead = 0;
                    while (bytesRead < bytesToRead)
                    {
                        int bytesReadThisTime = stream.Read(data, bytesRead, bytesToRead - bytesRead);
                        if (bytesReadThisTime == 0)
                            throw new IOException("Unexpected end of file.");
                        file.Write(data, bytesRead, bytesReadThisTime);
                        bytesRead += bytesReadThisTime;
                    }
                }
            }

            // Make the southwest corner the origin instead of the northwest.
            int index = 0;
            for (int row = TileHeight - 1; row >= 0; --row)
            {
                int rowIndex = row * TileWidth;
                for (int col = 0; col < TileWidth; ++col)
                {
                    result[index++] = BitConverter.ToInt16(data, 2 * (rowIndex + col));
                }
            }

            return PostsToTexture(result);
        }
Esempio n. 6
0
        public RasterTileRegion[] GetTilesInExtent(int west, int south, int east, int north)
        {
            int tileXStart = west / LongitudePostsPerTile;
            int tileXStop = east / LongitudePostsPerTile;

            if (west < 0)
            {
                --tileXStart;
            }
            if (east < 0)
            {
                --tileXStop;
            }

            int tileYStart = south / LatitudePostsPerTile;
            int tileYStop = north / LatitudePostsPerTile;

            if (south < 0)
            {
                --tileYStart;
            }
            if (north < 0)
            {
                --tileYStop;
            }

            int tileWidth = tileXStop - tileXStart + 1;
            int tileHeight = tileYStop - tileYStart + 1;

            RasterTileRegion[] result = new RasterTileRegion[tileWidth * tileHeight];
            int resultIndex = 0;

            for (int tileY = tileYStart; tileY <= tileYStop; ++tileY)
            {
                int tileYOrigin = tileY * LatitudePostsPerTile;

                int currentSouth = south - tileYOrigin;
                if (currentSouth < 0)
                    currentSouth = 0;

                int currentNorth = north - tileYOrigin;
                if (currentNorth >= LatitudePostsPerTile)
                    currentNorth = LatitudePostsPerTile - 1;

                for (int tileX = tileXStart; tileX <= tileXStop; ++tileX)
                {
                    int tileXOrigin = tileX * LongitudePostsPerTile;

                    int currentWest = west - tileXOrigin;
                    if (currentWest < 0)
                        currentWest = 0;

                    int currentEast = east - tileXOrigin;
                    if (currentEast >= LongitudePostsPerTile)
                        currentEast = LongitudePostsPerTile - 1;

                    RasterTileIdentifier tileID = new RasterTileIdentifier(_level, tileX, tileY);
                    RasterTile tile = Source.GetTile(tileID);
                    result[resultIndex] = new RasterTileRegion(tile, currentWest, currentSouth, currentEast, currentNorth);
                    ++resultIndex;
                }
            }

            return result;
        }
Esempio n. 7
0
        public override Texture2D LoadTileTexture(RasterTileIdentifier identifier)
        {
            //if (identifier.Level > 4)
                //return null;
            int level = identifier.Level; // 0 is -180 long
            int longitudeIndex = ((_levels[level].LongitudePosts / _levels[level].LongitudePostsPerTile) / 2 / 2) + identifier.X; //  (_levels[level].LongitudePosts / _levels[level].LongitudePostsPerTile) -
            int latitudeIndex = identifier.Y; // (_levels[level].LatitudePosts / _levels[level].LatitudePostsPerTile) -

            int damn = (1 << level) - latitudeIndex - 1;

            Console.WriteLine(" z {0} lat {1} lon {2} ", level, damn, longitudeIndex);

            GMap.NET.PureImage img = MainMap.Manager.ImageCacheLocal.GetImageFromCache(GMap.NET.MapType.GoogleSatellite, new GMap.NET.GPoint(longitudeIndex, damn), level);

            Application.DoEvents();

            try
            {
                Bitmap bitmap = new Bitmap(new Bitmap(img.Data), 256, 256);
                Graphics e = Graphics.FromImage(bitmap);
                e.DrawString(level + " " +longitudeIndex + "," + damn, new Font("Arial", 20), Brushes.White, new PointF(0, 0));

                return Device.CreateTexture2DRectangle(bitmap, TextureFormat.RedGreenBlue8);
            }
            catch {
                try
                {
                    return null;
                }
                catch { return null; }
            }
        }