Drawing a polygon using code.

5925
8
Jump to solution
05-23-2013 09:42 PM
Labels (1)
AnkitaBhatia
New Contributor III
Hi,
I am working on a requirement to have a region drawn on a graphics layer. This region should envelope a set of graphics already present. It can have two cases ->
1. Region might constitute a set of points (no. of points >4). How to draw a polygon in this case? The polygon as of I know takes 4 mappoints as argument.
2. Region might not have any points defined originally. How to draw a minimum area enveloping the set of graphics? Is there an inbuilt logic for the same?
0 Kudos
1 Solution

Accepted Solutions
AnkitaBhatia
New Contributor III
Thanks!
that solves it :).

View solution in original post

0 Kudos
8 Replies
AnttiKajanus1
Occasional Contributor III
See sample from ArcGIS Runtime SDK for WPF -> Mapping -> Graphics Layers -> Add interactively / Add interactively Online. This should give you at least starting point.
0 Kudos
AnkitaBhatia
New Contributor III
Hi,
I have used the Draw class, and true is the best solution to draw anything using Interaction with map.
My requirement is however to be able to draw a region when I have , say a set of 7-8 Points. These regions should be visible on map once the map loads.
0 Kudos
AnttiKajanus1
Occasional Contributor III
So you want to do something like this?


    <Window.Resources>
        <esri:SimpleRenderer x:Key="renderer">
            <esri:SimpleRenderer.Symbol>
                <esri:SimpleFillSymbol BorderBrush="Black" Fill="Red" BorderThickness="1"/>
            </esri:SimpleRenderer.Symbol>
        </esri:SimpleRenderer>


    </Window.Resources>
    <Grid>
        <esri:Map x:Name="_map">  
           <esri:ArcGISTiledMapServiceLayer ID="Basemap" 
                Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
            <esri:GraphicsLayer ID="Graphics" 
                Renderer="{StaticResource renderer}"/>
        </esri:Map>
    </Grid>

public MainWindow()
        {
            InitializeComponent();




            var layer = _map.Layers["Graphics"] as GraphicsLayer;


            var polygon = new Polygon();
            var pointCollection = new PointCollection();
            pointCollection.Add(new MapPoint(-13446927, 6296089, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-12564245, 6508379, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-11882681, 6586592, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-10810055, 5636871, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-11178770, 4508379, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-12173184, 4351955, new SpatialReference("102100")));
            pointCollection.Add(new MapPoint(-12877094, 4620111, new SpatialReference("102100")));
            // Closing point = same as first one.
            pointCollection.Add(new MapPoint(-13446927, 6296089, new SpatialReference("102100")));
            polygon.Rings.Add(pointCollection);
            polygon.SpatialReference = new SpatialReference("102100");


            var graphic = new Graphic() { Geometry = polygon };


            layer.Graphics.Add(graphic);
        }
}
0 Kudos
AnkitaBhatia
New Contributor III
Thanks 🙂 and yes I was looking for a response on same line.
This is actually what I tried in my application. And this seems to be the most common way to do it.

Next query is :
2. Region might not have any points defined originally. [Which means I donot have points to create Pointcollection).
How to draw a minimum area enveloping the set of graphics[say there are a number of Point geometries in an area] ? Is there an inbuilt logic for the same?
0 Kudos
AnttiKajanus1
Occasional Contributor III
If I understood your need correctly, you can create polygon by using Convex Hull (http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?esri.arcgis.client~esri.arcgis.cli...)

If you are using Standard version of the SDK, you can use local GeometryService and Convex Hull (See example Geometry -> Geometry operations -> Convex Hull) or if you are using basic you can use GeometryService provided by the ArcGIS Online or your own ArcGIS Server.
0 Kudos
AnkitaBhatia
New Contributor III
Thanks a ton!!
This is exactly what I am looking for.
Can we control the geometry that is generated. Right now it joins the points that are used to create the outer edge.

Please refer fig:
The yellow region is what the Convex Hull draws, and the black border would be what I am looking for. Can we provide some offset.
TIA.
0 Kudos
AnttiKajanus1
Occasional Contributor III
You can do about anything to that geometry. Like in this case, you could for example run it through Buffer. Refer sample Geometry -> Geometry Operations -> Buffer a Point.
0 Kudos
AnkitaBhatia
New Contributor III
Thanks!
that solves it :).
0 Kudos