示例#1
0
 public void Apply(IRasterDataProvider dataProvider, int bandNo, int[] aoi)
 {
     if (dataProvider == null)
     {
         return;
     }
     _aoi               = aoi;
     _aoiRect           = AOIHelper.ComputeAOIRect(aoi, new Size(dataProvider.Width, dataProvider.Height));
     _dataProvider      = dataProvider;
     _isNewDataProvider = false;
     txtFileName.Text   = _dataProvider.fileName;
     FillBandNos(dataProvider, bandNo);
     TrySetShpFile();
     //
     if (aoi == null || aoi.Length == 0)
     {
         rdAOI.Enabled       = false;
         rdFullImage.Checked = true;
     }
     else
     {
         rdAOI.Checked            = true;
         txtSamples.SelectedIndex = 0;
     }
 }
示例#2
0
        private const int MAX_BLOCK_SIZE = 1024 * 1024 * 10;//10MB

        public void Compute(IRasterBand[] srcRasters, int[] aoi, RasterQuickStatResult[] results, Action <int, string> progressTracker)
        {
            int       width   = srcRasters[0].Width;
            int       height  = srcRasters[0].Height;
            Rectangle aoiRect = AOIHelper.ComputeAOIRect(aoi, new Size(width, height));
            SortedList <int, AOIHelper.ColRangeOfRow> aoiRowRanges = AOIHelper.ComputeColRanges(aoi, new Size(width, height));

            T[]      buffer    = GetRowBuffer(aoiRowRanges);
            GCHandle handle    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   bufferPtr = handle.AddrOfPinnedObject();

            try
            {
                int cols = 0;
                for (int i = 0; i < srcRasters.Length; i++)
                {
                    foreach (int r in aoiRowRanges.Keys)
                    {
                        AOIHelper.ColRangeOfRow range = aoiRowRanges[r];
                        cols = range.EndCol - range.BeginCol + 1;
                        srcRasters[i].Read(range.BeginCol, range.Row, cols, 1, bufferPtr, srcRasters[i].DataType, cols, 1);
                        HandleHistogram(buffer, results[i], cols);
                    }
                }
                HandleMeanValues(srcRasters.Length, results, aoi.Length);
            }
            finally
            {
                handle.Free();
            }
        }
示例#3
0
        public ContourLine[] Generate(IRasterBand raster, double[] contourValues, int[] aoi, Action <int, string> tracker)
        {
            Size oSize = new Size(raster.Width, raster.Height);
            bool isNewRaster = false;
            int  offsetRow = 0, offsetCol = 0;

            if (aoi != null && aoi.Length > 0)
            {
                //感兴趣区域不采样
                _sample = 1;
                Rectangle rect = AOIHelper.ComputeAOIRect(aoi, oSize);
                offsetRow   = rect.Top;
                offsetCol   = rect.Left;
                raster      = GetAOIRaster(raster, aoi, rect, oSize);
                isNewRaster = true;
            }
            _raster         = raster;
            _width          = raster.Width / _sample;
            _height         = raster.Height / _sample;
            _gridWidth      = _width - 1;
            _gridHeight     = _height - 1;
            _triWidth       = _gridWidth * 2;
            _contourValues  = contourValues;
            _triangleCount  = 2 * _gridWidth * _gridHeight;
            _triangleIsUsed = new StatusRecorder(_triangleCount);
            _edgeCount      = _gridWidth * _height + _gridHeight * _width + _gridWidth * _gridHeight;
            _edgeIsUsed     = new StatusRecorder(_edgeCount);
            try
            {
                if (tracker != null)
                {
                    tracker(0, "正在准备数据...");
                }
                ReadBandValues();
                if (tracker != null)
                {
                    tracker(10, "正在提取边界三角形...");
                }
                GetBorderTriangles();
                if (tracker != null)
                {
                    tracker(12, "正在生成等值线...");
                }
                _contourLines = new List <ContourLine>();
                GenerateContours(tracker);
                if (tracker != null)
                {
                    tracker(100, "等值线生成完毕!");
                }
            }
            finally
            {
                if (isNewRaster)
                {
                    OffsetToFull(_contourLines, offsetRow, offsetCol);
                    raster.Dispose();
                }
            }
            return(_contourLines != null && _contourLines.Count > 0 ? _contourLines.ToArray() : null);
        }
示例#4
0
        /// <summary>
        /// 获取栅格数据的感兴趣区域部分
        /// </summary>
        /// <param name="raster"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        private unsafe IRasterBand GetAOIRaster(IRasterBand raster, int[] aoi, Rectangle rect, Size oSize)
        {
            int offsetRow = rect.Top;
            int offsetCol = rect.Left;
            int oWidth    = oSize.Width;
            int aoiWidth  = rect.Width;
            T   nodata    = GetNoDataValueOutsideAOI();

            T[]      buffer = new T[rect.Width * rect.Height];
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                raster.Read(rect.Left, rect.Top, rect.Width, rect.Height, handle.AddrOfPinnedObject(), raster.DataType, rect.Width, rect.Height);
            }
            finally
            {
                handle.Free();
            }
            //
            if (aoi != null && aoi.Length > 0)
            {
                //将全图AOI索引转换到AOI外包矩形
                fixed(int *ptr0 = aoi)
                {
                    int *ptr      = ptr0;
                    int  aoiCount = aoi.Length;

                    for (int i = 0; i < aoiCount; i++, ptr++)
                    {
                        *ptr = (*ptr / oWidth - offsetRow) * aoiWidth + (*ptr % oWidth - offsetCol);
                    }
                }

                //逆转AOI,将AOI外的像元替换为无效值
                aoi = AOIHelper.Reversal(aoi, new Size(rect.Width, rect.Height));
                if (aoi != null && aoi.Length > 0)
                {
                    fixed(int *ptr0 = aoi)
                    {
                        int *ptr      = ptr0;
                        int  aoiCount = aoi.Length;
                        int  idx      = 0;

                        for (int i = 0; i < aoiCount; i++, ptr++)
                        {
                            idx         = (*ptr / aoiWidth) * aoiWidth + *ptr % aoiWidth;
                            buffer[idx] = nodata;
                        }
                    }
                }
            }
            return(new ArrayRasterBand <T>(1, buffer, rect.Width, rect.Height, null));
        }
示例#5
0
        private void VisitByAOI(int[] aoi, Action <float, float> action, Action <int, string> progressTracker)
        {
            IRasterBand[] srcRasters = new IRasterBand[] { _xBand, _yBand };
            int           width      = _xBand.Width;
            int           height     = _xBand.Height;
            int           bandCount  = 2;
            Rectangle     aoiRect    = AOIHelper.ComputeAOIRect(aoi, new Size(width, height));
            //Dictionary<int, AOIHelper.AOIRowRange> aoiRowRanges = AOIHelper.ComputeRowIndexRange(aoi, new Size(width, height));
            SortedList <int, AOIHelper.ColRangeOfRow> aoiRowRanges = AOIHelper.ComputeColRanges(aoi, new Size(width, height));
            int rowCount = aoiRect.Height;
            int bRow = aoiRect.Top;
            int eRow = aoiRect.Bottom;
            int bCol = 0, eCol = 0;

            T[][]      srcBuffers = new T[bandCount][];
            GCHandle[] srcHandles = new GCHandle[bandCount];
            for (int i = 0; i < bandCount; i++)
            {
                srcBuffers[i] = new T[width];
                srcHandles[i] = GCHandle.Alloc(srcBuffers[i], GCHandleType.Pinned);
            }
            //
            try
            {
                AOIHelper.ColRangeOfRow range = null;
                for (int r = bRow; r < eRow; r++)
                {
                    //读取一行数据
                    for (int b = 0; b < bandCount; b++)
                    {
                        srcRasters[b].Read(0, r, width, 1, srcHandles[b].AddrOfPinnedObject(), _xBand.DataType, width, 1);
                    }
                    //获取该行AOI的起始列
                    if (aoiRowRanges.ContainsKey(r))
                    {
                        range = aoiRowRanges[r];
                    }
                    else
                    {
                        continue;
                    }
                    //调用计算委托
                    bCol = range.BeginCol;
                    eCol = range.EndCol;
                    for (int c = bCol; c < eCol; c++)
                    {
                        action(ToFloat(srcBuffers[0][c]), ToFloat(srcBuffers[1][c]));
                    }
                    //
                    if (progressTracker != null)
                    {
                        progressTracker(100 * (r - bRow + 1) / rowCount, string.Empty);
                    }
                }
            }
            finally
            {
                for (int i = 0; i < bandCount; i++)
                {
                    srcHandles[i].Free();
                }
            }
        }
示例#6
0
        private void ComputeByAOI(IRasterBand[] srcRasters, int[] aoi, out double[] minValues, out double[] maxValues, out double[] meanValues, Action <int, string> progressTracker)
        {
            enumDataType dataType = srcRasters[0].DataType;
            int          width    = srcRasters[0].Width;
            int          height   = srcRasters[0].Height;
            //
            int bandCount = srcRasters.Length;

            minValues  = new double[bandCount];
            maxValues  = new double[bandCount];
            meanValues = new double[bandCount];
            for (int i = 0; i < bandCount; i++)
            {
                minValues[i] = double.MaxValue;
                maxValues[i] = double.MinValue;
            }
            //
            Rectangle aoiRect = AOIHelper.ComputeAOIRect(aoi, new Size(width, height));
            SortedList <int, AOIHelper.ColRangeOfRow> aoiRowRanges = AOIHelper.ComputeColRanges(aoi, new Size(width, height));
            int rowCount = aoiRect.Height;
            int bRow = aoiRect.Top;
            int eRow = aoiRect.Bottom;
            int bCol = 0, eCol = 0;

            T[][]      srcBuffers = new T[bandCount][];
            GCHandle[] srcHandles = new GCHandle[bandCount];
            for (int i = 0; i < bandCount; i++)
            {
                srcBuffers[i] = new T[width];
                srcHandles[i] = GCHandle.Alloc(srcBuffers[i], GCHandleType.Pinned);
            }
            //
            try
            {
                int aoiPixelCount             = 0;
                AOIHelper.ColRangeOfRow range = null;
                for (int r = bRow; r < eRow; r++)
                {
                    try
                    {
                        //读取一行数据
                        for (int b = 0; b < bandCount; b++)
                        {
                            srcRasters[b].Read(0, r, width, 1, srcHandles[b].AddrOfPinnedObject(), dataType, width, 1);
                        }
                        //获取该行AOI的起始列
                        if (aoiRowRanges.ContainsKey(r))
                        {
                            range = aoiRowRanges[r];
                            //调用计算委托
                            bCol = range.BeginCol;
                            eCol = range.EndCol + 1;
                            HandleBlock(bandCount, srcBuffers, bCol, eCol, minValues, maxValues, meanValues);
                            aoiPixelCount += (eCol - bCol);
                            if (progressTracker != null)
                            {
                                progressTracker(100 * (r - bRow + 1) / rowCount, string.Empty);
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }
                }
                HandleMeanValues(bandCount, meanValues, aoiPixelCount);
            }
            finally
            {
                for (int i = 0; i < bandCount; i++)
                {
                    srcHandles[i].Free();
                }
            }
        }