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

            GC.SuppressFinalize(this);
        }
Esempio n. 2
0
        public void Dispose()
        {
            if (this.request != null)
            {
                this.request.Dispose();
                this.request = null;
            }

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

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

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

            if (File.Exists(this.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(this.TerrainTileFilePath);
                        if (tileInfo.Length == 0)
                        {
                            TimeSpan age = DateTime.Now.Subtract(tileInfo.LastWriteTime);
                            if (age < this.m_owner.TerrainTileRetryInterval)
                            {
                                // This tile is still flagged bad
                                this.IsInitialized = true;
                            }
                            else
                            {
                                // remove the empty 'flag' file
                                File.Delete(this.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(this.TerrainTileFilePath))
                    {
                        BinaryReader reader = new BinaryReader(s);
                        if (this.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 < this.SamplesPerTile; y++)
                            {
                                for (int x = 0; x < this.SamplesPerTile; x++)
                                {
                                    this.ElevationData[x, y] = reader.ReadInt16();
                                }
                            }
                        }
                        if (this.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 < this.SamplesPerTile; y++)
                            {
                                for (int x = 0; x < this.SamplesPerTile; x++)
                                {
                                    this.ElevationData[x, y] = reader.ReadSingle();
                                }
                            }
                        }

                        this.IsInitialized = true;
                        this.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(this.TerrainTileFilePath);
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(String.Format("Error while trying to delete corrupt terrain tile {0}", this.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}", this.TerrainTileFilePath), ex);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// This method initializes the terrain tile add switches to
        /// Initialize floating point/int 16 tiles
        /// </summary>
        internal 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);
            }
             }
        }