Error while solving a network analysis layer for directions using ArcObjectsJavaAPI

2777
5
11-10-2010 03:22 AM
Highlighted
New Contributor
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.INAServerPropertySets;
import com.esri.arcgis.networkanalyst.INAServerRouteResults;
import com.esri.arcgis.networkanalyst.INAServerSolverParams;
import com.esri.arcgis.networkanalyst.INAStreetDirections;
import com.esri.arcgis.networkanalyst.NAServerRouteParams;
import com.esri.arcgis.networkanalyst.esriNAServerLayerType;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectExtensionManager;
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.esriNAServerRouteLayer);  
   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(mapCtx.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.NAServerPropertySets");
   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(ImageDisplay.getClsid()));
  imageDesc = new IImageDescriptionProxy(context.createObject(ImageDescription.getClsid()));
  IImageType imageType = new IImageTypeProxy(context.createObject(ImageType.getClsid()));
  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;
 
  }

}
Tags (2)
Reply
0 Kudos
5 Replies
Highlighted
Occasional Contributor
Hi Srinath,

A couple of suggestions I can make on your code:
- try using default "en-US" instead of "english": routeParams.setDirectionsLanguage("en-US").
- add routeParams.setReturnRouteGeometries(false) call: Routes and RouteGeometries outputs are redundant and NAServer discourages returning them both populated.

If it still doesn't work, please review directions settings on the network dataset you are working with. With the changes mentioned above, your code works well against published Network Analyst Tutorial's SanFrancisco.mxd, so you can always use SanFrancisco network as a reference while defining directions on the custom network.


More general question: Do you have any specific requirements limiting you to work with NAServer objects via DCOM? Can you switch to SOAP or REST API?


Thanks,
Dmitry
Reply
0 Kudos
Highlighted
New Contributor
Hello;
i develop on silverlight.
if returndirection=false and returnroute=true then, no problem. The result is view as just routeresult.
when i set returndirections=true, that directionresult does always return null and also having an error which "error solving route".
What is the reason, is my arcgis server 9.3.1 limited or what is else?
Reply
0 Kudos
Highlighted
Occasional Contributor
Hello, Fatih,

Try building exactly the same route with driving directions using your network dataset in ArcMap.

If building driving directions in ArcMap works fine, go through the server log files and see if more detailed error message was written there: set the server Log Level to Detailed, send the request you are having problem with, and look into the ArcGIS Server log files (located <ArcGIS>\server\user\log).

If server log does not contain helpful information, please show the HTTP request your Silverlight application sends to server (you can use Fiddler: http://www.fiddler2.com/Fiddler2/version.asp)

Dmitry
Reply
0 Kudos
Highlighted
New Contributor
Hello Dmitry;
Thank you very much for your reply.
Yes, i read logs but i have not found to do anything.
If i set parameters as returndirections=true and call routetask execute, the following log details are inserted on log.

"Method failed.HRESULT = 0x80045034 : This is a FACILITY_ITF error that is specific to the interface that returned this error. See the documentation of the interface that returned this error for information about this HRESULT."

If i call the route task as directionresult=false, there are no problem the route can return.

Thank you again.

Fatih
Reply
0 Kudos
Highlighted
Occasional Contributor
Hi Fatih,

Can you please show both http requests:

  • http request with NO directions which works;

  • http request with directions which does not work.


Can you also tell which particular network dataset you are using on the server?

Thanks,
Dmitry
Reply
0 Kudos