How to generate a polygon / polyline from user input

1993
2
Jump to solution
02-13-2012 09:20 PM
BharathiChandran
New Contributor III
In my app, there is a requirement to let the user add  features using xy coordinate pairs as input. I have been able to get the user XY input and generate a MapPoint & update it to SDE.
However, when it comes to polyline or polygon, the user inputs have been updated to an array of mappoints. But from that array I am unable to generate graphics or create a feature which could be updated in SDE.

This is the code
switch(selectedFeatureLayer.layerDetails.geometryType)                 {                     case Geometry.POLYLINE:                     {                         Alert.show(myPointArray.toString(), myPointArray.length.toString());                                                  pline = new Polyline([myPointArray]);                         var myGraphicLine:Graphic = new Graphic;                         myGraphicLine.geometry = pline;                         myGraphicLine.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT,0x0000FF, 0.6,2);                         myGraphicsLayer.add(myGraphicLine);                                                   selectedFeatureLayer.applyEdits([myGraphicLine], null , null);                         break;                     }                                              case Geometry.POLYGON:                     { //                Get the points array and add the first point at the end of the array to make a polygon                                                  var onePointArray:Array = new Array;                         onePointArray=myPointArray[0];                         myPointArray.push([onePointArray]);                                                   //                Create polygon graphic                         poly = new Polygon([[myPointArray]]);                         myGraphicPoly.geometry = poly;                         myGraphicPoly.symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,0x00FF00, 0.7);                         myGraphicsLayer.add(myGraphicPoly);                            selectedFeatureLayer.applyEdits([myGraphicPoly], null , null);                                                  myPointArray = new Array;                         finishAdd.enabled = false;                             break;                     }                                              default:                     {                         break;                     }                 }


The user entered coordinates are stored in myPointArray. Trace shows that the pline / poly is not getting generated. How do I go about this? Whats the mistake?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IvanBespalov
Occasional Contributor III
Try this sample.

<?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"       xmlns:esri="http://www.esri.com/2008/ags">  <fx:Script>   <![CDATA[    import com.esri.ags.Graphic;    import com.esri.ags.SpatialReference;    import com.esri.ags.Units;    import com.esri.ags.geometry.Geometry;    import com.esri.ags.geometry.MapPoint;    import com.esri.ags.geometry.Polygon;    import com.esri.ags.geometry.Polyline;    import com.esri.ags.utils.GeometryUtil;        import mx.utils.StringUtil;        private const sr:SpatialReference = new SpatialReference(4326);     protected function onCreatePolyline(event:MouseEvent):void    {     addMessage("Create polyline clicked");          var pts:Array = new Array();     for (var i:int; i < 10; i++) // add 10 random points to path     {      var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);      pts.push(pt);     }          var pl:Polyline = new Polyline(new Array(pts), sr);          var lengths:Array = GeometryUtil.geodesicLengths(new Array(pl), Units.KILOMETERS);     if (lengths != null && lengths.length > 0)     {            addMessage(StringUtil.substitute("polyline created with length {0} km", lengths[0]));     }          addGraphic(pl);    }      protected function onCreatePolygon(event:MouseEvent):void    {     addMessage("Create polygon clicked");     var pts:Array = new Array();     for (var i:int; i < 10; i++) // add 10 random points to ring     {      var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);      pts.push(pt);     }          var pg:Polygon = new Polygon(new Array(pts), sr);          var areas:Array = GeometryUtil.geodesicAreas(new Array(pg), Units.SQUARE_KILOMETERS);     if (areas != null && areas.length > 0)     {            addMessage(StringUtil.substitute("polygon created with area {0} km²", Math.abs(areas[0])));     }          addGraphic(pg);    }        private function addMessage(message:String):void    {     log.text = StringUtil.substitute("> > > {0}\n{1}", message, log.text);     }        private function addGraphic(geometry:Geometry):void    {     var gr:Graphic = new Graphic(geometry);     grLayer.clear();     var grId:String = grLayer.add(gr);     addMessage(StringUtil.substitute("graphic added with id='{0}'", grId));     map.initialExtent = geometry.extent;     map.zoomToInitialExtent();    }    ]]>  </fx:Script>    <s:layout>   <s:VerticalLayout gap="10"          paddingBottom="10"          paddingLeft="10"         paddingRight="10"          paddingTop="10"/>  </s:layout>    <s:Button label="Create polyline"       click="onCreatePolyline(event)"/>    <s:Button label="Create polygon"       click="onCreatePolygon(event)"/>    <s:TextArea id="log"      width="100%"      height="100%"/>    <esri:Map id="map"                       zoomSliderVisible="false"      minHeight="200"      width="100%">      <esri:GraphicsLayer id="grLayer" />     </esri:Map>   </s:Application> 

View solution in original post

0 Kudos
2 Replies
IvanBespalov
Occasional Contributor III
Try this sample.

<?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"       xmlns:esri="http://www.esri.com/2008/ags">  <fx:Script>   <![CDATA[    import com.esri.ags.Graphic;    import com.esri.ags.SpatialReference;    import com.esri.ags.Units;    import com.esri.ags.geometry.Geometry;    import com.esri.ags.geometry.MapPoint;    import com.esri.ags.geometry.Polygon;    import com.esri.ags.geometry.Polyline;    import com.esri.ags.utils.GeometryUtil;        import mx.utils.StringUtil;        private const sr:SpatialReference = new SpatialReference(4326);     protected function onCreatePolyline(event:MouseEvent):void    {     addMessage("Create polyline clicked");          var pts:Array = new Array();     for (var i:int; i < 10; i++) // add 10 random points to path     {      var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);      pts.push(pt);     }          var pl:Polyline = new Polyline(new Array(pts), sr);          var lengths:Array = GeometryUtil.geodesicLengths(new Array(pl), Units.KILOMETERS);     if (lengths != null && lengths.length > 0)     {            addMessage(StringUtil.substitute("polyline created with length {0} km", lengths[0]));     }          addGraphic(pl);    }      protected function onCreatePolygon(event:MouseEvent):void    {     addMessage("Create polygon clicked");     var pts:Array = new Array();     for (var i:int; i < 10; i++) // add 10 random points to ring     {      var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);      pts.push(pt);     }          var pg:Polygon = new Polygon(new Array(pts), sr);          var areas:Array = GeometryUtil.geodesicAreas(new Array(pg), Units.SQUARE_KILOMETERS);     if (areas != null && areas.length > 0)     {            addMessage(StringUtil.substitute("polygon created with area {0} km²", Math.abs(areas[0])));     }          addGraphic(pg);    }        private function addMessage(message:String):void    {     log.text = StringUtil.substitute("> > > {0}\n{1}", message, log.text);     }        private function addGraphic(geometry:Geometry):void    {     var gr:Graphic = new Graphic(geometry);     grLayer.clear();     var grId:String = grLayer.add(gr);     addMessage(StringUtil.substitute("graphic added with id='{0}'", grId));     map.initialExtent = geometry.extent;     map.zoomToInitialExtent();    }    ]]>  </fx:Script>    <s:layout>   <s:VerticalLayout gap="10"          paddingBottom="10"          paddingLeft="10"         paddingRight="10"          paddingTop="10"/>  </s:layout>    <s:Button label="Create polyline"       click="onCreatePolyline(event)"/>    <s:Button label="Create polygon"       click="onCreatePolygon(event)"/>    <s:TextArea id="log"      width="100%"      height="100%"/>    <esri:Map id="map"                       zoomSliderVisible="false"      minHeight="200"      width="100%">      <esri:GraphicsLayer id="grLayer" />     </esri:Map>   </s:Application> 
0 Kudos
BharathiChandran
New Contributor III
Thanks Ivan!

The way I was populating the array was the root cause of the error.
Thanks once again for the help.

Bharathi.
0 Kudos