Creating a feature

When creating a feature, you must correctly establish the editing environment before you begin the actual edit operation. For a new feature, you can create an empty geometry of the type you want to capture (polygon, line, point, or multipoint) and set it to the SketchGraphicLayer.Geometry property. This geometry type must match the geometry type of the featuresource for which you want to create features. For example, if you're trying to add a new point feature into a polygon feature class, the operation will fail. Use the mouse, stylus, or rocker to work with the sketch (create a sketch via AddVertexSketchTool or delete a few vertices of an existing geometry via DeleteVertexSketchTool). Once you've finished your sketching or have selected an existing geometry, you pass the geometry to the geometry column of the new feature row. Whether you're creating a geometry or using an existing one, the process of creating a row and adding it to the featuredatatable is required for feature creation .

Steps:
  1. Get the FeatureSource that can allow new features to be created in the application.

    foreach (FeatureSource fsource in mobileCache1.FeatureSources)
    {
    //make the first editable featuresource from the local cache your target
    if (fsource.AllowNew)
      {
        editsource = fsource;
        break;
      }
    }
    

  2. Get the type of geometry to be edited from the target editable FeatureSource based on the feature type, and create an empty geometry.

    GeometryType geometryType = editsource.GeometryType;
    // creates a new empty instance of a geometry based on geometry type of the layer
    switch (geometryType)
    {
      case GeometryType.Polygon:
        sketchGraphicLayer1.Geometry = new Polygon();
        break;
      case GeometryType.Polyline:
        sketchGraphicLayer1.Geometry = new Polyline();
        break;
      case GeometryType.Point:
        sketchGraphicLayer1.Geometry = new ESRI.ArcGIS.Mobile.Geometries.Point();
        break;
      case GeometryType.Multipoint:
        sketchGraphicLayer1.Geometry = new Multipoint();
        break;
    }
    

  3. Set the MapAction to one of the sketch tools to draw the feature.

    // sets map action to add vertex sketch
    map1.MapAction = addVertexSketchTool1;
    

  4. After the geometry construction is completed through map sketching or GPS capturing, create a row for the feature and assign the geometry before saving the featurelayer.

    FeatureDataTable fTable = editsource.GetDataTable();
    // creates a new row
    DataRow editedFeature = fTable.NewRow();
    //sets the new geometry to the feature layer data table
    fTable.Rows.Add(editedFeature);         
    //sets the new geometry to the geometry field
    editedFeature[editsource.GeometryColumnIndex] = sketchGraphicLayer1.Geometry;
    // updates the feature layer data table
    fTable.SaveInFeatureSource();
    

To use an existing geometry for your new feature's geometry, complete the following steps:

Steps:
  1. Follow step 1 above.
  2. Select an existing geometry to use as your new geometry, and ensure it is for the same type of geometry.

    // if no feature selected exit
    if (selectionMapAction1.SelectedFeatures == null || selectionMapAction1.SelectedFeatures.Count == 0)
    {
      MessageBox.Show("You must select a feature to copy");
      return;
    }
    //
    if (selectionMapAction1.SelectedFeatures[0].FeatureSource.GeometryType != editsource.GeometryType )
    {
      MessageBox.Show("You must select a editable feature to copy");
      return;
    }
    

  3. Use the geometry of the selected location as your new feature's geometry.

    FeatureDataTable selectedLayerDataTable = selectionMapAction1.SelectedFeatures[0]; 
    
     //Create a new row in the FeatureDataTable
     FeatureDataTable fTable = editsource.GetDataTable();
    
     // creates a new row
     DataRow editedFeature = fTable.NewRow();
    
     //sets the new geometry to the feature layer data table
     fTable.Rows.Add(editedFeature);
    
     //sets the new geometry to the geometry field
     editedFeature[editsource.GeometryColumnIndex] = selectedLayerDataTable[0].Geometry;
    
     // updates the feature layer data table
     fTable.SaveInFeatureSource();
    

The geometry is created using the geometry classes and can represent anything from a point captured using a GPS location, a coordinate taken from an existing feature, or a shape the user has digitized on the device using a stylus.

The FeatureDataTable.SaveInFeatureSource() or FeatureSource.SaveEdits() methods internally create, add, modify, and delete tables containing your updates for the layer you're changing. The updates are stored in the mobile cache in delta tables until synchronization with the server is completed. Then the delta tables are erased after successful synchronization.

1/7/2015