English

How To: Add dynamic layer from a existing layer to the map using Java Connector API

Summary

There might be a need to add dynamic layer from an existing layer (a.k.a cloned layer) into the map object using ArcIMS Java Connector API. The thinking behind adding a dynamic layer would be to zoom to either set of features or to an envelope and change the rendering.

Comments within the code discuss:
-------------------------
- How to clone a layer from a existing layer (layer defined in the map configuration file)
- How to add spatial query to the new layer
- How to modify the rendering elements.

Note:
Dynamic layers will not work with ArcMapImage (MXD) service

Procedure


  1. Code:

    try {
    //Create new connection to the server
    //In this example we are using TCP Scheme
    ConnectionProxy cp = new ConnectionProxy();
    cp.setHost("imstest1");
    cp.setPort(5300);
    cp.setConnectionType("TCP");
    cp.setService("NorthAmerica");
    cp.setDisplayMessages(false);

    //Create a new map object for the above service
    Map map = new Map();
    map.initMap(cp,91,true,true,true,true);
    map.setHeight(500);
    map.setWidth(600);
    map.getLegend().setDisplay(false);
    map.setScaleSymbols(true);

    //Create new feature layer
    FeatureLayer flayer = new FeatureLayer("clonedlayer",null,null);
    flayer.setFeatureClass("polygon");
    flayer.setVisible(true);
    flayer.setName("mynewclone");

    //Set the datasource for the new layer
    Dataset ds = new Dataset();
    ds.setFromLayer("States");
    flayer.setDataset(ds);

    //Create a new filter and specify the spatial query
    String whereExp = "STATE_NAME='Nevada'";
    Filter filter = new Filter();
    filter.setWhereExpression(whereExp);
    filter.setGlobalEnvelope(true);

    //Set the filter on the new layer
    flayer.setFilterObject(filter);

    //Create new symbol
    SimplePolygonSymbol sps = new SimplePolygonSymbol();
    sps.setAntialiasing(true);
    sps.setBoundaryColor("0,0,0");
    sps.setBoundaryWidth(2);
    sps.setFillColor("255,255,0");
    sps.setFillTransparency(0.5);
    sps.setFillType("solid");

    //Add to renderer
    SimpleRenderer sr = new SimpleRenderer();
    sr.setSymbol(sps);

    //Create a label symbol
    TextSymbol ts = new TextSymbol();
    ts.setAntialiasing(true);
    ts.setFontSize(9);
    ts.setShadow("45,0,0");

    //Add to renderer
    SimpleLabelRenderer slr = new SimpleLabelRenderer();
    slr.setField("STATE_NAME");
    slr.setSymbol(ts);

    //Create Group renderer
    GroupRenderer gr = new GroupRenderer();

    //Add above two renderes to the group renderer
    gr.addRenderer(sr);
    gr.addRenderer(slr);

    //add group renderer to the new layer
    flayer.setRenderer(gr);

    //add the new layer to the map layer collection
    map.getLayers().add(flayer);

    // Add this line to send GET_IMAGE request rather than GET_FEATURE
    map.sendImageRequest();
    map.refresh();

    String mapURL = map.getMapOutput().getURL();
    System.out.println(mapURL);

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }

    Note:
    Typically Filter object is associated with Map Object for sending a GET_FEATURE request. In this example we are interested in getting an image rather than features. Map.sendImageRequest() method indicates that we would like to send a GET_IMAGE request instead of a GET_FEATURE

  2. Above code uses the following imports :

    Code:

    import com.esri.aims.mtier.io.ConnectionProxy;
    import com.esri.aims.mtier.model.map.Map;
    import com.esri.aims.mtier.model.map.layer.FeatureLayer;
    import com.esri.aims.mtier.model.workspace.Dataset;
    import com.esri.aims.mtier.model.map.layer.query.Filter;
    import com.esri.aims.mtier.model.map.layer.renderer.symbol.*;
    import com.esri.aims.mtier.model.map.layer.renderer.*;


  3. Sample output generated. Note that the new layer is shown in yellow with the label.
    Sample Output showing Nevada Highligted