ArcEngine UpdateFeature 更新要素几个方法的对比

Updating features

This topic explains how to updates features in a geodatabase feature class. Two approaches are shown, one that updates an individual feature using the IFeature.Store method, and one that uses a cursor to update multiple features at once. Additional information includes how to delete features and how to optimize performance in certain situations.

1.Updating individual features

// Get a known feature using its Object ID.
IFeature feature = featureClass.GetFeature(1);

// Assign a new point as the feature's shape.
IPoint newLocation = new PointClass { X = 1000, Y = 1000 };
feature.Shape = newLocation;

// Find the position of the "NAME" field and modify it.
int nameFieldIndex = featureClass.FindField("NAME");
feature.set_Value(nameFieldIndex, "My Value");

// Store the changes.
feature.Store();

2.Using cursors to perform updates

// Create a ComReleaser for cursor management.
using (ComReleaser comReleaser = new ComReleaser())
{
  // Use ITable.Update to create an update cursor.
  ICursor updateCursor = table.Update(null, true);
  comReleaser.ManageLifetime(updateCursor);

  // Find the positions of the fields used to get and set values.
  int firstNameIndex = table.FindField("FirstName");
  int lastNameIndex = table.FindField("LastName");
  int emailIndex = table.FindField("Email");
  IRow row = null;
  while ((row = updateCursor.NextRow()) != null)
  {
    // Get the first and last names.
    String firstName = Convert.ToString(row.get_Value(firstNameIndex));
    String lastName = Convert.ToString(row.get_Value(lastNameIndex));

    // Append the first letter of the first name and the entire last name with
    // an @ (at) symbol and the e-mail domain to make an e-mail address.
    String emailAddress = String.Concat(firstName[0], lastName, "@MyCompanyName.com");
    row.set_Value(emailIndex, emailAddress);
    updateCursor.UpdateRow(row);
  }
}

3.Using a search cursor

// Create a ComReleaser for cursor management.
using (ComReleaser comReleaser = new ComReleaser())
{
  // Use IFeatureClass.Search to create a search cursor.
  IFeatureCursor searchCursor = featureClass.Search(null, false);
  comReleaser.ManageLifetime(searchCursor);

  // Find the positions of the fields used to get and set values.
  int laneFieldIndex = featureClass.FindField("LANE_COUNT");
  int speedFieldIndex = featureClass.FindField("SPEED_LIMIT");
  IFeature feature = null;
  while ((feature = searchCursor.NextFeature()) != null)
  {
    // Check the lane count of the feature.
    int laneCount = Convert.ToInt32(feature.get_Value(laneFieldIndex));

    // Set the speed limit based on the lane count.
    int speedLimit = (laneCount == 2) ? 50 : 80;
    feature.set_Value(speedFieldIndex, speedLimit);
    feature.Store();
  }
}

4.Deleting features

// Define a constraint on the features to be deleted.
IQueryFilter queryFilter = new QueryFilterClass
{
  WhereClause = "TERM = 'Village'"
};

// Create a ComReleaser for cursor management.
using (ComReleaser comReleaser = new ComReleaser())
{
  // Create and manage a cursor.
  IFeatureCursor searchCursor = featureClass.Search(queryFilter, false);
  comReleaser.ManageLifetime(searchCursor);

  // Delete the retrieved features.
  IFeature feature = null;
  while ((feature = searchCursor.NextFeature()) != null)
  {
    feature.Delete();
  }
}
// Define a constraint on the features to be deleted.
IQueryFilter queryFilter = new QueryFilterClass
{
  WhereClause = "TERM = 'Village'"
};

// Cast the feature class to the ITable interface.
ITable table = (ITable)featureClass;
table.DeleteSearchedRows(queryFilter);


5.UpdateSearchedRows

// Find the position of the field that will be updated.
int typeFieldIndex = featureClass.FindField("TYPE");

// Create a query filter defining which fields will be updated
// (the subfields) and how to constrain which rows are updated
// (the where clause).
IQueryFilter queryFilter = new QueryFilterClass
{
  SubFields = "TYPE",
  WhereClause = "LANE_COUNT = 4"
};

// Create a ComReleaser for buffer management.
using (ComReleaser comReleaser = new ComReleaser())
{
  // Create a feature buffer containing the values to be updated.
  IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
  featureBuffer.set_Value(typeFieldIndex, "Highway");
  comReleaser.ManageLifetime(featureBuffer);

  // Cast the class to ITable and perform the updates.
  ITable table = (ITable)featureClass;
  IRowBuffer rowBuffer = (IRowBuffer)featureBuffer;
  table.UpdateSearchedRows(queryFilter, rowBuffer);
}

6.Scoping edit operations with search cursors

// One edit operation for every edit.
while ((feature = cursor.NextFeature()) != null)
{
  workspaceEdit.StartEditOperation();
  feature.set_Value(1, "abcde");
  feature.Store();
  Marshal.ReleaseComObject(feature);
  workspaceEdit.StopEditOperation();
}

// One edit operation for every 500 edits.
int count = 0;
while ((feature = cursor.NextFeature()) != null)
{
  count++;
  workspaceEdit.StartEditOperation();
  feature.set_Value(1, "abcde");
  feature.Store();
  Marshal.ReleaseComObject(feature);
  if (count % 500 == 0)
  {
    workspaceEdit.StopEditOperation();
  }
}
workspaceEdit.StopEditOperation();



你可能感兴趣的:(ArcEngine UpdateFeature 更新要素几个方法的对比)