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

            GC.SuppressFinalize(this);
        }
Beispiel #2
0
        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 List <float>(SamplesPerTile * SamplesPerTile);
            }

            if (File.Exists(TerrainTileFilePath))
            {
                // Load elevation file
                try
                {
                    using (Stream s = File.OpenRead(TerrainTileFilePath))
                    {
                        byte[] tfBuffer = new byte[SamplesPerTile * SamplesPerTile * 2];
                        if (s.Read(tfBuffer, 0, tfBuffer.Length) == tfBuffer.Length)
                        {
                            int offset = 0;

                            for (int y = 0; y < SamplesPerTile; y++)
                            {
                                for (int x = 0; x < SamplesPerTile; x++)
                                {
                                    ElevationData.Add(tfBuffer[offset++] + (short)(tfBuffer[offset++] << 8));
                                }
                            }
                            IsInitialized = true;
                            IsValid       = true;
                        }
                    }
                }
                catch
                {
                }

                if (!IsValid)
                {
                    try
                    {
                        // Remove corrupt/failed elevation files after preset time.
                        FileInfo badFileInfo = new FileInfo(TerrainTileFilePath);
                        TimeSpan age         = DateTime.Now.Subtract(badFileInfo.LastWriteTime);
                        if (age < BadTileRetryInterval)
                        {
                            // This tile is still flagged bad
                            IsInitialized = true;
                            return;
                        }

                        File.Delete(TerrainTileFilePath);
                    }
                    catch
                    {
                    }
                }
            }
        }