Step 3a: Create the Client Code for the Feature Service

Creating the SearchNearestRequest is a multi-step process as we need to specify:

  • the geometry to search from
  • the maximum candidates desired
  • the name and location of, and distance to, each subway station
  • the named table to query

The steps are:

  1. Create an instance of SearchNearestRequest and populate it with the desired parameters.
  2. Make the call to the searchNearest method and get the SearchNearestResponse.
  3. Return the FeatureCollection from SearchNearestResponse.

Here is the code:

 * Returns a FeatureCollection of the 5 subway stations nearest the specified point.
 * The specified point is assumed to be in WGS 84 (i.e. long/lat).
 * @param longitude the longitude (i.e. X ordinate) of the point to search from.
 * @param latitude the latitude (i.e. Y ordinate) of the point to search from.
 * @return a FeatureCollection of the 5 subway stations nearest the specified point.
private FeatureCollection findNearest(double longitude, double latitude) {
    // create the request and populate it along the way
    SearchNearestRequest request = new SearchNearestRequest();

    // create the search point
    Point searchPoint = new Point();
    Pos pos = new Pos();

    // set the search geometry which in this case is a point

    // we want at most 5 candidates

    // return the distance from the search point to each found point as an attribute called 'distance'

    // specify the attributes to return. in this case we want the name of the subway station and the geometry
    AttributeList projectionList = new AttributeList();

    // specify the Named Table to search against
    NamedTable table = new NamedTable();

    // create the client-side instance/interface to the FeatureService and specify the security settings to access the service
    FeatureService featureServiceBootstrap = new FeatureService();
    FeatureServiceInterface servicePort = featureServiceBootstrap.getPort(FeatureServiceInterface.class);
    ((BindingProvider)servicePort).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, ADMIN_USERNAME);
    ((BindingProvider)servicePort).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, ADMIN_PASSWORD);

    // query the Feature Service and return the resulting FeatureCollection
    try {
        SearchNearestResponse response = servicePort.searchNearest(request);
        return response.getFeatureCollection();
    } catch (ServiceException e) {
        throw new RuntimeException(e);