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); }
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> /// 创建栅格数据集,本地 /// </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); } }
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); }
public void GetGRIDArray(IRaster raster, out double dMax, out double dMin, out double dCounts) { IRawPixels rawPixels = null; IPixelBlock3 pixelBlock3 = null; IPnt pixelBlockOrigin = null; IPnt pixelBlockSize = null; IRasterBandCollection rasterBandCollection; IRasterProps rasterProps; // QI for IRawPixels and IRasterProps IList<IList<double>> gridarray = new List<IList<double>>(); IRaster pRaster = raster; rasterBandCollection = (IRasterBandCollection)pRaster; 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); // ��ȡ pixelblock rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); //��ȡ���� System.Array pixelData; pixelData = (System.Array)pixelBlock3.get_PixelDataByRef(0); float[,] griddata = (float[,])pixelData; double dNodata = Convert.ToDouble(rasterProps.NoDataValue); double ddMax = -10000; double ddMin = 100000; if (griddata == null) { dMax = ddMax; dMin = ddMin; dCounts = 0; return; } int irows = griddata.GetLength(0); int icols = griddata.GetLength(1); for (int i = 0; i < irows; i++) { for (int j = 0; j < icols; j++) { if ((griddata[i, j]-dNodata)>=0.0000001)//˫�����������е��ڲ��� { if (ddMax < griddata[i, j]) ddMax = griddata[i, j]; if (ddMin > griddata[i, j]) ddMin = griddata[i, j]; } } } dMax = ddMax; dMin = ddMin; dCounts = irows * icols; }
//逐点算法 public bool ImageReprojectionRange(IRaster pSrcRaster, out IRaster pDstRaster, ExOriPara exori, double dbFocus, double fx, double fy, int nImgWidth, int nImgHeight, int nGeoRange) { pDstRaster = null; Pt2i ptSubImgLeftTop = new Pt2i(); double[,] dbSubDemData = getSubDem(pSrcRaster, exori.pos, nGeoRange, ref ptSubImgLeftTop); if (dbSubDemData == null) { return(false); } IRasterProps pProps = pSrcRaster as IRasterProps; int nDemHeight = pProps.Height; int nDemWidth = pProps.Width; IRaster2 pRaster2 = pSrcRaster as IRaster2; double dbNoDataValue = getNoDataValue(pProps.NoDataValue); //object pNodata = pProps.NoDataValue; //double value=((double[])pNodata)[0]; //double dbNoDataValue =double.Parse((float[]pNodata)[0].ToString()); //double dbNoDataValue = Convert.ToDouble(((double[]))[0]); //object dbNoDataValue = Convert.ToDouble(pProps.NoDataValue.ToString()); //object nodatavalue = pProps.NoDataValue; //Type type=pProps.NoDataValue.GetType(); //double dbNoDataValue = type.IsArray ? nodatavalue[0] as double : nodatavalue as double; //初始步长 double dbInitialStep = 0; int nGridW = nDemWidth / 10; int nGridH = nDemHeight / 10; int nCount = 0; double dbHeightAvg = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { double dDemZ = double.NaN; double dbGeoX = pRaster2.ToMapX(i * nGridW); double dbGeoY = pRaster2.ToMapY(j * nGridH); if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, dbGeoX, dbGeoY, ref dDemZ) && Math.Abs(dDemZ - dbNoDataValue) >= 10e-10) { nCount++; dbHeightAvg += dDemZ; } } } if (nCount != 0) { dbHeightAvg /= nCount; } else { return(false); } double dbCurCameraZ = 0; //IRaster2 pRaster2 = pSrcRaster as IRaster2; //int nCameraImgX = pRaster2.ToPixelColumn(exori.pos.X);//dXCumulate - pDem.m_PtOrigin.X) / dXRdem; //int nCameraImgY = pRaster2.ToPixelRow(exori.pos.Y); // (dYCumulate - pDem.m_PtOrigin.Y) / dYRdem; //if (nCameraImgY >= 0 || nCameraImgY < nDemHeight || nCameraImgX >= 0 || nCameraImgX < nDemWidth) //{ if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, exori.pos.X, exori.pos.Y, ref dbCurCameraZ) && Math.Abs(dbCurCameraZ - dbNoDataValue) >= 10e-10) { dbHeightAvg = dbCurCameraZ; } //} dbInitialStep = Math.Abs(dbHeightAvg - exori.pos.Z) / 2; Point2D ptLeftTop = new Point2D(0, 0); double[] dbResolution = new double[2]; dbResolution[0] = pProps.MeanCellSize().X; dbResolution[1] = pProps.MeanCellSize().Y; int[] nSize = new int[2]; nSize[0] = nDemWidth; nSize[1] = nDemHeight; //pDstRaster = CreateRaster(ptLeftTop, dbResolution, nSize); //pDstRaster = createRasterWithoutData(ptLeftTop, dbResolution[0], nSize, "testOutput.tif"); // byte[,] dbData = new byte[nDemWidth, nDemHeight]; //double demGrayVal=double.NaN; //Pt3d demXYZ; //byte byteGray; //byte bt = (byte)255; //Pt2d pt; Matrix matA, matB; matA = new Matrix(2, 2); matB = new Matrix(2, 1); Matrix matR; matR = new Matrix(3, 3); //Matrix matX; double[] a = new double[4]; double[] b = new double[2]; OPK2RMat(exori.ori, ref matR); //求解相关系数 double a1 = matR.getNum(0, 0); double a2 = matR.getNum(0, 1); double a3 = matR.getNum(0, 2); double b1 = matR.getNum(1, 0); double b2 = matR.getNum(1, 1); double b3 = matR.getNum(1, 2); double c1 = matR.getNum(2, 0); double c2 = matR.getNum(2, 1); double c3 = matR.getNum(2, 2); double[] dbCoef = new double[] { a1, a2, a3, b1, b2, b3, c1, c2, c3 }; //double X, Y, Z, Z1; // Z = 0.0; //double dthreshold = 0.01; //初始化数组 //int[,] dbData = new int[nDemWidth, nDemHeight]; //for (int i = 0; i < nDemWidth; i++) //{ // for (int j = 0; j < nDemHeight; j++) // { // dbData[i, j] = 0; // } //} try { //生成RASTER Point2D pt2dTopLeft = new Point2D(); pt2dTopLeft.X = pProps.Extent.UpperLeft.X; pt2dTopLeft.Y = pProps.Extent.UpperLeft.Y; pDstRaster = CreateRaster(pt2dTopLeft, dbResolution, nSize); if (pDstRaster == null) { return(false); } //得到数组 IPnt pBlockSize = new DblPntClass(); pBlockSize.X = nDemWidth; pBlockSize.Y = nDemHeight; IPnt pntLeftTop = new DblPntClass(); pntLeftTop.SetCoords(ptLeftTop.X, ptLeftTop.Y); IPixelBlock pPixelBlock = pDstRaster.CreatePixelBlock(pBlockSize); IPixelBlock3 pPixelBlock3 = pPixelBlock as IPixelBlock3; pDstRaster.Read(pntLeftTop, pPixelBlock); System.Array pixels; pixels = (System.Array)pPixelBlock3.get_PixelData(0); //初始化 object oValue = getValidType(pDstRaster as IRasterProps, 0); for (int i = 0; i < nDemHeight; i++) { for (int j = 0; j < nDemWidth; j++) { pixels.SetValue(oValue, j, i); } } //逐像素判断是否可见 IRaster2 pSrcRaster2 = pSrcRaster as IRaster2; oValue = getValidType(pDstRaster as IRasterProps, 255); for (int i = 0; i < nImgHeight; i++) { for (int j = 0; j < nImgWidth; j++) { Point2D ptCurrent = null; if (getPixelIsCoverd(pSrcRaster, dbSubDemData, ptSubImgLeftTop, j, i, out ptCurrent, exori, dbCoef, dbFocus, fx, fy, dbInitialStep)) { int nCol = int.MaxValue, nRow = int.MaxValue; pSrcRaster2.MapToPixel(ptCurrent.X, ptCurrent.Y, out nCol, out nRow); if (nCol >= nDemWidth || nCol < 0 || nRow < 0 || nRow >= nDemHeight) { continue; } pixels.SetValue(oValue, nCol, nRow); //pixels[nCol, nRow] = 255; } } } pPixelBlock3.set_PixelData(0, (System.Array)pixels); //修改数据 IRasterEdit pRasterEdit = pDstRaster as IRasterEdit; pRasterEdit.Write(pntLeftTop, pPixelBlock); pRasterEdit.Refresh(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); return(false); } return(true); }
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() + "秒"); }