<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
pageTitle="Example - Using Esri Business Analyst Server API for Flex to execute a Remove Overlap task."
creationComplete="startSample()" xmlns:esri="http://www.esri.com/2008/ags">
<fx:Script>
<![CDATA[
import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.geometry.Extent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.utils.GraphicUtil;
import com.esri.bacore.BATaskCompletedEvent;
import com.esri.bacore.TaskResultOutput;
import com.esri.bacore.client.BAClient;
import com.esri.baserver.Boundaries;
import com.esri.baserver.OverlapRemoverOverlapMethod;
import com.esri.baserver.OverlapRemoverWeightMethod;
//import com.esri.baserver.sampleviewer.BASExampleClient;
//import com.esri.baserver.sampleviewer.Geofence;
import com.esri.baserver.tasks.BAServerClient; // Added
import com.esri.baserver.tasks.tradeareas.RemoveOverlapParameters;
import com.esri.baserver.tasks.tradeareas.RemoveOverlapTask;
import mx.controls.Alert;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
private var _tradeArea1 : Polygon;
private var _tradeArea2 : Polygon;
private var _drawSecond : Boolean = false;
// Initial map extent.
private var _initialExtent :Extent;
/**
* Initialize the polygon draw at sample creating
*/
private function startSample():void
{
_initialExtent = map.extent;
lblGuide.text = "This sample demonstrates setting up multiple trade or service areas " +
"and removing any overlaps between them. To begin, draw a polygon area " +
"to represent the first of two sample trade area polygons.";
drawToolbar.activate(DrawTool.POLYGON);
}
/**
* Event handler for draw completing event;
* capture the graphics from draw tollbar,
* add the graphics to Map and create an object
* based on graphic drawn.
*/
private function onDrawEnd(event:DrawEvent):void
{
var graphic:Graphic = event.graphic;
graphic.autoMoveToTop = false;
/*
if(!Geofence.checkGeometry(graphic.geometry))
{
map.extent = _initialExtent;
return;
}
*/
tradeAreasLayer.add(graphic);
// Capture the polygon geometry from the draw toolbar.
if (event.graphic.geometry is Polygon) {
if (!_drawSecond) {
_tradeArea1 = Polygon(event.graphic.geometry);
_tradeArea1.spatialReference = map.extent.spatialReference;
_drawSecond = true;
drawToolbar.deactivate();
drawToolbar.fillSymbol = sfs2;
drawToolbar.lineSymbol = sls2;
drawToolbar.activate(DrawTool.POLYGON);
lblGuide.text = "Now draw your second and last sample trade area polygon. " +
"In order to best view the results, " +
"be sure that your second area overlaps the first.";
}
else {
_tradeArea2 = Polygon(event.graphic.geometry);
_tradeArea2.spatialReference = map.extent.spatialReference;
_drawSecond = true;
drawToolbar.fillSymbol = sfs2;
drawToolbar.lineSymbol = sls2;
drawToolbar.deactivate();
executeTask();
lblGuide.text = "Task is executing. Please wait."
}
}
}
/**
* Executes the task.
*/
private function executeTask():void
{
if(_tradeArea1 && _tradeArea2){
//create the parameters used by this task
var params : RemoveOverlapParameters = createParameters();
cursorManager.setBusyCursor();
//Get a static reference to a singleton instance of the BAServerClient class.
//Instructions for how to create the BAServerClient class is provided at the top of this file.
//var client : BAClient = BASExampleClient.instance;
var client :BAServerClient = new BAServerClient(); // ADDED
client.url = "http://baserver.esri.com/ArcGIS/baserver/REST/services/RedlandsDemoMap/BAServer"; // ADDED
var task : RemoveOverlapTask = new RemoveOverlapTask(client);
task.execute(params, new mx.rpc.Responder(resultHandler, faultHandler));
}
else{
Alert.show("The polygon is not set.");
}
}
/**
* Creates parameters of the task.
*/
private function createParameters() : RemoveOverlapParameters
{
var params : RemoveOverlapParameters = new RemoveOverlapParameters();
params.outputSpatialReference = map.extent.spatialReference;
params.overlapMethod = OverlapRemoverOverlapMethod.GRID;
params.weightMethod = OverlapRemoverWeightMethod.USE_AREA;
params.areaIDField = "AREA_ID";
// features - from drawn polygon
var features:Array = new Array();
var attr1:Object = {
OID:1,
AREA_ID:"1_1",
STORE_ID:"1",
RING:1,
RING_DEFN:"5",
AREA_DESC:"Area 1",
SALES : "100",
COUNT : "3"};
var feature1:Graphic = new Graphic(_tradeArea1, null, attr1);
features.push(feature1);
var attr2:Object = {
OID:2,
AREA_ID:"2_1",
STORE_ID:"2",
RING:1,
RING_DEFN:"5",
AREA_DESC:"Area 2",
SALES : "300",
COUNT : "5"};
var feature2:Graphic = new Graphic(_tradeArea2, null, attr2);
features.push(feature2);
var fs:FeatureSet = new FeatureSet(features);
fs.geometryType = Geometry.POLYGON;
fs.spatialReference = _tradeArea1.spatialReference;
params.boundaries = new Boundaries( fs );
return params;
}
/**
* Result handler for the successful completion of the task execution.
*/
private function resultHandler(event:BATaskCompletedEvent, token:Object=null):void
{
var taskResultOutput : TaskResultOutput = event.result;
//draw the features on the map
var fs : FeatureSet = taskResultOutput.recordSet;
if(fs){
tradeAreasLayer.clear();
resultLayer.clear();
// add the resulting features to map
for (var i:Number = 0; i < fs.features.length; i++) {
var g : Graphic = fs.features;
// set tooltip from attributes
g.toolTip = fs.features.attributes["AREA_DESC"];
g.autoMoveToTop = false;
resultLayer.add(g);
}
//zoom to the feature extent
map.extent =
GraphicUtil.getGraphicsExtent(resultLayer.graphicProvider.source as Array).expand(1.25);
}
cursorManager.removeBusyCursor();
lblGuide.text = "Task complete. The trade areas with the removed overlapping areas are displayed."
}
/**
* Generalized fault handler for an asynchronous failed task execution.
*/
private function faultHandler(event:FaultEvent, token:Object=null):void
{
var message:String = "An error occurred while executing the underlying Web Service request.";
message += " Please try again later.";
Alert.show(message, "Error");
cursorManager.removeBusyCursor();
lblGuide.text = "Task failed.";
resultLayer.clear();
tradeAreasLayer.clear();
map.extent = _initialExtent;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Set symbol for polygon draw 1 -->
<esri:SimpleLineSymbol id="sls" style="solid" color="0xFF0000" width="1" alpha="1"/>
<esri:SimpleFillSymbol id="sfs" alpha="0.6" color="0xFF0000">
<esri:SimpleLineSymbol color="#F00000" width="4" alpha="1" style="solid"/>
</esri:SimpleFillSymbol>
<!-- Set symbol for polygon draw 2 -->
<esri:SimpleLineSymbol id="sls2" style="solid" color="0x00FF00" width="1" alpha="1"/>
<esri:SimpleFillSymbol id="sfs2" alpha="0.6" color="0x00FF00">
<esri:SimpleLineSymbol color="#F00000" width="4" alpha="1" style="solid"/>
</esri:SimpleFillSymbol>
<!-- Control to user draw -->
<esri:DrawTool id="drawToolbar"
map="{map}"
lineSymbol="{sls}"
fillSymbol="{sfs}"
drawEnd="onDrawEnd(event)" />
<!-- Set renderer for Spatial Overlay output feature class -->
<esri:SimpleFillSymbol id="bFill" alpha="0.5" color="0x0000FF"/>
<esri:SimpleFillSymbol id="gFill" alpha="0.5" color="0x00FF00"/>
<esri:SimpleFillSymbol id="rFill" alpha="0.5" color="0xFF0000"/>
<esri:UniqueValueRenderer id="uniqueValueRenderer" attribute="STORE_ID">
<esri:UniqueValueInfo value="1" symbol="{bFill}"/>
<esri:UniqueValueInfo value="2" symbol="{gFill}"/>
<esri:UniqueValueInfo value="3" symbol="{rFill}"/>
</esri:UniqueValueRenderer>
</fx:Declarations>
<s:controlBarContent>
<s:Label width="100%"
id="lblGuide"
color="#6E6F00"
fontSize="12" />
</s:controlBarContent>
<!-- Map control -->
<esri:Map id="map">
<esri:extent>
<esri:Extent xmin="-13051515.8843363" ymin="4020082.2778777" xmax="-13030416.9436797" ymax="4043380.91587083">
<esri:SpatialReference wkid="3857"/>
</esri:Extent>
</esri:extent>
<esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
<!-- Graphics Layer to draw both user input and resulting features-->
<esri:GraphicsLayer id="tradeAreasLayer" renderer="{uniqueValueRenderer}"/>
<esri:GraphicsLayer id="resultLayer" renderer="{uniqueValueRenderer}"/>
</esri:Map>
</s:Application>