Select to view content in your preferred language

How to retrieve the geometry of a selected polygon?

2568
6
Jump to solution
03-26-2012 07:29 PM
FabienNAPPA
Regular Contributor
Hello,

How to retrieve the geometry of a selected polygon?

Thanks
Fabien
0 Kudos
1 Solution

Accepted Solutions
FabienNAPPA
Regular Contributor
Thank you for your help.

The second method works very well.

Thank you again.

Fabien

View solution in original post

0 Kudos
6 Replies
DominiqueBroux
Esri Frequent Contributor
To get the selected graphics inside a graphics layer, you can use the SelectedGraphics property.

Then to get the geometry of a graphic, use the Geometry property.

If you graphic is a polygon, you can cast that geometry to a polygon.
0 Kudos
FabienNAPPA
Regular Contributor
Thank you for your help.

Here is my code that retrieves the geometry of the selected object and then returns the data in a datagrid. But I can't accumulate the result of the selected graphics. Is it possible to join the geometry? Or there another easier way?

In advance thank you.
Fabien


        private void QueryDataAfterSelectGraphicLayer(ESRI.ArcGIS.Client.Geometry.Geometry geometry)
        {
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");
            queryTask.Failed += QueryTask_Failed;

            Binding resultFeaturesBinding = new Binding("LastResult.Features");
            resultFeaturesBinding.Source = queryTask;
            MyDataGridSelection.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
            Query query = new ESRI.ArcGIS.Client.Tasks.Query();
 
     query.OutFields.Add("*");
            query.Geometry = geometry;
            query.ReturnGeometry = true;
            query.OutSpatialReference = Map.SpatialReference;

            queryTask.ExecuteAsync(query);
        }


        private void FeatureLayer_MouseLeftButtonDown(object sender, GraphicMouseButtonEventArgs args)
        {
 
            args.Graphic.Selected = !args.Graphic.Selected;

            Envelope env = new Envelope();

            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            foreach (Graphic item in selectionGraphicslayer.Graphics)
            {
                if (item.Selected == true)
                {
                    ESRI.ArcGIS.Client.Geometry.Geometry geometry = item.Geometry;
                    QueryDataAfterSelectGraphicLayer(geometry);
                }
            }

        }

0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can create a multi rings polygon at the client side with code like:

private Polygon GetMultiRingsPolygon(GraphicsLayer graphicsLayer)
{
    var graphics = graphicsLayer.Graphics.Where(g => g.Geometry is Polygon).ToList();
    var geometry = graphics.FirstOrDefault().Geometry as Polygon;
    foreach (var gra in graphics.Skip(1))
    {
        foreach(var ring in ((Polygon)gra.Geometry).Rings)
            geometry.Rings.Add(ring);
    }
    return geometry;
}


Then this multi rings polygon should allow you to get your results with only one querytask.

Another option (probably better if your polygons overlap) is to use an Union geometry task at the server side.
0 Kudos
FabienNAPPA
Regular Contributor
Thank you for your answer.

I wanted to use the Union method. But is it possible to make this union operation without drawing? I just want to do the math union and recover the geometry area.

And thank you again for your help.

Also, have you read the private message that I sent you?

I contacted yesterday Emmanuele G. of acrgis to find aditional technical support.

I need to advance in a project that is not very complicated, but I'm in a difficult geographical context.

Thank you for answering me about it privately.

Fabien
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I wanted to use the Union method. But is it possible to make this union operation without drawing?


Sure. If you look at this sample you'll notice that the drawing is done by code:

         parcelGraphicsLayer.Graphics.Add(new Graphic() { Geometry = e.Result, Symbol = LayoutRoot.Resources["BlueFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol });
But basically e.Result is just the result geometry and you can do what you want with it.
0 Kudos
FabienNAPPA
Regular Contributor
Thank you for your help.

The second method works very well.

Thank you again.

Fabien
0 Kudos