Paso 3b: creación de un código de cliente para Mapping Service

El siguiente paso es crear una imagen de mapa con las estaciones de metro encontradas, usando Toronto como mapa base. Se requiere usar el método RenderNamedMapWithOverlay de Mapping Service.

Los pasos son:

  1. Crear una instancia de RenderNamedMapWithOverlayRequest.
  2. Establecer el ancho y la atura deseados de la imagen.
  3. Establecer la vista (límites) del mapa.
  4. Establecer el mapa con nombre asignado que se va a generar.
  5. Establecer el tipo MIME (formato de imagen) que se va a generar.
  6. Crear una superposición FeatureLayer a partir de la FeatureCollection que se especifica.
  7. Establecer ciertas propiedades de rótulo, para rotular cada estación de metro con su nombre y distancia en metros.
  8. Crear un tema para dar un estilo reconocible a las estaciones de metro.
  9. Ejecutar la solicitud y arrojar los bytes de la imagen.

A continuación se muestra el código:



/**
 * Returns the image of the map overlayed with Features in the specified FeatureCollection.
 * @param fc the FeatureCollection to render on top of the map.
 * @param centerLongitude the longitude of the center of the map
 * @param centerLatitude the latitude of the center of the map
 * @param zoom the zoom (distance across the map) assumed to be kilometers.
 * @return the image of the map overlayed with Features in the specified FeatureCollection.
 */
private byte[] createMap(FeatureCollection fc, double centerLongitude, double centerLatitude, double zoom) {
    RenderNamedMapWithOverlayRequest request = new RenderNamedMapWithOverlayRequest();
    ZoomAndCenterMapView mapView = new ZoomAndCenterMapView();

    // set the dimensions of the returned image to be 640 x 480
    mapView.setWidth(640);
    mapView.setHeight(480);

    // set the view. in this case we are using the zoom/center view
    Point center = new Point();
    center.setSrsName("epsg:4326");
    center.setPos(new Pos());
    center.getPos().setX(centerLatitude);
    center.getPos().setY(centerLongitude);
    mapView.setMapCenter(center);

    Distance zoomLevel = new Distance();
    zoomLevel.setValue(zoom);
    zoomLevel.setUom(DistanceUnit.KILOMETER);
    mapView.setZoomLevel(zoomLevel);

    request.setMapView(mapView);

    // use the /demo/namedmaps/ontario Named Map for the background
    String namedMap = "/demo/namedmaps/ontario";
    request.setNamedMap(namedMap);

    // we want a GIF
    String mimeType = "image/gif";
    request.setImageMimeType(mimeType);

    // return the image NOT a URL to the image
    request.setReturnImage(true);

    // now create the FeatureCollection overlay using a MemoryTable and a FeatureLayer
    OverlayList overlayList = new OverlayList();
    List<Layer> overlays = overlayList.getOverlay();
    MemoryTable table = new MemoryTable();
    table.setFeatureCollection(fc);

    FeatureLayer layer = new FeatureLayer();
    layer.setTable(table);

    {
        // create the label properties
        MapBasicPointStyle style = new MapBasicPointStyle();

        MapBasicFontSymbol mapBasicSymbol;
        mapBasicSymbol = new MapBasicFontSymbol();
        mapBasicSymbol.setBold(true);
        mapBasicSymbol.setColor("black");
        mapBasicSymbol.setFontName("Arial");
        mapBasicSymbol.setSize((short) 14);
        mapBasicSymbol.setShape(33);
        style.setMapBasicSymbol(mapBasicSymbol);

        LabelProperties labelProperties = new LabelProperties();
        // the label expression is an MI SQL fragment
        labelProperties.setExpression("name + '(' + round(distance) + ' m)'");
        labelProperties.setAllowDuplicate(AllowDuplicateType.ALL);
        labelProperties.setAllowOverlap(true);
        labelProperties.setXOffset(10.0); // a small offset in the X direction so the label does not overwrite the symbol
        layer.setLabelProperties(labelProperties);
    }

    overlays.add(layer);
    request.setOverlayList(overlayList);

    // add theme to set the style of the overlay
    OverrideTheme theme = new OverrideTheme();
    MapBasicPointStyle featureStyle = new MapBasicPointStyle();
    MapBasicFontSymbol mapBasicFontSymbol = new MapBasicFontSymbol();
    mapBasicFontSymbol.setShape(64);
    mapBasicFontSymbol.setColor("red");
    mapBasicFontSymbol.setFontName("MapInfo Transportation");
    mapBasicFontSymbol.setSize((short) 14);
    featureStyle.setMapBasicSymbol(mapBasicFontSymbol);
    theme.setStyle(featureStyle);

    ThemeList themeList = new ThemeList();
    themeList.getTheme().add(theme);
    layer.setThemeList(themeList);

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

    RenderNamedMapWithOverlayResponse response;
    try {
        response = servicePort.renderNamedMapWithOverlay(request);
    } catch (com.mapinfo.midev.service.mapping.ws.v1.ServiceException e) {
        throw new RuntimeException(e);
    }
    MapImage mapImage = response.getMapImage();
    return mapImage.getImage();
}