/// <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()); } }
/// <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; } }
private double[][] getValues(IPoint pnt, out double[] rwVls) { rwVls = new double[bndCnt]; double[][] outJagArr = new double[(geoerro * geoerro)][]; for (int i = 0; i < outJagArr.Length; i++) { outJagArr[i] = new double[bndCnt]; } IRasterBandCollection rsBc = (IRasterBandCollection)FunctionRasterDataset; IRasterBand rsB; IPnt pSize = new PntClass(); pSize.SetCoords(geoerro, geoerro); IPnt pLoc = new PntClass(); int clm, rw; IGeometry geo = (IGeometry)FunctionRasterDataset.RasterInfo.Extent; IPoint ul = FunctionRasterDataset.RasterInfo.Extent.UpperLeft; IPnt cellSize = FunctionRasterDataset.RasterInfo.CellSize; int sub = (geoerro / 2); rsUtil.getClmRw(ul, cellSize, pnt, out clm, out rw); int nclm = clm - sub; int nrw = rw - sub; pLoc.SetCoords(System.Convert.ToDouble(nclm), System.Convert.ToDouble(nrw)); for (int i = 0; i < bndCnt; i++) { rsB = rsBc.Item(i); IRawPixels rpix = (IRawPixels)rsB; IPixelBlock pb = rpix.CreatePixelBlock(pSize); rpix.Read(pLoc, pb); int pbCnt = 0; for (int r = 0; r < pb.Height; r++) { for (int c = 0; c < pb.Width; c++) { object vlObj = pb.GetVal(0, c, r); if (vlObj != null) { outJagArr[pbCnt][i] = System.Convert.ToDouble(vlObj); } pbCnt++; } } } rwVls = outJagArr[(geoerro * geoerro / 2)]; return(outJagArr); }
public static IRasterDataset createFileRasterDataset(IRasterWorkspace2 irasterWorkspace2_0, string string_0, int int_0, rstPixelType rstPixelType_0, ISpatialReference ispatialReference_0) { try { IRasterDataset dataset = null; IPoint origin = new Point(); origin.PutCoords(0.0, 0.0); if (ispatialReference_0 == null) { ispatialReference_0 = new UnknownCoordinateSystem() as ISpatialReference; } dataset = irasterWorkspace2_0.CreateRasterDataset(string_0, "IMAGINE Image", origin, 200, 100, 1.0, 1.0, int_0, rstPixelType_0, ispatialReference_0, true); IRawPixels pixels = null; IPixelBlock3 block = null; IPnt tlc = null; IPnt size = null; IRasterBandCollection bands = (IRasterBandCollection)dataset; pixels = (IRawPixels)bands.Item(0); IRasterProps props = (IRasterProps)pixels; tlc = new DblPnt(); tlc.SetCoords(0.0, 0.0); size = new DblPnt(); size.SetCoords((double)props.Width, (double)props.Height); block = (IPixelBlock3)pixels.CreatePixelBlock(size); pixels.Read(tlc, (IPixelBlock)block); object[,] objArray = (object[, ])block.get_PixelDataByRef(0); for (int i = 0; i < props.Width; i++) { for (int j = 0; j < props.Height; j++) { objArray[i, j] = (i * j) % 255; } } object cache = pixels.AcquireCache(); pixels.Write(tlc, (IPixelBlock)block); pixels.ReturnCache(cache); return(dataset); } catch (Exception exception) { Debug.WriteLine(exception.Message); return(null); } }
private void button1_Click(object sender, EventArgs e) { string jieguo2; for (int i = 0; i < frm.mainMapControl.LayerCount; i++) { //////// if (frm.axMapControl1.get_Layer(i).Name == input) //////// { //////// temp = i; //////// break; //////// } ////////} ////////IRasterLayer pRasterLayer = frm.axMapControl1.get_Layer(temp) as IRasterLayer; string inputname = frm.mainMapControl.get_Layer(i).Name; int location2 = inputname.LastIndexOf('.'); string sss2 = inputname; jieguo2 = sss2.Substring(0, location2);////"."后文件名称 IRasterLayer pRasterLayer = frm.mainMapControl.get_Layer(i) as IRasterLayer; IRaster2 raster = pRasterLayer.Raster as IRaster2; System.Array array; //IRaster2 raster = pRasterLayer.Raster as IRaster2; IRasterDataset rasterDataset = raster.RasterDataset; IPoint pPointOrign = new PointClass(); pPointOrign.X = 0; pPointOrign.Y = 0; IPnt pixelBlockOrigin = null; pixelBlockOrigin = new DblPntClass(); pixelBlockOrigin.SetCoords(pPointOrign.X, pPointOrign.Y); IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset; IRasterBand dirRasterBand = dirBandCollection.Item(0); IRawPixels dirRawPixels = (IRawPixels)dirRasterBand; IRasterProps dirProps = (IRasterProps)dirRawPixels; int dirColumns = dirProps.Width; //列数 int dirRows = dirProps.Height; //行数 double cellSizeX = dirProps.MeanCellSize().X; //得到x方向栅格大小 double cellSizeY = dirProps.MeanCellSize().Y; ////得到y方向栅格大小 IPnt dirBlockSize = new DblPntClass(); dirBlockSize.X = dirColumns; dirBlockSize.Y = dirRows; IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize); dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock); array = (System.Array)dirPixelBlock.get_PixelDataByRef(0); string ss = ""; double[,] b = new double[dirRows, dirColumns]; for (int row = dirRows - 1; row < dirRows; row++) { for (int col = 0; col < dirColumns; col++) { b[row, col] = Convert.ToSingle(array.GetValue(col, row)); } } int minnum, maxnum, chuangkou; if (textBox1.Text == "") { minnum = 2; } else { minnum = Convert.ToInt32(textBox1.Text); } if (textBox2.Text == "") { maxnum = 1; } else { maxnum = Convert.ToInt32(textBox2.Text); } chuangkou = minnum; ////////////////////////////////////////////////////////////////////////// if (textBox3.Text == "") { MessageBox.Show("文件保存名不能为空"); } String savename = textBox3.Text + "\\" + jieguo2 + ".txt"; ////////////////////////////////////////////////////////////////////////// for (int c = 0; c < maxnum; c++) { double result, sum, mean, fangcha, ste, a;////////修改程序2011年11月15日 a = 0; ste = 0; sum = 0; fangcha = 0; for (int row = dirRows - 1; row < dirRows; row++) ////窗口大小与图像行列数关系 { for (int col = 0; col < dirColumns - chuangkou + 1; col++) ////窗口大小与图像行列数关系 { for (int k = row; k < row + 1; k++) ////窗口大小与图像栅格和的关系 { for (int l = col; l < col + chuangkou; l++) { sum = sum + b[k, l]; } } mean = sum / (chuangkou);/////一维图像就像是窗口大小 for (int m = row; m < row + 1; m++) { for (int n = col; n < col + chuangkou; n++) { fangcha = fangcha + (b[m, n] - mean) * (b[m, n] - mean); } } result = System.Math.Sqrt(fangcha / (chuangkou));/////一维图像就像是窗口大小 fangcha = 0; sum = 0; ste = ste + result; a = a + 1; } } //ss += ste / a + " " + chuangkou + "\r\n"; ss += ste / a + " " + ste + " " + a + "\r\n"; //string varString = Convert.ToString(chuangkou); ////////////////////////////////////////////////////////////////////////// ///////////////改写内容 String Strsavefile; Strsavefile = savename; ////////FileStream fs = new FileStream(Strsavefile); StreamWriter sw = new StreamWriter(Strsavefile); sw.WriteLine(ss); sw.Close(); ////////////////////////////////////////////////////////////////////////// chuangkou = chuangkou + 1; //MessageBox.Show(varString,"OK"); //this.Hide(); } } MessageBox.Show("OK"); this.Hide(); }
/// <summary> /// TIN数据转栅格数据并进行坡度分析 张琪 20110614 /// </summary> /// <param name="pTinAdvanced"></param> /// <param name="pRastConvType"></param> /// <param name="sDir"></param> /// <param name="sName"></param> /// <param name="ePixelType"></param> /// <param name="cellsize"></param> /// <param name="pExtent"></param> /// <param name="bPerm"></param> /// <param name="strType"></param> /// <returns></returns> public IRasterDataset TinToRaster(ITinAdvanced pTinAdvanced, esriRasterizationType pRastConvType, String sDir, String sName, rstPixelType ePixelType, Double cellsize, IEnvelope pExtent, bool bPerm, String strType) { try { ESRI.ArcGIS.Geometry.IPoint pOrigin = pExtent.LowerLeft; pOrigin.X = pOrigin.X - (cellsize * 0.5); pOrigin.Y = pOrigin.Y - (cellsize * 0.5); int nCol, nRow; nCol = Convert.ToInt32(Math.Round(pExtent.Width / cellsize)) + 1; nRow = Convert.ToInt32(Math.Round(pExtent.Height / cellsize)) + 1; IGeoDataset pGeoDataset = pTinAdvanced as IGeoDataset; ISpatialReference2 pSpatialReference2 = pGeoDataset.SpatialReference as ISpatialReference2; IRasterDataset pRasterDataset = CreateRasterSurf(sDir, sName, strType, pOrigin, nCol, nRow, cellsize, cellsize, ePixelType, pSpatialReference2, bPerm); IRawPixels pRawPixels = GetRawPixels(pRasterDataset, 0); object pCache = pRawPixels.AcquireCache(); ITinSurface pTinSurface = pTinAdvanced as ITinSurface; IGeoDatabaseBridge2 pbridge2 = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass(); IRasterProps pRasterProps = pRawPixels as IRasterProps; //float nodataFloat; //int nodataInt; double dZMin = pTinAdvanced.Extent.ZMin; object vNoData; if (ePixelType.ToString() == "PT_FLOAT") { vNoData = (dZMin - 1).ToString(); } else { vNoData = Convert.ToInt32((dZMin - 1)); } pRasterProps.NoDataValue = vNoData; IPnt pOffset = new DblPntClass(); int lMaxBlockX = 2048; if (nCol < lMaxBlockX) { lMaxBlockX = nCol; } int lMaxBlockY = 2048; if (nRow < lMaxBlockY) { lMaxBlockY = nRow; } IPnt pBlockSize = new DblPntClass(); pBlockSize.X = lMaxBlockX; pBlockSize.Y = lMaxBlockY; IPixelBlock3 pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3; object blockArray = pPixelBlock.get_PixelDataByRef(0); ITrackCancel pCancel = new CancelTrackerClass(); pCancel.CancelOnClick = false; pCancel.CancelOnKeyPress = true; int lBlockCount = Convert.ToInt32(Math.Round((nCol / lMaxBlockX) + 0.49) * Math.Round((nRow / lMaxBlockY) + 0.49)); ESRI.ArcGIS.Geometry.IPoint pBlockOrigin = new ESRI.ArcGIS.Geometry.PointClass(); int lColOffset, lRowOffset; for (lRowOffset = 0; lRowOffset < (nRow - 1);) { for (lColOffset = 0; lColOffset < (nCol - 1);) { if ((nCol - lColOffset) < lMaxBlockX) { pBlockSize.X = (nCol - lColOffset); pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3; blockArray = pPixelBlock.get_PixelDataByRef(0); } pBlockOrigin.X = pOrigin.X + (lColOffset * cellsize) + (cellsize * 0.5); pBlockOrigin.Y = pOrigin.Y + ((nRow - lRowOffset) * cellsize) - (cellsize * 0.5); pbridge2.QueryPixelBlock(pTinSurface, pBlockOrigin.X, pBlockOrigin.Y, cellsize, cellsize, pRastConvType, vNoData, ref blockArray); //pTinSurface.QueryPixelBlock(pBlockOrigin.X, pBlockOrigin.Y, cellsize, cellsize, pRastConvType, vNoData, blockArray); pOffset.X = lColOffset; pOffset.Y = lRowOffset; pPixelBlock.set_PixelData(0, (System.Object)blockArray); pRawPixels.Write(pOffset, pPixelBlock as IPixelBlock); if (lBlockCount > 1) { if (!pCancel.Continue()) { break; } else if (pTinAdvanced.ProcessCancelled) { break; } } lColOffset = lColOffset + lMaxBlockX; } bool bReset = false; if (pBlockSize.X != lMaxBlockX) { pBlockSize.X = lMaxBlockX; bReset = true; } if ((nRow - lRowOffset) < lMaxBlockY) { pBlockSize.Y = (nRow - lRowOffset); bReset = true; } if (bReset) { pPixelBlock.set_PixelData(0, blockArray); pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3; blockArray = pPixelBlock.get_PixelDataByRef(0); } lRowOffset = lRowOffset + lMaxBlockY; } pRawPixels.ReturnCache(pCache); pCache = null; pRawPixels = null; pPixelBlock = null; pRasterProps = null; blockArray = 0; pRasterDataset = OpenRasterDataset(sDir, sName); if (lBlockCount == 1) { pTinAdvanced.TrackCancel = null; } return(pRasterDataset); } catch { return(null); } }
public IRasterDataset TinToRaster2(ITinAdvanced pTinAdvanced, esriRasterizationType pRastConvType, String sDir, String sName, rstPixelType ePixelType, Double cellsize, IEnvelope pExtent, bool bPerm, String strType) { try { ESRI.ArcGIS.Geometry.IPoint pOrigin = pExtent.LowerLeft; pOrigin.X = pOrigin.X - (cellsize * 0.5); pOrigin.Y = pOrigin.Y - (cellsize * 0.5); int nCol, nRow; nCol = Convert.ToInt32(Math.Round(pExtent.Width / cellsize)) + 1; nRow = Convert.ToInt32(Math.Round(pExtent.Height / cellsize)) + 1; IGeoDataset pGeoDataset = pTinAdvanced as IGeoDataset; ISpatialReference2 pSpatialReference2 = pGeoDataset.SpatialReference as ISpatialReference2; IRasterDataset pRasterDataset = CreateRasterSurf(sDir, sName, strType, pOrigin, nCol, nRow, cellsize, cellsize, ePixelType, pSpatialReference2, bPerm); IRawPixels pRawPixels = GetRawPixels(pRasterDataset, 0); IPnt pBlockSize = new DblPntClass(); pBlockSize.X = nCol; pBlockSize.Y = nRow; IPixelBlock3 pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize) as IPixelBlock3; object blockArray = pPixelBlock.get_PixelDataByRef(0); ITinSurface pTinSurface = pTinAdvanced as ITinSurface; IGeoDatabaseBridge2 pbridge2 = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass(); IRasterProps pRasterProps = pRawPixels as IRasterProps; object nodataFloat; object nodataInt; pOrigin.X = pOrigin.X + (cellsize * 0.5); pOrigin.Y = pOrigin.Y + (cellsize * nRow) - (cellsize * 0.5); if (ePixelType.ToString() == "PT_FLOAT") { nodataFloat = pRasterProps.NoDataValue; pTinSurface.QueryPixelBlock(pOrigin.X, pOrigin.Y, cellsize, cellsize, pRastConvType, nodataFloat, blockArray); } else { nodataInt = pRasterProps.NoDataValue; pTinSurface.QueryPixelBlock(pOrigin.X, pOrigin.Y, cellsize, cellsize, pRastConvType, nodataInt, blockArray); } if (pTinAdvanced.ProcessCancelled == false) { return(null); } IPnt pOffset = new DblPntClass(); pOffset.X = 0; pOffset.Y = 0; pRawPixels.Write(pOffset, pPixelBlock as IPixelBlock); if (!bPerm && ePixelType.ToString() == "PT_FLOAT") { IRasterBand pBand = pRawPixels as IRasterBand; IRasterStatistics pStats = pBand.Statistics; pStats.Recalculate(); } if (bPerm) { pRawPixels = null; pPixelBlock = null; pRasterProps = null; blockArray = 0; pRasterDataset = OpenRasterDataset(sDir, sName); } return(pRasterDataset); } catch { return(null); } }
/// <summary> /// 创建栅格数据集,本地 /// </summary> /// <param name="directoryName"></param> /// <param name="fileName"></param> /// <returns></returns> private IRasterDataset CreateFileRasterDataset(string directoryName, string fileName) { // This function creates a new img file in the given workspace // and then assigns pixel values try { IRasterDataset rasterDataset = null; IPoint originPoint = new PointClass(); originPoint.PutCoords(0, 0); // Create the dataset IRasterWorkspace2 rasterWorkspace2 = null; rasterWorkspace2 = CreateRasterWorkspace(directoryName); rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, new UnknownCoordinateSystemClass(), true); IRawPixels rawPixels = null; IPixelBlock3 pixelBlock3 = null; IPnt pixelBlockOrigin = null; IPnt pixelBlockSize = null; IRasterBandCollection rasterBandCollection; IRasterProps rasterProps; // QI for IRawPixels and IRasterProps rasterBandCollection = (IRasterBandCollection)rasterDataset; rawPixels = (IRawPixels)rasterBandCollection.Item(0); rasterProps = (IRasterProps)rawPixels; // Create pixelblock pixelBlockOrigin = new DblPntClass(); pixelBlockOrigin.SetCoords(0, 0); pixelBlockSize = new DblPntClass(); pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height); pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize); // Read pixelblock rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); // Get pixeldata array System.Object[,] pixelData; pixelData = (System.Object[, ])pixelBlock3.get_PixelDataByRef(0); // Loop through all the pixels and assign value for (int i = 0; i < rasterProps.Width; i++) { for (int j = 0; j < rasterProps.Height; j++) { pixelData[i, j] = (i * j) % 255; } } // Write the pixeldata back System.Object cachePointer; cachePointer = rawPixels.AcquireCache(); rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3); rawPixels.ReturnCache(cachePointer); // Return raster dataset return(rasterDataset); } catch (Exception ex) { //******************************************************************* //guozheng added if (ModData.SysLog != null) { ModData.SysLog.Write(ex, null, DateTime.Now); } else { ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog(); ModData.SysLog.Write(ex, null, DateTime.Now); } //******************************************************************** System.Diagnostics.Debug.WriteLine(ex.Message); return(null); } }
public void DEMToTIN(IRaster iRaster, string tinFileName) { try { //***************生成TIN模型********************************************* IGeoDataset pGeoData = iRaster as IGeoDataset; IEnvelope pExtent = pGeoData.Extent; IRasterBandCollection pRasBC = iRaster as IRasterBandCollection; IRasterBand pRasBand = pRasBC.Item(0); IRawPixels pRawPixels = pRasBand as IRawPixels; IRasterProps pProps = pRawPixels as IRasterProps; int iWid = pProps.Width; int iHei = pProps.Height; double w = iWid / 1000.0f; double h = iHei / 1000.0f; IPnt pBlockSize = new DblPntClass(); bool IterationFlag; if (w < 1 && h < 1) //横纵都小于1000个像素 { pBlockSize.X = iWid; pBlockSize.Y = iHei; IterationFlag = false; } else { pBlockSize.X = 1001.0f; pBlockSize.Y = 1001.0f; IterationFlag = true; } double cellsize = 0.0f; //栅格大小 IPnt pPnt1 = pProps.MeanCellSize(); //栅格平均大小 cellsize = pPnt1.X; ITinEdit pTinEdit = new TinClass() as ITinEdit; pTinEdit.InitNew(pExtent); ISpatialReference pSpatial = pGeoData.SpatialReference; pExtent.SpatialReference = pSpatial; IPnt pOrigin = new DblPntClass(); IPnt pPixelBlockOrigin = new DblPntClass(); //栅格左上角像素中心坐标 double bX = pBlockSize.X; double bY = pBlockSize.Y; pBlockSize.SetCoords(bX, bY); IPixelBlock pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize); object nodata = pProps.NoDataValue; //无值标记 ITinAdvanced2 pTinNodeCount = pTinEdit as ITinAdvanced2; int nodeCount = pTinNodeCount.NodeCount; object vtMissing = Type.Missing; object vPixels = null; //格子 double m_zTolerance = 0; if (!IterationFlag) //当为一个处理单元格子时;(w < 1 && h < 1) //横纵都小于1000个像素 { pPixelBlockOrigin.SetCoords(0.0f, 0.0f); pRawPixels.Read(pPixelBlockOrigin, pPixelBlock); vPixels = pPixelBlock.get_SafeArray(0); double xMin = pExtent.XMin; double yMax = pExtent.YMax; pOrigin.X = xMin + cellsize / 2; pOrigin.Y = yMax - cellsize / 2; bX = pOrigin.X; bY = pOrigin.Y; pTinEdit.AddFromPixelBlock(bX, bY, cellsize, cellsize, nodata, vPixels, m_zTolerance, ref vtMissing, out vtMissing); } else //当有多个处理单元格时,依次循环处理每个单元格 { int i = 0, j = 0, count = 0; int FirstGoNodeCount = 0; //while (nodeCount != FirstGoNodeCount) //{ count++; nodeCount = pTinNodeCount.NodeCount; int bwidth = 0; int bheight = 0; //依次循环处理 for (i = 0; i < (int)h + 1; i++) { if (i < (int)h) { bheight = 1000; } else { bheight = iHei - 1000 * i; } for (j = 0; j < (int)w + 1; j++) { if (j < (int)w) { bwidth = 1000; } else { bwidth = iWid - 1000 * j; } pBlockSize.SetCoords(bwidth, bheight); pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize); double bX1, bY1, xMin1, yMax1; bX1 = pBlockSize.X; bY1 = pBlockSize.Y; pPixelBlockOrigin.SetCoords(j * 1000, i * 1000); pRawPixels.Read(pPixelBlockOrigin, pPixelBlock); vPixels = pPixelBlock.get_SafeArray(0); xMin1 = pExtent.XMin; yMax1 = pExtent.YMax; //bX1 = pBlockSize.X; //bY1 = pBlockSize.Y; pOrigin.X = xMin1 + j * 1000 * cellsize + cellsize / 2.0f; pOrigin.Y = yMax1 - i * 1000 * cellsize - cellsize / 2.0f; bX1 = pOrigin.X; bY1 = pOrigin.Y; pTinEdit.AddFromPixelBlock(bX1, bY1, cellsize, cellsize, nodata, vPixels, m_zTolerance, ref vtMissing, out vtMissing); FirstGoNodeCount = pTinNodeCount.NodeCount; } } //} } //保存TIN文件 pTinEdit.SaveAs(tinFileName, ref vtMissing); pTinEdit.StopEditing(true); m_pTin = pTinEdit as ITin; MessageBox.Show("转换成功!"); } catch (SystemException e) { MessageBox.Show(e.Message); } }
public static IRasterDataset TinToRaster(ITinAdvanced itinAdvanced_0, esriRasterizationType esriRasterizationType_0, string string_0, string string_1, string string_2, rstPixelType rstPixelType_0, double double_0, IEnvelope ienvelope_0, bool bool_0) { object obj3; 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; int num2 = ((int)Math.Round((double)(ienvelope_0.Height / double_0))) + 1; IGeoDataset dataset = itinAdvanced_0 as IGeoDataset; ISpatialReference2 spatialReference = dataset.SpatialReference as ISpatialReference2; IRasterDataset dataset2 = CreateRasterSurf(string_0, string_1, string_2, lowerLeft, num, num2, double_0, double_0, 1, rstPixelType_0, spatialReference, bool_0); IRawPixels rawPixels = GetRawPixels(dataset2, 0); object cache = rawPixels.AcquireCache(); ITinSurface pSurface = itinAdvanced_0 as ITinSurface; IRasterProps o = rawPixels as IRasterProps; double zMin = itinAdvanced_0.Extent.ZMin; if (rstPixelType_0 == rstPixelType.PT_FLOAT) { obj3 = (float)(zMin - 1.0); } else { obj3 = (int)(zMin - 1.0); } o.NoDataValue = obj3; IPnt tlc = new DblPnt(); int num4 = 2048; if (num < 2048) { num4 = num; } int num5 = 2048; if (num2 < 2048) { num5 = num2; } IPnt size = new DblPnt { X = num4, Y = num5 }; IPixelBlock3 block = rawPixels.CreatePixelBlock(size) as IPixelBlock3; ITrackCancel cancel = new CancelTracker { CancelOnClick = false, CancelOnKeyPress = true }; int num6 = (int)(Math.Round((double)((num / num4) + 0.49)) * Math.Round((double)((num2 / num5) + 0.49))); if (num6 == 1) { itinAdvanced_0.TrackCancel = cancel; } IPoint point2 = new Point(); object obj4 = block.get_PixelDataByRef(0); for (int i = 0; i < num2; i += num5) { for (int j = 0; j < num; j += num4) { if ((num - j) < num4) { size.X = num - j; block = rawPixels.CreatePixelBlock(size) as IPixelBlock3; obj4 = block.get_PixelDataByRef(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, obj3, ref obj4); tlc.X = j; tlc.Y = i; block.set_PixelData(0, obj4); rawPixels.Write(tlc, block as IPixelBlock); } bool flag = false; if (size.X != num4) { size.X = num4; flag = true; } if ((num2 - i) < num5) { size.Y = num2 - i; flag = true; } if (flag) { block = rawPixels.CreatePixelBlock(size) as IPixelBlock3; obj4 = block.get_PixelDataByRef(0); } } rawPixels.ReturnCache(cache); Marshal.ReleaseComObject(cache); cache = null; Marshal.ReleaseComObject(rawPixels); rawPixels = null; Marshal.ReleaseComObject(block); block = null; Marshal.ReleaseComObject(o); o = null; obj4 = 0; GC.Collect(); return(dataset2); }
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 List <byte> GetBlockUniqueValue(IRasterLayer rasterLyr, IElement element) { //get top visable raster if (rasterLyr == null) { XtraMessageBox.Show("读取分类结果文件失败!"); return(null); } IRaster2 raster2 = rasterLyr.Raster as IRaster2; //element.Geometry.Project(rasterProps.SpatialReference); //if(element.Geometry.SpatialReference ==null) // element.Geometry.SpatialReference = EnviVars.instance.MapControl.SpatialReference ; //element.Geometry.Project(rasterProps.SpatialReference); IRasterBandCollection rasterBandCollection = raster2.RasterDataset as IRasterBandCollection; IRasterBand rasterBand = rasterBandCollection.Item(0); IRawPixels rawPixels = rasterBand as IRawPixels; IPnt pixelBlockOrigin = new DblPntClass(); IPnt pixelBlockSize = new DblPntClass(); IPixelBlock3 pixelBlock3 = null; //calculate element column and row in raster int startColum, startRow, endColum, endRow = -1; IEnvelope envelope = element.Geometry.Envelope; //当前空间参考与栅格不一致时,重投影绘制坐标。 IPoint upleft = new PointClass(); IPoint lowright = new PointClass(); ISpatialReference fromReference = EnviVars.instance.MapControl.Map.SpatialReference; ISpatialReference toReference = (raster2 as IGeoDataset).SpatialReference; upleft.X = envelope.XMin; upleft.Y = envelope.YMax; upleft.SpatialReference = fromReference; lowright.X = envelope.XMax; lowright.Y = envelope.YMin; lowright.SpatialReference = fromReference; if (!GFS.Common.EngineAPI.IsEqualSpatialReference(fromReference, toReference)) { upleft.Project(toReference); lowright.Project(toReference); } raster2.MapToPixel(upleft.X, upleft.Y, out startColum, out startRow); //raster2.MapToPixel(envelope.XMin, envelope.YMax, out startColum, out startRow); if (startColum < 0 && startRow < 0) { XtraMessageBox.Show("读取起始行列失败!"); return(null); } pixelBlockOrigin.SetCoords(startColum, startRow); raster2.MapToPixel(lowright.X, lowright.Y, out endColum, out endRow); //raster2.MapToPixel(envelope.XMax, envelope.YMin, out endColum, out endRow); if (endColum < 0 && endRow < 0) { XtraMessageBox.Show("读取起始行列失败!"); return(null); } pixelBlockSize.SetCoords(endColum - startColum + 1, endRow - startRow + 1); pixelBlock3 = rawPixels.CreatePixelBlock(pixelBlockSize) as IPixelBlock3; //read pixel rawPixels.Read(pixelBlockOrigin, pixelBlock3 as IPixelBlock); byte[,] pixelData = (byte[, ])pixelBlock3.get_PixelDataByRef(0); //get unique value List <byte> uniqueValue = new List <byte>(); foreach (byte value in pixelData) { if (!uniqueValue.Contains(value)) { uniqueValue.Add(value); } } //sort uniqueValue.Sort(); return(uniqueValue); }
private void ShowRasterInfo(IRasterLayer rLayer) { rtxbRasterInfo.Text = "Boand Number is " + rLayer.BandCount.ToString();// "\r\n" +"Resolution is " + rLayer.DisplayResolutionFactor.ToString() + "\r\n" + "Row number is " + rLayer.RowCount.ToString(); IRaster raster = rLayer.Raster; //Raster Data IRasterBandCollection rBandCon = raster as IRasterBandCollection; int numBands = rBandCon.Count; for (int i = 0; i < numBands; i++) { // IRasterBand rBand = rBandCon.Item(i); IRasterProps rasterData = rBand as IRasterProps; //Raster Height int height = rasterData.Height; //Raster Width int width = rasterData.Width; //Raster Cell Size IPnt cell = rasterData.MeanCellSize(); rstPixelType rType = rasterData.PixelType; MessageBox.Show(rType.ToString()); if (rType == rstPixelType.PT_LONG) { //Error code //Read Raster //Wrong QI //IRaster pRaster = rBand as IRaster; ITable rasterTable = rBand.AttributeTable; IQueryFilter qFilter = new QueryFilter(); for (int j = 0; j < rasterTable.Fields.FieldCount; j++) { IField aField = rasterTable.Fields.get_Field(j); qFilter.AddField(aField.ToString()); } ICursor cursor = rasterTable.Search(qFilter, true); IRow aRow = cursor.NextRow(); if (aRow != null) { //aRow.get_Value(0); MessageBox.Show(cell.X.ToString() + "x" + cell.Y + ":" + aRow.get_Value(0).ToString()); //Read Raster // rBand. } } // Create pixelblock IRawPixels rawPixels = rBand as IRawPixels; //cannot be used as follows //IRawPixels rawPixels = raster as IRawPixels; IPnt pixelBlockOrigin = new DblPnt(); pixelBlockOrigin.SetCoords(0, 0); IPnt pixelBlockSize = new DblPnt(); pixelBlockSize.SetCoords(rasterData.Width, rasterData.Height); IPixelBlock3 pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize); //Read rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); //GetVal( int brand, int X, int Y); brand=0 int x = 3600; int y = 800; object cellD = pixelBlock3.GetVal(0, x, y); //MessageBox.Show(cellD.ToString()); rtxbRasterInfo.Text = rtxbRasterInfo.Text + "\r\n" + rBand.Bandname.ToString() + ":" + width.ToString() + "x" + height.ToString() + ":(" + x.ToString() + "," + y.ToString() + ")=" + cellD.ToString(); } }
private void BtnOK_Click(object sender, EventArgs e) { int i = 0; int j = 0; if (string.IsNullOrEmpty(this.ComboBoxInLayer.Text)) { MessageBox.Show("请选择需要二值化的图层!", "栅格二值化", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } int intLyrCount = frm.mainMapControl.LayerCount; for (i = 0; i <= intLyrCount - 1; i++) { if ((frm.mainMapControl.Map.get_Layer(i).Name == this.ComboBoxInLayer.Text.Trim())) { IRasterLayer pRasterLayer = frm.mainMapControl.get_Layer(i) as IRasterLayer; IRaster2 raster = pRasterLayer.Raster as IRaster2; IRasterDataset rasterDataset = raster.RasterDataset; IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset; IRasterBand dirRasterBand = dirBandCollection.Item(0); IRawPixels dirRawPixels = (IRawPixels)dirRasterBand; IPnt pixelBlockOrigin = null; IPnt dirBlockSize = new DblPntClass(); IRasterProps dirProps = (IRasterProps)dirRawPixels; int dirColumns = dirProps.Width; //列数 int dirRows = dirProps.Height; //行数 IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize); dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock); System.Array array; array = (System.Array)dirPixelBlock.get_PixelDataByRef(0);//获取栅格数组 int min; if (textBox1.Text == "") { min = 20; } else { min = Convert.ToInt32(textBox1.Text); } /////获得栅格数据像元值 double[,] b = new double[dirRows, dirColumns]; double value; string strExcute = null; for (int row = 0; row < dirRows; row++) { for (int col = 0; col < dirColumns; col++) { b[row, col] = Convert.ToSingle(array.GetValue(col, row)); /////阈值判断 if (b[row, col] > min) { value = 1; } else { value = 0; } strExcute = Convert.ToString(value); } } IGeoDataset tempGeodata1 = (IGeoDataset)pRster; IMapAlgebraOp pMapAlgebraOp = new RasterMapAlgebraOpClass(); //设置栅格运算空间 IRasterAnalysisEnvironment pRasterAnalysisEnvironment = (IRasterAnalysisEnvironment)pMapAlgebraOp; IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactoryClass(); string outPath = null; string outLayerName = null; outPath = this.TxtBox.Text.Trim(); j = this.TxtBox.Text.Trim().LastIndexOf("\\"); outPath = this.TxtBox.Text.Substring(0, j); outLayerName = this.TxtBox.Text.Substring(j + 1); string DataSetName = this.TxtBox.Text.Substring(j + 1); //设置输出空间 IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(outPath, 0); pRasterAnalysisEnvironment.OutWorkspace = pWorkspace; IGeoDataset outGetDataset = pMapAlgebraOp.Execute(strExcute); IRasterLayer pCreatRalyr = new RasterLayerClass(); pCreatRalyr.CreateFromRaster((IRaster)outGetDataset); pCreatRalyr.Name = outLayerName; frm.mainMapControl.AddLayer(pCreatRalyr); break; } } }
private void btnCaculate_Click(object sender, EventArgs e) { FolderBrowserDialog m_FolderBrowserDialog = new FolderBrowserDialog(); string m_DataPathName = ""; string m_DataName = ""; string parentPath = ""; if (m_DataPathName != "" || m_DataName != "" || parentPath != "") { m_DataPathName = m_FolderBrowserDialog.SelectedPath; DirectoryInfo m_DirectoryInfo = new DirectoryInfo(m_DataPathName); m_DataName = m_DirectoryInfo.Name; DirectoryInfo pDirectoryInfo = m_DirectoryInfo.Parent; parentPath = pDirectoryInfo.FullName; } textBox3.Text = m_DataPathName; stw.Start(); string jieguo2; for (int i = 0; i < frm.mainMapControl.LayerCount; i++) { string inputname = frm.mainMapControl.get_Layer(i).Name; int location2 = inputname.LastIndexOf('.'); string sss2 = inputname; jieguo2 = sss2.Substring(0, location2);////"."后文件名称 IRasterLayer pRasterLayer = frm.mainMapControl.get_Layer(i) as IRasterLayer; IRaster2 raster = pRasterLayer.Raster as IRaster2; IRasterDataset rasterDataset = raster.RasterDataset; IPnt pixelBlockOrigin = new DblPntClass(); pixelBlockOrigin.SetCoords(0, 0); IRasterBandCollection dirBandCollection = (IRasterBandCollection)rasterDataset; IRasterBand dirRasterBand = dirBandCollection.Item(0); IRawPixels dirRawPixels = (IRawPixels)dirRasterBand; IRasterProps dirProps = (IRasterProps)dirRawPixels; int dirColumns = dirProps.Width; //列数 int dirRows = dirProps.Height; //行数 double cellSizeX = dirProps.MeanCellSize().X; //得到x方向栅格大小 double cellSizeY = dirProps.MeanCellSize().Y; //得到y方向栅格大小 IPnt dirBlockSize = new DblPntClass(); dirBlockSize.X = dirColumns; dirBlockSize.Y = dirRows; IPixelBlock3 dirPixelBlock = (IPixelBlock3)dirRawPixels.CreatePixelBlock(dirBlockSize); dirRawPixels.Read(pixelBlockOrigin, (IPixelBlock)dirPixelBlock); System.Array array = (System.Array)dirPixelBlock.get_PixelDataByRef(0); string ss = ""; double[,] b = new double[dirRows, dirColumns]; for (int row = 0; row < dirRows; row++) { for (int col = 0; col < dirColumns; col++) { b[row, col] = Convert.ToSingle(array.GetValue(col, row));/////得到按行排列的值,注意这个为GetValue(col, row)) } } if (textBox3.Text == "") { MessageBox.Show("文件保存名不能为空"); } String savename = textBox3.Text + "\\" + jieguo2 + ".txt"; for (int c = 0; c < maxnum; c++) { double sum, mean, a;//修改程序2012年7月27日 a = 0; sum = 0; mean = 0; if (cbBDirection.Text == "左边--->右边") { for (int row = 0; row < dirRows; row++) ////窗口大小与图像行列数关系 { for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系 { sum += Math.Pow((b[row, col] - b[row, col + k]), 2); a = a + 1; } } } if (cbBDirection.Text == "上边--->下边") { for (int row = 0; row < dirRows - k; row++) ////窗口大小与图像行列数关系 { for (int col = 0; col < dirColumns; col++) ////窗口大小与图像行列数关系 { sum = sum + Math.Pow((b[row, col] - b[row + k, col]), 2); a = a + 1; } } } if (cbBDirection.Text == "左下--->右上") { for (int row = 0; row < dirRows - k; row++) ////窗口大小与图像行列数关系 { for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系 { sum += Math.Pow((b[row, col] - b[row + k, col + k]), 2); a = a + 1; } } } if (cbBDirection.Text == "左上--->右下") { for (int row = 0; row < dirRows - k; row++) ////窗口大小与图像行列数关系 { for (int col = 0; col < dirColumns - k; col++) ////窗口大小与图像行列数关系 { sum += Math.Pow((b[row, col + k] - b[row + k, col]), 2); a = a + 1; } } } mean = sum / (2 * a); ss += mean + " " + a + " " + k + "\r\n"; String Strsavefile = savename; StreamWriter sw = new StreamWriter(Strsavefile); sw.WriteLine(ss); sw.Close(); k = k + 1; } } stw.Stop(); MessageBox.Show("程序共运行时间:" + stw.Elapsed.Minutes.ToString() + "分钟" + stw.Elapsed.Seconds.ToString() + "秒"); }