arcgissamples\spatialanalyst\CalculateCellStatistics.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ /* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt. * */ package arcgissamples.spatialanalyst; import java.io.File; import java.io.IOException; import com.esri.arcgis.datasourcesraster.Raster; import com.esri.arcgis.datasourcesraster.RasterDataset; import com.esri.arcgis.datasourcesraster.RasterWorkspace; import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory; import com.esri.arcgis.geoanalyst.esriGeoAnalysisStatisticsEnum; import com.esri.arcgis.spatialanalyst.RasterLocalOp; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; public class CalculateCellStatistics { public CalculateCellStatistics() { } public static void main(String[] args) { System.out.println("Starting CalculateCellStatistics - An ArcObjects Java SDK Developer Sample"); try { // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); String inputPath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "raster" + File.separator + "rasterworkspace"; String inputRaster = "gtopo30_n_relief_e.jp2"; String outputPath = getOutputDir(); CalculateCellStatistics cellStatsCalculator = new CalculateCellStatistics(); Raster raster = cellStatsCalculator.calculateCellStatistics(inputPath, inputRaster, outputPath); System.out.println("Result Raster: " + "Raster Height: " + raster.getHeight() + ", " + "Raster Width: " + raster.getWidth() + ", " + "Raster Count: " + raster.getCount()); System.out.println("Done. See result raster in " + outputPath); aoInit.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * This sample code demonstrates how to perform cell-by-cell statistics using RasterLocalOp.localStatistics() * * @param inDataPath * : Directory path on disk for input raster dataset * @param inDataName * : Name of the input raster * @param outDataPath * : Output workspace location */ public Raster calculateCellStatistics(String inputPath, String inputRasterName, String outputPath) throws IOException { // get the raster data set to use in the calculation RasterWorkspaceFactory rasterWSFactory = new RasterWorkspaceFactory(); RasterWorkspace inputRasterWS = new RasterWorkspace(rasterWSFactory.openFromFile(inputPath, 0)); RasterDataset rasterDataset = (RasterDataset) inputRasterWS.openRasterDataset(inputRasterName); // create a RasterLocalOp operator and set up the output environment for the operator RasterLocalOp rasLocalOp = new RasterLocalOp(); RasterWorkspace outputRasterWS = new RasterWorkspace(rasterWSFactory.openFromFile(outputPath, 0)); rasLocalOp.setOutWorkspaceByRef(outputRasterWS); /* Compute cell-by-cell (local) statistics. , in this case it calculates the mean of all the bands * of multiband image for each cell. * If you give a single band raster then this function will just return the same raster as input. */ Raster output = new Raster(rasLocalOp.localStatistics(rasterDataset, esriGeoAnalysisStatisticsEnum.esriGeoAnalysisStatsMean)); return output; } /** * Initializes the lowest available ArcGIS License */ /** * Returns output directory * * @return */ private static String getOutputDir() { String userDir; if (System.getProperty("os.name").toLowerCase().indexOf("win") > -1) userDir = System.getenv("UserProfile"); else userDir = System.getenv("HOME"); String outputDir = userDir + File.separator + "arcgis_sample_output"; System.out.println("Creating output directory - " + outputDir); new File(outputDir).mkdir(); return outputDir; } static void initializeArcGISLicenses(AoInitialize ao) { try { if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo); else { System.err.println("Could not initialize an Engine, ArcView, ArcEditor, or ArcInfo license. Exiting application."); System.exit(-1); } ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst); } catch (Exception e) { e.printStackTrace(); } } }