Unique Value Renderer

Download Sample Application

A unique value renderer allows you to define a single symbol for every feature with a specific attribute value. In this sample each state has been provided with a color according to the value of its ‘Sub-Region’ attribute. For example a state with a ‘Sub-Region’ attribute value of ‘Pacific’ has been given the polygon fill color of green.


//symbolize using a renderer
AGSUniqueValueRenderer *uniqueValueRenderer = [[AGSUniqueValueRenderer alloc] init];
uniqueValueRenderer.defaultSymbol = [AGSSimpleFillSymbol simpleFillSymbol];
uniqueValueRenderer.fields = @[@"SUB_REGION"];

//array to store unique values
NSMutableArray *uniqueValuesArray = [NSMutableArray array];
    
//define unique value symbols
AGSSimpleFillSymbol *pacific = [AGSSimpleFillSymbol simpleFillSymbol];
pacific.color = [[NSColor greenColor] colorWithAlphaComponent:0.6];
[uniqueValuesArray addObject:[AGSUniqueValue uniqueValueWithValue:@"Pacific" symbol:pacific]];
...
//repeat for each unique value

//set unique values array to renderer
uniqueValueRenderer.uniqueValues = uniqueValuesArray;

//create the feature layer and apply the unique value renderer
AGSFeatureLayer *featureLayer = 
  [AGSFeatureLayer featureServiceLayerWithURL:[NSURL URLWithString:@"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"] 
  mode:AGSFeatureLayerModeOnDemand];
featureLayer.delegate = self;
featureLayer.outFields = @[@"*"];
featureLayer.renderer = uniqueValueRenderer;
[self.mapView addMapLayer:featureLayer withName:@"US States"];
    

Sample Code

//SWIFT SAMPLE CODE
/*
Copyright 2014 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Cocoa
import ArcGIS

//layer urls
let kUVBaseMapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
let kUVFeatureLayerURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"

class UniqueValueRendererSwiftSample: NSViewController, AGSLayerDelegate {

    @IBOutlet weak var mapView:AGSMapView!
    
    //MARK: - awakeFromNib
    
    // -------------------------------------------------------------------------------
    //  awakeFromNib
    // -------------------------------------------------------------------------------
    override func awakeFromNib() {
    
        //enable wrap around
        self.mapView.enableWrapAround()
        
        //add base layer to map and set delegate to know when layer loads or fails to load
        let baseMapLayer = AGSTiledMapServiceLayer(URL: NSURL(string: kUVBaseMapURL))
        baseMapLayer.delegate = self
        self.mapView.addMapLayer(baseMapLayer, withName:"Base Layer")
        
        //add feature layer to map and set delegate to know when layer loads or fails to load
        //set outFields to return all fields
        //set feature layer's renderer so features will be rendered with accordingly
        let featureLayer = AGSFeatureLayer(URL: NSURL(string: kUVFeatureLayerURL), mode: .OnDemand)
        featureLayer.delegate = self
        featureLayer.outFields = ["*"]
        featureLayer.renderer = self.uniqueValueRenderer()
        self.mapView.addMapLayer(featureLayer, withName:"US States")
        
        //zoom to predefined extend with known spatial reference of the map
        let envelope = AGSEnvelope(xmin: -19839095.588617, ymin:2145730.010627, xmax:-7454984.983701, ymax:11542629.109590, spatialReference:AGSSpatialReference.webMercatorSpatialReference())
        self.mapView.zoomToEnvelope(envelope, animated:true)
    }
    
    //MARK: - Renderer
    
    // -------------------------------------------------------------------------------
    //  uniqueValueRenderer
    // -------------------------------------------------------------------------------
    func uniqueValueRenderer() -> AGSUniqueValueRenderer {
    
        //array to store unique values
        var uniqueValues = Array<AGSUniqueValue>()
        
        //symbolize using a renderer
        let uniqueValueRenderer = AGSUniqueValueRenderer()
        uniqueValueRenderer.defaultSymbol = AGSSimpleFillSymbol()
        uniqueValueRenderer.fields = ["SUB_REGION"]
        
        //define unique value symbols
        let pacific = AGSSimpleFillSymbol()
        pacific.color = NSColor.greenColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("Pacific", symbol: pacific) as AGSUniqueValue)

        let mtn = AGSSimpleFillSymbol()
        mtn.color = NSColor.redColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("Mountain", symbol: mtn) as AGSUniqueValue)
        
        let nEng = AGSSimpleFillSymbol()
        nEng.color = NSColor.blueColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("New England", symbol: nEng) as AGSUniqueValue)
        
        let sAtl = AGSSimpleFillSymbol()
        sAtl.color = NSColor.cyanColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("South Atlantic", symbol: sAtl) as AGSUniqueValue)
        
        let midAtl = AGSSimpleFillSymbol()
        midAtl.color = NSColor.brownColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("Middle Atlantic", symbol: midAtl) as AGSUniqueValue)

        let enCen = AGSSimpleFillSymbol()
        enCen.color = NSColor.yellowColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("East North Central", symbol: enCen) as AGSUniqueValue)
        
        let wnCen = AGSSimpleFillSymbol()
        wnCen.color = NSColor.darkGrayColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("West North Central", symbol: wnCen) as AGSUniqueValue)
        
        let esCen = AGSSimpleFillSymbol()
        esCen.color = NSColor.lightGrayColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("East South Central", symbol: esCen) as AGSUniqueValue)
        
        let wsCen = AGSSimpleFillSymbol()
        wsCen.color = NSColor.magentaColor().colorWithAlphaComponent(0.6)
        uniqueValues.append(AGSUniqueValue.uniqueValueWithValue("West North Central", symbol: wsCen) as AGSUniqueValue)
        
        //set unique values to renderer
        uniqueValueRenderer.uniqueValues = uniqueValues
        
        //return renderer
        return uniqueValueRenderer
    }
    
    //MARK: - Layer Delegate
    
    // -------------------------------------------------------------------------------
    //  layerDidLoad:layer
    // -------------------------------------------------------------------------------
    func layerDidLoad(layer: AGSLayer!) {
        println("Layer \(layer.name) loaded..")
    }
    
    // -------------------------------------------------------------------------------
    //  layer:didFailToLoadWithError:error
    // -------------------------------------------------------------------------------
    func layer(layer: AGSLayer!, didFailToLoadWithError error: NSError!) {
        if let viewWindow = self.view.window {
            let alert = NSAlert()
            alert.messageText = "Layer '\(layer.name)' failed to load"
            alert.informativeText = error.localizedDescription
            alert.beginSheetModalForWindow(viewWindow, modalDelegate:self, didEndSelector:nil, contextInfo:nil)
        }
    }
    
}
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import "UniqueValueRendererSample.h"

//layer urls
#define kBaseMapURL @"http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
#define kFeatureLayerURL @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"

@interface UniqueValueRendererSample ()

@end

@implementation UniqueValueRendererSample

#pragma mark - awakeFromNib

// -------------------------------------------------------------------------------
//  awakeFromNib
// -------------------------------------------------------------------------------
- (void)awakeFromNib {
    
    //enable wrap around
    [self.mapView enableWrapAround];
        
    //add base layer to map and set delegate to know when layer loads or fails to load
	AGSTiledMapServiceLayer *baseMapLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kBaseMapURL]];
    baseMapLayer.delegate = self;
    [self.mapView addMapLayer:baseMapLayer withName:@"Base Layer"];
    
    //add feature layer to map and set delegate to know when layer loads or fails to load
    //set outFields to return all fields
    //set feature layer's renderer so features will be rendered with accordingly
    AGSFeatureLayer *featureLayer = [AGSFeatureLayer featureServiceLayerWithURL:[NSURL URLWithString:kFeatureLayerURL] mode:AGSFeatureLayerModeOnDemand];
    featureLayer.delegate = self;
    featureLayer.outFields = @[@"*"];
    featureLayer.renderer = [self uniqueValueRenderer];
    [self.mapView addMapLayer:featureLayer withName:@"US States"];
    
    //zoom to predefined extend with known spatial reference of the map
    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-19839095.588617 ymin:2145730.010627 xmax:-7454984.983701 ymax:11542629.109590 spatialReference:[AGSSpatialReference webMercatorSpatialReference]];
    [self.mapView zoomToEnvelope:envelope animated:YES];
}

#pragma mark - Renderer

// -------------------------------------------------------------------------------
//  uniqueValueRenderer
// -------------------------------------------------------------------------------
- (AGSUniqueValueRenderer*)uniqueValueRenderer {
    
    //array to store unique values
    NSMutableArray *uniqueValues = [NSMutableArray array];
    
    //symbolize using a renderer
    AGSUniqueValueRenderer *uniqueValueRenderer = [[AGSUniqueValueRenderer alloc] init];
    uniqueValueRenderer.defaultSymbol = [AGSSimpleFillSymbol simpleFillSymbol];
    uniqueValueRenderer.fields = @[@"SUB_REGION"];
    
    //define unique value symbols
    AGSSimpleFillSymbol *pacific = [AGSSimpleFillSymbol simpleFillSymbol];
    pacific.color = [[NSColor greenColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"Pacific" symbol:pacific]];
    
    AGSSimpleFillSymbol *mtn = [AGSSimpleFillSymbol simpleFillSymbol];
    mtn.color = [[NSColor redColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"Mountain" symbol:mtn]];
    
    AGSSimpleFillSymbol *nEng = [AGSSimpleFillSymbol simpleFillSymbol];
    nEng.color = [[NSColor blueColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"New England" symbol:nEng]];
    
    AGSSimpleFillSymbol *sAtl = [AGSSimpleFillSymbol simpleFillSymbol];
    sAtl.color = [[NSColor cyanColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"South Atlantic" symbol:sAtl]];
    
    AGSSimpleFillSymbol *midAtl = [AGSSimpleFillSymbol simpleFillSymbol];
    midAtl.color = [[NSColor brownColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"Middle Atlantic" symbol:midAtl]];
    
    AGSSimpleFillSymbol *enCen = [AGSSimpleFillSymbol simpleFillSymbol];
    enCen.color = [[NSColor yellowColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"East North Central" symbol:enCen]];
    
    AGSSimpleFillSymbol *wnCen = [AGSSimpleFillSymbol simpleFillSymbol];
    wnCen.color = [[NSColor darkGrayColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"West North Central" symbol:wnCen]];
    
    AGSSimpleFillSymbol *esCen = [AGSSimpleFillSymbol simpleFillSymbol];
    esCen.color = [[NSColor darkGrayColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"East South Central" symbol:esCen]];
    
    AGSSimpleFillSymbol *wsCen = [AGSSimpleFillSymbol simpleFillSymbol];
    wsCen.color = [[NSColor magentaColor] colorWithAlphaComponent:0.6];
    [uniqueValues addObject:[AGSUniqueValue uniqueValueWithValue:@"West South Central" symbol:wsCen]];
    
    //set unique values to renderer
    uniqueValueRenderer.uniqueValues = uniqueValues;
    
    //return renderer
    return uniqueValueRenderer;
}

#pragma mark - Layer Delegate

// -------------------------------------------------------------------------------
//  layerDidLoad:layer
// -------------------------------------------------------------------------------
- (void)layerDidLoad:(AGSLayer *)layer {
    NSLog(@"Layer %@ loaded..",layer.name);
}

// -------------------------------------------------------------------------------
//  layer:didFailToLoadWithError:error
// -------------------------------------------------------------------------------
- (void)layer:(AGSLayer *)layer didFailToLoadWithError:(NSError *)error {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:[NSString stringWithFormat:@"Layer '%@' failed to load",layer.name]];
    [alert setInformativeText:[NSString stringWithFormat:@"%@",error]];
    [alert beginSheetModalForWindow:self.view.window modalDelegate:self didEndSelector:nil contextInfo:nil];
}

@end
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import <Cocoa/Cocoa.h>

@interface UniqueValueRendererSample : NSViewController <AGSLayerDelegate>

@property (strong) IBOutlet AGSMapView *mapView;

@end
Feedback on this topic?