Select to view content in your preferred language

Insert data in an existing layer from a table without deleting

628
3
Jump to solution
08-17-2023 10:22 AM
GISNewbieDev
New Contributor II

 

Hi,

First time I am creating and inserting a data in table. Then creating and adding a layer based on that table in the active map using the following method

CreateLayer<T>( 
   LayerCreationParams layerParams,
   ILayerContainerEdit container
)

Now from second time onwards I have to delete the data from that table as well as layer and insert new data in the same table as well in the same existing layer. Again if I will use the above method it will create one more layer with the same name which I don’t want. 

I am able to delete the data from the table and I will have a table with the new data. But how to insert data in that existing layer in the active map without deleting it?

I saw one option but that is using editing and also at row/feature level. Also in this way I have to again create a dict to fill it with new column value. But since I have a ready whole table with new data, any other option to convert/fill that data directly in the existing layer present in active map view.

public RowToken Create( 
   Layer layer,
   Geometry geometry,
   Dictionary<string,object> values
)

 

@Wolf @CharlesMacleod @GKmieliauskas 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can use this snippet to delete all data from a layer:

        var theFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
        if (theFeatureLayer == null)
        {
          MessageBox.Show("Unable to find a FeatureLayer in the Table of Content");
          return;
        }
        QueuedTask.Run(() =>
        {
        theFeatureLayer.GetTable().DeleteRows(new QueryFilter () );
        });

and the code to copy data from one layer to another is included in the code above.

// copy some data
await QueuedTask.Run(() =>
{
  // create an edit operation
  EditOperation copyOperation = new EditOperation()
  {
    Name = "Copy Data",
    ProgressMessage = "Working...",
    CancelMessage = "Operation canceled.",
    ErrorMessage = "Error copying polygons",
    SelectModifiedFeatures = false,
    SelectNewFeatures = false
  };
  using var rowCursor = originalLayer.Search();
  while (rowCursor.MoveNext())
  {
    using (var row = rowCursor.Current as Feature)
    {
      var geom = row.GetShape().Clone();
      if (geom == null)
        continue;
      var newAttributes = new Dictionary<string, object>
      {
        { "field1", 1.0 },
        { "field2", 2.0 }
      };
      copyOperation.Create(newLyr, geom, newAttributes);
    }
  }
  // execute the operation onoy if changes where made
  if (!copyOperation.IsEmpty
      && !copyOperation.Execute())
  {
    MessageBox.Show($@"Copy operation failed {copyOperation.ErrorMessage}");
    return;
  }
});
// check for edits
if (Project.Current.HasEdits)
{
  var saveEdits = MessageBox.Show("Save edits?",
                "Save Edits?", System.Windows.MessageBoxButton.YesNoCancel);
  if (saveEdits == System.Windows.MessageBoxResult.Cancel)
    return;
  else if (saveEdits == System.Windows.MessageBoxResult.No)
    _ = Project.Current.DiscardEditsAsync();
  else
  {
    _ = Project.Current.SaveEditsAsync();
  }
}

View solution in original post

0 Kudos
3 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I attached a sample that creates a feature class, adds that feature class as a layer to the map, and then copies some data.   

Screen2.png

 

To delete the content of a whole table or feature class you can use the DeleteRows  method for the Table or FeatureClass.

0 Kudos
GISNewbieDev
New Contributor II

@Wolf But I think you are creating new layer by the name of New_{originalLayer.Name}  and then first creating the table structure from the original layer and editing the new layer with the new data but I want to delete the old data from the existing layer and then insert the new data in that same existing layer.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can use this snippet to delete all data from a layer:

        var theFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
        if (theFeatureLayer == null)
        {
          MessageBox.Show("Unable to find a FeatureLayer in the Table of Content");
          return;
        }
        QueuedTask.Run(() =>
        {
        theFeatureLayer.GetTable().DeleteRows(new QueryFilter () );
        });

and the code to copy data from one layer to another is included in the code above.

// copy some data
await QueuedTask.Run(() =>
{
  // create an edit operation
  EditOperation copyOperation = new EditOperation()
  {
    Name = "Copy Data",
    ProgressMessage = "Working...",
    CancelMessage = "Operation canceled.",
    ErrorMessage = "Error copying polygons",
    SelectModifiedFeatures = false,
    SelectNewFeatures = false
  };
  using var rowCursor = originalLayer.Search();
  while (rowCursor.MoveNext())
  {
    using (var row = rowCursor.Current as Feature)
    {
      var geom = row.GetShape().Clone();
      if (geom == null)
        continue;
      var newAttributes = new Dictionary<string, object>
      {
        { "field1", 1.0 },
        { "field2", 2.0 }
      };
      copyOperation.Create(newLyr, geom, newAttributes);
    }
  }
  // execute the operation onoy if changes where made
  if (!copyOperation.IsEmpty
      && !copyOperation.Execute())
  {
    MessageBox.Show($@"Copy operation failed {copyOperation.ErrorMessage}");
    return;
  }
});
// check for edits
if (Project.Current.HasEdits)
{
  var saveEdits = MessageBox.Show("Save edits?",
                "Save Edits?", System.Windows.MessageBoxButton.YesNoCancel);
  if (saveEdits == System.Windows.MessageBoxResult.Cancel)
    return;
  else if (saveEdits == System.Windows.MessageBoxResult.No)
    _ = Project.Current.DiscardEditsAsync();
  else
  {
    _ = Project.Current.SaveEditsAsync();
  }
}
0 Kudos