arcgissamples\spatialanalyst\InterpolateFeatureToRaster.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 com.esri.arcgis.datasourcesraster.Raster; import com.esri.arcgis.geoanalyst.FeatureClassDescriptor; import com.esri.arcgis.geoanalyst.RasterInterpolationOp; import com.esri.arcgis.geoanalyst.RasterRadius; import com.esri.arcgis.geoanalyst.esriRasterEnvSettingEnum; import com.esri.arcgis.geodatabase.FeatureClass; import com.esri.arcgis.geodatabase.IWorkspace; import com.esri.arcgis.geoprocessing.GPUtilities; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; public class InterpolateFeatureToRaster { public InterpolateFeatureToRaster() { } public static void main(String[] args) { System.out.println("Starting InterpolateFeatureToRaster - 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 + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; String inputFC = "wind.shp"; GPUtilities gpUtilities = new GPUtilities(); FeatureClass inputFeatureClass = new FeatureClass(gpUtilities.openFeatureClassFromString(inputPath + File.separator + inputFC)); InterpolateFeatureToRaster interpolator = new InterpolateFeatureToRaster(); Raster raster = interpolator.interpolateNaturalNeighbors(inputFeatureClass, "VELOCITY"); System.out.println("Result Raster in " + inputPath + ": " + "Raster Height: " + raster.getHeight() + ", " + "Raster Width: " + raster.getWidth() + ", " + "Raster Count: " + raster.getCount()); aoInit.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * This function takes a point feature class and creates a raster in the same folder that is an interpolation of the * point data * * @param pointFeatureClass * - a feature class containing point data with at least 1 quantitative field * @param fieldName * - the name of the field you want to use for interpolation * @return - A raster of the interpolation */ public Raster interpolateNaturalNeighbors(FeatureClass pointFeatureClass, String fieldName) { Raster result = null; try { // first create the feature descriptor. The String is the field we are going to use to interpolate FeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptor(); featureClassDescriptor.create(pointFeatureClass, null, fieldName); // make a RasterInterpolationOp, set it's cell size and output workspace RasterInterpolationOp rasterInterpolationOp = new RasterInterpolationOp(); rasterInterpolationOp.setCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, new Double(0.25)); IWorkspace workspace = pointFeatureClass.getWorkspace(); rasterInterpolationOp.setOutWorkspaceByRef(workspace); // Create raster radius and specify variable distance, pick up 12 neighbors no matter their distance RasterRadius rasterRadius = new RasterRadius(); rasterRadius.setVariable(12, null); result = new Raster(rasterInterpolationOp.iDW(featureClassDescriptor, 1, rasterRadius, null)); } catch (Exception e) { System.out.println("Error occured in interpolation operation."); e.printStackTrace(); } return result; } /** * Initializes the lowest available ArcGIS License */ 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(); } } }