Ejemplo n.º 1
0
        public void Dispose()
        {
            if (request != null)
            {
                request.Dispose();
                request = null;
            }

            GC.SuppressFinalize(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method initializes the terrain tile add switches to
        /// Initialize floating point/int 16 tiles
        /// </summary>
        public void Initialize()
        {
            if (IsInitialized)
            {
                return;
            }

            if (!File.Exists(TerrainTileFilePath))
            {
                // Download elevation
                if (request == null)
                {
                    using (request = new TerrainDownloadRequest(this, m_owner, Row, Col, TargetLevel))
                    {
                        request.SaveFilePath = TerrainTileFilePath;
                        request.DownloadInForeground();
                    }
                }
            }

            if (ElevationData == null)
            {
                ElevationData = new float[SamplesPerTile, SamplesPerTile];
            }

            if (File.Exists(TerrainTileFilePath))
            {
                // Load elevation file
                try
                {
                    // TerrainDownloadRequest's FlagBadTile() creates empty files
                    // as a way to flag "bad" terrain tiles.
                    // Remove the empty 'flag' files after preset time.
                    try
                    {
                        FileInfo tileInfo = new FileInfo(TerrainTileFilePath);
                        if (tileInfo.Length == 0)
                        {
                            TimeSpan age = DateTime.Now.Subtract(tileInfo.LastWriteTime);
                            if (age < m_owner.TerrainTileRetryInterval)
                            {
                                // This tile is still flagged bad
                                IsInitialized = true;
                            }
                            else
                            {
                                // remove the empty 'flag' file
                                File.Delete(TerrainTileFilePath);
                            }
                            return;
                        }
                    }
                    catch
                    {
                        // Ignore any errors in the above block, and continue.
                        // For example, if someone had the empty 'flag' file
                        // open, the delete would fail.
                    }

                    using (Stream s = File.OpenRead(TerrainTileFilePath))
                    {
                        BinaryReader reader = new BinaryReader(s);
                        if (m_owner.DataType == "Int16")
                        {
                            /*
                             * byte[] tfBuffer = new byte[SamplesPerTile*SamplesPerTile*2];
                             * if (s.Read(tfBuffer,0,tfBuffer.Length) < tfBuffer.Length)
                             *      throw new IOException(string.Format("End of file error while reading terrain file '{0}'.", TerrainTileFilePath) );
                             *
                             * int offset = 0;
                             * for(int y = 0; y < SamplesPerTile; y++)
                             *      for(int x = 0; x < SamplesPerTile; x++)
                             *              ElevationData[x,y] = tfBuffer[offset++] + (short)(tfBuffer[offset++]<<8);
                             */
                            for (int y = 0; y < SamplesPerTile; y++)
                            {
                                for (int x = 0; x < SamplesPerTile; x++)
                                {
                                    ElevationData[x, y] = reader.ReadInt16();
                                }
                            }
                        }
                        if (m_owner.DataType == "Float32")
                        {
                            /*
                             * byte[] tfBuffer = new byte[SamplesPerTile*SamplesPerTile*4];
                             * if (s.Read(tfBuffer,0,tfBuffer.Length) < tfBuffer.Length)
                             *              throw new IOException(string.Format("End of file error while reading terrain file '{0}'.", TerrainTileFilePath) );
                             */
                            for (int y = 0; y < SamplesPerTile; y++)
                            {
                                for (int x = 0; x < SamplesPerTile; x++)
                                {
                                    ElevationData[x, y] = reader.ReadSingle();
                                }
                            }
                        }
                        IsInitialized = true;
                        IsValid       = true;
                    }
                    return;
                }
                catch (IOException)
                {
                    // If there is an IO exception when reading the terrain tile,
                    // then either something is wrong with the file, or with
                    // access to the file, so try and remove it.
                    try
                    {
                        File.Delete(TerrainTileFilePath);
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(String.Format("Error while trying to delete corrupt terrain tile {0}", TerrainTileFilePath), ex);
                    }
                }
                catch (Exception ex)
                {
                    // Some other type of error when reading the terrain tile.
                    throw new ApplicationException(String.Format("Error while trying to read terrain tile {0}", TerrainTileFilePath), ex);
                }
            }
        }