dot
Creating a Feature layer

Feature layers are a special type of Graphics layer that allow you to display features in a layer hosted by an ArcGIS Server map service or in a table served by the MapIt Spatial Data Service. Like Graphics layers, Feature layers contain and display graphic features, which consist of geometry and attributes. The following table highlights feature layer sources supported in this API:

FeatureLayer source Description
ArcGIS Server

Dynamic (non-cached) ArcGIS Server map services can contain feature layers. Each layer is represented by an explicit Url that references the map service name and layer id assigned by the map service. The Url is usually retrieved using ArcGIS Services Directory.

For example:

http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer/8

The California map service contains a layer with county features assigned the unique id 8.

MapIt MapIt includes a REST-ful Web service termed the Spatial Data Service. It provides access to tables in databases hosted by a database instance, such as SQL Server. A table may be spatially-enabled, which means it has a column that contains geometry.

For example:

http://serverapps.esri.com/SDS/databases/Demo/dbo.USStates_Geographic

The Demo database contains a spatially enabled table dbo.USStates_Geographic.



Feature layers have several properties that enable functionality that is not available with Graphics layers. The following table highlights some of the key members included with FeatureLayer:

FeatureLayer member Description
Url property

Address of a layer in an ArcGIS Server map service or table hosted by a MapIt spatial data service (see table above for examples). Feature layers are automatically populated with features from the source.

Where property

Query that defines which features in the specified layer are displayed. If not specified, all features will be retrieved ("1=1").

FeatureSymbol property

References a single symbol to use when rendering individual graphics in the layer.

DisableClientCaching property

By default, a feature layer uses browser caching to improve performance. If the data underlying your feature layer changes (e.g. the table hosted by MapIt's spatial data service is being updated with new content) and you want to see the changes immediately, set this property to true.

Update method

This method will re-query the data underlying your feature layer. If layer or table contents have changed on the server, you can call this method to see changes in the feature layer on the client. Note, you may need to disable client caching.

 

How to add a Feature layer

Like Graphics layers, you will usually declare Feature layers in XAML. They should be inserted inside a Map control element below any base layers. If you have not already done so, create a Silverlight application with a Map. If you do not know how to do this, refer to the Creating a map topic. Inside the Map element, declare a Feature layer with an ID of "MyFeatureLayer":

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer" >
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>

The Url property determines the Map service layer that the Feature layer will use. To find the URL, you can use the ArcGIS Services Directory. See the Discovering Services topic for more information. The code here specifies the cities layer of the ESRI_StatesCitiesRivers_USA service.

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer"
            Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" >
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>

The Where property defines a filter to apply to the specified Map service layer. Only features with attributes that satisfy the filter expression will be drawn. The filter specified below will make it so that the Feature layer only includes features with a value greater than 75000 in the POP1990 field. This corresponds to cities with a 1990 population of greater than 75000. Note that, if you wish to display all the features in the specified layer, simply define a filter expression that is always true, such as "1 = 1."

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer"
            Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
            Where="POP1990 > 75000" >
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>

The FeatureSymbol property defines a symbol to apply to all the features in the layer. In order to specify this property, you first have to declare a Symbol in your XAML. Symbols are included in the ESRI.ArcGIS.Client.Symbols namespace of the ESRI.ArcGIS.Client assembly. So to declare a Symbol, you need to map an XML namespace to the ESRI.ArcGIS.Client.Symbols CLR namepsace:

<UserControl x:Class="SilverlightApp.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

Now a Symbol can be declared as a resource. Typically such a resource will be declared as a resource of the root layout element, which is a Grid by default. Assuming this is the case in your Silverlight application, you can insert the following code above your Map to declare a green diamond marker symbol:

<Grid.Resources>
	<esriSymbols:SimpleMarkerSymbol x:Name="MyMarkerSymbol" Color="Green" Style="Diamond" Size="14" />
</Grid.Resources>

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer"
            Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
            Where="POP1990 > 75000" >
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>

Now you can use the marker symbol to specify the FeatureSymbol. Note that you use the x:Name of the Symbol when binding it:

<Grid.Resources>
	<esriSymbols:SimpleMarkerSymbol x:Name="MyMarkerSymbol" Color="Green" Style="Diamond" Size="14" />
</Grid.Resources>

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer"
            Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
            Where="POP1990 > 75000" FeatureSymbol="{StaticResource MyMarkerSymbol}" >
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>

If you run the application, you will see all the US cities with a 1990 population of more than 75000 marked with a green diamond:

There are two issues with displaying the cities in this way. First, there are many areas of the country where the city symbols overlap, making it impossible to see how many cities are displayed. Second, a relatively large number of Graphics are displayed. Depending on the processing power of the computer the application is running on, this may introduce performance problems in drawing the graphics.

These issues can be alleviated by setting the ClusterFeatures property of the Feature layer to true. See the Working with Clustering topic for additional information on clustering features to provide a more presentable interface and user experience. In cases like the one shown above, this will aggregate multiple point Graphics into fewer point graphics, each of which displays a number showing how many features it represents. After applying a clusterer, the cities layer in the application may appear as follows:

 

With Feature layers, you also have the ability to specify which fields the layer's Graphics will include. The fields specified will be populated with the corresponding values from the Feature layer's Map service layer. The fields are specified as strings within the OutFields property. In order to specify strings as XAML elements, you must first map a XML namespace to the CLR namespace that contains the String class. This class is contained in the System namepsace of the mscorlib assembly. Add the XML namespace as follows:

<UserControl x:Class="SilverlightApp.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

To include the CITY_NAME and POP1990 fields, you would specify the OutFields property as follows. The values of the fields specified here can be displayed by using Silverlight data binding to bind them to MapTips or other data controls (e.g. DataGrids).

<Grid.Resources>
	<esriSymbols:SimpleMarkerSymbol x:Name="MyMarkerSymbol" Color="Green" Style="Diamond" Size="14" />
</Grid.Resources>

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
	<esri:Map.Layers>
		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
		<esri:FeatureLayer ID="MyFeatureLayer"
            Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
            Where="POP1990 > 75000" FeatureSymbol="{StaticResource MyMarkerSymbol}"  ClusterFeatures="True" 
			FlareBackground="#99FF0000" FlareForeground="White" MaximumFlareCount="9" >
			<esri:FeatureLayer.OutFields>
				<sys:String>CITY_NAME</sys:String>
				<sys:String>POP1990</sys:String>
			</esri:FeatureLayer.OutFields>
		</esri:FeatureLayer>
	</esri:Map.Layers>
</esri:Map>