ArcGIS Runtime SDK for Java: About Using Definition Expressions

The use of a Definition Expression is an extremely powerful technique for displaying a subset of features in a layer that is satisfied by a standard SQL statement. For example, I have a “countries” layer used in my map, but at certain times in my application’s life-cycle I may only want to display Germany on the map. I could apply a temporary definition expression to the layer by giving a query such as “Country_name = ‘GERMANY’”. At this stage, only the polygon feature for Germany will be available for display and querying in that map layer.

ArcGIS applications built with the Runtime SDK for Java (or WPF, by the way) can have this same capability.

Dynamic Layers and LayerInfos

Of course, as you may already know, your Map Packages that you create with ArcGIS for Desktop will honor all definition expressions that have been applied to layers in the map. These can remain static as you use these map layers in your ArcGIS Runtime applications.

However, this discussion explains how the client API can be used to dynamically apply and remove definition expressions on the fly during application runtime.

The first thing to keep in mind if you decide to use this technique is that it is a function of the DynamicLayer capability of the Runtime. To be able to apply and remove definition expressions on a layer, you must call setEnableDynamicLayers(true) on that layer before using definition expressions.

The key object to use in the model for making this work is the LayerInfo class. It has setDefinitionExpression(String expression) and getDefinitionExpression() members.

Try it out

Here is a quick example. We have a Map Package that we want to use in an application. It has a layer in it called “cities” that we want to apply a definition expression to. Later, we want to “unset” or “clear” away that expression.

Here we go:

Create your layer, setEnableDynamicLayers(true) and add it to the map.

        
final ArcGISLocalDynamicMapServiceLayer localMSLayer =
            new ArcGISLocalDynamicMapServiceLayer("C:\\data\\mpks\\USCitiesStates.mpk"); 
localMSLayer.setEnableDynamicLayers(true);
map.getLayers().add(localMSLayer);

An ArcGISLocalDynamicMapServiceLayer instance contains a collection of LayerInfos, one for each layer in the ArcGISLocalDynamicMapServiceLayer. You will want to keep a reference to this collection in case you want to clear away the applied definition expression.

...
private DynamicLayerInfoCollection defaultInfosColl ;
...
 map.addMapEventListener(new MapEventListener(){

	public void mapDispose(MapEvent event) {

	}

	public void mapExtentChanged(MapEvent event) {

	}

	public void mapReady(MapEvent event) {
		defaultInfosColl = localMSLayer.getDynamicLayerInfos();
		toolBar.setEnabled(true);
	}

});

Once the map is ready and fully initialized, you can get the LayerInfo for your layer and set a definition expression. In this case, layer (0) is the cities layer I want to set the expression on.

DynamicLayerInfo citiesInfo = localMSLayer.getDynamicLayerInfos().get(0);
citiesInfo.setDefinitionExpression("STFIPS = '06'");
localMSLayer.refresh();

When you want to dynamically clear all the definitions, use the original DynamicLayerInfoCollection and set it back to the ArcGISLocalDynamicMapServiceLayer.

localMSLayer.setDynamicLayerInfos(defaultInfosColl);
This entry was posted in Developer and tagged , . Bookmark the permalink.

Leave a Reply

One Comment

  1. coloncm says:

    Can I do that with Footprint’s symbols by category and labels by class’ settings from a Mosaic Dataset Layer?