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 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 CreateRasterDataset(string filePath, string fileName, IRasterLayer rasterLayer, StructRasterMetaData structRasterMetaData, int[,] data, int noDataValue) { try { IRasterWorkspace2 rasterWorkspace2 = OpenRasterWorkspace(filePath); //Define the origin for the raster dataset, which is the lower left corner of the raster. IPoint originPoint = new PointClass(); originPoint.PutCoords(structRasterMetaData.XMin, structRasterMetaData.YMin); //Define the dimensions of the raster dataset. int width = structRasterMetaData.RowCount; //This is the width of the raster dataset. int height = structRasterMetaData.ColumnCount; //This is the height of the raster dataset. IRaster r = rasterLayer.Raster; IRasterDefaultProps rdp = r as IRasterDefaultProps; double xCellSize = rdp.DefaultPixelWidth; //This is the cell size in x direction. double yCellSize = rdp.DefaultPixelHeight; //This is the cell size in y direction. ISpatialReference spatialReference = rdp.DefaultSpatialReference; int bandCount = 1; // This is the number of bands the raster dataset contains. //Create a raster dataset in TIFF format. IRasterDataset rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, height, width, xCellSize, yCellSize, bandCount, rstPixelType.PT_UCHAR, spatialReference, true); //If you need to set NoData for some of the pixels, you need to set it on band //to get the raster band. IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset; IRasterBand rasterBand; IRasterProps rasterProps; rasterBand = rasterBands.Item(0); rasterProps = (IRasterProps)rasterBand; //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band. rasterProps.NoDataValue = -9999f; //Create a raster from the dataset. IRaster raster = rasterDataset.CreateDefaultRaster(); //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". IPnt blocksize = new PntClass(); blocksize.SetCoords(height, width); IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3; object temp = pixelblock.get_PixelDataByRef(0); System.Byte[,] pixelData = (System.Byte[, ])temp; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (data[i, j] == -9999f) { pixelData[j, i] = (System.Byte)noDataValue; //System.Diagnostics.Debug.WriteLine(i.ToString() + "+" + j.ToString()); } else { if (pixelData[j, i] != Convert.ToByte(data[i, j])) { pixelData[j, i] = Convert.ToByte(data[i, j]); } //System.Diagnostics.Debug.WriteLine(i.ToString() + "-" + j.ToString()); } } } pixelblock.set_PixelData(0, pixelData); //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. System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); return(rasterDataset); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return(null); } }
/// <summary> /// 写入栅格数据。 /// </summary> public static void WriteRaster(IRasterLayer rasterLayer, float[,] datas) { int columnCount = rasterLayer.ColumnCount; int rowCount = rasterLayer.RowCount; IRaster2 raster = rasterLayer.Raster as IRaster2; IPnt fromPnt = new PntClass(); fromPnt.SetCoords(0, 0); IPnt blockSize = new PntClass(); blockSize.SetCoords(columnCount, rowCount); IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize); rasterLayer.Raster.Read(fromPnt, pixelBlock); IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock; object temp = pixelBlock3.get_PixelDataByRef(0); if ((pixelBlock3.get_PixelType(0) == rstPixelType.PT_CLONG) || (pixelBlock3.get_PixelType(0) == rstPixelType.PT_ULONG)) { System.Int32[,] pixelData = (System.Int32[, ])temp; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { if (datas[i, j] == -9999f) { pixelData[j, i] = 0; } else { if (pixelData[j, i] != Convert.ToInt32(datas[i, j])) { pixelData[j, i] = Convert.ToInt32(datas[i, j]); } } } } pixelBlock3.set_PixelData(0, pixelData); } else if ((pixelBlock3.get_PixelType(0) == rstPixelType.PT_SHORT) || (pixelBlock3.get_PixelType(0) == rstPixelType.PT_USHORT)) { System.Int16[,] pixelData = (System.Int16[, ])temp; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { if (datas[i, j] == -9999f) { pixelData[j, i] = 0; } else { if (pixelData[j, i] != Convert.ToInt16(datas[i, j])) { pixelData[j, i] = Convert.ToInt16(datas[i, j]); } } } } pixelBlock3.set_PixelData(0, pixelData); } else if ((pixelBlock3.get_PixelType(0) == rstPixelType.PT_CHAR) || (pixelBlock3.get_PixelType(0) == rstPixelType.PT_UCHAR)) { System.Byte[,] pixelData = (System.Byte[, ])temp; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { if (datas[i, j] == -9999f) { //pixelData[j, i] = 0; ; } else { if (pixelData[j, i] != Convert.ToByte(datas[i, j])) { pixelData[j, i] = Convert.ToByte(datas[i, j]); } } } } pixelBlock3.set_PixelData(0, pixelData); } else if (pixelBlock3.get_PixelType(0) == rstPixelType.PT_FLOAT) { System.Single[,] pixelData = (System.Single[, ])temp; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { if (datas[i, j] == -9999f) { //pixelData[j, i] =0; ; } else { if (pixelData[j, i] != Convert.ToSingle(datas[i, j])) { pixelData[j, i] = Convert.ToSingle(datas[i, j]); } } } } pixelBlock3.set_PixelData(0, pixelData); } ((IRasterEdit)rasterLayer.Raster).Write(fromPnt, (IPixelBlock)pixelBlock3); ((IRasterEdit)rasterLayer.Raster).Refresh(); }
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 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() + "秒"); }