Ejemplo n.º 1
0
        public static VirtualRasterHeader Create(CoordEnvelope coordEnvelope, float resolutionX, float resolutionY)
        {
            VirtualRasterHeader vr = new VirtualRasterHeader();

            vr.CoordEnvelope = coordEnvelope;
            vr.ResolutionX   = resolutionX;
            vr.ResolutionY   = resolutionY;
            vr.Width         = RasterRectOffset.GetInteger(coordEnvelope.Width / resolutionX);
            vr.Height        = RasterRectOffset.GetInteger(coordEnvelope.Height / resolutionY);
            return(vr);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 修改日期2013年1月3日
        /// 修改内容:
        /// 取相交区域,
        /// if (tWidth == intInSrc.Width && intInSrc.Height == tHeight)即分辨率一致时候,后面的读取参数偏移量应当使用前面修正后的toffsetx, toffsety
        /// 应为toffsetx, toffsety可能和intInSrc.X,intInSrc.Y 有偏差(由于浮点运算的缘故)。
        /// 读取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bandNo"></param>
        /// <param name="vOffsetX"></param>
        /// <param name="vOffsetY"></param>
        /// <param name="vSizex"></param>
        /// <param name="vSizey"></param>
        /// <param name="nullValue">非相交区域数据填充值(默认为default(T))</param>
        /// <returns></returns>
        public T[] ReadData <T>(int bandNo, int vOffsetX, int vOffsetY, int vSizex, int vSizey, T nullValue)
        {
            if (_bandMaps != null)
            {
                bandNo = _bandMaps[bandNo - 1];
            }
            if (vOffsetX < 0 || vOffsetY < 0 || vSizex > _vHeader.Width || vSizey > _vHeader.Height)
            {
                return(null);
            }
            GCHandle buffer = new GCHandle();
            IntPtr   bufferPtr;

            try
            {
                T            df           = default(T);
                Type         type         = df.GetType();
                enumDataType dataType     = GetDataTypeFrom(type);// _raster.DataType;
                int          dataTypeSize = Marshal.SizeOf(type);
                T[]          vBufferData  = null;
                //计算当前分块的位置
                RasterRectOffset offset   = _inOffset.Offset(vOffsetX, vOffsetY, vSizex, vSizey);
                Rectangle        rectDst  = offset.rectDst;
                Rectangle        srcInDst = offset.srcInDst;
                Rectangle        intInSrc = offset.intInSrc;
                Rectangle        intInDst = offset.intInDst;
                vBufferData = new T[intInDst.Width * intInDst.Height];
                buffer      = GetHandles(vBufferData);
                bufferPtr   = buffer.AddrOfPinnedObject();
                //获取相交区域的数据
                int toffsetx = RasterRectOffset.GetInteger(intInSrc.X / offset.ResolutionXScale);
                int toffsety = RasterRectOffset.GetInteger(intInSrc.Y / offset.ResolutionYScale);

                #region by chennan 20140812 分辨率不同,切块剩余行数小于分辨率放大倍数时,误判断为不相交

                if (intInSrc.Width == 0 || intInSrc.Height == 0)//当前分块不相交,造成无数据
                {
                    return(null);
                }
                int tWidth  = RasterRectOffset.GetWHInteger(intInSrc.Width / offset.ResolutionXScale);
                int tHeight = RasterRectOffset.GetWHInteger(intInSrc.Height / offset.ResolutionYScale);

                //int tWidth = RasterRectOffset.GetInteger(intInSrc.Width / offset.ResolutionXScale);
                //int tHeight = RasterRectOffset.GetInteger(intInSrc.Height / offset.ResolutionYScale);
                //if (tWidth == 0 || tHeight == 0)//当前分块不相交,造成无数据
                //    return null;

                #endregion

                if (tWidth + toffsetx > _raster.Width)
                {
                    tWidth = _raster.Width - toffsetx;
                }
                if (tHeight + toffsety > _raster.Height)
                {
                    tHeight = _raster.Height - toffsety;
                }
                _raster.GetRasterBand(bandNo).Read(toffsetx, toffsety, tWidth, tHeight, bufferPtr, dataType, intInSrc.Width, intInSrc.Height);
                //如果相交区域即是目标区域
                if (intInDst == rectDst)
                {
                    return(vBufferData);
                }
                else//按行列填充目标区域
                {
                    int readH     = intInDst.Height;
                    int readW     = intInDst.Width;
                    int padLeft   = intInDst.Left;
                    int padRight  = (vSizey + vOffsetY) - intInDst.Right;
                    int topOffset = intInDst.Top - rectDst.Top;

                    T[] tdata = new T[vSizex * vSizey];
                    if (!nullValue.Equals(df))
                    {
                        for (int i = 0; i < vSizex * vSizey; i++)
                        {
                            tdata[i] = nullValue;
                        }
                    }
                    for (int readLine = 0; readLine < readH; readLine++)//按行拷贝
                    {
                        int srcOffset = readLine * readW;
                        int dstOffset = (topOffset + readLine) * vSizex + padLeft;
                        Array.Copy(vBufferData, srcOffset, tdata, dstOffset, readW);
                    }
                    return(tdata);
                }
            }
            finally
            {
                if (buffer.IsAllocated)
                {
                    buffer.Free();
                }
            }
        }