arcgissamples\networkanalyst\SolveRoute.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. * */ package arcgissamples.networkanalyst; import java.io.File; import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory; import com.esri.arcgis.display.CancelTracker; import com.esri.arcgis.geodatabase.DENetworkDataset; import com.esri.arcgis.geodatabase.FeatureCursor; import com.esri.arcgis.geodatabase.GPMessages; import com.esri.arcgis.geodatabase.IDataset; import com.esri.arcgis.geodatabase.IDatasetContainer2; import com.esri.arcgis.geodatabase.IDatasetContainer2Proxy; import com.esri.arcgis.geodatabase.IFeatureClass; import com.esri.arcgis.geodatabase.IFeatureClassContainer; import com.esri.arcgis.geodatabase.IFeatureClassContainerProxy; import com.esri.arcgis.geodatabase.IFeatureDataset; import com.esri.arcgis.geodatabase.IFeatureDatasetExtension; import com.esri.arcgis.geodatabase.IFeatureDatasetExtensionContainer; import com.esri.arcgis.geodatabase.IFeatureDatasetExtensionContainerProxy; import com.esri.arcgis.geodatabase.IFeatureWorkspace; import com.esri.arcgis.geodatabase.IFeatureWorkspaceProxy; import com.esri.arcgis.geodatabase.IWorkspaceFactory; import com.esri.arcgis.geodatabase.NetworkDataset; import com.esri.arcgis.geodatabase.QueryFilter; import com.esri.arcgis.geodatabase.esriDatasetType; import com.esri.arcgis.geoprocessing.GeoProcessor; import com.esri.arcgis.geoprocessing.tools.datamanagementtools.SaveToLayerFile; import com.esri.arcgis.networkanalyst.INAClass; import com.esri.arcgis.networkanalyst.INAClassFieldMap; import com.esri.arcgis.networkanalyst.INAClassLoader; import com.esri.arcgis.networkanalyst.INAContext; import com.esri.arcgis.networkanalyst.INALayer; import com.esri.arcgis.networkanalyst.INASolver; import com.esri.arcgis.networkanalyst.NAClassFieldMap; import com.esri.arcgis.networkanalyst.NAClassLoader; import com.esri.arcgis.networkanalyst.NAContext; import com.esri.arcgis.networkanalyst.NALayer; import com.esri.arcgis.networkanalyst.NARouteSolver; import com.esri.arcgis.networkanalyst.esriNAOutputLineType; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.IStringArray; import com.esri.arcgis.system.ITrackCancel; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class SolveRoute { public SolveRoute() { } public static void main(String[] args) { System.out.println("Starting SolveRoute - 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"); if (devKitHome == null) { System.err.println("Environment variable 'agsdevkitjava' not set. Exiting application"); System.exit(-2); } String workspacePath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "SanFrancisco" + File.separator + "SanFrancisco.gdb"; String inputStopsFeatureDataset = "Analysis"; String inputStopsFC = "Stores"; String shapeInputNameField = "Name"; String networkDatasetFeatureDataset = "Transportation"; String networkDatasetName = "Streets_ND"; String outDataPath = getOutputDir(); String outLayerPath = outDataPath + File.separator + "route.lyr"; File outData = new File(outDataPath); outData.mkdirs(); SolveRoute routeSolver = new SolveRoute(); routeSolver.solveRoute( workspacePath ,inputStopsFeatureDataset, inputStopsFC ,shapeInputNameField ,networkDatasetFeatureDataset, networkDatasetName ,outLayerPath ); System.out.println("Done.\nGenerated layer in " + outData.getAbsolutePath()); System.out.println("Add it to a map to see the results."); aoInit.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * Solves a route based on specified network dataset and stops fc * @param workspacePath * @param inputStopsFC * @param shapeInputNameField * @param networkDatasetName * @param outLayerPath */ public void solveRoute( String workspacePath ,String inputStopsFeatureDataset, String inputStopsFC ,String shapeInputNameField ,String networkDatasetFeatureDataset, String networkDatasetName ,String outLayerPath ){ // Open the feature workspace, input feature class, and network dataset try { //open file geodatabase IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory(); IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy(workspaceFactory.openFromFile(workspacePath, 0)); //open input feature class IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset(inputStopsFeatureDataset); IFeatureClassContainer inputStopsFeatureCC = new IFeatureClassContainerProxy(featureDataset); IFeatureClass inputStopsFClass = inputStopsFeatureCC.getClassByName(inputStopsFC); //open network dataset featureDataset = featureWorkspace.openFeatureDataset(networkDatasetFeatureDataset); IFeatureDatasetExtensionContainer fdsExtCont = new IFeatureDatasetExtensionContainerProxy(featureDataset); IFeatureDatasetExtension fdsExt = fdsExtCont.findExtension(esriDatasetType.esriDTNetworkDataset); IDatasetContainer2 dsCont = new IDatasetContainer2Proxy(fdsExt); IDataset dataset = dsCont.getDatasetByName(esriDatasetType.esriDTNetworkDataset, networkDatasetName); NetworkDataset networkDataset = new NetworkDataset(dataset); // Create the Route NALayer INALayer naLayer = createRouteAnalysisLayer("Route", networkDataset); INAContext naContext = naLayer.getContext(); INAClass stopsNAClass = (INAClass) naContext.getNAClasses().getItemByName("Stops"); // Load the Stops INAClassFieldMap naClassFieldMap = new NAClassFieldMap(); naClassFieldMap.setMappedField("Name", shapeInputNameField); INAClassLoader naLoader = new NAClassLoader(); naLoader.setLocatorByRef(naContext.getLocator()); naLoader.setNAClassByRef(stopsNAClass); naLoader.setFieldMapByRef(naClassFieldMap); int[] rowsInCursor = { 0 }; int[] rowsLocated = { 0 }; ITrackCancel cancelTracker = new CancelTracker(); FeatureCursor cursor = new FeatureCursor(inputStopsFClass.search(new QueryFilter(), false)); naLoader.load(cursor, cancelTracker, rowsInCursor, rowsLocated); // Solve INASolver naSolver = naContext.getSolver(); naSolver.solve(naContext, new GPMessages(), cancelTracker); // Save the layer to disk saveLayerToDisk(naLayer, outLayerPath); } catch (Exception e) { e.printStackTrace(); } } /** * Creates a Route Analysis layer using specified network dataset * @param layerName * @param networkDataset * @return */ private INALayer createRouteAnalysisLayer(String layerName, NetworkDataset networkDataset) { NALayer naLayer = null; try { NARouteSolver naRouteSolver = new NARouteSolver(); // Get the NetworkDataset's Data Element DENetworkDataset deNetworkDataset = (DENetworkDataset) networkDataset.getDataElement(); // Create the NAContext and bind to it NAContext naContext = (NAContext) naRouteSolver.createContext(deNetworkDataset, layerName); naContext.bind(networkDataset, new GPMessages()); // Create the NALayer naLayer = (NALayer) naRouteSolver.createLayer(naContext); naLayer.setName(layerName); // Set some properties on the the route solver interface naRouteSolver.setFindBestSequence(true); naRouteSolver.setPreserveFirstStop(true); naRouteSolver.setPreserveLastStop(false); naRouteSolver.setUseTimeWindows(false); naRouteSolver.setOutputLines(esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure); // Set some properties on the general INASolverSettings interface IStringArray restrictions = naRouteSolver.getRestrictionAttributeNames(); restrictions.add("Oneway"); naRouteSolver.setRestrictionAttributeNamesByRef(restrictions); // Update the context based on the changes made to the solver // settings naRouteSolver.updateContext(naContext, deNetworkDataset, new GPMessages()); } catch (Exception e) { e.printStackTrace(); } // Return the layer... return naLayer; } /** * Saves specified layer to disk as .lyr file, using GeoProcessing * @param inLayer * @param outLayer */ private void saveLayerToDisk(INALayer inLayer, String outLayer) { // Here, use GeoProcessing to save the layer to disk... try { GeoProcessor gp = new GeoProcessor(); gp.setOverwriteOutput(true); // Never use the default constructor for a GP tool.... SaveToLayerFile sTool = new SaveToLayerFile(inLayer, outLayer); // Execute the tool... gp.execute(sTool, null); } catch (Exception e) { e.printStackTrace(); } } /** * Initializes the lowest available ArcGIS License */ private static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); } else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } else { System.err.println("Could not initialize an Engine or ArcView license. Exiting application."); System.exit(-1); } aoInit.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork); } 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; } }