public static float[,] Raster2Mat(IRasterLayer rasterlayer) //将栅格数据转为二元数组 { IRaster raster = rasterlayer.Raster; IRaster2 raster2 = raster as IRaster2; IRasterProps pRasterProps = (IRasterProps)raster; IPnt pntstart = new DblPntClass(); pntstart.SetCoords(0, 0); IPnt pntend = new DblPntClass(); pntend.SetCoords(pRasterProps.Width, pRasterProps.Height); IPixelBlock3 unionPixelBlock = (IPixelBlock3)raster.CreatePixelBlock(pntend); System.Single[,] floatMat; try { raster.Read(pntstart, (IPixelBlock)unionPixelBlock); floatMat = (System.Single[, ])unionPixelBlock.get_PixelData(0); } catch (Exception e) { raster.Read(pntstart, (IPixelBlock)unionPixelBlock); int[,] intMat = (int[, ])unionPixelBlock.get_PixelData(0); floatMat = new System.Single[pRasterProps.Width, pRasterProps.Height]; Parallel.For(0, pRasterProps.Width, i => { for (int j = 0; j < pRasterProps.Height; j++) { floatMat[i, j] = Convert.ToSingle(intMat[i, j]); } }); } return(floatMat); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); //IPixelBlock3 inVlsPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); //myFunctionHelperCoef.Read(pTlc,null,myFunctionHelperCoef.Raster, (IPixelBlock)inVlsPb); IPixelBlock3 outPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outArr = (System.Array)outPixelBlock.get_PixelData(0); System.Array[] inArr = new System.Array[inrs.RasterInfo.BandCount]; IRasterBandCollection rsBc = (IRasterBandCollection)inrs; for (int b = 0; b < inrs.RasterInfo.BandCount; b++) { IRasterBand rsB = rsBc.Item(b); IRawPixels rPix = (IRawPixels)rsB; IPixelBlock pb = rPix.CreatePixelBlock(pbSize); rPix.Read(pTlc, pb); inArr[b] = (System.Array)pb.get_SafeArray(b); } updateOutArr(outPixelBlock, inArr, outArr); outPixelBlock.set_PixelData(0, outArr); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public void Write(float?[,] rasterValue, string format) { FileHelper.DeleteFile(_workSpace, _fileName, ".tif", ".tfw", ".tif.aux"); IRasterWorkspace2 rasterWs = OpenRasterWorkspace(); if (rasterWs == null) { throw new NullReferenceException("栅格文件打开失败"); } IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(_fileName + ".tif", format, RasterInfo.OriginPoint, RasterInfo.Width, RasterInfo.Height, RasterInfo.XCellSize, RasterInfo.YCellSize, 1, rstPixelType.PT_FLOAT, RasterInfo.SpatialReference, true); IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset; var rasterBand = rasterBands.Item(0); var rasterProps = (IRasterProps)rasterBand; //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band. rasterProps.NoDataValue = -1; //Create a raster from the dataset. IRaster raster = rasterDataset.CreateDefaultRaster(); //Create a pixel block. IPnt blocksize = new PntClass(); blocksize.SetCoords(RasterInfo.Width, RasterInfo.Height); IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3; //Populate some pixel values to the pixel block. var pixels = (Array)pixelblock.get_PixelData(0); for (int i = 0; i < RasterInfo.Width; i++) { for (int j = 0; j < RasterInfo.Height; j++) { if (rasterValue[i, j].HasValue) { pixels.SetValue((float)rasterValue[i, j], i, j); } else { pixels.SetValue(-1, i, j); } } } pixelblock.set_PixelData(0, pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(0, 0); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //Release rasterEdit explicitly. Marshal.ReleaseComObject(rasterEdit); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. //System.Array noDataValueArr = (System.Array)((IRasterProps)inrsBandsCoef).NoDataValue; //Console.WriteLine("Before Read"); myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); //Console.WriteLine("After Read"); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)inrsBandsCoef.CreatePixelBlock(pbSize);//independent variables inrsBandsCoef.Read(pTlc, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; //System.Array[] pArr = new System.Array[outPb.Planes]; //for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) //{ // System.Array pixelValues = (System.Array)(outPb.get_PixelData(coefnBand)); // pArr[coefnBand] = pixelValues; //} System.Array pValues = (System.Array)ipPixelBlock.get_PixelData(0);//(System.Array)(td); for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { double[] xVls = new double[outPb.Planes]; bool ndT = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(coefnBand)); object pObj = outPb.GetVal(coefnBand, k, i); if (pObj == null) { ndT = false; break; } float pixelValue = Convert.ToSingle(pObj); xVls[coefnBand] = pixelValue; } if (ndT) { int c = cluster.computNew(xVls); pValues.SetValue(c, k, i); } } } ipPixelBlock.set_PixelData(0, pValues); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of Cluster Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
/// <summary> /// this method reads values from the current block of pixels of the raster /// </summary> /// <param name="pixelblock3">current block of pixels</param> protected override void DoOperationsWithPixels(IPixelBlock3 pixelblock3) { blockwidth = pixelblock3.Width; blockheight = pixelblock3.Height; pixelblock3.Mask(255); //Get the pixel array. pixels = (System.Array)pixelblock3.get_PixelData(0); //iterate through each line in an image for (long i = 0; i < blockwidth; i++) { //iterate through each pixel in the line for (long j = 0; j < blockheight; j++) { double val; //Get the pixel value v = pixels.GetValue(i, j); val = Convert.ToDouble(v.ToString()); if (val > 240) { val = 0; //if value is higher than 240, then value is probaly incorrect } //write pixel value to the array allBlocks[numOfBlock, i + j * 64] = val; } } }
private object splitArray(int[] vls, IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int cnt = 0; System.Array outArr = (System.Array)pb3.get_PixelData(0); rstPixelType rsp = pb3.get_PixelType(0); foreach (int i in vls) { double div = System.Convert.ToDouble(cnt) / width; int r = (int)div; int c = cnt - (r * width); try { object newvl = rasterUtil.getSafeValue(i, rsp); outArr.SetValue(i, c, r); } catch { object newvl = rasterUtil.getSafeValue(900, rsp); outArr.SetValue(900, c, r); } cnt++; } return(outArr); }
/// <summary> /// this method writes values to the current block of pixels of the raster /// </summary> /// <param name="pixelblock3">current block of pixels</param> protected override void DoOperationsWithPixels(IPixelBlock3 pixelblock3) { //Get the pixel array. pixels = (System.Array)pixelblock3.get_PixelData(0); blockwidth = pixelblock3.Width; blockheight = pixelblock3.Height; pixelblock3.Mask(255); object k = null; //iterate through each line in an image for (int i = 0; i < blockwidth; i++) { //iterate through each pixel in the line for (int j = 0; j < blockheight; j++) { k = Convert.ToSingle(blocksWithValues[numOfBlock, i + j * 64]); pixels.SetValue(k, i, j); } } //Set the pixel array to the pixel block. pixelblock3.set_PixelData(0, pixels); //Write back to the raster. tlc = rasterCursor.TopLeft; rasterEdit.Write(tlc, (IPixelBlock)pixelblock3); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 pPixelBlock3 = (IPixelBlock3)pPixelBlock; IRaster2 inRs2 = (IRaster2)inrs; IRaster2 outRs2 = (IRaster2)outrs; double mx, my; outRs2.PixelToMap((int)pTlc.X, (int)pTlc.Y, out mx, out my); int clm, rw; inRs2.MapToPixel(mx, my, out clm, out rw); IPnt pntLoc = new PntClass(); pntLoc.SetCoords(clm, rw); IPnt pntSize = new PntClass(); pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock3 pBIn = (IPixelBlock3)inrs.CreatePixelBlock(pntSize); inrs.Read(pntLoc, (IPixelBlock)pBIn); for (int p = 0; p < pPixelBlock.Planes; p++) { pPixelBlock3.set_PixelData(p, pBIn.get_PixelData(p)); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { double vl = 0; float pixelValue = 0f; try { System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock; #region Load log object for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); System.Array dArr = (System.Array)pb3.get_PixelData(nBand); for (int r = 0; r < pPixelBlock.Height; r++) { for (int c = 0; c < pPixelBlock.Width; c++) { vl = System.Convert.ToDouble(dArr.GetValue(c, r)); if (rasterUtil.isNullData(System.Convert.ToSingle(vl), noDataValue)) { continue; } pixelValue = System.Convert.ToSingle(getFunctionValue(vl)); dArr.SetValue(pixelValue, c, r); } } pb3.set_PixelData(nBand, dArr); //unsafe //{ // System.Array dArr = (System.Array)pb3.get_PixelData(nBand); // int lng = dArr.Length; // fixed (float* dValue = (float[,])dArr) // { // for (int i = 0; i < lng; i++) // { // pixelValue = *(dValue + i); // if (rasterUtil.isNullData(pixelValue, noDataValue)) // { // continue; // } // pixelValue = System.Convert.ToSingle(getFunctionValue(pixelValue)); // *(dValue + i) = pixelValue; // } // pb3.set_PixelData(nBand, dArr); // } //} } #endregion } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method math Function. " + exc.Message, exc); throw myExc; } }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; List <System.Array[]> inPixelValuesArrLst = new List <System.Array[]>(); List <System.Array> inPixelNoDataArrLst = new List <System.Array>(); IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps; IFeatureCursor fCur = ftrCls.Search(spFlt, false); int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes]; for (int i = 0; i < inputPb.Planes; i++) { inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i); } inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue); inPixelValuesArrLst.Add(inPixelValuesArr); ftr = fCur.NextFeature(); } for (int i = 0; i < outPixelValuesArr.Length; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst); outPixelValuesArr[i].SetValue(vl, c, r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
private bool writeBlockDataToFile(Point2D ptLeftTop, byte[,] dbData, int[] nSize, IRaster pRaster /*, IRasterEdit rasterEdit*/) { if (pRaster == null) { return(false); } try { //Create a pixel block using the weight and height of the raster dataset. //If the raster dataset is large, a smaller pixel block should be used. //Refer to the topic "How to access pixel data using a raster cursor". int nWidth = nSize[0]; int nHeight = nSize[1]; IPnt blocksize = new PntClass(); blocksize.SetCoords(nWidth, nHeight); IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3; //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < nWidth; i++) { for (int j = 0; j < nHeight; j++) { pixels.SetValue(dbData[i, j], i, j); } } pixelblock.set_PixelData(0, (System.Array)pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)pRaster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); rasterEdit.Refresh(); return(true); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error: " + ex.Message); return(false); } GC.Collect(); return(true); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth + clms; int pbBigHt = pBHeight + rws; int l, t; l = (clms - 1) / 2; t = (rws - 1) / 2; //Console.WriteLine("lt = " + (pTlc.X - l).ToString() + ":" + (pTlc.Y - t).ToString()); pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t)); IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig); for (int nBand = 0; nBand < pbBig.Planes; nBand++) { System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); rstPixelType rsp = ipPixelBlock.get_PixelType(nBand); //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object objVl = ipPixelBlock.GetVal(nBand, c, r); if (objVl == null) { continue; } else { float outVl = System.Convert.ToSingle(getTransformedValue(pbBig, c + l, r + t, nBand)); object newVl = rasterUtil.getSafeValue(outVl, rsp); //Console.WriteLine(outVl.ToString()); pixelValues.SetValue(newVl, c, r); } } } ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// 栅格操作修改栅格的值 /// </summary> /// <param name="pRasterDataset2"></param> public static void ChangeRasterValue(IRasterDataset2 pRasterDataset2) { //设置读取栅格的大小 IRaster2 pRaster2 = pRasterDataset2.CreateFullRaster() as IRaster2; IPnt pPntBlock = new PntClass(); pPntBlock.X = 128; pPntBlock.Y = 128; IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(pPntBlock); IRasterEdit pRasterEdit = pRaster2 as IRasterEdit; if (pRasterEdit.CanEdit()) { //循环波段,长和宽度 IRasterBandCollection pBands = pRasterDataset2 as IRasterBandCollection; IPixelBlock3 pPixelblock3 = null; int pBlockwidth = 0; int pBlockheight = 0; System.Array pixels; object pValue; long pBandCount = pBands.Count; // IRasterProps pRasterProps = pRaster2 as IRasterProps; object nodata = pRasterProps.NoDataValue; do { pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3; pBlockwidth = pPixelblock3.Width; pBlockheight = pPixelblock3.Height; for (int k = 0; k < pBandCount; k++) { pixels = (System.Array)pPixelblock3.get_PixelData(k); for (int i = 0; i < pBlockwidth; i++) { for (int j = 0; j < pBlockheight; j++) { pValue = pixels.GetValue(i, j); int value = Convert.ToInt32(pValue); if (Convert.ToInt32(pValue) != 3) { pixels.SetValue(Convert.ToByte(0), i, j); } } } pPixelblock3.set_PixelData(k, pixels); } pPntBlock = pRasterCursor.TopLeft; pRasterEdit.Write(pPntBlock, (IPixelBlock)pPixelblock3); } while (pRasterCursor.Next()); System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit); } }
//计算栅格数据块的高程值 public void CalPixelArray(ITinSurface pTinSurf, double UpLeftX, double UpLeftY, double XCellsize, double YCellsize, ref IPixelBlock3 pixelblock) { System.Array pArray = pixelblock.get_PixelData(0) as System.Array; for (int i = 0; i < pixelblock.Width; i++) { for (int j = 0; j < pixelblock.Height; j++) { double dheight = (double)pTinSurf.get_Z(UpLeftX + i * XCellsize, UpLeftY - j * YCellsize); pArray.SetValue(dheight, i, j); } } pixelblock.set_PixelData(0, pArray); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth + clms; int pbBigHt = pBHeight + rws; int l, t; l = clms / 2; t = rws / 2; pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X - l), (pTlc.Y - t)); IPixelBlock3 pbBig = (IPixelBlock3)myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, (IPixelBlock)pbBig); noDataValue = float.MinValue;//System.Convert.ToSingle(noDataValueArr.GetValue(0)); for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++) { rstPixelType pbt = ipPixelBlock.get_PixelType(nBand); System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object inVlobj = ipPixelBlock.GetVal(nBand, c, r); if (inVlobj == null) { continue; } else { updateGLCMDic(pbBig, c, r, nBand); float outVl = System.Convert.ToSingle(getTransformedValue(countDic)); object newVl = rasterUtil.getSafeValue(outVl, pbt); pixelValues.SetValue(newVl, c, r); } } } ((IPixelBlock3)pPixelBlock).set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] inArr = new System.Array[inrsBands.RasterInfo.BandCount]; IRasterBandCollection rsBc = (IRasterBandCollection)inrsBands; for (int p = 0; p < inArr.Length; p++) { IRasterBand rsB = rsBc.Item(p); IRawPixels rP = (IRawPixels)rsB; IPixelBlock pb = rP.CreatePixelBlock(pbSize); IPixelBlock3 pb3 = (IPixelBlock3)pb; rP.Read(pTlc, pb); inArr[p] = (System.Array)pb3.get_PixelData(0); } System.Array outArr = (System.Array)pPixelBlock.get_SafeArray(0); rstPixelType rsPt = pPixelBlock.get_PixelType(0); for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object outVlObj = ipPixelBlock.GetVal(0, c, r); if (outVlObj != null) { float outVl; if (getOutPutVl(inArr, c, r, out outVl)) { object newVl = rasterUtil.getSafeValue(outVl, rsPt);//convertVl(outVl,rsPt); outArr.SetValue(newVl, c, r); } } } } ipPixelBlock.set_PixelData(0, outArr); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc); Console.Write(exc.ToString()); throw myExc; } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); //System.Windows.Forms.MessageBox.Show("Read resized raster"); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; IPnt pbBigSize = new PntClass(); IPnt pbBigLoc = new PntClass(); int pbBigWd = pBWidth * cells; int pbBigHt = pBHeight * cells; pbBigSize.SetCoords(pbBigWd, pbBigHt); pbBigLoc.SetCoords((pTlc.X * cells), (pTlc.Y * cells)); IPixelBlock pbOrig = myFunctionHelperOrig.Raster.CreatePixelBlock(pbBigSize); //System.Windows.Forms.MessageBox.Show("Read original Raster"); myFunctionHelperOrig.Read(pbBigLoc, null, myFunctionHelperOrig.Raster, pbOrig); IPixelBlock3 pbBig = (IPixelBlock3)pbOrig; //int cnt = 0; for (int nBand = 0; nBand < ipPixelBlock.Planes; nBand++) { //Console.WriteLine(ipPixelBlock.get_PixelType(nBand).ToString()); //object noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); //System.Array pixelValuesBig = (System.Array)(pbBig.get_PixelData(nBand)); rstPixelType pTy = pbBig.get_PixelType(nBand); System.Array pixelValues = (System.Array)(ipPixelBlock.get_PixelData(nBand)); for (int r = 0; r < pBHeight; r++) { for (int c = 0; c < pBWidth; c++) { object objVl = ipPixelBlock.GetVal(nBand, c, r); if (objVl != null) { object outVl = getTransformedValue(pbBig, nBand, c, r, cells); object newVl = rasterUtil.getSafeValue(outVl, pTy); //System.Windows.Forms.MessageBox.Show(outVl.ToString()); pixelValues.SetValue(newVl, c, r); } } } ipPixelBlock.set_PixelData(nBand, pixelValues); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 outPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)outPb); int pBRowIndex = 0; int pBColIndex = 0; IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outArr = (System.Array)ipPixelBlock.get_PixelData(0); rstPixelType pty = ipPixelBlock.get_PixelType(0); double[] vArr = new double[outPb.Planes]; for (int i = pBRowIndex; i < pBHeight; i++) { for (int k = pBColIndex; k < pBWidth; k++) { bool checkVl = true; for (int coefnBand = 0; coefnBand < outPb.Planes; coefnBand++) { object vlObj = outPb.GetVal(0, k, i); if (vlObj == null) { checkVl = false; break; } vArr[coefnBand] = System.Convert.ToDouble(vlObj); } if (checkVl) { double tVl = glm.computeNew(vArr); object newVl = rasterUtil.getSafeValue(tVl, pty); outArr.SetValue(newVl, k, i); } } } ipPixelBlock.set_PixelData(0, outArr); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of GLM Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
public bool readBlockDataToFile(Point2D ptLeftTop, ref double[,] dbData, IRaster pRaster /*, IRasterEdit rasterEdit*/) { if (pRaster == null) { return(false); } try { //Create a pixel block using the weight and height of the raster dataset. //If the raster dataset is large, a smaller pixel block should be used. //Refer to the topic "How to access pixel data using a raster cursor". int nWidth = dbData.GetLength(0); int nHeight = dbData.GetLength(1); IPnt blocksize = new PntClass(); blocksize.SetCoords(nWidth, nHeight); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y); IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3; pRaster.Read(upperLeft, pixelblock as IPixelBlock); //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < nWidth; i++) { for (int j = 0; j < nHeight; j++) { dbData[i, j] = Convert.ToDouble(pixels.GetValue(i, j));//.GetType().; } } return(true); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error: " + ex.Message); return(false); } GC.Collect(); return(true); }
public void CalRasterAtt(IRaster2 pRaster2, ref double dmax, ref double dmin) { double pixelmax = -999999999; //记录栅格回的最大值 double pixermin = 999999999; //记录栅格灰度最小值 object obj = null; double dPixel = 0.0; int nWidth = 0; int nHeight = 0; System.Array pixels; IPixelBlock3 pixelBlock3 = null; IRasterCursor rasterCursor = pRaster2.CreateCursorEx(null);//null时为128*128 do { pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3; nWidth = pixelBlock3.Width; nHeight = pixelBlock3.Height; pixels = (System.Array)pixelBlock3.get_PixelData(0); for (int i = 0; i < nWidth; i++) { for (int j = 0; j < nHeight; j++) { obj = pixels.GetValue(i, j); double ob = Convert.ToDouble(obj); if (ob > -99999 && ob < 99999) { dPixel = Convert.ToDouble(obj); if (dPixel >= pixelmax) { pixelmax = dPixel; } if (dPixel <= pixermin) { pixermin = dPixel; } } } } } while (rasterCursor.Next() == true); dmax = pixelmax; dmin = pixermin; }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); #region Load log object int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; Pnt pbSize = new PntClass(); pbSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array outPutArr = (System.Array)ipPixelBlock.get_PixelData(0); IPixelBlock3 CoefPb = (IPixelBlock3)myFunctionHelperCoef.Raster.CreatePixelBlock(pbSize); myFunctionHelperCoef.Read(pTlc, null, myFunctionHelperCoef.Raster, (IPixelBlock)CoefPb); for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object vlObj = CoefPb.GetVal(0, c, r); if (vlObj != null) { if ((float)vlObj > 0) { vlObj = CoefPb.GetVal(1, c, r); } else { vlObj = CoefPb.GetVal(2, c, r); } if (vlObj != null) { outPutArr.SetValue(vlObj, c, r); } } } } ipPixelBlock.set_PixelData(0, outPutArr); #endregion } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of con Function. " + exc.Message, exc); throw myExc; } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { //double vl = 0; //float pixelValue = 0f; try { //System.Array noDataValueArr = (System.Array)((IRasterProps)pRaster).NoDataValue; // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock; #region Load log object for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); System.Array dArr = (System.Array)pb3.get_PixelData(nBand); rstPixelType pbT = pb3.get_PixelType(nBand); for (int r = 0; r < pPixelBlock.Height; r++) { for (int c = 0; c < pPixelBlock.Width; c++) { object objVl = pb3.GetVal(nBand, c, r); if (objVl == null) { continue; } else { //vl = System.Convert.ToSingle(objVl); //pixelValue = (float)getFunctionValue(vl); double objVl2 = getFunctionValue(System.Convert.ToDouble(objVl)); object newVl = rasterUtil.getSafeValue(objVl2, pbT); dArr.SetValue(newVl, c, r); } } } pb3.set_PixelData(nBand, dArr); } #endregion } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method math Function. " + exc.Message, exc); throw myExc; } }
/// <summary> /// TRANSPOSE ARRAY /// </summary> /// <param name="pixelblock">PixelBlock of data values</param> /// <param name="noDataValue">Default NoData value</param> /// <returns>Array of values transposed</returns> internal static Array TransposeArray(IPixelBlock3 pixelblock, object noDataValue) { System.Array oldArray = (System.Array)pixelblock.get_PixelData(0); int cols = oldArray.GetLength(0); int rows = oldArray.GetLength(1); System.Array newArray = System.Array.CreateInstance(oldArray.GetType().GetElementType(), rows, cols); for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { object noDataMaskValue = pixelblock.GetNoDataMaskVal(0, col, row); object pixelValue = (Convert.ToByte(noDataMaskValue, CultureInfo.InvariantCulture) == 1) ? oldArray.GetValue(col, row) : noDataValue; newArray.SetValue(pixelValue, row, col); } } return(newArray); }
public static IRasterDataset exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat) { string directory = parth.Substring(0, parth.LastIndexOf("\\")); string name = parth.Substring(parth.LastIndexOf("\\") + 1); IWorkspaceFactory workspaceFac = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2; IRasterInfo rasterInfo = (rasterLayer.Raster as IRawBlocks).RasterInfo; IPoint originPoint = new Point(); originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y); IRasterProps rasterProps = rasterLayer.Raster as IRasterProps; IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true); IRaster2 raster2 = rasterDataSet.CreateDefaultRaster() as IRaster2; IPnt pntClass = new Pnt(); pntClass.X = rasterProps.Width; pntClass.Y = rasterProps.Height; IRasterCursor rasterCursor = raster2.CreateCursorEx(pntClass); IRasterEdit rasterEdit = raster2 as IRasterEdit; if (rasterEdit.CanEdit()) { IRasterBandCollection bands = rasterDataSet as IRasterBandCollection; IPixelBlock3 pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3; System.Array pixels = (System.Array)pixelBlock3.get_PixelData(0); for (int i = 0; i < rasterProps.Width; i++) { for (int j = 0; j < rasterProps.Height; j++) { pixels.SetValue(Convert.ToSingle(rasterMat[j, i]), i, j); } } pixelBlock3.set_PixelData(0, (System.Array)pixels); rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3); } (raster2 as IRasterProps).NoDataValue = 0f; rasterEdit.Refresh(); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); return(rasterDataSet); }
/// <summary> /// 读取指定块处的像元值到数组. /// </summary> /// <param name="pRasterDS">The p raster ds.</param> /// <param name="pBlockSize0">左上角点.</param> /// <param name="pBlockSize1">块大小.</param> /// <returns>System.Single[].</returns> public static float[,] ReadFloat(IRasterDataset2 pRasterDS, IPnt pBlockSize0, IPnt pBlockSize1) { try { //IRaster pRaster = thisRasterLayer.Raster; IRaster pRaster = pRasterDS.CreateFullRaster(); IRaster2 pRaster2 = (IRaster2)pRaster; IRasterProps pRSp = (IRasterProps)pRaster; IPixelBlock pixelBlock = pRaster2.CreateCursorEx(pBlockSize1).PixelBlock; pRaster.Read(pBlockSize0, pixelBlock); IPixelBlock3 block2 = (IPixelBlock3)pixelBlock; return((float[, ])block2.get_PixelData(0)); } catch (Exception ex) { XtraMessageBox.Show("读取影像失败!\n" + ex.Message); return(null); } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPnt pntSize = new PntClass(); pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock3 pb3 = (IPixelBlock3)pPixelBlock; for (int nBand = 0; nBand < pPixelBlock.Planes; nBand++) { //float noDataValue = System.Convert.ToSingle(noDataValueArr.GetValue(nBand)); object dArr = vPb.get_PixelData(nBand); pb3.set_PixelData(nBand, dArr); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method PixelBlock To Raster Function. " + exc.Message, exc); throw myExc; } }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); int pBHeight = pPixelBlock.Height; int pBWidth = pPixelBlock.Width; IPnt pbSize = new PntClass(); pbSize.SetCoords(pBWidth, pBHeight); IPixelBlock3 inputPb = (IPixelBlock3)myFunctionHelperInput.Raster.CreatePixelBlock(pbSize);//independent variables myFunctionHelperInput.Read(pTlc, null, myFunctionHelperInput.Raster, (IPixelBlock)inputPb); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array pixelValues = (System.Array)ipPixelBlock.get_PixelData(0); createRegions(inputPb, pixelValues); ipPixelBlock.set_PixelData(0, pixelValues); } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of the Region Group function Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
private bool WriteToRaster(IRaster pRaster, byte[,] dbData, Point2D ptLeftTop) { //Create a pixel block using the weight and height of the raster dataset. //If the raster dataset is large, a smaller pixel block should be used. //Refer to the topic "How to access pixel data using a raster cursor". IRasterProps pProps = pRaster as IRasterProps; int width = pProps.Width; int height = pProps.Height; IPnt blocksize = new PntClass(); blocksize.SetCoords(width, height); IPixelBlock3 pixelblock = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3; //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { pixels.SetValue(dbData[i, j], i, j); } } pixelblock.set_PixelData(0, (System.Array)pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)pRaster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); return(true); }
/// <summary> /// Read pixels from the input Raster and fill the PixelBlock provided with processed pixels. /// The RasterFunctionHelper object is used to handle pixel type conversion and resampling. /// The log raster is the natural log of the raster. /// </summary> /// <param name="pTlc">Point to start the reading from in the Raster</param> /// <param name="pRaster">Reference Raster for the PixelBlock</param> /// <param name="pPixelBlock">PixelBlock to be filled in</param> public void Read(IPnt pTlc, IRaster pRaster, IPixelBlock pPixelBlock) { try { // Call Read method of the Raster Function Helper object. myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IPixelBlock3 ipPixelBlock = (IPixelBlock3)pPixelBlock; System.Array[] outArr = new System.Array[bndCnt]; int pbHeight = ipPixelBlock.Height; int pbWidth = ipPixelBlock.Width; for (int p = 0; p < ipPixelBlock.Planes; p++) { outArr[p] = (System.Array)ipPixelBlock.get_PixelData(p); } for (int r = 0; r < pbHeight; r++) { for (int c = 0; c < pbWidth; c++) { object vlObj = ipPixelBlock.GetVal(0, c, r); if (vlObj != null) { float vl = System.Convert.ToSingle(vlObj); getOutPutVl(ipPixelBlock, outArr, vl, c, r); } } } for (int p = 0; p < ipPixelBlock.Planes; p++) { ipPixelBlock.set_PixelData(p, outArr[p]); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of localMean Function. " + exc.Message, exc); throw myExc; } }
/// <summary> /// 一次读取整幅影像像元值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pRasterDS">The p raster ds.</param> /// <returns>T[].</returns> public static T[,] ReadPixelValue <T>(IRasterDataset2 pRasterDS) { try { //IRaster pRaster = thisRasterLayer.Raster; IRaster pRaster = pRasterDS.CreateFullRaster(); IRaster2 pRaster2 = (IRaster2)pRaster; IRasterProps pRSp = (IRasterProps)pRaster; IPnt pBlockSize = new PntClass(); pBlockSize.SetCoords((float)pRSp.Width, (float)pRSp.Height); IPixelBlock pixelBlock = pRaster2.CreateCursorEx(pBlockSize).PixelBlock; pBlockSize.SetCoords(0.0, 0.0); pRaster.Read(pBlockSize, pixelBlock); IPixelBlock3 block2 = (IPixelBlock3)pixelBlock; return((T[, ])block2.get_PixelData(0));; } catch (Exception ex) { XtraMessageBox.Show("读取分类影像失败!\n" + ex.Message); return(null); } }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; List<System.Array[]> inPixelValuesArrLst = new List<System.Array[]>(); List<System.Array> inPixelNoDataArrLst = new List<System.Array>(); IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps; IFeatureCursor fCur = ftrCls.Search(spFlt, false); int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(fIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); System.Array[] inPixelValuesArr = new System.Array[inputPb.Planes]; for (int i = 0; i < inputPb.Planes; i++) { inPixelValuesArr[i] = (System.Array)inputPb.get_SafeArray(i); } inPixelNoDataArrLst.Add((System.Array)((IRasterProps)rs).NoDataValue); inPixelValuesArrLst.Add(inPixelValuesArr); ftr = fCur.NextFeature(); } for (int i = 0; i < outPixelValuesArr.Length; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { double vl = getValue(i, c, r, inPixelValuesArrLst, inPixelNoDataArrLst); outPixelValuesArr[i].SetValue(vl,c,r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
private void updateWithMergedValues(IEnvelope env, IPixelBlock3 ipPixelBlock) { System.Array[] outPixelValuesArr = new System.Array[ipPixelBlock.Planes]; IPnt pntSize = new PntClass(); pntSize.SetCoords(ipPixelBlock.Width, ipPixelBlock.Height); ISpatialFilter spFlt = new SpatialFilterClass(); spFlt.Geometry = (IGeometry)env; spFlt.GeometryField = ftrCls.ShapeFieldName; spFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIndexIntersects; IFeatureCursor fCur = ftrCls.Search(spFlt, false); ////int fIndex = ftrCls.FindField("catIndex"); IFeature ftr = fCur.NextFeature(); for (int i = 0; i < ipPixelBlock.Planes; i++) { outPixelValuesArr[i] = (System.Array)ipPixelBlock.get_PixelData(i); } //IQueryFilter qf = new QueryFilterClass(); //string s1 = "(XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s2 = "(XMIN > " + env.XMin.ToString() + " AND XMIN < " + env.XMax.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s3 = "(XMAX < " + env.XMax.ToString() + " AND XMIN > " + env.XMin.ToString() + " AND YMIN <= " + env.YMin.ToString() + " AND YMAX >= " + env.YMax.ToString() + ")"; //string s4 = "(YMIN > " + env.YMin.ToString() + " AND YMIN < " + env.YMax.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")"; //string s5 = "(YMAX < " + env.YMax.ToString() + " AND YMIN > " + env.YMin.ToString() + " AND XMIN <= " + env.XMin.ToString() + " AND XMAX >= " + env.XMax.ToString() + ")"; //qf.WhereClause = s1 + " OR " + s2 + " OR " + s3 + " OR " + s4 + " OR " + s5; //ICursor cur = tbl.Search(qf, false); //IRow rw = cur.NextRow(); List<IPixelBlock> inPbValue = new List<IPixelBlock>(); //while (rw != null) //{ // int rsIndex = System.Convert.ToInt32(rw.get_Value(catIndex)); // IRaster rs = inrs[rsIndex]; // IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); // IRaster2 rs2 = (IRaster2)rs; // int pClm, pRw; // rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); // IPnt tlc = new PntClass(); // tlc.SetCoords(pClm, pRw); // rs.Read(tlc, inputPb); // inPbValue.Add(inputPb); // rw = cur.NextRow(); //} while (ftr != null) { int rsIndex = System.Convert.ToInt32(ftr.get_Value(catIndex)); IRaster rs = inrs[rsIndex]; IPixelBlock inputPb = rs.CreatePixelBlock(pntSize); IRaster2 rs2 = (IRaster2)rs; int pClm, pRw; rs2.MapToPixel(env.XMin, env.YMax, out pClm, out pRw); IPnt tlc = new PntClass(); tlc.SetCoords(pClm, pRw); rs.Read(tlc, inputPb); inPbValue.Add(inputPb); ftr = fCur.NextFeature(); } for (int i = 0; i < ipPixelBlock.Planes; i++) { for (int r = 0; r < ipPixelBlock.Height; r++) { for (int c = 0; c < ipPixelBlock.Width; c++) { object vl = getValue(i, c, r, inPbValue); outPixelValuesArr[i].SetValue(vl,c,r); } } } for (int i = 0; i < outPixelValuesArr.Length; i++) { ipPixelBlock.set_PixelData(i, outPixelValuesArr[i]); } }
private object splitArray(int[] vls, IPixelBlock3 pb3) { int width = pb3.Width; int height = pb3.Height; int cnt = 0; System.Array outArr = (System.Array)pb3.get_PixelData(0); rstPixelType rsp = pb3.get_PixelType(0); foreach (int i in vls) { double div = System.Convert.ToDouble(cnt) / width; int r = (int)div; int c = cnt - (r * width); try { object newvl = rasterUtil.getSafeValue(i, rsp); outArr.SetValue(i, c, r); } catch { object newvl = rasterUtil.getSafeValue(900, rsp); outArr.SetValue(900, c, r); } cnt++; } return outArr; }