Select to view content in your preferred language

Adding an element to the feature layer programmatically

4277
13
01-03-2012 06:49 AM
MarvinOconitrillo
Occasional Contributor
Hi, I was trying to add a new element into my featurelayer service programmatically, but when I save the editions, it seems that doesn't work because I can't see changes on the map neither the data.
I was trying to use on my code behind an editor declared on the xaml, getting an instance of the feature layer from there, and then adding my new element with an Graphic instance that I create before, so, I use the add method of the feature layer and after that the saveEdit method, and finally the refresh methos to refresh my map.
Can anybody help me, I would like to know what I'm doing wrong about that, I'm posting fragments of my code to show what I explained before.
Thanks.

*Fragment of the xaml declaring the editor

<Grid.Resources>
            <esri:Editor x:Key="Editor" 
                         Map="{Binding ElementName=Map}" 
                         LayerIDs="Poligonos"                          GeometryServiceUrl="http://crbd02/ArcGIS/rest/services/Geometry/GeometryServer"
                             />
</Grid.Resources>


*Fragment of the code declaring the Map and the feature layer
<!-- Map Control -->
        <esri:Map x:Name="Map" Background="White" WrapAround="true" IsLogoVisible="False" Extent="-1442632.89843668,-274130.672780001,8713596.00123668,5756744.12838">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseEnter">
                    <ei:ChangePropertyAction TargetName="myMenuItems" PropertyName="Visibility">
                        <ei:ChangePropertyAction.Value>
                            <Visibility>Collapsed</Visibility>
                        </ei:ChangePropertyAction.Value>
                    </ei:ChangePropertyAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <esri:FeatureLayer ID="Poligonos" Url="http://crbd02/ArcGIS/rest/services/DemosAPIs/Edicion_APIS/FeatureServer/0" AutoSave="False" x:Name="Poligonos"
                                Mode="OnDemand" ValidateEdits="True" DisableClientCaching="True"/>
        
</esri:Map>


*Fragment of the code adding the new element into the feature layer, the object gra have the graphic that I draw before and I want to add to the feature layer.
Graphic gra = null;

                gra = VentanaDerroteroEnPanel.FinalizarDibujo();
                PanelVentanaDerrotero.Visibility = System.Windows.Visibility.Collapsed;

                Editor editor = LayoutRoot.Resources["Editor"] as Editor;
                foreach (GraphicsLayer graphicsLayer in editor.GraphicsLayers)
                {
                    if (graphicsLayer is FeatureLayer)
                    {
                        FeatureLayer featureLayer = graphicsLayer as FeatureLayer;
                        if (featureLayer.ID == "Poligonos")
                        {
                            featureLayer.Graphics.Add(gra);
                            featureLayer.SaveEdits();
                            featureLayer.Refresh();
                        }
                    }
                }
0 Kudos
13 Replies
michaelrosen
Deactivated User
I also am facing this issue.  I've got a map that has an editable featurelayer.  I can programmatically -- without using the Editor -- add a Graphic to the layer and it renders properly.  However, the subsequent "Save" fails with the error message "Unable to complete operation."

I'll greatly appreciate some help here.

Here's what I'm doing:

      private void ImportCollection(String name, MapPoint LL, MapPoint UR, SpatialReference srs)
      {
          ArcGISLocalFeatureLayer featureLayer = mapControl.Layers["theArcGISLocalFeatureLayer"] as ArcGISLocalFeatureLayer;


          //// Hook up some diagnostic event handler so we can see what is going wrong.
          featureLayer.BeginSaveEdits += (s1, beginEditEventArgs) =>
          {   /// this part is fine.   Adds.Count shows 1.
              System.Diagnostics.Trace.WriteLine(String.Format("s1: {0}, number of Adds: {1}", s1, beginEditEventArgs.Adds.Count));
          };

          featureLayer.EndSaveEdits += (s2, endEditEventArgs) =>
          {    /// No joy here.  We fail instead.
              System.Diagnostics.Trace.WriteLine(String.Format("featureLayer.Graphics.Count: {0}", featureLayer.Graphics.Count));
          };

          featureLayer.SaveEditsFailed += (s3, taskFailedEventArgs) =>
          {    //// We can tell the Save failed here ... "Unable to complete operation." ... But why?
              System.Diagnostics.Trace.WriteLine(String.Format("taskFailedEventArgs.Error.Message: {0}", taskFailedEventArgs.Error.Message));
          };          
          
          
          List<Graphic> listPnt = _geometryTask.Project(new List<Graphic>() { new Graphic() { Geometry = LL } }, mapControl.SpatialReference) as List<Graphic>;
          listPnt = _geometryTask.Project(new List<Graphic>() { new Graphic() { Geometry = LL } }, mapControl.SpatialReference) as List<Graphic>;
          MapPoint wgs84Pnt = listPnt[0].Geometry as MapPoint;
          LL.X = wgs84Pnt.X;
          LL.Y = wgs84Pnt.Y;

          listPnt = _geometryTask.Project(new List<Graphic>() { new Graphic() { Geometry = UR } }, mapControl.SpatialReference) as List<Graphic>;
          wgs84Pnt = listPnt[0].Geometry as MapPoint;
          UR.X = wgs84Pnt.X;
          UR.Y = wgs84Pnt.Y;

          Graphic gra = new Graphic(); ////           Create a new Graphic and add it to the layer ... this works to the extent that it renders properly on the map!
          gra.Geometry = new Envelope(LL, UR);
          gra.Geometry = new Envelope(LL, UR);
          gra.Symbol = featureLayer.Renderer.GetSymbol(gra);
          featureLayer.Graphics.Add(gra);
      }
0 Kudos
PreetiMaske
Esri Regular Contributor
Actually the problem is that you are trying to add a graphic with envelope as geometry when layer expects geometry of type polygon. In your sample code if you change the code as follows, the graphic will be saved correctly.

 
ESRI.ArcGIS.Client.Geometry.Polygon poly = new ESRI.ArcGIS.Client.Geometry.Polygon();
      ESRI.ArcGIS.Client.Geometry.PointCollection pts = new ESRI.ArcGIS.Client.Geometry.PointCollection();
      Graphic gra = new Graphic(); 
      //gra.Geometry = new Envelope(LL, UR);      
      pts.Add(LL);
      pts.Add(new MapPoint(UR.X, LL.Y));
      pts.Add(UR);
      pts.Add(new MapPoint(LL.X, UR.Y));
      poly.SpatialReference = MyMap.SpatialReference;
      poly.Rings.Add(pts);
      gra.Geometry = poly;

      gra.Symbol = featureLayer.Renderer.GetSymbol(gra);
      featureLayer.Graphics.Add(gra);
0 Kudos
michaelrosen
Deactivated User
Right you are Preeti.  Thanks so much for this!

msr
0 Kudos
ahmedelashry
Emerging Contributor
am having a bad time in adding graphic point to feature layer using another graphic point from another feature

there is no exceptions or errors , and am sure of every single line , and debugging gets into the if condition with has edits , and it saves 

no reflection in the database, i wish to know why

the code is implemented in the FeatureLayer_MouseLeftButtonUp  e
   private void FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
        {
            FeatureLayer featureLayer = sender as FeatureLayer;
            
            foreach (Graphic g in featureLayer.Graphics)
                if (g.Selected)
                {
                    g.UnSelect();
                }

            e.Graphic.Select();
      
            MyFeatureDataForm.FeatureLayer = featureLayer;
            MyFeatureDataForm.GraphicSource = e.Graphic;
            FeatureDataFormBorder.Visibility = Visibility.Visible;

            Graphic newg = new Graphic();
            newg.Geometry = e.Graphic.Geometry;
            newg.Symbol = LayoutRoot.Resources["RedMarkerSymbol"] as MarkerSymbol;

            FeatureLayer ft = MyMap.Layers["events"] as FeatureLayer;
            ft.Graphics.Add(newg);

            if (ft.HasEdits)
            {
                ft.SaveEdits();
            }
            else
            {
                MessageBox.Show("no Edits");
            }
            //Editor edit = LayoutRoot.Resources["MyEditor"] as Editor;
            //foreach (GraphicsLayer graphicsLayer in edit.GraphicsLayers)
            //{
            //    if (graphicsLayer is FeatureLayer)
            //    {
                    
            //        ft = graphicsLayer as FeatureLayer;
            //        ft.Graphics.Add(newg);
                    
                    
                   
            //    }
            //}
        }





my project dead line is tomorrow ps:
0 Kudos