//执行栅格函数 public void Init() { try { //创建NDVI栅格函数参数对象 INDVIFunctionArguments ndviFunctionArguments = (INDVIFunctionArguments) new NDVIFunctionArguments(); //设置近红外波段信息 ndviFunctionArguments.InfraredBandID = InfraredBandID; //设置红波段信息 ndviFunctionArguments.VisibleBandID = VisibleBandID; //设置要处理的栅格 ndviFunctionArguments.Raster = m_raster; //创建栅格函数 IRasterFunction rasterFunction = new NDVIFunction(); //创建栅格函数数据集对象 IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); //设置栅格函数数据集名称对象,设置其临时文件存储地点 IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; //执行栅格函数 functionRasterDataset.Init(rasterFunction, ndviFunctionArguments); //将执行结果存入成员变量中 IRasterDataset2 rstDatasetr2 = (IRasterDataset2)functionRasterDataset; m_raster = rstDatasetr2.CreateFullRaster(); } catch (System.Exception ex)//异常处理,输出错误信息 { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Init() { try { IConvolutionFunctionArguments rasterFunctionArguments = (IConvolutionFunctionArguments) new ConvolutionFunctionArguments(); //设置输入栅格数据 rasterFunctionArguments.Raster = m_raster; rasterFunctionArguments.Type = (esriRasterFilterTypeEnum)type; //创建Raster Function对象 IRasterFunction rasterFunction = new ConvolutionFunction(); IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\\RDB" + "\\" + outputRaster; functionRasterDataset.FullName = (IName)functionRasterDatasetName; functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); IRasterDataset rasData = functionRasterDataset as IRasterDataset; m_raster = ((IRasterDataset2)rasData).CreateFullRaster(); } catch (System.Exception ex)//异常处理,输出错误信息 { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Init() { XmlNode Zfactor = m_xmlNode.SelectSingleNode("Zfactor"); double Zfactorvalue; if (Zfactor != null) { Zfactorvalue = double.Parse(Zfactor.InnerText); IRaster2 raster2 = m_raster as IRaster2; ISlopeFunctionArguments slopeFunctionArugments = (ISlopeFunctionArguments) new SlopeFunctionArguments(); slopeFunctionArugments.DEM = raster2; slopeFunctionArugments.ZFactor = Zfactorvalue; IRasterFunction rasterFunction = new SlopeFunction(); IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; functionRasterDataset.Init(rasterFunction, slopeFunctionArugments); IRasterDataset rstDataset = functionRasterDataset as IRasterDataset; m_raster = rstDataset.CreateDefaultRaster(); } else { MessageBox.Show(" no Zfactor", "ERROR"); } }
//执行山体阴影操作 public void Init() { //遍历子节点读取参数 foreach (XmlNode xnl1 in m_xmlnode.ChildNodes) { if (xnl1 is XmlComment) { continue; } XmlElement xe = (XmlElement)xnl1; //找到Azimuth节点,读取其参数 if (xe.Name == "Azimuth") { m_azimuth = double.Parse(xe.InnerText); } //找到ZFactor节点,读取其参数 if (xe.Name == "ZFactor") { m_zfactor = double.Parse(xe.InnerText); } } //山体阴影的实现 try { //将m_raster转为IRaster接口实行栅格相关操作 IRaster2 raster2 = m_raster as IRaster2; //使用IHillshadeFunctionArguments接口进行山体阴影操作 IHillshadeFunctionArguments hillshadeFunctionArugments = (IHillshadeFunctionArguments) new HillshadeFunctionArguments(); //设置生成山体阴影所需要的参数Azimuth和ZFactor的值 hillshadeFunctionArugments.Azimuth = m_azimuth; hillshadeFunctionArugments.ZFactor = m_zfactor; //设置数据源为该raster hillshadeFunctionArugments.DEM = raster2; //创建一个IRasterFunction IRasterFunction rasterFunction = new HillshadeFunction(); //创建IFunctionRasterDataset以调用Init方法执行函数 IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); //设置IFunctionRasterDataset的相关参数 IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; //调用Init执行 functionRasterDataset.Init(rasterFunction, hillshadeFunctionArugments); //所得结果转为IRasterDataset IRasterDataset rasData = functionRasterDataset as IRasterDataset; //修改m_raster m_raster = rasData.CreateDefaultRaster(); } catch (System.Exception ex)//捕获异常,输出异常信息 { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static void CreateMathFunctionRasterDataset() { //Create the Raster Function object and Function Arguments object for first operation IRasterFunction rasterFunction1 = new MathFunction(); IMathFunctionArguments mathFunctionArguments1 = new MathFunctionArguments() as IMathFunctionArguments; //Specify operation to be "Plus" for the first operation mathFunctionArguments1.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionPlus; //Specify input rasters to the operation IRasterDataset ras01 = OpenRasterDataset("c:\\data\\test", "degs"); IRasterDataset ras02 = OpenRasterDataset("c:\\data\\test", "negs"); mathFunctionArguments1.Raster = ras01; mathFunctionArguments1.Raster2 = ras02; //Create and initialize 1st function raster dataset with the Raster Function object and its arguments object IFunctionRasterDataset functionRasterDataset1; functionRasterDataset1 = new FunctionRasterDataset(); functionRasterDataset1.Init(rasterFunction1, mathFunctionArguments1); //Create the Raster Function and the Function Arguments object for the 2nd operation IRasterFunction rasterFunction2 = new MathFunction(); IMathFunctionArguments mathFunctionArguments2 = new MathFunctionArguments() as IMathFunctionArguments; //Specify operation to be "Divide" for the 2nd operation mathFunctionArguments2.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionDivide; //Specify input rasters to the 2nd operation //Use the output function raster dataset from the 1st operation as one of the input mathFunctionArguments2.Raster = functionRasterDataset1; IRasterDataset ras03 = OpenRasterDataset("c:\\data\\test", "cost"); mathFunctionArguments2.Raster2 = ras03; //Create and initialize the 2nd function raster dataset IFunctionRasterDataset functionRasterDataset2; functionRasterDataset2 = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetName(); functionRasterDatasetName.FullName = "c:\\output\\math_out.afr"; functionRasterDataset2.FullName = (IName)functionRasterDatasetName; functionRasterDataset2.Init(rasterFunction2, mathFunctionArguments2); //Save the 2nd function raster dataset ITemporaryDataset temporaryDataset = (ITemporaryDataset)functionRasterDataset2; temporaryDataset.MakePermanent(); }
//执行山体阴影操作 public void Init() { //遍历子节点读取参数 foreach (XmlNode xnl1 in m_xmlnode.ChildNodes) { if (xnl1 is XmlComment) { continue; } XmlElement xe = (XmlElement)xnl1; if (xe.Name == "Azimuth") { m_azimuth = double.Parse(xe.InnerText); } if (xe.Name == "ZFactor") { m_zfactor = double.Parse(xe.InnerText); } } //山体阴影的实现 try { IRaster2 raster2 = m_raster as IRaster2; IHillshadeFunctionArguments hillshadeFunctionArugments = (IHillshadeFunctionArguments) new HillshadeFunctionArguments(); hillshadeFunctionArugments.Azimuth = m_azimuth; hillshadeFunctionArugments.ZFactor = m_zfactor; hillshadeFunctionArugments.DEM = raster2; IRasterFunction rasterFunction = new HillshadeFunction(); IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); //随机生成文件名 //Random ran = new Random(); //int rannum = ran.Next(1000); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; functionRasterDataset.Init(rasterFunction, hillshadeFunctionArugments); IRasterDataset rasData = functionRasterDataset as IRasterDataset; //修改m_raster m_raster = rasData.CreateDefaultRaster(); } catch (System.Exception ex)//捕获异常,输出异常信息 { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Init() { IRaster2 raster2 = m_raster as IRaster2; IRasterFunction rasterFunction = new AspectFunction(); IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; functionRasterDataset.Init(rasterFunction, raster2); IRasterDataset rstDataset = functionRasterDataset as IRasterDataset; m_raster = rstDataset.CreateDefaultRaster(); }
public static bool AddWatermarkToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { try { // Create Watermark Function IRasterFunction rasterFunction = new CustomFunction.WatermarkFunction(); // Create the Watermark Function Arguments object IWatermarkFunctionArguments rasterFunctionArguments = new WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = watermarkImagePath; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // Set the Raster Dataset as the input raster rasterFunctionArguments.Raster = RasterDataset; // Create Function Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a Function Raster Dataset Name object IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetName(); // Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = System.IO.Path.Combine(OutputFolder, OutputName); functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the Function Raster Dataset with the function and // its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // Save as Function Raster Dataset as an .afr file ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); Console.WriteLine("Generated " + OutputName + "."); Console.WriteLine("Success."); return(true); } catch (Exception exc) { Console.WriteLine("Exception Caught while adding watermark to Raster Dataset: " + exc.Message); Console.WriteLine("Failed."); return(false); } }
public static void CreateMathFunctionRasterDataset() { //Create the Raster Function object and Function Arguments object for first operation IRasterFunction rasterFunction1 = new MathFunction(); IMathFunctionArguments mathFunctionArguments1 = new MathFunctionArguments() as IMathFunctionArguments; //Specify operation to be "Plus" for the first operation mathFunctionArguments1.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionPlus; //Specify input rasters to the operation IRasterDataset ras01 = OpenRasterDataset("c:\\data\\test", "degs"); IRasterDataset ras02 = OpenRasterDataset("c:\\data\\test", "negs"); mathFunctionArguments1.Raster = ras01; mathFunctionArguments1.Raster2 = ras02; //Create and initialize 1st function raster dataset with the Raster Function object and its arguments object IFunctionRasterDataset functionRasterDataset1; functionRasterDataset1 = new FunctionRasterDataset(); functionRasterDataset1.Init(rasterFunction1, mathFunctionArguments1); //Create the Raster Function and the Function Arguments object for the 2nd operation IRasterFunction rasterFunction2 = new MathFunction(); IMathFunctionArguments mathFunctionArguments2 = new MathFunctionArguments() as IMathFunctionArguments; //Specify operation to be "Divide" for the 2nd operation mathFunctionArguments2.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionDivide; //Specify input rasters to the 2nd operation //Use the output function raster dataset from the 1st operation as one of the input mathFunctionArguments2.Raster = functionRasterDataset1; IRasterDataset ras03 = OpenRasterDataset("c:\\data\\test", "cost"); mathFunctionArguments2.Raster2 = ras03; //Create and initialize the 2nd function raster dataset IFunctionRasterDataset functionRasterDataset2; functionRasterDataset2 = new FunctionRasterDataset(); IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName(); functionRasterDatasetName.FullName = "c:\\output\\math_out.afr"; functionRasterDataset2.FullName = (IName)functionRasterDatasetName; functionRasterDataset2.Init(rasterFunction2, mathFunctionArguments2); //Save the 2nd function raster dataset ITemporaryDataset temporaryDataset = (ITemporaryDataset)functionRasterDataset2; temporaryDataset.MakePermanent(); }
public static bool AddNDVICustomToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string bandIndices) { try { // Create NDVI Custom Function IRasterFunction rasterFunction = new CustomFunction.NDVICustomFunction(); // Create the NDVI Custom Function Arguments object INDVICustomFunctionArguments rasterFunctionArguments = new NDVICustomFunctionArguments(); // Set the Band Indices rasterFunctionArguments.BandIndices = bandIndices; // Set the RasterDataset as the input raster rasterFunctionArguments.Raster = RasterDataset; // Create Function Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a Function Raster Dataset Name object IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetName(); // Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = System.IO.Path.Combine(OutputFolder, OutputName); functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the Function Raster Dataset with the function and // its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // Save as Function Raster Dataset as an .afr file ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); Console.WriteLine("Generated " + OutputName + "."); Console.WriteLine("Success."); return(true); } catch (Exception exc) { Console.WriteLine("Exception Caught while adding NDVI Custom Function to Raster Dataset: " + exc.Message); Console.WriteLine("Failed."); return(false); } }
//执行函数 public void Init() { try { //创建Stretch栅格函数参数对象 IStretchFunctionArguments stretchFunctionArguments = (IStretchFunctionArguments) new StretchFunctionArguments(); //设置要处理的栅格 stretchFunctionArguments.Raster = m_raster; //设置拉伸类型 stretchFunctionArguments.StretchType = (esriRasterStretchType)StretchType; if (cmb_StretchType.SelectedItem.ToString() == "StandardDeviation")//如果是按标准差拉伸,需要设置参数N { //设置参数N stretchFunctionArguments.NumberOfStandardDeviations = NumberOfStandardDeviations; } else if (cmb_StretchType.SelectedItem.ToString() == "PercentMinimumMaximum")//如果是按最大最小百分比拉伸,需要设置最大最小百分比 { //设置最大最小百分比 stretchFunctionArguments.MinPercent = MinPercent; stretchFunctionArguments.MaxPercent = MaxPercent; } //创建栅格函数 IRasterFunction rasterFunction = new StretchFunction(); //创建栅格函数数据集对象 IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); //设置栅格函数数据集名称对象,设置其临时文件存储地点 IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName) new FunctionRasterDatasetNameClass(); functionRasterDatasetName.FullName = @"D:\RDB"; functionRasterDataset.FullName = (IName)functionRasterDatasetName; //执行栅格函数 functionRasterDataset.Init(rasterFunction, stretchFunctionArguments); //将执行结果存入成员变量中 IRasterDataset2 rstDatasetr2 = (IRasterDataset2)functionRasterDataset; m_raster = rstDatasetr2.CreateFullRaster(); } catch (System.Exception ex)//异常处理,输出错误信息 { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
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(); } }
private ISimpleSurface GetRasterSurface( [NotNull] IRaster sourceRaster, [NotNull] IEnvelope box, [CanBeNull] out IDataset memoryRasterDataset) { // Remark: // surface values outside 'raster'.envelope (but within 'box') are 0 Assert.ArgumentNotNull(sourceRaster, nameof(sourceRaster)); Assert.ArgumentNotNull(box, nameof(box)); IRasterFunction rasterFunction = new ClipFunctionClass(); IClipFunctionArguments functionArguments = new ClipFunctionArgumentsClass(); functionArguments.Raster = sourceRaster; IEnvelope clipBox = GetClipBox(box, sourceRaster); functionArguments.Extent = clipBox; functionArguments.ClippingGeometry = clipBox; functionArguments.ClippingType = esriRasterClippingType.esriRasterClippingOutside; IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); functionRasterDataset.Init(rasterFunction, functionArguments); functionRasterDataset.RasterInfo.NoData = (float)-9999; // unsicher, wie die memory verwaltung ist // zum sicherstellen der memory-verwaltung // --> inmemory raster erstellen var save = (ISaveAs2)functionRasterDataset; IWorkspaceName wsName = WorkspaceUtils.CreateInMemoryWorkspace("raster"); var ws = (IWorkspace)((IName)wsName).Open(); save.SaveAs("clipped", ws, "MEM"); IRasterDataset rasterDataset = ((IRasterWorkspace2)ws).OpenRasterDataset("clipped"); IRaster rasterData = rasterDataset.CreateDefaultRaster(); // Problems and workarounds for raster(-mosaic) data in surfaces // ------------------------------------------------------------- // // Extent completly within footprint of mosaic dataset: -> no NoDataValues -> no problems // Extent partly within footprint of mosaic dataset : -> double.NaN returned for values outside footprint -> no problems (TODO: verifiy) // Extent completely outside footprint of mosaic dataset, but within extent of mosaic data set: // 0-values returned for Null-raster values // workaround: ((IRasterProps) rasterData).NoDataValue = (float)0; works, if ((IRasterProps) raster).NoDataValue = null and no Height == 0 // workaround to test: use CustomPixelFilter (see https://github.com/Esri/arcobjects-sdk-community-samples/tree/master/Net/Raster/CustomNodataFilter/CSharp // Extent completely outside extent of mosaic dataset // 0-values returned for Null-raster values, rasterData.Height = 1, rasterData.Width = 1 // workaround 1: use custom RasterSurface class. // workaround 2: ((IRasterProps) rasterData).NoDataValue = (float)0; works, if ((IRasterProps) raster).NoDataValue = null and no Height == 0 memoryRasterDataset = (IDataset)rasterDataset; ISimpleSurface rasterSurface = RasterReference.CreateSurface(rasterData); return(rasterSurface); }
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 { #region Specify input directory and dataset name // The directory which contains the Panchromatic Image. string panDir = @"C:\Data\QB\Pan"; // The directory which contains the Multispectral Image. string rgbDir = @"C:\Data\QB\MS"; string panImageName = "05JAN27104436-P1BS-005533787010_01_P002.TIF"; string rgbImageName = "05JAN27104436-M1BS-005533787010_01_P002.TIF"; // Output filename. string outputDataset = @"c:\Temp\QBTemplateCS.afr"; #endregion #region Initialize // Setup Workspaces. Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IRasterWorkspace panRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(panDir, 0); IRasterWorkspace rgbRasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(rgbDir, 0); // Open Datasets IRasterDataset panDataset = panRasterWorkspace.OpenRasterDataset(panImageName); IRasterDataset rgbDataset = rgbRasterWorkspace.OpenRasterDataset(rgbImageName); #endregion #region Create Variables // Create one variable of type IRasterFunctionVariable for each // Raster Dataset opened above // Create a new Raster Function Variable IRasterFunctionVariable panVar = new RasterFunctionVariableClass(); // Set the name of the variable panVar.Name = "panImage"; // Describe the variable panVar.Description = "Panchromatic Image to be used for pansharpening"; // Specify whether it represents a dataset panVar.IsDataset = true; // Create a new Raster Function Variable IRasterFunctionVariable rgbVar = new RasterFunctionVariableClass(); // Set the name of the variable rgbVar.Name = "rgbImage"; // Describe the variable rgbVar.Description = "Multispectral Image to be used for pansharpening"; // Specify whether it represents a dataset rgbVar.IsDataset = true; #endregion #region Prepare the Pan Image // Setup statistics for each band IArray statsArrayPan = new ArrayClass(); IRasterStatistics statsPanBand = new RasterStatisticsClass(); statsPanBand.Minimum = 1; statsPanBand.Maximum = 2047; statsArrayPan.Add(statsPanBand); // Create the arguments object for the stretching function IStretchFunctionArguments stretchingPanFunctionArguements = new StretchFunctionArgumentsClass(); // Set the stretching type stretchingPanFunctionArguements.StretchType = esriRasterStretchType.esriRasterStretchMinimumMaximum; // Set the statistics created above stretchingPanFunctionArguements.Statistics = statsArrayPan; // Set the input raster, in this case, the variable for the Pan Image stretchingPanFunctionArguements.Raster = panVar; // Create the function object to stretch the Pan Image. IRasterFunction stretchingPanFunction = new StretchFunction(); // Create a Raster Function Template object for the stretch function IRasterFunctionTemplate stretchingPanFunctionT = new RasterFunctionTemplateClass(); // Set the function on the template stretchingPanFunctionT.Function = (IRasterFunction)stretchingPanFunction; // Set the arguments for the function stretchingPanFunctionT.Arguments = stretchingPanFunctionArguements; #endregion #region Prepare the Multispectral (RGB) Image // Create an array which defines the order of bands ILongArray bandIDs = new LongArrayClass(); bandIDs.Add(2); bandIDs.Add(1); bandIDs.Add(0); // Create an Extract Band Function Arguments object IExtractBandFunctionArguments extractRgbBandFunctionArgs = (IExtractBandFunctionArguments) new ExtractBandFunctionArguments(); // Set the order of bands of the output extractRgbBandFunctionArgs.BandIDs = bandIDs; // Set the input raster, in this case the variable for the Multispectral Image extractRgbBandFunctionArgs.Raster = rgbVar; // Create the Extract Band Function object IRasterFunction extractRgbBandFunction = new ExtractBandFunction(); // Create a Raster Function Template object for the function created above IRasterFunctionTemplate extractRgbBandFunctionT = new RasterFunctionTemplateClass(); // Set the function on the template extractRgbBandFunctionT.Function = (IRasterFunction)extractRgbBandFunction; // Set the arguments for the function extractRgbBandFunctionT.Arguments = extractRgbBandFunctionArgs; // Setup statistics for each band IArray statsArray = new ArrayClass(); IRasterStatistics statsMulBand1 = new RasterStatisticsClass(); statsMulBand1.Minimum = 100; statsMulBand1.Maximum = 1721; statsArray.Add(statsMulBand1); IRasterStatistics statsMulBand2 = new RasterStatisticsClass(); statsMulBand2.Minimum = 95; statsMulBand2.Maximum = 2047; statsArray.Add(statsMulBand2); IRasterStatistics statsMulBand3 = new RasterStatisticsClass(); statsMulBand3.Minimum = 34; statsMulBand3.Maximum = 2006; statsArray.Add(statsMulBand3); // Create a stretching function for the multispectral image IRasterFunction stretchingRGBFunction = new StretchFunction(); // Create an arguments object for the stretch function IStretchFunctionArguments stretchingRGBFunctionArguments = new StretchFunctionArgumentsClass(); // Set the type of stretchings to perform stretchingRGBFunctionArguments.StretchType = esriRasterStretchType.esriRasterStretchMinimumMaximum; // Set the statistics created above stretchingRGBFunctionArguments.Statistics = statsArray; // Set the extract band function template created above as the input stretchingRGBFunctionArguments.Raster = extractRgbBandFunctionT; // Create a Raster Function Template object for the function created above IRasterFunctionTemplate stretchingRGBFunctionT = new RasterFunctionTemplateClass(); // Set the function on the template stretchingRGBFunctionT.Function = (IRasterFunction)stretchingRGBFunction; // Set the arguments for the function stretchingRGBFunctionT.Arguments = stretchingRGBFunctionArguments; #endregion #region Pansharpen the Pan Image with the Multispectral // Create a Raster Function Arguments object for the pansharpen function IPansharpeningFunctionArguments pansharpFunctionArguements = new PansharpeningFunctionArgumentsClass(); // Set the Panchromatic image which has been prepared above pansharpFunctionArguements.PanImage = stretchingPanFunctionT; // Set the Multispectral image which has been prepared above pansharpFunctionArguements.MSImage = stretchingRGBFunctionT; // Create a new pansharpen raster function object IRasterFunction pansharpenFunction = new PansharpeningFunction(); // Create a new pansharpen function arguments object IPansharpeningFunctionArguments pansharpenFunctionArguements = new PansharpeningFunctionArgumentsClass(); // Set the pansharpening type pansharpenFunctionArguements.PansharpeningType = esriPansharpeningType.esriPansharpeningESRI; // Create an array of doubles that sets the weights for each band IDoubleArray weightsArray = new DoubleArrayClass(); weightsArray.Add(0.167); weightsArray.Add(0.166); weightsArray.Add(0.166); // and set it on the arguments object pansharpenFunctionArguements.Weights = weightsArray; // Create a Raster Function Template object for the pansharpen function IRasterFunctionTemplate rasterFunction = new RasterFunctionTemplateClass(); rasterFunction.Function = (IRasterFunction)pansharpenFunction; rasterFunction.Arguments = pansharpFunctionArguements; ((IRasterFunction)rasterFunction).PixelType = rstPixelType.PT_UCHAR; #endregion #region Resolve variables // Create a PropertySet object to set the values for the // Raster Function Variables created above IPropertySet variables = new PropertySet(); variables.SetProperty("panImage", panDataset); variables.SetProperty("rgbImage", rgbDataset); #endregion #region Create the Function Raster Dataset // Create the Function Raster Dataset Object for the Pansharpened Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a name object for the Function Raster Dataset. IFunctionRasterDatasetName functionRasterDatasetName = new FunctionRasterDatasetNameClass(); // 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 // Template and the property set containing the variables and their values. functionRasterDataset.Init((IRasterFunction)rasterFunction, variables); ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); #endregion #region Shutdown Console.WriteLine("Success."); Console.WriteLine("Press any key..."); Console.ReadKey(); // Shutdown License aoInit.Shutdown(); #endregion } catch (Exception exc) { #region Shutdown 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(); #endregion } }
public static bool AddWatermarkToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { try { // Create Watermark Function IRasterFunction rasterFunction = new CustomFunction.WatermarkFunction(); // Create the Watermark Function Arguments object IWatermarkFunctionArguments rasterFunctionArguments = new WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = watermarkImagePath; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // Set the Raster Dataset as the input raster rasterFunctionArguments.Raster = RasterDataset; // Create Function Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a Function Raster Dataset Name object IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName(); // Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = System.IO.Path.Combine(OutputFolder, OutputName); functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the Function Raster Dataset with the function and // its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // Save as Function Raster Dataset as an .afr file ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); Console.WriteLine("Generated " + OutputName + "."); Console.WriteLine("Success."); return true; } catch (Exception exc) { Console.WriteLine("Exception Caught while adding watermark to Raster Dataset: " + exc.Message); Console.WriteLine("Failed."); return false; } }
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(); } }
public static bool AddNDVICustomToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string bandIndices) { try { // Create NDVI Custom Function IRasterFunction rasterFunction = new CustomFunction.NDVICustomFunction(); // Create the NDVI Custom Function Arguments object INDVICustomFunctionArguments rasterFunctionArguments = new NDVICustomFunctionArguments(); // Set the Band Indices rasterFunctionArguments.BandIndices = bandIndices; // Set the RasterDataset as the input raster rasterFunctionArguments.Raster = RasterDataset; // Create Function Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a Function Raster Dataset Name object IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName(); // Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = System.IO.Path.Combine(OutputFolder, OutputName); functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the Function Raster Dataset with the function and // its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // Save as Function Raster Dataset as an .afr file ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); Console.WriteLine("Generated " + OutputName + "."); Console.WriteLine("Success."); return true; } catch (Exception exc) { Console.WriteLine("Exception Caught while adding NDVI Custom Function to Raster Dataset: " + exc.Message); Console.WriteLine("Failed."); return false; } }