Ejemplo n.º 1
0
 /// <summary>
 /// 获取瓦片数据块对应在原始图像(1:1)中的像素坐标位置
 /// </summary>
 /// <param name="tile"></param>
 /// <param name="beginRow"></param>
 /// <param name="beginCol"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public void GetOriginalRowColByDataBlock(TileIdentify tile, ref int beginRow, ref int beginCol, ref int width, ref int height)
 {
     beginRow = (int)(tile.BeginRow / Scale);
     beginCol = (int)(tile.BeginCol / Scale);
     width    = (int)(tile.Width / Scale);
     height   = (int)(tile.Height / Scale);
 }
Ejemplo n.º 2
0
 private void DoAsyncCache(T[][] buffers, TileIdentify tile, int[] selectedBandNos)
 {
     for (int i = 0; i < _selectedBandNos.Length; i++)
     {
         _fileDiskCacheManager.PutTile <T>(tile, _selectedBandNos[i], buffers[i]);
     }
 }
Ejemplo n.º 3
0
        void _loadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool isGDAL = _dataProvider is GeoDo.RSS.DF.GDAL.IGDALRasterDataProvider;

            try
            {
                _loadWorkerIsCompleted = false;
                if (isGDAL)
                {
                    while (_loadStack.Count > 0)
                    {
                        lock (_lockDoWork)
                        {
                            TileIdentify tile = _loadStack.Pop();
                            DoLoadTile(tile);
                        }
                    }
                }
                else
                {
                    while (_loadStack.Count > 0)
                    {
                        TileIdentify tile = _loadStack.Pop();
                        DoLoadTile(tile);
                    }
                }
            }
            finally
            {
                _loadWorkerIsCompleted = true;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 获取瓦片原始图像(1:1)中的像素坐标位置
 /// </summary>
 /// <param name="tile"></param>
 /// <param name="beginRow"></param>
 /// <param name="beginCol"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public void GetOriginalRowCol(int tileSize, TileIdentify tile, ref int beginRow, ref int beginCol, ref int width, ref int height)
 {
     beginRow = (int)(tile.BeginRow / Scale - tile.OffsetY / Scale);
     beginCol = (int)(tile.BeginCol / Scale - tile.OffsetX / Scale);
     width    = (int)(tileSize / Scale);
     height   = (int)(tileSize / Scale);
 }
Ejemplo n.º 5
0
 private Bitmap BuildBitmap(T[][] buffers, LevelDef level, TileIdentify tile)
 {
     using (IBitmapBuilder <T> builder = GetBitmapBuilder())
     {
         Bitmap bitmap = null;
         if (buffers.Length == 1)
         {
             bitmap         = new Bitmap(_tileSize, _tileSize, PixelFormat.Format8bppIndexed);
             bitmap.Palette = BitmapBuilderFactory.GetDefaultGrayColorPalette();
             if (st1 == null && _stretcher == null)
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], ref bitmap);
             }
             else
             {
                 if (_stretcher != null)
                 {
                     builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], _stretcher, ref bitmap);
                 }
                 else
                 {
                     builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], st1.Stretcher, ref bitmap);
                 }
             }
             //
             if (_colorMapTable != null)
             {
                 ColorPalette plt = BitmapBuilderFactory.GetDefaultGrayColorPalette();
                 for (int i = 0; i < 256; i++)
                 {
                     plt.Entries[i] = Color.Black;
                 }
                 int idx = 1;
                 foreach (ColorMapItem <int> item in _colorMapTable.Items)
                 {
                     for (int v = item.MinValue; v < item.MaxValue; v++)
                     {
                         plt.Entries[idx] = item.Color;
                     }
                     idx++;
                 }
                 bitmap.Palette = plt;
             }
         }
         else if (buffers.Length == 3)
         {
             bitmap = new Bitmap(_tileSize, _tileSize, PixelFormat.Format24bppRgb);
             if (st1 == null || st2 == null || st3 == null)
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], buffers[1], buffers[2], ref bitmap);
             }
             else
             {
                 builder.Build(tile.Width, tile.Height, tile.OffsetX, tile.OffsetY, buffers[0], buffers[1], buffers[2], st1.Stretcher, st2.Stretcher, st3.Stretcher, ref bitmap);
             }
         }
         return(bitmap);
     }
 }
Ejemplo n.º 6
0
 public void Dispose()
 {
     if (Bitmap != null)
     {
         Bitmap.Dispose();
         Bitmap = null;
     }
     Tile = null;
 }
Ejemplo n.º 7
0
 public bool IsExist(TileIdentify tile, int[] selectedBandNos)
 {
     foreach (int b in selectedBandNos)
     {
         long id = long.Parse(b.ToString() + tile.TileNo.ToString());
         if (!_cachedFiles.Contains(id))
         {
             return(false);
         }
     }
     Console.WriteLine("existed : " + tile.TileNo.ToString());
     return(true);
 }
Ejemplo n.º 8
0
        private TileBitmap LoadTileBitmap(TileIdentify tile)
        {
            int      beginRow = 0, beginCol = 0, width = 0, height = 0;
            LevelDef level = tile.Level;

            level.GetOriginalRowColByDataBlock(tile, ref beginRow, ref beginCol, ref width, ref height);
            TileBitmap tb = _dataProviderReader.CreateBitmapByTile(level, beginRow, beginCol, width, height, tile);

            if (!_disposed && tb != null)
            {
                _tileCacheManager.Put(tb);
            }
            return(tb);
        }
Ejemplo n.º 9
0
        private TileBitmap LoadTileBitmap(TileIdentify tile, out bool memoryIsNotEnough)
        {
            memoryIsNotEnough = false;
            int      beginRow = 0, beginCol = 0, width = 0, height = 0;
            LevelDef level = tile.Level;

            level.GetOriginalRowColByDataBlock(tile, ref beginRow, ref beginCol, ref width, ref height);
            TileBitmap tb = _dataProviderReader.CreateBitmapByTile(level, beginRow, beginCol, width, height, tile, out memoryIsNotEnough);

            if (!_disposed && tb != null)
            {
                _tileCacheManager.Put(tb);
                _loadedTitleCount++;
            }
            return(tb);
        }
Ejemplo n.º 10
0
 void _loadWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     while (_loadStack.Count > 0)
     {
         TileIdentify tile = _loadStack.Pop();
         if (tile == null)
         {
             continue;
         }
         if (!_tileCacheManager.IsExist(tile))
         {
             LoadTileBitmap(tile);
             _asyncRefreshCanvas.BeginInvoke(tile, new AsyncCallback((ret) => { _isNotifyRefreshing = false; }), null);
         }
     }
 }
Ejemplo n.º 11
0
 private void DoLoadTile(TileIdentify tile)
 {
     if (!_tileCacheManager.IsExist(tile))
     {
         bool memoryIsNotEnough = false;
         LoadTileBitmap(tile, out memoryIsNotEnough);
         if (memoryIsNotEnough)
         {
             Console.WriteLine("_loadWorker_DoWork:内存不足,无法读取请求的瓦片!");
             _loadWorkerIsCompleted = true;//这里的处理是错误的,应该是瓦片交换
             return;
         }
         _loadWorker.ReportProgress((int)(_loadedTitleCount * 100f / _totalTileCount));
         _asyncRefreshCanvas.BeginInvoke(tile, new AsyncCallback((ret) => { _isNotifyRefreshing = false; }), null);
     }
 }
Ejemplo n.º 12
0
        private long GetOffsetByLevelBufferWhole(LevelDef[] levels, int iLevel, TileIdentify t)
        {
            int      levelCount = levels.Length;
            long     offset     = 0;
            LevelDef lv;

            for (int i = levelCount - 1; i >= 0; i--)
            {
                if (i == iLevel)
                {
                    break;
                }
                lv     = levels[i];
                offset = lv.Size.Width * lv.Size.Height * _dataTypeSize + t.BeginRow * lv.Size.Width + t.BeginCol;
            }
            return(offset);
        }
Ejemplo n.º 13
0
        public void PutTile <T>(TileIdentify tile, int bandNo, T[] buffer)
        {
            long id = long.Parse(bandNo.ToString() + tile.TileNo.ToString());

            if (_cachedFiles.Contains(id))
            {
                return;
            }
            string fname = GetTileBufferFileName(tile, bandNo);
            int    len   = tile.Width * tile.Height * DataTypeHelper.SizeOf(_dataProvider.DataType);

            byte[]   dstBuffer = new byte[len];
            GCHandle srcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            GCHandle dstHandle = GCHandle.Alloc(dstBuffer, GCHandleType.Pinned);

            try
            {
                WinAPI.MemoryCopy(dstHandle.AddrOfPinnedObject(), srcHandle.AddrOfPinnedObject(), len);
tryAgainLine:
                try
                {
                    File.WriteAllBytes(fname, dstBuffer);
                }
                catch (DirectoryNotFoundException)//缓存目录被删除
                {
                    Directory.CreateDirectory(_fDir);
                    goto tryAgainLine;
                }
                catch (IOException ioex)
                {
                    if (ioex.Message.Contains("磁盘空间"))
                    {
                        FileDiskCacheManagerFactory.TryFreeDiskspace(_fDir);
                    }
                    return;
                }
                _cachedFiles.Add(id);
            }
            finally
            {
                srcHandle.Free();
                dstHandle.Free();
            }
        }
Ejemplo n.º 14
0
 private void TryRefreshCanvas(TileIdentify tile)
 {
     _isNotifyRefreshing = true;
     if (_currentLevel.LevelNo == tile.Level.LevelNo)
     {
         int beginRow = 0, beginCol = 0, width = 0, height = 0;
         tile.Level.GetOriginalRowCol(_tileSetting.TileSize, tile, ref beginRow, ref beginCol, ref width, ref height);
         if (beginRow > (_bRowAtViewWnd + _heightAtViewWnd) ||
             beginCol > (_bColAtViewWnd + _widthAtViewWnd) ||
             (beginRow + height) < _bRowAtViewWnd ||
             (beginCol + width) < _bColAtViewWnd)
         {
             return;
         }
         _canvas.Refresh(enumRefreshType.All);
         if (_canvas.SomeTileIsArrived != null)
         {
             _canvas.SomeTileIsArrived();
         }
     }
 }
Ejemplo n.º 15
0
        public T[] GetTile <T>(TileIdentify tile, int bandNo)
        {
            long id = long.Parse(bandNo.ToString() + tile.TileNo.ToString());

            if (!_cachedFiles.Contains(id))
            {
                return(null);
            }
            int size      = tile.Width * tile.Height;
            int bytesSize = size * _dataTypeSize;

            T[] dstBuffer = new T[size];
            try
            {
                string fname = GetTileBufferFileName(tile, bandNo);
                using (FileStream fs = new FileStream(fname, FileMode.Open))
                {
                    fs.Read(_tileBuffer, 0, bytesSize);
                    GCHandle dstHandle = GCHandle.Alloc(dstBuffer, GCHandleType.Pinned);
                    try
                    {
                        WinAPI.MemoryCopy(dstHandle.AddrOfPinnedObject(), _tileBufferHandle.AddrOfPinnedObject(), bytesSize);
                    }
                    finally
                    {
                        dstHandle.Free();
                    }
                    return(dstBuffer);
                }
            }
            catch (FileNotFoundException)
            {
                NotifyCacheIsCleared();
                return(null);
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 获取视窗范围内指定行列号位置周围的瓦片
        /// </summary>
        /// <param name="tileSize">瓦片大小</param>
        /// <param name="centerRow">指定行号</param>
        /// <param name="centerCol">指定列号</param>
        /// <param name="wndSize">视窗大小</param>
        /// <returns></returns>
        public       TileIdentify[] GetAroundTiles(int tileSize, int centerRow, int centerCol, Size wndSize)
        {
            int bRow = centerRow, eRow = centerRow;
            int bCol = centerCol, eCol = centerCol;

            if (wndSize.Height > tileSize)
            {
                while ((eRow - bRow + 1) * tileSize < wndSize.Height)
                {
                    eRow++;
                    bRow--;
                }
            }
            if (wndSize.Width > tileSize)
            {
                while ((eCol - bCol + 1) * tileSize < wndSize.Width)
                {
                    eCol++;
                    bCol--;
                }
            }
            List <TileIdentify> tiles = new List <TileIdentify>();

            for (int r = bRow; r <= eRow; r++)
            {
                for (int c = bCol; c < eCol; c++)
                {
                    TileIdentify t = GetTileByRowCol(r, c);
                    if (t != null)
                    {
                        tiles.Add(t);
                    }
                }
            }
            return(tiles.Count > 0 ? tiles.ToArray() : null);
        }
Ejemplo n.º 17
0
 private string GetKey(TileIdentify tile)
 {
     return(string.Format("L{0}R{1}C{2}", tile.Level.LevelNo, tile.Row, tile.Col));
 }
Ejemplo n.º 18
0
        public TileBitmap GetTileBitmap(TileIdentify tile)
        {
            LevelDef level = tile.Level;

            return(_tileCacheManager.Get(level.LevelNo, tile.Row, tile.Col));
        }
Ejemplo n.º 19
0
 private string GetTileBufferFileName(TileIdentify tile, int bandNo)
 {
     return(Path.Combine(_fDir, string.Format("{0}{1}.RAW", bandNo, tile.TileNo)));
 }
Ejemplo n.º 20
0
        public TileBitmap CreateBitmapByTile(LevelDef level, int beginRow, int beginCol, int width, int height, TileIdentify tile)
        {
            lock (_lockObj)
            {
                T[][] buffers = null;
                //从磁盘中读取缓存的瓦片
tryAgainLine:
                if (_enableDiskCache && _fileDiskCacheManager.IsExist(tile, _selectedBandNos))
                {
                    buffers = new T[_selectedBandNos.Length][];
                    for (int i = 0; i < _selectedBandNos.Length; i++)
                    {
                        buffers[i] = _fileDiskCacheManager.GetTile <T>(tile, _selectedBandNos[i]);
                        if (buffers[i] == null)//缓存已经从磁盘清除
                        {
                            goto tryAgainLine;
                        }
                    }
                }
                else
                {
                    bool isOK = ReadRasterFile(out buffers, beginRow, beginCol, width, height, tile);
                    if (!isOK)
                    {
                        return(null);
                    }
                    Console.WriteLine("direct read , " + Environment.TickCount.ToString());
                    //将该瓦片保存在磁盘
                    if (_enableDiskCache)
                    {
                        _asyncCacheHandler.BeginInvoke(buffers, tile, _selectedBandNos, null, null);
                    }
                }
                TileBitmap tb = new TileBitmap();
                tb.Level  = level;
                tb.Tile   = tile;
                tb.Bitmap = BuildBitmap(buffers, level, tile);
                return(tb);
            }
        }
Ejemplo n.º 21
0
 public TileBitmap CreateBitmapByTile(LevelDef level, int beginRow, int beginCol, int width, int height, TileIdentify tile, out bool memoryIsNotEnough)
 {
     memoryIsNotEnough = false;
     try
     {
         T[][] buffers = null;
         bool  isOK    = ReadRasterFile(out buffers, beginRow, beginCol, width, height, tile);
         if (!isOK)
         {
             return(null);
         }
         TileBitmap tb = new TileBitmap();
         tb.Level  = level;
         tb.Tile   = tile;
         tb.Bitmap = BuildBitmap(buffers, level, tile);
         return(tb);
     }
     catch (OutOfMemoryException)
     {
         memoryIsNotEnough = true;
     }
     catch (ArgumentException)
     {
         memoryIsNotEnough = true;
     }
     return(null);
 }
Ejemplo n.º 22
0
        public TileBitmap Get(int levelNo, int row, int col)
        {
            int key = TileIdentify.GetTileNo(levelNo, row, col);

            return(_cachedTiles.ContainsKey(key) ? _cachedTiles[key] : new TileBitmap());
        }
Ejemplo n.º 23
0
 public Tile(TileIdentify id, int bandNo, T[] data)
 {
     _id     = id;
     _bandNo = bandNo;
     _data   = data;
 }
Ejemplo n.º 24
0
 public bool IsExist(TileIdentify tile)
 {
     return(_cachedTiles.ContainsKey(tile.TileNo));
 }
Ejemplo n.º 25
0
        public TileIdentify[] GetTileIdentifies(int width, int height, out int rowCount, out int colCount)
        {
            int hPlaceholders     = GetPlaceholder(width);
            int hPlaceholderLeft  = hPlaceholders / 2;
            int hPlaceholderRight = hPlaceholderLeft;

            if (hPlaceholders % 2 != 0)
            {
                hPlaceholderRight++;
            }
            //
            int vPlaceholders      = GetPlaceholder(height);
            int vPlaceholderTop    = vPlaceholders / 2;
            int vPlaceholderBottom = vPlaceholderTop;

            if (vPlaceholders % 2 != 0)
            {
                vPlaceholderBottom++;
            }
            //
            int sizeX = width + hPlaceholderLeft + hPlaceholderRight;
            int sizeY = height + vPlaceholderTop + vPlaceholderBottom;

            rowCount = sizeY / _tileSize;
            colCount = sizeX / _tileSize;
            int beginRow = 0, beginCol = 0, dataWidth = 0, dataHeight = 0;
            int offsetX = 0, offsetY = 0;
            List <TileIdentify> tiles = new List <TileIdentify>();

            beginRow = 0;
            beginCol = 0;
            for (int r = 0; r < rowCount; r++, beginRow = r * _tileSize - vPlaceholderTop, dataHeight = _tileSize, beginCol = 0)
            {
                offsetY    = 0;
                dataHeight = _tileSize;
                if (r == 0)
                {
                    offsetY     = vPlaceholderTop;
                    dataHeight -= vPlaceholderTop;
                }
                if (r == rowCount - 1)
                {
                    dataHeight -= vPlaceholderBottom;
                }

                for (int c = 0; c < colCount; c++, beginCol = c * _tileSize - hPlaceholderLeft, dataWidth = _tileSize)
                {
                    offsetX   = 0;
                    dataWidth = _tileSize;
                    if (c == 0)
                    {
                        offsetX    = hPlaceholderLeft;
                        dataWidth -= hPlaceholderLeft;
                    }
                    if (c == colCount - 1)
                    {
                        dataWidth -= hPlaceholderRight;
                    }
                    //
                    TileIdentify tile = new TileIdentify(r, c, offsetX, offsetY, beginRow, beginCol, dataWidth, dataHeight);
                    tiles.Add(tile);
                }
            }
            return(tiles.Count > 0 ? tiles.ToArray() : null);
        }
Ejemplo n.º 26
0
        private bool ReadRasterFile(out T[][] buffers, int offsetY, int offsetX, int xSize, int ySize, TileIdentify tile)
        {
            int size  = tile.Width * tile.Height;
            int bands = _selectedBandNos.Length;

            buffers = new T[bands][];
            for (int i = 0; i < bands; i++)
            {
                //eg:1,2,1
                if (_selectedBandNos.Length == 3)
                {
                    for (int p = 0; p < i; p++)
                    {
                        if (_selectedBandNos[p] == _selectedBandNos[i])
                        {
                            buffers[i] = buffers[p];
                            goto nexti;
                        }
                    }
                }
                buffers[i] = new T[size];
                GCHandle handle = GCHandle.Alloc(buffers[i], GCHandleType.Pinned);
                try
                {
                    //disposed
                    if (_dataProvider == null || i >= _selectedBandNos.Length)
                    {
                        return(false);
                    }
                    _dataProvider.GetRasterBand(_selectedBandNos[i]).Read(offsetX, offsetY, xSize, ySize, handle.AddrOfPinnedObject(), _dataProvider.DataType, tile.Width, tile.Height);
                }
                finally
                {
                    handle.Free();
                }
                nexti :;
            }
            return(true);
        }