Creating a feature
When creating a new 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 wish 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 layer in which you wish to create features. For example, if you are 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 new sketch via AddVertexSketchTool or delete a few vertices of an existing geometry via DeleteVertexSketchTool). Once you have finished your sketching or have selected an existing geometry, you input the geometry to the geomtrycolumn of the new feature row. Whether you are creating a geometry or using an existing one, the process of creating a new row and adding it to the featuredatatable is central to feature creation and must occur.
- Get the layers that can allow new features created in the application.
foreach (FeatureLayer flayer in mobileCache1.Layers); { //make the first editable layer from the local cache our target if (flayer.AllowNew) FeatureLayer editlayer = flayer ; }
- Get the type of geometry to be edited from the target edit layer based on the feature type and create a new geometry that wil be set to your location.
GeometryType geometryType = editlayer.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; }
- Set the MapAction to one of the sketch tools to draw the feature.
// sets map action to add vertex sketch map1.CurrentMapAction = addVertexSketchTool1;
- Create a new row for the feature and assign the geometry before saving the featurelayer.
FeatureDataTable fTable = editlayer.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[editlayer.GeometryColumnIndex] = sketchGraphicLayer1.Geometry; // updates the feature layer data table fTable.SaveInFeatureLayer();
If you wish to use an existing location for your new feature's geometry you can use the following steps.
- Use Step 1. as above.
- Select an existing location to use as
your new geometry and then check to 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].FeatureLayer.GeometryType != editlayer.GeometryType ) { MessageBox.Show("You must select a editable feature to copy"); return; }
- Use the geometry of the selected location as
your new feature's geometry.
FeatureDataTable selectedLayerDataTable = selectionMapAction1.SelectedFeatures[0]; //Create a new row in the featuretable FeatureDataTable fTable = editlayer.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[editlayer.GeometryColumnIndex] = selectedLayerDataTable[0].Geometry; // updates the feature layer data table fTable.SaveInFeatureLayer();
The geometry is created using the geometry classes and may 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.SaveInFeatureLayer() or FeatureLayer.SaveEdits() methods internally create, add, modify and delete tables containing your updates for the layer you are changing. The updates are stored in the mobile cache in delta tables until synchronization with the server. Then the delta tables are erased.