示例#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;
 }
示例#2
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));
        }
示例#3
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);
        }
示例#4
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);
        }
        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));
        }
示例#6
0
 public abstract Texture2D LoadTileTexture(RasterTileIdentifier identifier);