Beispiel #1
0
        private void CalcRasterOffset()
        {
            CoordEnvelope oEnvelope = _raster.CoordEnvelope;
            CoordEnvelope tEnvelope = _vHeader.CoordEnvelope;
            Size          oSize = new Size(_raster.Width, _raster.Height);
            Size          tSize = new Size(_vHeader.Width, _vHeader.Height);
            float         xScale = _raster.ResolutionX / _vHeader.ResolutionX;
            float         yScale = _raster.ResolutionY / _vHeader.ResolutionY;
            Rectangle     rectDst, srcInDst, intInDst, intInSrc;
            bool          isInternal = RasterRectOffset.ComputeBeginEndRowCol(
                oEnvelope, oSize, new PointF(_raster.ResolutionX, _raster.ResolutionY),
                tEnvelope, tSize, new PointF(_vHeader.ResolutionX, _vHeader.ResolutionY),
                out rectDst, out srcInDst, out intInDst, out intInSrc);

            if (!isInternal)
            {
                ;//不相交...
            }
            _inOffset                  = new RasterRectOffset();
            _inOffset.rectDst          = rectDst;
            _inOffset.srcInDst         = srcInDst;
            _inOffset.intInDst         = intInDst;
            _inOffset.intInSrc         = intInSrc;
            _inOffset.ResolutionXScale = xScale;
            _inOffset.ResolutionYScale = yScale;
        }
Beispiel #2
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);
        }
Beispiel #3
0
        /// <summary>
        /// 计算进一步偏移分块后的偏移结果
        /// 传入的参数都是对目标的偏移。
        /// </summary>
        /// <param name="vOffsetX"></param>
        /// <param name="vOffsetY"></param>
        /// <param name="vSizex"></param>
        /// <param name="vSizey"></param>
        /// <returns></returns>
        internal RasterRectOffset Offset(int vOffsetX, int vOffsetY, int vSizex, int vSizey)
        {
            //新的坐标基准
            Rectangle newRectDst  = new Rectangle(0, 0, vSizex, vSizey);
            int       left        = srcInDst.Left - vOffsetX;
            int       top         = srcInDst.Top - vOffsetY;
            int       width       = srcInDst.Width;
            int       height      = srcInDst.Height;
            Rectangle newsrcInDst = new Rectangle(left, top, width, height);
            Rectangle newintInDst = Rectangle.Intersect(newRectDst, newsrcInDst);
            Rectangle newintInSrc = new Rectangle(newintInDst.Left - newsrcInDst.Left, newintInDst.Top - newsrcInDst.Top, newintInDst.Width, newintInDst.Height);

            RasterRectOffset off = new RasterRectOffset();

            off.rectDst          = newRectDst;
            off.srcInDst         = newsrcInDst;
            off.intInDst         = newintInDst;
            off.intInSrc         = newintInSrc;
            off.ResolutionXScale = this.ResolutionXScale;
            off.ResolutionYScale = this.ResolutionYScale;
            return(off);
        }
Beispiel #4
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();
                }
            }
        }