arcgissamples\spatialanalyst\CalculateSlope.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.RasterSurfaceOp; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; public class CalculateSlope { public CalculateSlope() { } public static void main(String[] args) { System.out.println("Starting CalculateSlope - 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 inRaster = "gtopo30_n_relief_e.jp2"; String outputPath = getOutputDir(); CalculateSlope slopeCalculator = new CalculateSlope(); Raster raster = slopeCalculator.calculateSlope(inputPath, inRaster, 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(); } } /** * Calculates slope using the RasterSurfaceOp.slope() method * * @param sourcePath * @param sourceName * @param outputPath * @return * @throws IOException */ public Raster calculateSlope(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 RasterSurfaceOp operator RasterSurfaceOp surOp = new RasterSurfaceOp(); //create the output raster workspace RasterWorkspace outputRasterWS = new RasterWorkspace(rasterWSFactory.openFromFile(outputPath, 0)); surOp.setOutWorkspaceByRef(outputRasterWS); // perform spatial operation and return Raster Raster output = new Raster(surOp.slope(rasterDataset, 1, null)); return output; } /** * Initializes the lowest available ArcGIS License */ private 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(); } } /** * 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; } }