Select to view content in your preferred language

How to create a new graphic similar to a graphic in some featureLayer

2601
4
05-12-2011 12:01 PM
MuralidharMoka
Emerging Contributor
Hi,

I have a FeatureLayer   say f
and it has say some graphics/feature in it and say it has 15 attributes.

Now say if I want to create a new graphic which has the same attributes 15 attrutes.
I mean a graphic similar to the graphic in the feature layer f.
can I do some thing like this

Graphic g=new Graphic();

      g=new f.Graphic();  //some thing like this so that I get all the attributes  created with say null values. I  know this sytax is not correct.

Thanks
Muralidhar Moka
0 Kudos
4 Replies
MuralidharMoka
Emerging Contributor
HI,

Continued to the above question, I am giving some more information.
Here I am creating a new polygon graphic by using draw object and then want to add this graphic
to be a part of a featureLayer(f). The featureLayer has like 50 attributes. I only add a few attributes to the created graphic since i dont need to add the rest.Can we even do this.

Code below

if (draw.DrawMode == DrawMode.Polygon)
            {
                ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as   ESRI.ArcGIS.Client.Geometry.Polygon;
                polygon.SpatialReference = MyMap.SpatialReference;
                Graphic graphic = new Graphic()
                {
                    Symbol = SelectFillSymbol,
                    Geometry = polygon
                };
                string GUID = Guid.NewGuid().ToString();
                string OBJECTID_1 = Guid.NewGuid().ToString(); //adding a uniqe id
                graphic.Attributes.Add("A1",GUID);
                graphic.Attributes.Add("A2",null);
                graphic.Attributes.Add("A3",null);
                graphic.Attributes.Add("A4",null);
                f.Graphics.Add(graphic);  //throws an error out of range of valid values
                f.SaveEdits();
                draw.IsEnabled = false;
            }
0 Kudos
dotMorten_esri
Esri Notable Contributor
private static Graphic CloneGraphic(Graphic g)
{
    if(g == null) return null;
    Graphic clone = new Graphic() { Geometry = g.Geometry };
    foreach(var key in g.Attributes.Keys)
       clone.Attributes[key] = g.Attributes[key]; //or set to null if you want all values to be null. You might also want to skip ObjectID
    return clone;
}
0 Kudos
MuralidharMoka
Emerging Contributor
I created a  polygonon a graphic layer using DrawObject.
I want to add it to some featureLayer.
is it possible ?

eg
featurelayer f= MyMap.FeatureLayer["myfeaturelayer'] as FeatureLayer;

Graphic g=new Graphic();
g.symbol=MySimpleFillSymbol;
g.geometry= args.geometry;

f.Graphics.Add(g)
f.SaveEdits();
I tried this and it does not allow to do this.

//is there any other way to do this.
0 Kudos
JenniferNery
Esri Regular Contributor
This should be possible if the FeatureLayer is already initialized. However you don't need to set the graphic symbol when adding to a FeatureLayer and you may also need to give the graphic default attributes.

Usually, you want to use Editor.Add or TemplatePicker so the geometry you add is simplified and a specific type with default attributes is added. But you can use Draw class and add these attributes to your graphic before you add to your layer. (see post#4 in this thread: http://forums.arcgis.com/threads/18542-How-to-save-the-feature-in-featurelayer-back-to-geometry-serv...)
0 Kudos