Buffer Operation

Download Sample Application

Buffer a point, line or polygon using the buffer operation provided by the GeometryEngine. Simply sketch new geometries, modify existing geometries, and then see the effect of the buffer operation by clicking on the Buffer button.

@property (strong) AGSGeometryEngine *geometryEngine;

//cache a reference to the default geometry engine
self.geometryEngine = [AGSGeometryEngine defaultGeometryEngine];
    
//buffer geometry using geometry engine
AGSGeometry *bufferedGeometry = [self.geometryEngine bufferGeometry:graphic.geometry byDistance:bufferDistance];       

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

//service urls
let kBOBaseMapURL = "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"

class BufferOperationSwiftSample: NSViewController {
    
    @IBOutlet weak var mapView:AGSMapView!
    @IBOutlet weak var bufferDistanceLabel:NSTextField!
    @IBOutlet weak var bufferDistanceSlider:NSSlider!
    @IBOutlet weak var messageLabel:NSTextField!
    
    var geometryEngine:AGSGeometryEngine!
    var graphicsLayer:AGSGraphicsLayer!
    var sketchLayer:AGSSketchGraphicsLayer!
    var bufferedGraphics = Array<AGSGraphic>()
    var bufferDistance:Int = 0
    var simpleMarkerSymbol:AGSSimpleMarkerSymbol!
    var simpleLineSymbol:AGSSimpleLineSymbol!
    var simpleFillSymbol:AGSSimpleFillSymbol!
    
    // -------------------------------------------------------------------------------
    //  awakeFromNib
    // -------------------------------------------------------------------------------
    override func awakeFromNib() {
    
        //add base layer to map
        let baseMapLayer = AGSTiledMapServiceLayer(URL: NSURL(string: kBOBaseMapURL))
        self.mapView.addMapLayer(baseMapLayer, withName:"Base Layer")
        
        //add  graphics layer to show buffered features
        self.graphicsLayer = AGSGraphicsLayer()
        self.mapView.addMapLayer(self.graphicsLayer, withName:"Graphics Layer")
        
        //add sketch graphics layer and set as mapView's touch delegate so it begin tracking touch events
        self.sketchLayer = AGSSketchGraphicsLayer(geometry: AGSMutablePoint(spatialReference: AGSSpatialReference.webMercatorSpatialReference()))
        self.mapView.touchDelegate = self.sketchLayer
        self.mapView.addMapLayer(self.sketchLayer, withName:"Sketch Layer")
        
        //cache a reference to the default geometry engine
        self.geometryEngine = AGSGeometryEngine.defaultGeometryEngine()
        
        //define marker symbol
        self.simpleMarkerSymbol = AGSSimpleMarkerSymbol()
        self.simpleMarkerSymbol.color = NSColor.redColor()
        self.simpleMarkerSymbol.style = .Circle
        
        //define line symbol
        self.simpleLineSymbol = AGSSimpleLineSymbol()
        self.simpleLineSymbol.color = NSColor.redColor()
        self.simpleLineSymbol.style =  .Dash
        self.simpleLineSymbol.width = 2
        
        //define fill symbol
        self.simpleFillSymbol = AGSSimpleFillSymbol()
        self.simpleFillSymbol.color = NSColor.greenColor().colorWithAlphaComponent(0.4)
        self.simpleFillSymbol.outline.color = NSColor(red: 0, green:128, blue:0, alpha:1.0)
        self.simpleFillSymbol.outline.width = 2
        
        //set buffer distance
        self.bufferDistance = self.bufferDistanceSlider.integerValue
        
        //init buffered graphics array
        self.bufferedGraphics = Array<AGSGraphic>()
        
        //zoom to predefined extend with known spatial reference of the map
        let envelope = AGSEnvelope(xmin: -10049589.670344, ymin:3480099.843772, xmax:-10010071.251113, ymax:3512023.489701, spatialReference:AGSSpatialReference.webMercatorSpatialReference())
        self.mapView.zoomToEnvelope(envelope, animated:true)
    }
    
    //MARK: - Actions
    
    // -------------------------------------------------------------------------------
    //  sketchToolsAction
    // -------------------------------------------------------------------------------
    @IBAction func sketchToolsAction(sender:NSSegmentedControl) {
    
        switch (sender.selectedSegment) {
        
        case 0://point tool
            //sketch layer should begin tracking touch events to sketch a point
            self.mapView.touchDelegate = self.sketchLayer;
            self.sketchLayer.geometry = AGSMutablePoint(spatialReference: self.mapView.spatialReference)
        
        case 1://polyline tool
            //sketch layer should begin tracking touch events to sketch a polyline
            self.mapView.touchDelegate = self.sketchLayer;
            self.sketchLayer.geometry = AGSMutablePolyline(spatialReference: self.mapView.spatialReference)
        
        case 2://polygon tool
            //sketch layer should begin tracking touch events to sketch a polygon
            self.mapView.touchDelegate = self.sketchLayer;
            self.sketchLayer.geometry = AGSMutablePolygon(spatialReference: self.mapView.spatialReference)
            
        default:
            break
        }
    }
    
    // -------------------------------------------------------------------------------
    //  bufferAction
    // -------------------------------------------------------------------------------
    @IBAction func bufferAction(sender:AnyObject) {
    
        //get the sketch geometry
        let sketchGeometry = self.sketchLayer.geometry.copy() as AGSGeometry
        
        //create the graphic and assign it the correct symbol according to its geometry type
        //note: lines and polygons are symbolized with the simple line symbol here
        let graphic = AGSGraphic(geometry: sketchGeometry, symbol:nil, attributes:nil)
        
        //check type of geometry and set symbol
        if sketchGeometry is AGSPoint {
            graphic.symbol = self.simpleMarkerSymbol
        }
        else {
            graphic.symbol = self.simpleLineSymbol
        }
        
        //add graphic to the graphics layer
        self.graphicsLayer.addGraphic(graphic)
        
        //buffer geometry using geometry engine
        let bufferedGeometry = self.geometryEngine.bufferGeometry(sketchGeometry, byDistance:Double(self.bufferDistance))
        
        //create a graphic with buffer geometry and add to graphics layer
        let bufferedGraphic = AGSGraphic(geometry: bufferedGeometry, symbol:self.simpleFillSymbol, attributes:nil)
        
        self.graphicsLayer.addGraphic(bufferedGraphic)
        
        //add graphic to buffered graphics array
        self.bufferedGraphics.append(bufferedGraphic)
        
        //clear sketch
        self.sketchLayer.clear()
        
        //update message
        self.messageLabel.stringValue = "Reset or add another geometry..."
    }
    
    // -------------------------------------------------------------------------------
    //  resetAction
    // -------------------------------------------------------------------------------
    @IBAction func resetAction(sender:AnyObject) {
    
        //update message
        self.messageLabel.stringValue = "Sketch a geometry and tap Buffer button..."
        
        //reset buffered graphics array
        self.bufferedGraphics = Array<AGSGraphic>()
        
        //remove all graphics from graphics layer
        self.graphicsLayer.removeAllGraphics()
        
        //clear sketch layer
        self.sketchLayer.clear()
        
        //set buffer distance indicator
        self.bufferDistanceSlider.intValue = 1500
        
        //set buffer distance
        self.bufferDistance = self.bufferDistanceSlider.integerValue
    
    }
    
    // -------------------------------------------------------------------------------
    //  bufferDistanceChangedAction
    // -------------------------------------------------------------------------------
    @IBAction func bufferDistanceChangedAction(sender:AnyObject) {
    
        //set buffer distance value
        self.bufferDistance = self.bufferDistanceSlider.integerValue
        
        //if there are buffered graphics then update
        if self.bufferedGraphics.count > 0 {
        
            //remove old buffered graphics
            self.graphicsLayer.removeGraphics(self.bufferedGraphics)
            
            //init buffered graphics array
            self.bufferedGraphics = Array<AGSGraphic>()
            
            //loop through all graphics in graphics layer
            for graphic in self.graphicsLayer.graphics {
            
                //buffer geometry using geometry engine
                let bufferedGeometry = self.geometryEngine.bufferGeometry(graphic.geometry, byDistance:Double(self.bufferDistance))
                
                //create a graphic with buffer geometry and add to graphics layer
                let bufferedGraphic = AGSGraphic(geometry: bufferedGeometry, symbol:self.simpleFillSymbol, attributes:nil)
                
                //add to new buffered graphics array
                self.bufferedGraphics.append(bufferedGraphic)
            }
            
            //add buffer graphics to the graphics layer
            self.graphicsLayer.addGraphics(bufferedGraphics)
        }
    }
    
}
//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 "BufferOperationSample.h"

//service urls
#define kBaseMapURL @"http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"

@interface BufferOperationSample ()

@end

@implementation BufferOperationSample

// -------------------------------------------------------------------------------
//  awakeFromNib
// -------------------------------------------------------------------------------
- (void)awakeFromNib {
    
    //add base layer to map
	AGSTiledMapServiceLayer *baseMapLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kBaseMapURL]];
    [self.mapView addMapLayer:baseMapLayer withName:@"Base Layer"];
    
    //add  graphics layer to show buffered features
    self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:self.graphicsLayer withName:@"Graphics Layer"];
        
    //add sketch graphics layer and set as mapView's touch delegate so it begin tracking touch events
    self.sketchLayer = [[AGSSketchGraphicsLayer alloc] initWithGeometry:[[AGSMutablePoint alloc] initWithSpatialReference:[AGSSpatialReference webMercatorSpatialReference]]];
    self.mapView.touchDelegate = self.sketchLayer;
    [self.mapView addMapLayer:self.sketchLayer withName:@"Sketch Layer"];
    
    //cache a reference to the default geometry engine
    self.geometryEngine = [AGSGeometryEngine defaultGeometryEngine];
    
    //define marker symbol
    self.simpleMarkerSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol];
    self.simpleMarkerSymbol.color = [NSColor redColor];
    self.simpleMarkerSymbol.style = AGSSimpleMarkerSymbolStyleCircle;
    
    //define line symbol
	self.simpleLineSymbol = [AGSSimpleLineSymbol simpleLineSymbol];
	self.simpleLineSymbol.color= [NSColor redColor];
    self.simpleLineSymbol.style =  AGSSimpleLineSymbolStyleDash;
	self.simpleLineSymbol.width = 2;
    
    //define fill symbol
    self.simpleFillSymbol = [[AGSSimpleFillSymbol alloc] init];
	self.simpleFillSymbol.color = [[NSColor greenColor] colorWithAlphaComponent:0.4];
    self.simpleFillSymbol.outline.color = [NSColor colorWithRed:0 green:128 blue:0 alpha:1.0];
    self.simpleFillSymbol.outline.width = 2;
    
    //set buffer distance
    self.bufferDistance = [self.bufferDistanceSlider intValue];
    
    //init buffered graphics array
    self.bufferedGraphics = [NSMutableArray array];
    
    //zoom to predefined extend with known spatial reference of the map
    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-10049589.670344 ymin:3480099.843772 xmax:-10010071.251113 ymax:3512023.489701 spatialReference:[AGSSpatialReference webMercatorSpatialReference]];
    [self.mapView zoomToEnvelope:envelope animated:YES];
}

#pragma mark - Actions

// -------------------------------------------------------------------------------
//  sketchToolsAction
// -------------------------------------------------------------------------------
- (IBAction)sketchToolsAction:(id)sender {
    
    switch ([sender selectedSegment]) {
            
		case 0://point tool
			//sketch layer should begin tracking touch events to sketch a point
			self.mapView.touchDelegate = self.sketchLayer;
			self.sketchLayer.geometry = [[AGSMutablePoint alloc] initWithSpatialReference:self.mapView.spatialReference];
			break;
            
		case 1://polyline tool
			//sketch layer should begin tracking touch events to sketch a polyline
			self.mapView.touchDelegate = self.sketchLayer;
			self.sketchLayer.geometry = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference];
			break;
            
		case 2://polygon tool
			//sketch layer should begin tracking touch events to sketch a polygon
			self.mapView.touchDelegate = self.sketchLayer;
			self.sketchLayer.geometry = [[AGSMutablePolygon alloc] initWithSpatialReference:self.mapView.spatialReference];
			break;

		default:
			break;
	}
}

// -------------------------------------------------------------------------------
//  bufferAction
// -------------------------------------------------------------------------------
- (IBAction)bufferAction:(id)sender {
    
    //get the sketch geometry
	AGSGeometry* sketchGeometry = [self.sketchLayer.geometry copy];
       
    //create the graphic and assign it the correct symbol according to its geometry type
    //note: lines and polygons are symbolized with the simple line symbol here
    AGSGraphic* graphic = [AGSGraphic graphicWithGeometry:sketchGeometry symbol:nil attributes:nil];
    
    //check type of geometry and set symbol
    if ([sketchGeometry isKindOfClass:[AGSPoint class]]) {
        graphic.symbol = self.simpleMarkerSymbol;
    }
    else {
        graphic.symbol = self.simpleLineSymbol;
    }
    
    //add graphic to the graphics layer
    [self.graphicsLayer addGraphic:graphic];

    //buffer geometry using geometry engine
    AGSGeometry *bufferedGeometry = [self.geometryEngine bufferGeometry:sketchGeometry byDistance:self.bufferDistance];
    
    //create a graphic with buffer geometry and add to graphics layer
    AGSGraphic *bufferedGraphic = [AGSGraphic graphicWithGeometry:bufferedGeometry symbol:self.simpleFillSymbol attributes:nil];
    
    [self.graphicsLayer addGraphic:bufferedGraphic];

    //add graphic to buffered graphics array
    [self.bufferedGraphics addObject:bufferedGraphic];
    
    //clear sketch
    [self.sketchLayer clear];
    
    //update message
    self.messageLabel.stringValue = @"Reset or add another geometry...";
}

// -------------------------------------------------------------------------------
//  resetAction
// -------------------------------------------------------------------------------
- (IBAction)resetAction:(id)sender {
    
    //update message
    self.messageLabel.stringValue = @"Sketch a geometry and tap Buffer button...";
    
    //reset buffered graphics array
    self.bufferedGraphics = [NSMutableArray array];
    
    //remove all graphics from graphics layer
    [self.graphicsLayer removeAllGraphics];
    
    //clear sketch layer
    [self.sketchLayer clear];
    
    //set buffer distance indicator
    [self.bufferDistanceSlider setIntValue:1500];
    
    //set buffer distance
    self.bufferDistance = [self.bufferDistanceSlider intValue];
    
}

// -------------------------------------------------------------------------------
//  bufferDistanceChangedAction
// -------------------------------------------------------------------------------
- (IBAction)bufferDistanceChangedAction:(id)sender {
    
    //set buffer distance value
    self.bufferDistance = [self.bufferDistanceSlider intValue];
    
    //if there are buffered graphics then update
    if ([self.bufferedGraphics count] > 0) {
        
        //remove old buffered graphics
        [self.graphicsLayer removeGraphics:self.bufferedGraphics];
        
        //init buffered graphics array
        NSMutableArray *bufferedGraphics = [NSMutableArray array];
        
        //loop through all graphics in graphics layer
        for (AGSGraphic *graphic in self.graphicsLayer.graphics) {
            
            //buffer geometry using geometry engine
            AGSGeometry *bufferedGeometry = [self.geometryEngine bufferGeometry:graphic.geometry byDistance:self.bufferDistance];
            
            //create a graphic with buffer geometry and add to graphics layer
            AGSGraphic *bufferedGraphic = [AGSGraphic graphicWithGeometry:bufferedGeometry symbol:self.simpleFillSymbol attributes:nil];
            
            //add to new buffered graphics array
            [bufferedGraphics addObject:bufferedGraphic];
        }
        
        //remember buffer graphics so we can remove them
        self.bufferedGraphics = bufferedGraphics;
        
        //add buffer graphics to the graphics layer
        [self.graphicsLayer addGraphics:bufferedGraphics];
    }
}

@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 BufferOperationSample : NSViewController

@property (strong) IBOutlet AGSMapView *mapView;
@property (strong) IBOutlet NSTextField *bufferDistanceLabel;
@property (strong) IBOutlet NSSlider *bufferDistanceSlider;
@property (strong) IBOutlet NSTextField *messageLabel;
@property (strong) AGSGeometryEngine *geometryEngine;
@property (strong) AGSGraphicsLayer *graphicsLayer;
@property (strong) AGSSketchGraphicsLayer *sketchLayer;
@property (strong) NSMutableArray *bufferedGraphics;
@property (assign) int bufferDistance;
@property (strong) AGSSimpleMarkerSymbol *simpleMarkerSymbol;
@property (strong) AGSSimpleLineSymbol *simpleLineSymbol;
@property (strong) AGSSimpleFillSymbol *simpleFillSymbol;

- (IBAction)sketchToolsAction:(id)sender;
- (IBAction)bufferAction:(id)sender;
- (IBAction)resetAction:(id)sender;
- (IBAction)bufferDistanceChangedAction:(id)sender;

@end
Feedback on this topic?