static void Main(string[] args) { ESRI.ArcGIS.esriSystem.AoInitialize aoInit; #region Initialize License try { Console.WriteLine("Obtaining license"); ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); aoInit = new AoInitializeClass(); esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic); Console.WriteLine("Ready with license."); } catch (Exception exc) { // If it fails at this point, shutdown the test and ignore any subsequent errors. Console.WriteLine(exc.Message); return; } #endregion try { // Specify input directory and dataset name. string inputWorkspace = @"C:\Data"; string inputDatasetName = "8bitSampleImage.tif"; // Specify output filename. string outputDataset = @"c:\Temp\testArithmaticCS.afr"; // Open the Raster Dataset Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace workspace = workspaceFactory.OpenFromFile(inputWorkspace, 0); IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspace; IRasterDataset myRasterDataset = rasterWorkspace.OpenRasterDataset(inputDatasetName); // Create the Function Arguments object IArithmeticFunctionArguments rasterFunctionArguments = (IArithmeticFunctionArguments) new ArithmeticFunctionArguments(); // Set the parameters for the function: // Specify the operation as addition (esriRasterPlus) rasterFunctionArguments.Operation = esriRasterArithmeticOperation.esriRasterPlus; // Specify the first operand, i.e. the Raster Dataset opened above. rasterFunctionArguments.Raster = myRasterDataset; // For the second operand, create an array of double values // containing the scalar value to be used as the second operand // to each band of the input dataset. // The number of values in the array should equal the number // of bands of the input dataset. double[] scalars = { 128.0, 128.0, 128.0 }; // Create a new Scalar object and specify // the array as its value. IScalar scalarVals = new ScalarClass(); scalarVals.Value = scalars; // Specify the scalar object as the second operand. rasterFunctionArguments.Raster2 = scalarVals; // Create the Raster Function object. IRasterFunction rasterFunction = new ArithmeticFunction(); rasterFunction.PixelType = rstPixelType.PT_USHORT; // Create the Function Raster Dataset Object. IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a name object for the Function Raster Dataset. IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetName(); // Specify the output filename for the new dataset (including // the .afr extension at the end). functionRasterDatasetName.FullName = outputDataset; functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the new Function Raster Dataset with the Raster Function // and its arguments. functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // QI for the Temporary Dataset interface ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; // and make it a permanent dataset. This creates the afr file. myTempDset.MakePermanent(); // Report Console.WriteLine("Success."); Console.WriteLine("Press any key..."); Console.ReadKey(); // Shutdown License aoInit.Shutdown(); } catch (Exception exc) { // Report Console.WriteLine("Exception Caught while creating Function Raster Dataset. " + exc.Message); Console.WriteLine("Failed."); Console.WriteLine("Press any key..."); Console.ReadKey(); // Shutdown License aoInit.Shutdown(); } }
static void Main(string[] args) { ESRI.ArcGIS.esriSystem.AoInitialize aoInit; #region Initialize License try { Console.WriteLine("Obtaining license"); ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); aoInit = new AoInitializeClass(); esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic); Console.WriteLine("Ready with license."); } catch (Exception exc) { // If it fails at this point, shutdown the test and ignore any subsequent errors. Console.WriteLine(exc.Message); return; } #endregion try { // Specify input directory and dataset name. string inputWorkspace = @"C:\Data"; string inputDatasetName = "8bitSampleImage.tif"; // Specify output filename. string outputDataset = @"c:\Temp\testArithmaticCS.afr"; // Open the Raster Dataset Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace workspace = workspaceFactory.OpenFromFile(inputWorkspace, 0); IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspace; IRasterDataset myRasterDataset = rasterWorkspace.OpenRasterDataset(inputDatasetName); // Create the Function Arguments object IArithmeticFunctionArguments rasterFunctionArguments = (IArithmeticFunctionArguments)new ArithmeticFunctionArguments(); // Set the parameters for the function: // Specify the operation as addition (esriRasterPlus) rasterFunctionArguments.Operation = esriRasterArithmeticOperation.esriRasterPlus; // Specify the first operand, i.e. the Raster Dataset opened above. rasterFunctionArguments.Raster = myRasterDataset; // For the second operand, create an array of double values // containing the scalar value to be used as the second operand // to each band of the input dataset. // The number of values in the array should equal the number // of bands of the input dataset. double[] scalars = { 128.0, 128.0, 128.0 }; // Create a new Scalar object and specify // the array as its value. IScalar scalarVals = new ScalarClass(); scalarVals.Value = scalars; // Specify the scalar object as the second operand. rasterFunctionArguments.Raster2 = scalarVals; // Create the Raster Function object. IRasterFunction rasterFunction = new ArithmeticFunction(); rasterFunction.PixelType = rstPixelType.PT_USHORT; // Create the Function Raster Dataset Object. IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a name object for the Function Raster Dataset. IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName(); // Specify the output filename for the new dataset (including // the .afr extension at the end). functionRasterDatasetName.FullName = outputDataset; functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the new Function Raster Dataset with the Raster Function // and its arguments. functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // QI for the Temporary Dataset interface ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; // and make it a permanent dataset. This creates the afr file. myTempDset.MakePermanent(); // Report Console.WriteLine("Success."); Console.WriteLine("Press any key..."); Console.ReadKey(); // Shutdown License aoInit.Shutdown(); } catch (Exception exc) { // Report Console.WriteLine("Exception Caught while creating Function Raster Dataset. " + exc.Message); Console.WriteLine("Failed."); Console.WriteLine("Press any key..."); Console.ReadKey(); // Shutdown License aoInit.Shutdown(); } }
//, esriCellsizeType outCellSize = esriCellsizeType.esriCellsizeMinOf, esriExtentType outExtent = esriExtentType.esriExtentIntersectionOf ) /// <summary> /// Will perform an arithmeticOperation on an input raster all bands /// </summary> /// <param name="inRaster1">either IRaster, IRasterDataset, or a valid path pointing to a raster</param> /// <param name="inRaster2">either IRaster, IRasterDataset, a numeric value, or a valid path pointing to a raster</param> /// <param name="op">the type of operation</param> /// <returns>a IRaster that can be used for further analysis</returns> public IFunctionRasterDataset calcArithmaticFunction(object inRaster1, object inRaster2, esriRasterArithmeticOperation op, rstPixelType outRasterType = rstPixelType.PT_FLOAT) { string tempAr = funcDir + "\\" + FuncCnt + ".afr"; IFunctionRasterDataset frDset = new FunctionRasterDatasetClass(); IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass(); frDsetName.FullName = tempAr; frDset.FullName = (IName)frDsetName; IRasterFunction rsFunc = new ArithmeticFunctionClass(); rsFunc.PixelType = outRasterType; //IArithmeticFunctionArguments2 args = new ArithmeticFunctionArgumentsClass(); IArithmeticFunctionArguments args = new ArithmeticFunctionArgumentsClass(); if(isNumeric(inRaster1.ToString())&&isNumeric(inRaster2.ToString())) { Console.WriteLine("Must have at least one raster"); return null; } args.Operation = op; object iR1, iR2; if (isNumeric(inRaster1.ToString())&&!isNumeric(inRaster2.ToString())) { iR2 = createIdentityRaster(inRaster2,rstPixelType.PT_FLOAT); IScalar sc = new ScalarClass(); int bCnt = ((IRasterBandCollection)iR2).Count; float[] d = new float[bCnt]; for (int i = 0; i < bCnt; i++) { d[i] = System.Convert.ToSingle(inRaster1); } sc.Value = d; iR1 = sc; } else if (isNumeric(inRaster2.ToString()) && !isNumeric(inRaster1.ToString())) { iR1 = createIdentityRaster(inRaster1, rstPixelType.PT_FLOAT); IScalar sc = new ScalarClass(); int bCnt = ((IRasterBandCollection)iR1).Count; float[] d = new float[bCnt]; for (int i = 0; i < bCnt; i++) { d[i] = System.Convert.ToSingle(inRaster2); } sc.Value = d; iR2 = sc; } else { iR1 = createIdentityRaster(inRaster1, rstPixelType.PT_FLOAT); iR2 = createIdentityRaster(inRaster2, rstPixelType.PT_FLOAT); IRasterBandCollection rsBc1 = (IRasterBandCollection)iR1; IRasterBandCollection rsBc2 = (IRasterBandCollection)iR2; int bCnt1,bCnt2; bCnt1 = rsBc1.Count; bCnt2 = rsBc2.Count; if (bCnt1 != rsBc2.Count) { int dif = bCnt1-bCnt2; int absDif = Math.Abs(dif); if (dif > 0) { IRaster rsB = createRaster(getBand(iR2, 0)); IRaster[] rsArr = new IRaster[absDif]; for (int i = 0; i < absDif; i++) { rsArr[i] = rsB; } iR2 = compositeBandFunction(rsArr); } else { IRaster rsB = createRaster(getBand(iR1, 0)); IRaster[] rsArr = new IRaster[absDif]; for (int i = 0; i < absDif; i++) { rsArr[i] = rsB; } iR1 = compositeBandFunction(rsArr); } } } args.Raster = iR1; args.Raster2 = iR2; //args.CellsizeType = esriCellsizeType.esriCellsizeMinOf; //args.ExtentType = esriExtentType.esriExtentIntersectionOf; frDset.Init(rsFunc, args); return frDset; }