public static void estimateStatistics(IRaster outRaster) { IRasterBandCollection rsbco = (IRasterBandCollection)outRaster; for (int i = 0; i < rsbco.Count; i++) { IRasterBand rsBo = rsbco.Item(i); IRasterStatistics rsBStatso = rsBo.Statistics; if (rsBStatso == null) { rsBStatso = new RasterStatisticsClass(); } rsBStatso.Minimum = 0; rsBStatso.Maximum = 1; rsBStatso.Mean = 0.5; rsBStatso.StandardDeviation = 0.08; } }
public static void estimateStatistics(IRaster outRaster, dem dem) { IRasterBandCollection rsbco = (IRasterBandCollection)outRaster; for (int i = 0; i < rsbco.Count; i++) { IRasterBand rsBo = rsbco.Item(i); IRasterStatistics rsBStatso = rsBo.Statistics; if (rsBStatso == null) { rsBStatso = new RasterStatisticsClass(); } switch (dem) { case dem.NorthSouth: rsBStatso.Minimum = 0; rsBStatso.Maximum = 180; rsBStatso.Mean = 90; rsBStatso.StandardDeviation = 57.6; break; case dem.EastWest: rsBStatso.Minimum = 0; rsBStatso.Maximum = 180; rsBStatso.Mean = 90; rsBStatso.StandardDeviation = 57.6; break; case dem.Slope: rsBStatso.Minimum = 0; rsBStatso.Maximum = 90; rsBStatso.Mean = 45; rsBStatso.StandardDeviation = 26.3; break; case dem.Aspect: rsBStatso.Minimum = 0; rsBStatso.Maximum = 360; rsBStatso.Mean = 180; rsBStatso.StandardDeviation = 115.2; break; default: break; } } }
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 } }
/// <summary> /// Sets band properties on a given dataset including stats, band names and wavelengths. /// </summary> /// <param name="dataset">The dataset to set properties on.</param> /// <param name="dimParser">Dimap parser to read properties from.</param> private void SetBandProperties(IDataset dataset, DiMapParser dimParser) { try { // Set band band props. IRasterKeyProperties rasterKeyProps = (IRasterKeyProperties)dataset; IRasterBandCollection rasterBandColl = (IRasterBandCollection)dataset; int imageNumBands = ((IFunctionRasterDataset)dataset).RasterInfo.BandCount; int dinNumBands = dimParser.NumBands; int[] bandIndexes = new int[dinNumBands]; IStringArray bandNames = new StrArrayClass(); for (int i = 0; i < dinNumBands; ++i) { // Get band index for the first band. bandIndexes[i] = Convert.ToInt16(dimParser.GetBandIndex(i)); // Validate band index. if (bandIndexes[i] > 0 && bandIndexes[i] <= imageNumBands) { // Get Band Name for the index. bandNames.Add(dimParser.GetBandDesc(bandIndexes[i])); // Get Band stats for the index. IRasterStatistics bandStats = new RasterStatisticsClass(); bandStats.Minimum = Convert.ToDouble(dimParser.GetBandStatMin(bandIndexes[i])); bandStats.Maximum = Convert.ToDouble(dimParser.GetBandStatMax(bandIndexes[i])); bandStats.Mean = Convert.ToDouble(dimParser.GetBandStatMean(bandIndexes[i])); bandStats.StandardDeviation = Convert.ToDouble(dimParser.GetBandStatStdDev(bandIndexes[i])); // Set stats on the dataset. ((IRasterBandEdit2)rasterBandColl.Item(bandIndexes[i] - 1)).AlterStatistics(bandStats); // Set Band Name and wavelengths according to the name. rasterKeyProps.SetBandProperty("BandName", (bandIndexes[i] - 1), bandNames.get_Element(i)); SetBandWavelengths(dataset, (bandIndexes[i] - 1)); // Refresh dataset so changes are saved. ((IRasterDataset3)dataset).Refresh(); } } } catch (Exception exc) { string error = exc.Message; } }
/// <summary> /// Remaps values equal to the compare Raster or value cell values = 1. Values less than or greater than compare raster or value = 0 /// </summary> /// <param name="inRaster">string, IRasterDataset, or IRaster</param> /// <param name="vl">value to compare against</param> /// <returns>IRaster</returns> public IFunctionRasterDataset calcEqualFunction(object inRaster, object compareRaster) { IFunctionRasterDataset rs = createIdentityRaster(inRaster); IFunctionRasterDataset outRs = null; if (isNumeric(compareRaster.ToString())) { double vl = System.Convert.ToDouble(compareRaster); IRemapFilter rFilt = new RemapFilterClass(); rstPixelType pType = rs.RasterInfo.PixelType; double max, min; max = 0; min = 0; #region set min max getMinMax(pType, ref max, ref min); #endregion double vlP1 = vl + 0.000001; rFilt.AddClass(min, vl, 0); rFilt.AddClass(vl, vlP1, 1); rFilt.AddClass(vlP1, max, 0); outRs = calcRemapFunction(rs, rFilt); } else { IFunctionRasterDataset minRst = calcArithmaticFunction(rs, compareRaster, esriRasterArithmeticOperation.esriRasterMinus); outRs = calcEqualFunction(minRst, 0); } IRasterInfo2 rsInfo2 = (IRasterInfo2)outRs.RasterInfo; IRasterStatistics rsStats = new RasterStatisticsClass(); rsStats.Mean = 0.5; rsStats.Maximum = 1; rsStats.Minimum = 0; rsStats.StandardDeviation = 0.25; rsStats.SkipFactorX = 1; rsStats.SkipFactorY = 1; rsStats.IsValid = true; for (int i = 0; i < rsInfo2.BandCount; i++) { rsInfo2.set_Statistics(i, rsStats); } return outRs; }
/// <summary> /// calculates a and function /// </summary> /// <param name="rs1"></param> /// <param name="rs2"></param> /// <returns></returns> public IFunctionRasterDataset calcAndFunction(object rs1, object rs2) { IFunctionRasterDataset rs3 = calcGreaterEqualFunction(rs1, 1); IFunctionRasterDataset rs4 = calcGreaterEqualFunction(rs2, 1); IFunctionRasterDataset rs5 = calcArithmaticFunction(rs3, rs4, esriRasterArithmeticOperation.esriRasterPlus); IFunctionRasterDataset outRs = calcEqualFunction(rs5,2); IRasterInfo2 rsInfo2 = (IRasterInfo2)outRs.RasterInfo; IRasterStatistics rsStats = new RasterStatisticsClass(); rsStats.Mean = 0.5; rsStats.Maximum = 1; rsStats.Minimum = 0; rsStats.StandardDeviation = 0.25; rsStats.SkipFactorX = 1; rsStats.SkipFactorY = 1; rsStats.IsValid = true; for (int i = 0; i < rsInfo2.BandCount; i++) { rsInfo2.set_Statistics(i, rsStats); } return outRs; }
public IFunctionRasterDataset constantRasterFunction(IRaster template, IEnvelope NewExtent, double rasterValue, IPnt cellSize) { IFunctionRasterDataset tDset = createIdentityRaster(template, rstPixelType.PT_FLOAT); IConstantFunctionArguments rasterFunctionArguments = (IConstantFunctionArguments)new ConstantFunctionArguments(); rasterFunctionArguments.Constant = rasterValue; IRasterInfo rsInfo = tDset.RasterInfo; rsInfo.NativeExtent = NewExtent; rsInfo.Extent = NewExtent; rsInfo.CellSize = cellSize; rasterFunctionArguments.RasterInfo = rsInfo; string tempAr = funcDir + "\\" + FuncCnt + ".afr"; IFunctionRasterDataset frDset = new FunctionRasterDatasetClass(); IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass(); frDsetName.FullName = tempAr; frDset.FullName = (IName)frDsetName; IRasterFunction rsFunc = new ConstantFunction(); frDset.Init(rsFunc, rasterFunctionArguments); IRasterInfo2 rsInfo2 = (IRasterInfo2)frDset.RasterInfo; IRasterStatistics rsStats = new RasterStatisticsClass(); rsStats.Mean = rasterValue; rsStats.Maximum = rasterValue; rsStats.Minimum = rasterValue; rsStats.StandardDeviation = rasterValue; rsStats.SkipFactorX = 1; rsStats.SkipFactorY = 1; rsStats.IsValid = true; for (int i = 0; i < rsInfo2.BandCount; i++) { rsInfo2.set_Statistics(i, rsStats); } return frDset; }
/// <summary> /// calculates a slope function /// </summary> /// <param name="inRaster"></param> /// <returns></returns> public IFunctionRasterDataset calcSlopeFunction(IRaster inRaster) { IRaster rRst = returnRaster(inRaster,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 SlopeFunctionClass(); ISlopeFunctionArguments args = new SlopeFunctionArgumentsClass(); args.DEM = rRst; args.ZFactor = 1; frDset.Init(rsFunc, args); IRasterStatistics stats = new RasterStatisticsClass(); stats.Mean = 50; stats.Minimum = 0; stats.Maximum = 90; stats.StandardDeviation = 25; stats.IsValid = true; for (int i = 0; i < frDset.RasterInfo.BandCount; i++) { ((IRasterInfo2)frDset.RasterInfo).set_Statistics(i, stats); } return frDset; }
public IFunctionRasterDataset calcRandomForestFunction(object inRaster, Statistics.dataPrepRandomForest rf) { IFunctionRasterDataset rRst = createIdentityRaster(inRaster); string tempAr = funcDir + "\\" + FuncCnt + ".afr"; IFunctionRasterDataset frDset = new FunctionRasterDatasetClass(); IFunctionRasterDatasetName frDsetName = new FunctionRasterDatasetNameClass(); frDsetName.FullName = tempAr; frDset.FullName = (IName)frDsetName; IRasterFunction rsFunc = new FunctionRasters.randomForestDataset(); FunctionRasters.randomForestArguments args = new FunctionRasters.randomForestArguments(this); args.InRasterCoefficients = rRst; args.RandomForestModel = rf; frDset.Init(rsFunc, args); IRasterInfo2 rsInfo2 = (IRasterInfo2)frDset.RasterInfo; IRasterStatistics rsStats = new RasterStatisticsClass(); rsStats.Mean = 0.5; rsStats.Maximum = 1; rsStats.Minimum = 0; rsStats.StandardDeviation = 0.25; rsStats.SkipFactorX = 1; rsStats.SkipFactorY = 1; rsStats.IsValid = true; if (rf.Regression) { double pMin = rf.computNew(rf.minValues)[0]; double pMax = rf.computNew(rf.MaxValues)[0]; double pMean = (pMax-pMin)/2; rsStats.Maximum = rf.maxValues[0]; rsStats.Minimum = rf.minValues[0]; rsStats.Mean = pMean; rsStats.StandardDeviation = pMean * 0.5; } for (int i = 0; i < rsInfo2.BandCount; i++) { rsInfo2.set_Statistics(i, rsStats); } return frDset; }
public IFunctionRasterDataset calcNDVIFunction(object inRaster, int visibleBandId, int irBandId) { IFunctionRasterDataset visRs = getBand(inRaster, visibleBandId); IFunctionRasterDataset irRs = getBand(inRaster, irBandId); IFunctionRasterDataset sRs = calcArithmaticFunction(irRs, visRs, esriRasterArithmeticOperation.esriRasterMinus); IFunctionRasterDataset pRs = calcArithmaticFunction(irRs, visRs, esriRasterArithmeticOperation.esriRasterPlus); IFunctionRasterDataset fDset = calcArithmaticFunction(sRs, pRs, esriRasterArithmeticOperation.esriRasterDivide); IRasterStatistics rsStats = new RasterStatisticsClass(); rsStats.IsValid = true; rsStats.Mean = 0; rsStats.Maximum = 1; rsStats.Minimum = -1; rsStats.StandardDeviation = 0.5; rsStats.SkipFactorX = 1; rsStats.SkipFactorY = 1; ((IRasterInfo2)fDset.RasterInfo).set_Statistics(0, rsStats); return fDset; }