public static string GetSaveResultString(tkOgrSaveResult saveResult)
        {
            switch (saveResult)
            {
            case tkOgrSaveResult.osrNoChanges:
                return("OGR layer has no changes");

            case tkOgrSaveResult.osrAllSaved:
                return("All changes saved");

            case tkOgrSaveResult.osrSomeSaved:
                return("Some changes saved");

            case tkOgrSaveResult.osrNoneSaved:
                return("Changes aren't saved");
            }
            return("");
        }
        public static bool SaveLayerChanges(int layerHandle)
        {
            var map = App.Map;

            string prompt = string.Format("Save changes for the layer: {0}?", map.get_LayerName(layerHandle));
            var    result = MessageHelper.AskYesNoCancel(prompt);

            switch (result)
            {
            case DialogResult.Yes:
            case DialogResult.No:
                var sf       = map.get_Shapefile(layerHandle);
                var ogrLayer = map.get_OgrLayer(layerHandle);

                bool save    = result == DialogResult.Yes;
                bool success = false;
                if (ogrLayer != null)
                {
                    int             savedCount;
                    tkOgrSaveResult saveResult = ogrLayer.SaveChanges(out savedCount);
                    success = saveResult == tkOgrSaveResult.osrAllSaved || saveResult == tkOgrSaveResult.osrNoChanges;
                    string msg = string.Format("{0}: {1}; features: {2}", GetSaveResultString(saveResult), ogrLayer.Name, savedCount);
                    MessageHelper.Info(msg);
                }
                else
                {
                    success = sf.StopEditingShapes(save, true, null);
                }

                if (success)
                {
                    sf.InteractiveEditing = false;
                    map.ShapeEditor.Clear();
                    map.UndoList.ClearForLayer(layerHandle);
                }
                map.Redraw();
                return(true);

            case DialogResult.Cancel:
            default:
                return(false);
            }
        }
        public bool SaveEdits()
        {
            if (CurrentEditingLayer == null)
            {
                Events.MapControl_Error error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.UseStartEditing, InMethod = "StopEditingLayer", AxMapError = ""
                };
                On_Error(error);
                return(false);
            }
            Events.MapControl_EditingStateChange editingStateChange = new Events.MapControl_EditingStateChange()
            {
                EditingState = Events.EditingState.SaveEditing, EditingLayer = CurrentEditingLayer
            };
            On_EditingStateChange(editingStateChange);
            var sf       = AxMap.get_Shapefile(CurrentEditingLayer.Handle);
            var ogrLayer = AxMap.get_OgrLayer(CurrentEditingLayer.Handle);

            bool success = false;

            if (ogrLayer != null)
            {
                ogrLayer = ((ResTBPostGISLayer)CurrentEditingLayer).EditingLayer;

                ((ResTBPostGISLayer)CurrentEditingLayer).SaveAttributes(AxMap);

                int             savedCount;
                tkOgrSaveResult saveResult = ogrLayer.SaveChanges(out savedCount);
                success = saveResult == tkOgrSaveResult.osrAllSaved || saveResult == tkOgrSaveResult.osrNoChanges;

                // if hazard map before --> copy to hazardmap after
                if ((success) && (((ResTBPostGISLayer)CurrentEditingLayer).ResTBPostGISType == ResTBPostGISType.HazardMapBefore) && (isAdding))
                {
                    string newName = ((ResTBHazardMapLayer)CurrentEditingLayer).CopyToAfter(MapControlTools);

                    ILayer HMAfter = MapControlTools.Layers.Where(m => m.Name == newName).FirstOrDefault();
                    if (HMAfter != null)
                    {
                        ogrLayer = AxMap.get_OgrLayer(HMAfter.Handle);
                        ogrLayer.ReloadFromSource();
                    }
                }


                ogrLayer = AxMap.get_OgrLayer(CurrentEditingLayer.Handle);
                ogrLayer.ReloadFromSource();
            }

            if (success)
            {
                sf.InteractiveEditing = false;
                AxMap.ShapeEditor.Clear();
                AxMap.UndoList.ClearForLayer(CurrentEditingLayer.Handle);
                Events.MapControl_EditingStateChange editingStateChangeStop = new Events.MapControl_EditingStateChange()
                {
                    EditingState = Events.EditingState.StopEditing, EditingLayer = CurrentEditingLayer
                };
                On_EditingStateChange(editingStateChangeStop);

                Events.MapControl_LayerChange layerChange = new Events.MapControl_LayerChange()
                {
                    Layer = this.CurrentEditingLayer, LayerChangeReason = Events.LayerChangeReason.EditedLayer
                };
                On_LayerChange(layerChange);
                return(true);
            }
            AxMap.Redraw();
            return(false);
        }
        /// <summary>
        ///     The test edit layer.
        /// </summary>
        /// <returns>
        ///     The <see cref="bool" />.
        /// </returns>
        private static bool TestEditLayer()
        {
            var layer = new OgrLayer();

            layer.GlobalCallback = form;

            if (!layer.OpenFromDatabase(CONNECTION_STRING, "buildings", true))
            {
                Debug.Print("Failed to open layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
                return(false);
            }

            // check if editing is supported for driver
            Debug.Print("Driver supports editing: " + layer.TestCapability(tkOgrLayerCapability.olcRandomWrite));

            // now check if we can actually do it, as there can be other limitations
            if (!layer.get_SupportsEditing(tkOgrSaveType.ostSaveAll))
            {
                Debug.Print("Can't edit a layer: " + layer.get_ErrorMsg(layer.LastErrorCode));

                layer.Close();
                return(false);
            }

            Shapefile sf = layer.GetBuffer();

            if (sf != null)
            {
                // possible types of editing
                bool editValue   = true;
                bool addShape    = true;
                bool editShape   = true;
                bool removeShape = true;

                if (editValue)
                {
                    int    shapeIndex = 0;
                    int    fieldIndex = 2;
                    object val        = sf.get_CellValue(fieldIndex, shapeIndex);
                    sf.EditCellValue(fieldIndex, shapeIndex, "test_writing");

                    // this flag will notify the driver that changes should saved back to source
                    sf.ShapeModified[shapeIndex] = true;
                }

                if (addShape)
                {
                    int   shapeIndex = sf.NumShapes;
                    Shape shp        = sf.get_Shape(0);
                    shp = shp.Buffer(1, 50);

                    // modified flag is set automatically in this case
                    bool result = sf.EditInsertShape(shp, ref shapeIndex);
                    Debug.Print("Shape was inserted: " + result);
                }

                if (editShape)
                {
                    // since shapefile is in in-memory mode, geometry of shapes can be changed directly;
                    // bear in mind that this won't work for file-based shapefiles, in that case get_Shape will
                    // populate Shape object which will have no futher link with parent shapefile
                    Shape shp = sf.get_Shape(sf.NumShapes - 1);
                    for (int i = 0; i < shp.numPoints; i++)
                    {
                        double x = 0.0, y = 0.0;
                        if (shp.get_XY(i, ref x, ref y))
                        {
                            shp.put_XY(i, x + 0.01, y + 0.01); // let's move it a little
                        }
                    }
                }

                if (removeShape)
                {
                    bool result = sf.EditDeleteShape(sf.NumShapes - 1);
                    Debug.Print("Shape was deleted: " + result);
                }

                // saving it
                int             count;
                tkOgrSaveResult saveResults = layer.SaveChanges(out count);

                Debug.Print("Save result: " + saveResults);
                Debug.Print("Number of shapes saved: " + count);

                // displaying info on errors
                for (int i = 0; i < layer.UpdateSourceErrorCount; i++)
                {
                    Debug.Print(
                        "Error for shape id {0}: {1}",
                        layer.UpdateSourceErrorShapeIndex[i],
                        layer.UpdateSourceErrorMsg[i]);
                }

                return(true);
            }

            layer.Close();
            return(false);
        }