Esempio n. 1
0
        /// <summary>
        /// Handles mouse down event, removes the shape under cursor
        /// </summary>
        private void AxMap1MouseDownEvent1(object sender, _DMapEvents_MouseDownEvent e)
        {
            // it's assumed here that the layer we want to edit is the first 1 (with 0 index)
            int       layerHandle = axMap1.get_LayerHandle(0);
            Shapefile sf          = axMap1.get_Shapefile(layerHandle);

            if (sf != null)
            {
                double projX = 0.0;
                double projY = 0.0;
                axMap1.PixelToProj(e.x, e.y, ref projX, ref projY);

                object  result = null;
                Extents ext    = new Extents();
                ext.SetBounds(projX, projY, 0.0, projX, projY, 0.0);
                if (sf.SelectShapes(ext, 0.0, SelectMode.INCLUSION, ref result))
                {
                    int[] shapes = result as int[];
                    if (shapes == null)
                    {
                        return;
                    }

                    if (shapes.Length > 1)
                    {
                        string s = "More than one shapes were selected. Shape indices:";
                        for (int i = 0; i < shapes.Length; i++)
                        {
                            s += shapes[i] + Environment.NewLine;
                        }
                        MessageBox.Show(s);
                    }
                    else
                    {
                        if (!sf.EditDeleteShape(shapes[0]))
                        {
                            MessageBox.Show("Failed to delete a shape: " + sf.ErrorMsg[sf.LastErrorCode]);
                        }
                        else
                        {
                            MessageBox.Show("Shape was removed. Index = " + shapes[0]);
                            sf.Labels.Expression = sf.Labels.Expression;
                            for (int i = 0; i < sf.Labels.Count; i++)
                            {
                                sf.Labels.Label[i, 0].Text += "; " + i;
                            }
                            axMap1.Redraw();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Nothing was selected");
                }
            }

            // Execute this code if you want to save the results.
            // sf.StopEditingShapes(true, true, null);
        }
Esempio n. 2
0
        public static void Remove(Shapefile sf, int layerHandle)
        {
            var list = App.Map.UndoList;

            list.BeginBatch();
            for (int i = sf.NumShapes - 1; i >= 0; i--)
            {
                if (sf.ShapeSelected[i])
                {
                    list.Add(tkUndoOperation.uoRemoveShape, layerHandle, i);
                    sf.EditDeleteShape(i);
                }
            }
            list.EndBatch();
        }
        private static MergeResult Merge(int layerHandle, Shapefile sf)
        {
            // merging
            Shape shp = null;

            for (int i = 0; i < sf.NumShapes; i++)
            {
                if (sf.ShapeSelected[i])
                {
                    shp = shp == null ? sf.Shape[i].Clone() : shp.Clip(sf.Shape[i], tkClipOperation.clUnion);
                }
            }

            if (shp == null)
            {
                return(MergeResult.Failed);
            }

            // TODO: request for attributes

            // registering in undo list
            var undoList = App.Map.UndoList;

            undoList.BeginBatch();

            for (int i = sf.NumShapes - 1; i >= 0; i--)
            {
                if (sf.ShapeSelected[i])
                {
                    undoList.Add(tkUndoOperation.uoRemoveShape, layerHandle, i);
                    sf.EditDeleteShape(i);
                }
            }
            int shapeIndex = sf.EditAddShape(shp);

            if (shapeIndex != -1)
            {
                undoList.Add(tkUndoOperation.uoAddShape, layerHandle, shapeIndex);
            }

            undoList.EndBatch();

            return(MergeResult.Ok);
        }
Esempio n. 4
0
 public bool EditDelete(int index)
 {
     return(_shapefile.EditDeleteShape(index));
 }
        /// <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);
        }
        /// <summary>
        /// Runs explode operation
        /// </summary>
        private static ExplodeResult Explode(int layerHandle, Shapefile sf)
        {
            var dict = new Dictionary <int, Shape[]>();

            // exploding
            for (int i = 0; i < sf.NumShapes; i++)
            {
                object result = null;
                if (sf.ShapeSelected[i])
                {
                    sf.Shape[i].Explode(ref result);
                    var shapes = result as object[];
                    if (shapes != null && shapes.Any())
                    {
                        dict[i] = shapes.Cast <Shape>().ToArray();
                    }
                    else
                    {
                        return(ExplodeResult.Failed);
                    }
                }
            }

            int newSelectionStart = sf.NumShapes - sf.NumSelected;
            var undoList          = App.Map.UndoList;

            undoList.BeginBatch();

            // add new shapes
            var list = dict.ToList();

            foreach (var item in list)
            {
                foreach (var shp in item.Value.ToList())
                {
                    int shapeIndex = sf.EditAddShape(shp);

                    sf.CopyAttributes(item.Key, shapeIndex);

                    if (shapeIndex != -1)
                    {
                        undoList.Add(tkUndoOperation.uoAddShape, layerHandle, shapeIndex);
                    }
                }
            }

            // remove the old ones
            for (int i = sf.NumShapes - 1; i >= 0; i--)
            {
                if (sf.ShapeSelected[i])
                {
                    undoList.Add(tkUndoOperation.uoRemoveShape, layerHandle, i);
                    sf.EditDeleteShape(i);
                }
            }

            undoList.EndBatch();

            for (int i = newSelectionStart; i < sf.NumShapes; i++)
            {
                sf.ShapeSelected[i] = true;
            }

            return(ExplodeResult.Ok);
        }