Etape 3a : Création du code client pour Feature Service

La création de SearchNearestRequest est un processus en plusieurs étapes, car nous devons préciser les points suivants :

  • La géométrie à partir de laquelle effectuer la recherche
  • Le nombre maximal de candidats souhaités
  • Le nom et l'emplacement de chaque station de métro et sa distance
  • La table nommée à interroger

Les étapes sont les suivantes :

  1. Créez une instance de SearchNearestRequest et renseignez-la avec les paramètres souhaités.
  2. Appelez la méthode searchNearest et obtenez SearchNearestResponse.
  3. Renvoyez FeatureCollection depuis SearchNearestResponse.

Voici le 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();
    searchPoint.setSrsName("epsg:4326");
    Pos pos = new Pos();
    pos.setX(longitude);
    pos.setY(latitude);
    searchPoint.setPos(pos);

    // set the search geometry which in this case is a point
    request.setGeometry(searchPoint);

    // we want at most 5 candidates
    request.setMaxNumberOfCandidates(5);

    // return the distance from the search point to each found point as an attribute called 'distance'
    request.setReturnedDistanceAttributeName(DISTANCE_ATTRIBUTE_NAME);
    request.setReturnedDistanceUnit(DISTANCE_UNITS);

    // specify the attributes to return. in this case we want the name of the subway station and the geometry
    AttributeList projectionList = new AttributeList();
    projectionList.getAttributeName().add("Name");
    projectionList.getAttributeName().add("Obj");
    request.setAttributeList(projectionList);

    // specify the Named Table to search against
    NamedTable table = new NamedTable();
    table.setName("/demo/stations");
    request.setTable(table);

    // 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);
    }
}