Select to view content in your preferred language

GraphicsLayer geometry of a polygon and buffering

2741
5
08-24-2011 11:27 AM
JanicePeal
Deactivated User
Greetings!  I'm new to programming the mapping components (using the flex 4.5 api) and need to find out how to get the geometries of the result of a GraphicsLayer that is a polygon, and then buffer that.  Using the buffer sample (only mapPoint is available), I want to input the resulting geometries array of the selection into bufferParameters.geometries =[???].  Can I capture this array, and how is it accessed?  I am having trouble finding where to look in the API reference.  Many thanks in advance!
Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Janice,

   Here is all you have to do:

                var graLayAC:ArrayCollection = graphicsLayer.graphicProvider as ArrayCollection;
                if (graLayAC.length > 0) 
                {
                    geomArr = [];
                    for each (var graphic:Graphic in graphicsLayer.graphicProvider)
                    {
                        geomArr.push(graphic.geometry);
                    }
                    //send the geomArr to buffer
                }
0 Kudos
sumedhasilla
Emerging Contributor
Hope you find the following sample helpful. You may click the button to view the buffer applied to the polygon graphic:

<?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:esri="http://www.esri.com/2008/ags"
      pageTitle="Adding graphics using MXML and/or ActionScript">

<s:layout>
  <s:VerticalLayout horizontalAlign="center" paddingTop="3"/>
</s:layout>

<fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.GeometryServiceEvent;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.geometry.Polygon;
   import com.esri.ags.symbols.PictureMarkerSymbol;
   import com.esri.ags.tasks.supportClasses.BufferParameters;
  
   import mx.controls.Alert;
  
   private function bufferPolygonGraphic():void
   {
     //Alert.show(polyGraphic.geometry.type);
  
    var bufferParameters:BufferParameters = new BufferParameters();
    bufferParameters.geometries = [polyGraphic.geometry];//[ drawnLine ];
    bufferParameters.distances = [ 100 ];
    bufferParameters.unit = GeometryService.UNIT_KILOMETER;
    bufferParameters.bufferSpatialReference = new SpatialReference(102100);
    myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
    myGeometryService.buffer(bufferParameters);
   }
  
   function bufferCompleteHandler(event:GeometryServiceEvent):void
   {
    myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
    // Note: As of version 2.0, GeometryService returns geometries (instead of graphics)
    for each (var polygon:Polygon in event.result)
    {
     var graphic:Graphic = new Graphic(polygon);
     graphic.symbol = sfs2;
     graphic.toolTip = "Buffered Stuff";
     myGraphicsLayer.add(graphic);
    
    }
   }
  ]]>
</fx:Script>
<fx:Declarations>
  <esri:GeometryService id="myGeometryService" url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
  <esri:SimpleFillSymbol id="sfs2" alpha="0.4" color="0x990033">
   <esri:SimpleLineSymbol color="0x000000"/>
  </esri:SimpleFillSymbol>
 
</fx:Declarations>


<s:Button id="btn"
     click="bufferPolygonGraphic()"
     label="Buffer Polygon "/>
<esri:Map id="myMap"
     level="2"
     wrapAround180="true">
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
  <esri:GraphicsLayer id="myGraphicsLayer">  
   <esri:Graphic id="polyGraphic" toolTip="Brazilian polygon with a SimpleFillSymbol">
    <esri:geometry>
     <esri:Polygon spatialReference="{new SpatialReference(102100)}">
      <fx:Array>
       <fx:Array>
        <esri:MapPoint x="-3867905" y="-671044"/>
        <esri:MapPoint x="-4533702" y="-2578326"/>
        <esri:MapPoint x="-5316417" y="-2832708"/>
        <esri:MapPoint x="-5844750" y="-3869806"/>
        <esri:MapPoint x="-6333947" y="-3498016"/>
        <esri:MapPoint x="-6412218" y="-1942370"/>
        <esri:MapPoint x="-8211974" y="-954779"/>
        <esri:MapPoint x="-7703209" y="229077"/>
        <esri:MapPoint x="-5736637" y="454597"/>
        <esri:MapPoint x="-3867905" y="-671044"/>
       </fx:Array>
      </fx:Array>
     </esri:Polygon>
    </esri:geometry>
    <esri:symbol>
     <esri:SimpleFillSymbol id="sfs" alpha="0.8" color="0x009933"/>
    
    </esri:symbol>
   
   </esri:Graphic>
  </esri:GraphicsLayer>
</esri:Map>
</s:Application>
0 Kudos
JanicePeal
Deactivated User
Thank you both very much for taking your time to share these samples (I thought I was on an auto-email notification for this post, but apparently I wasn't...I didn't even know someone responded!).

I finally have an array of MapPoints coming in, but something looks askew.  Sample geomArr is in attachment txt file.

My buffer never draws (it never gets to the bufferCompleteHandler).  is that because the extent or spatialReference is null?  Well, in this example, the spatial reference is null, but there's a new spatial reference for every map point, when I figured it would all be the same (which should be 3424 - NJ State Plane Coordinates).

This is my buffer function:

private function bufferPolygonGraphic():void
{
   var graphicProvider:ArrayCollection = _gl_selectAndZoom.graphicProvider as ArrayCollection;
   var geomArr:Array = [];
   for each (var graphic:Graphic in _gl_selectAndZoom.graphicProvider)//_gl_selectAndZoom is the resulting graphic drawn on a selection of a parcel
   {
geomArr.push(graphic.geometry);//results are in attached text file
   }
   //Alert.show(""+geomArr, "geomArr");
  var bufferParameters:BufferParameters = new BufferParameters();
  bufferParameters.geometries =[geomArr]; //[polyGraphic.geometry];//[ drawnLine ];
  bufferParameters.distances = [ 100 ];
  bufferParameters.unit = GeometryService.UNIT_FOOT;//because i am using NAD 83 NJ State Plane Coordinates 
   bufferParameters.bufferSpatialReference = new SpatialReference(3424);  the same spatial reference as my map    
   this._gs_buffer.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
  _gs_buffer.buffer(bufferParameters);//something is angry here!:mad:
}
  
public function bufferCompleteHandler(event:GeometryServiceEvent):void
{
   _gs_buffer.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
   for each (var polygon:Polygon in event.result)
   {
        var graphic:Graphic = new Graphic(polygon);
        graphic.symbol = this._sfs_buffer;//my simple fill symbol for my buffer
        graphic.toolTip = "Buffered Stuff";
         this._gl_selectAndZoom.add(graphic);
    }
}

I am assuming my problem lies in the spatial reference or extents, but I have no idea why it is failing. Am I on the right track here?  I used the code in the samples for select and zoom and buffer center of map, and what I've changed is basically the spatial reference.  Not sure where else to look.  Thanks in advance for your guidance.

<fx:Declarations>
      <esri:Extent id="_initialExtent" xmin="375565" ymin="653153" xmax="565258" ymax="828541">
<esri:SpatialReference wkid="3424" />
      </esri:Extent>
      <esri:SimpleFillSymbol id="_sfs_SelectAndZoom" alpha="0.7" color="0xFF0000"/>
      <esri:SimpleFillSymbol id="_sfs_buffer" color="0xFFFF1F"  >
<esri:SimpleLineSymbol color="0xFFFF1F"/>
       </esri:SimpleFillSymbol>

      <esri:GeometryService id="_gs_buffer" url="[myurl]/arcgis/rest/services/Geometry/GeometryServer" /><!--I know this url is OK -->

 
</fx:Declarations>
     <esri:Map id="_mapControl" level="3" extent="{_initialExtent}">
        <esri:ArcGISTiledMapServiceLayer  id="_aerialMap" show="layerShowHandler(event)" url="http://......./MapServer"/>
        <esri:ArcGISDynamicMapServiceLayer   id="_dynamicMap"  url="http://....../MapServer"/>
        <esri:GraphicsLayer id="_gl_selectAndZoom" symbol="{_sfs_SelectAndZoom}"/>
        <esri:GraphicsLayer id="_bufferGraphicsLayer" />
</esri:Map>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Janice,

   It looks like your issue is that you are taking the array of geometries and putting them in another array.

bufferParameters.geometries =[geomArr]; 

geomArr is already an array and then adding squage brackets around adds it to another array.

Try:
bufferParameters.geometries =geomArr; 
0 Kudos
JanicePeal
Deactivated User
I removed the brackets.  "World's sloppiest programmer" award goes to me!  That did the trick, Robert, and thanks again to everyone who assisted.  Each of you contributed very valuable information in assisting me!  I'll get the hang of this someday!
0 Kudos