srinath01

Error while solving a network analysis layer for directions using ArcObjectsJavaAPI

Discussion created by srinath01 on Nov 10, 2010
Hi,

I have created and built a network data set with street data. An MXD with the network analysis layer pointing to the network data set is also present. A map service is also created which points to this MXD and I am trying to access this map service using a process which is coded using ArcObjects API in Java.

My objective is to set two stop points, solve for the route, and get the directions from the start point to the end point using ArcObjects API in a java process.

In the java process, I have set the solver parameters. When the solve method is executed, an Automated Exception is generated. When I comment the setReturnDirections method of the object of class NAServerRouteParams, the code executes without an error, and I get the route plotted on the map as an image. When the setReturnDirections method is set to true, the code errors out.

I tried to manually add two stop points and solve for the route and directions using ArcMap, and I was successful.

Am I missing any other parameter in the code which might also have to be set or is there a problem with the network data set?

Please find below the code,

import java.io.IOException;
import java.util.Date;

import com.esri.arcgis.carto.IImageDescription;
import com.esri.arcgis.carto.IImageDescriptionProxy;
import com.esri.arcgis.carto.IImageDisplay;
import com.esri.arcgis.carto.IImageDisplayProxy;
import com.esri.arcgis.carto.IImageType;
import com.esri.arcgis.carto.IImageTypeProxy;
import com.esri.arcgis.carto.IMapImage;
import com.esri.arcgis.carto.ImageDescription;
import com.esri.arcgis.carto.ImageDisplay;
import com.esri.arcgis.carto.ImageType;
import com.esri.arcgis.carto.esriImageFormat;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.networkanalyst.INAServer;
import com.esri.arcgis.networkanalyst.INAServerPropertySe ts;
import com.esri.arcgis.networkanalyst.INAServerRouteResul ts;
import com.esri.arcgis.networkanalyst.INAServerSolverPara ms;
import com.esri.arcgis.networkanalyst.INAStreetDirections ;
import com.esri.arcgis.networkanalyst.NAServerRouteParams ;
import com.esri.arcgis.networkanalyst.esriNAServerLayerTy pe;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectExtensionManag er;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.IPropertySet;
import com.esri.arcgis.system.IPropertySetArray;
import com.esri.arcgis.system.ServerInitializer;

public class MapServiceTest {

/**
* Class level variables
*/

ServerConnection con;
IServerContext mapCtx = null;
Date date;

INAServerRouteResults routeResults;
INAStreetDirections[] directions;

public static void main(String[] args) {

MapServiceTest mapServiceTest = new MapServiceTest();

mapServiceTest.testService();

System.out.println("********End of Process**********");

}

public void testService(){



//Initialize Server

System.out.println("Start of Testing Network Map Service");
new ServerInitializer().initializeServer("hstst46.hydc .sbc.com", "admin", "admin");
System.out.println("After ServerInitializer");

try{

//Create connection to ArcGIS Server

con = new ServerConnection();
con.connect("hstst46.hydc.sbc.com");
System.out.println("After creating ArcGIS Server connection");

//Create Server Object Manager

System.out.println("Creating Server Object Manager");
IServerObjectManager som = con.getServerObjectManager();

//Create ServerContext

mapCtx = som.createServerContext("new_Route","MapServer");
//mapCtx = som.createServerContext("Route","MapServer");
System.out.println("MapContext : " + mapCtx.toString());

IServerObjectExtensionManager soex = (IServerObjectExtensionManager) mapCtx.getServerObject();
System.out.println("After getting Server Object Extension Manager");

INAServer naServer = (INAServer) soex.findExtensionByTypeName("NAServer");
System.out.println("After getting the network analysis service");

String[] naLayers = naServer.getNALayerNames(esriNAServerLayerType.esr iNAServerRouteLayer);
System.out.println("Network Analysis Layer: " + naLayers[0] + " Length of the array: " + naLayers.length);

String routeLayer = naLayers[0];

INAServerSolverParams solverParams = naServer.getSolverParameters(routeLayer);


NAServerRouteParams routeParams = (NAServerRouteParams) solverParams;
routeParams.setReturnMap(true);
routeParams.setReturnRouteGeometries(true);
date = new Date();
routeParams.setStartTime(date);
routeParams.setImageDescriptionByRef(getImageDesc( mapCtx));
routeParams.setDirectionsLanguage("english");
routeParams.setCreateTraversalResult(true);
routeParams.setFindBestSequence(true);
routeParams.setReturnDirections(true);
routeParams.setReturnRoutes(true);

System.out.println("Network Layer name from routeParams: " + routeParams.getNALayerName());

//Setting the Latitude and Longitude values of Start Point and End Point

//double spx = 38.636581; //Start point Latitude
double spx = 38.365170;
Double spxobj = new Double(spx);
//double spy = -90.192249; //Start point Longitude
double spy = -93.711498;
Double spyobj = new Double(spy);
//double epx = 38.627513; //End point Latitude
double epx = 38.349617;
Double epxobj = new Double(epx);
//double epy = -90.194313; //End point Longitude
double epy = -93.680525;
Double epyobj = new Double(epy);
System.out.println("After setting the Latitude and Longitude values");

//Setting the Property Start Set
IPropertySet propStartSet = (IPropertySet) mapCtx.createObject("esriSystem.PropertySet");
propStartSet.setProperty("Name", "Start Point");
propStartSet.setProperty("X", spyobj); // W
propStartSet.setProperty("Y", spxobj); // N
System.out.println("After setting the Property Start Set");

//Setting the Property End Set
IPropertySet propEndSet = (IPropertySet) mapCtx.createObject("esriSystem.PropertySet");
propEndSet.setProperty("Name", "End Point");
propEndSet.setProperty("X", epyobj);
propEndSet.setProperty("Y", epxobj);
System.out.println("After setting the Property End Set");

//Adding the Property Sets to the array
IPropertySetArray pPropSets = new com.esri.arcgis.system.IPropertySetArrayProxy(mapC tx.createObject("esriSystem.PropertySetArray"));
pPropSets.add(propStartSet);
pPropSets.add(propEndSet);
System.out.println("After setting the Property Array Set");

//Setting the Server Property Sets for the Network Analysis
INAServerPropertySets stopsPropSets = (INAServerPropertySets) mapCtx.createObject("esriNetworkAnalyst.NAServerPr opertySets");
stopsPropSets.setPropertySetsByRef(pPropSets);
System.out.println("After setting the Stop Property Array Set");
routeParams.setStopsByRef(stopsPropSets);

//Solve the Network
solverParams.setMaxSnapTolerance(500000);

System.out.println("Before Solving the network");
routeResults = (INAServerRouteResults) naServer.solve(solverParams); //Error in this portion
System.out.println("After Solving the network");

//Get the directions
directions = routeResults.getDirections();

IMapImage iMapImage = routeResults.getMapImage();

System.out.println("After getting the directions");
System.out.println("Length of the directions array " + directions.length);
System.out.println("URL of the Map Image" + iMapImage.getURL());

}

catch(AutomationException aeExp){

System.out.println("AutomationException Caught: ");
aeExp.printStackTrace();

System.out.println("Code: " + aeExp.getCode());
System.out.println("Description: " + aeExp.getDescription());

}

catch(IOException ioExp){

System.out.println("IOException Caught: ");
ioExp.printStackTrace();
}

catch (Exception exp){

System.out.println("Exception Caught: ");
exp.printStackTrace();
}

finally {
try {
mapCtx.releaseContext();
mapCtx = null;
con = null;
} catch (Exception e) {

System.out.println("Exception while closing the connection and the map context");
e.printStackTrace();
}
}
}

public static IImageDescription getImageDesc(IServerContext context) throws IOException
{

System.out.println("Inside getImageDesc");

IImageDescription imageDesc = null;

IImageDisplay display = new IImageDisplayProxy(context.createObject(ImageDispl ay.getClsid()));
imageDesc = new IImageDescriptionProxy(context.createObject(ImageD escription.getClsid()));
IImageType imageType = new IImageTypeProxy(context.createObject(ImageType.get Clsid()));
imageType.setFormat(esriImageFormat.esriImageJPG);
display.setDeviceResolution(96.0);
display.setHeight(700);
display.setWidth(700);
imageDesc.setDisplay(display);
imageDesc.setType(imageType);

System.out.println("End of getImageDesc");

return imageDesc;

}

}

Outcomes