/// <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. //Console.WriteLine("Before Read"); myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); IRaster mosRs = (IRaster)mos; IPnt pntSize = new PntClass(); pntSize.SetCoords(pPixelBlock.Width, pPixelBlock.Height); IPixelBlock pb = mosRs.CreatePixelBlock(pntSize); mosRs.Read(pTlc, pb); for (int i = 0; i < pb.Planes; i++) { pPixelBlock.set_SafeArray(i, pb.get_SafeArray(i)); } } catch (Exception exc) { System.Exception myExc = new System.Exception("Exception caught in Read method of mosaic Function. " + exc.Message, exc); Console.WriteLine(exc.ToString()); } }
//分析的主要实现函数 private IRasterLayer Analyze(IRasterLayer pRasterLayer) { IRaster pRaster = pRasterLayer.Raster; IRasterProps rasterProps = (IRasterProps)pRaster; //设置栅格数据起始点 IPnt pBlockSize = new Pnt(); pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height); //选取整个范围 IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize); //左上点坐标 IPnt tlp = new Pnt(); tlp.SetCoords(0, 0); //读入栅格 IRasterBandCollection pRasterBands = pRaster as IRasterBandCollection; IRasterBand pRasterBand = pRasterBands.Item(0); IRawPixels pRawRixels = pRasterBands.Item(0) as IRawPixels; pRawRixels.Read(tlp, pPixelBlock); //将PixBlock的值组成数组 Array pSafeArray = pPixelBlock.get_SafeArray(0) as Array; //Array转数组 double[,] myDoubleArr = new double[pSafeArray.GetLength(0), pSafeArray.GetLength(1)]; for (int i = 0; i < myDoubleArr.GetLength(0); i++) { for (int j = 0; j < myDoubleArr.GetLength(1); j++) { myDoubleArr[i, j] = Convert.ToDouble(pSafeArray.GetValue(i, j)); } } for (int i = 0; i < myDoubleArr.GetLength(0); i++) { for (int j = 0; j < myDoubleArr.GetLength(1); j++) { if (myDoubleArr[i, j] == 255) { myDoubleArr[i, j] = 0; } } } double[,] ZeroArray = GetArray(myDoubleArr, Convert.ToInt32(textBox4.Text)); double[,] OArray = SumArray(ZeroArray, Convert.ToInt32(textBox4.Text)); double[,] LastArray = ReturnLastArray(OArray, Convert.ToInt32(textBox4.Text)); pPixelBlock.set_SafeArray(0, LastArray); //StreamWriter sw = File.AppendText(@"E:\GIS底层实验\WorkSpace\result\arrry.txt"); //for (int y = 0; y < rasterProps.Height; y++) //{ // for (int x = 0; x < rasterProps.Width; x++) // { // //int value = Convert.ToInt32(pSafeArray.GetValue(x, y)); // Byte value = Convert.ToByte(pSafeArray.GetValue(x, y)); // string TxtCon = ("X:" + Convert.ToString(x) + "," + "Y:" + Convert.ToString(y) + "," + "Value" + pSafeArray.GetValue(x, y) + "\n"); // sw.Write(TxtCon); // } //} //sw.Flush(); //sw.Close(); // 编辑raster,将更新的值写入raster中 IRasterEdit rasterEdit = pRaster as IRasterEdit; rasterEdit.Write(tlp, pPixelBlock); rasterEdit.Refresh(); return(pRasterLayer); }
public static IRasterDataset TinToRaster(ITinAdvanced itinAdvanced_0, esriRasterizationType esriRasterizationType_0, string string_0, string string_1, rstPixelType rstPixelType_0, double double_0, IEnvelope ienvelope_0, bool bool_0) { IPoint lowerLeft = ienvelope_0.LowerLeft; lowerLeft.X -= double_0 * 0.5; lowerLeft.Y -= double_0 * 0.5; int num = (int)Math.Round((double)((ienvelope_0.Width / double_0) + 1.0)); int num2 = (int)Math.Round((double)((ienvelope_0.Height / double_0) + 1.0)); IGeoDataset dataset = itinAdvanced_0 as IGeoDataset; ISpatialReference2 spatialReference = dataset.SpatialReference as ISpatialReference2; IRasterDataset dataset2 = CreateRasterSurf(string_0, string_1, "GRID", lowerLeft, num, num2, double_0, double_0, rstPixelType_0, spatialReference, true); IRasterBandCollection bands = dataset2 as IRasterBandCollection; IRawPixels pixels = bands.Item(0) as IRawPixels; ITinSurface pSurface = itinAdvanced_0 as ITinSurface; pSurface.RasterInterpolationMethod = esriSurfaceInterpolationType.esriNaturalNeighborInterpolation; IRasterProps props = pixels as IRasterProps; object noDataValue = props.NoDataValue; IPnt tlc = new DblPnt(); int num3 = 2048; int num4 = 2048; if (num < 2048) { num3 = num; } if (num2 < num4) { num4 = num2; } IPnt size = new DblPnt { X = num3, Y = num4 }; IPixelBlock pxls = pixels.CreatePixelBlock(size); object block = pxls.get_SafeArray(0); IPoint point2 = new Point(); for (int i = 0; i < num2; i += num4) { for (int j = 0; j < num; j += num3) { if ((num - j) < num3) { size.X = num - j; pxls = pixels.CreatePixelBlock(size); block = pxls.get_SafeArray(0); } point2.X = (lowerLeft.X + (j * double_0)) + (double_0 * 0.5); point2.Y = (lowerLeft.Y + ((num2 - i) * double_0)) - (double_0 * 0.5); IGeoDatabaseBridge2 bridge = new GeoDatabaseHelper() as IGeoDatabaseBridge2; bridge.QueryPixelBlock(pSurface, point2.X, point2.Y, double_0, double_0, esriRasterizationType_0, noDataValue, ref block); tlc.X = j; tlc.Y = i; pxls.set_SafeArray(0, block); pixels.Write(tlc, pxls); } bool flag = false; if (size.X != num3) { size.X = num3; flag = true; } if ((num2 - i) < num4) { size.Y = num2 - i; } if (flag) { block = pixels.CreatePixelBlock(size).get_SafeArray(0); } } return(dataset2); }
private void middleRowColumn(IPnt loc) { double x = loc.X; double y = loc.Y; string outLocStr = x.ToString() + ":" + y.ToString(); IPnt readLoc = new PntClass(); readLoc.SetCoords(x - 1, y - 1); IRasterEdit regRsE = (IRasterEdit)OutRaster; IPnt writePbSize = new PntClass(); writePbSize.SetCoords(pbwidth, pbheight); IPnt readPbSize = new PntClass(); readPbSize.SetCoords(pbwidth + 2, pbheight + 2); IPixelBlock pb = inRs.CreatePixelBlock(readPbSize); IPixelBlock pb2 = OutRaster.CreatePixelBlock(writePbSize); System.Array[] inOutArr; System.Array inArr = null; System.Array outArr = null; if (outArrDic.TryGetValue(outLocStr, out inOutArr)) { inArr = inOutArr[0]; outArr = inOutArr[1]; } else { OutRaster.Read(loc, pb2); inRs.Read(readLoc, pb); OutRaster.Read(loc, pb2); inArr = (System.Array)pb.get_SafeArray(0); outArr = (System.Array)pb2.get_SafeArray(0); outArrDic[outLocStr] = new System.Array[] { inArr, outArr }; } int height = pb2.Height; int width = pb2.Width; for (int c = 0; c < width; c++) { int ic = c + 1; for (int r = 0; r < height; r++) { List <string> cr = new List <string>(); cr.Add(c.ToString() + ":" + r.ToString()); int ir = r + 1; int inVl = System.Convert.ToInt32(inArr.GetValue(ic, ir)); //Console.WriteLine("Invalue = " + inVl.ToString()); if ((inVl == noDataVl) || (inVl == (noDataVl - 1))) { Console.WriteLine("Invalue = " + inVl.ToString()); continue; } else { int outVl32 = System.Convert.ToInt32(outArr.GetValue(c, r)); if (outVl32 == noDataVl2) { rCnt = 0; rPerm = 0; outArr.SetValue(counter, c, r); List <int>[] nextArray = { new List <int>(), new List <int>(), new List <int>(), new List <int>() };//determines if the next pixel block must be queried {left,top,right,bottom} while (cr.Count > 0) { rCnt++; rPerm += findRegion(inVl, counter, noDataVl2, inArr, outArr, cr, nextArray); } for (int i = 0; i < nextArray.Length; i++) { List <int> pbNextLst = nextArray[i]; if (pbNextLst.Count > 0) { int[] startClms = new int[pbNextLst.Count]; int[] startRws = new int[pbNextLst.Count]; IPnt newLoc = new PntClass(); double nClP = loc.X; double nRwP = loc.Y; switch (i) { case 0: nClP = nClP - pbwidth; startRws = pbNextLst.ToArray(); int stcl = pbwidth - 1; for (int k = 0; k < startRws.Length; k++) { startClms[k] = stcl; } break; case 1: nRwP = nRwP - pbheight; startClms = pbNextLst.ToArray(); //rws=pbHeight-1 int strw = pbheight - 1; for (int k = 0; k < startClms.Length; k++) { startRws[k] = strw; } break; case 2: nClP = nClP + pbwidth; startRws = pbNextLst.ToArray(); //clms=0; break; default: nRwP = nRwP + pbheight; startClms = pbNextLst.ToArray(); //rws = 0; break; } if ((nClP >= 0 && nRwP >= 0 & nClP <= rsProps2.Width && nRwP <= rsProps2.Height)) { newLoc.SetCoords(nClP, nRwP); middleRowColumn(newLoc, startRws, startClms); } } } IRow row = vatTable.CreateRow(); row.set_Value(valueIndex, counter); row.set_Value(countIndex, rCnt); row.set_Value(permIndex, rPerm); row.Store(); counter++; } else { } } } } pb2.set_SafeArray(0, (System.Array)outArr); regRsE.Write(loc, pb2); outArrDic.Remove(outLocStr); }
/// <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. //IRasterProps rsP = (IRasterProps)pRaster; //rsP.PixelType = convPType; myFunctionHelper.Read(pTlc, null, pRaster, pPixelBlock); for (int p = 0; p < pPixelBlock.Planes; p++) { System.Array outArr = (System.Array)pPixelBlock.get_SafeArray(p); for (int r = 0; r < pPixelBlock.Height; r++) { for (int c = 0; c < pPixelBlock.Width; c++) { object vlObj = pPixelBlock.GetVal(p, c, r); if (vlObj != null) { object newVl = convertVl(vlObj); outArr.SetValue(newVl, c, r); } else { outArr.SetValue(null, c, r); } } } pPixelBlock.set_SafeArray(p, outArr); } //int pBHeight = pPixelBlock.Height; //int pBWidth = pPixelBlock.Width; //IPnt pbSize = new PntClass(); //pbSize.SetCoords(pBWidth, pBHeight); //IRasterBandCollection rsBc = (IRasterBandCollection)inrs; //for (int p = 0; p < pPixelBlock.Planes; p++) //{ // IRasterBand rsB = rsBc.Item(p); // IRawPixels rP = (IRawPixels)rsB; // IPixelBlock pb = rP.CreatePixelBlock(pbSize); // rP.Read(pTlc, pb); // System.Array outArr = (System.Array)((IPixelBlock3)pPixelBlock).get_PixelData(p); // for (int r = 0; r < pBHeight; r++) // { // for (int c = 0; c < pBWidth; c++) // { // object outVlObj = pb.GetVal(0, c, r); // if (outVlObj != null) // { // object adVl = convertVl(outVlObj); // outArr.SetValue(adVl, c, r); // } // else // { // //Console.WriteLine("outVlObj = null for r,c " + r.ToString() + ", " + c.ToString()); // } // } // } // ((IPixelBlock3)pPixelBlock).set_PixelData(p, 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; } }