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); } }
/// <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 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); }