Create Shape, Edit Shape, Edit Attributes, Save Results

Shapefile Type = SHP_POINT,
I have several Points in this Point_Shapefile.
The Shape to Add is a Point.

TO DO:

  1. Create a New Point, Save Shapefile or Update Shapefile to include the New Point

  2. Edit the New Point (or any point in the Shapefile), by moving Point Interactively to another position, then Save.

3.Edit any Point’s Attributes using the ‘Edith Attributes’ Method, Save.

  1. Delete Point Shapefile.

SOLUTIONS and PROBLEMS:

  1. Create New Point Shape was done easily following example by @jerryfaust(Mapwingis 5.0.1 tkCursorMode.cmEditShape)
    //SUMMARY
    private void BtnAddNew_Click(object sender, EventArgs e)
    {
    _shpfile.InteractiveEditing = true;
    axMap1.CursorMode = tkCursorMode.cmAddShape;
    axMap1.MapCursor = tkCursor.crsrMapDefault;
    }
    Point Added Successfully,
    PROBLEM: New Point Shape NOT Saved when Page is Refreshed.

  2. Edit Point Shapefile also worked without problem.
    @jerryfaust( Mapwingis 5.0.1 tkCursorMode.cmEditShape)
    //SUMMARRY
    private void BtnEdit_Click(object sender, EventArgs e)
    {
    _shpfile.InteractiveEditing = true;
    axMap1.CursorMode = tkCursorMode.cmEditShape;
    axMap1.MapCursor = tkCursor.crsrMapDefault;
    _shpfile.Save();
    }
    The Point is moved successfully to another position on the map using the ‘cmEditShape’ tool.
    PROBLEM: Edited Point NOT Saved when Page is Refreshed.

  3. Using the ‘Edit Attributes’ Example; (https://www.mapwindow.org/documentation/mapwingis4.9/_edit_attributes_8cs-example.html)
    EditAttributes
    //SUMMARRY:
    int layerHandle = axMap1.get_LayerHandle(0); // it’s assumed here that the layer we want to edit is the first 1 (with 0 index)
    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
                 {
                     sf.set_ShapeSelected(shapes[0], true);  // selecting the shape we are about to edit
                     axMap1.Redraw(); Application.DoEvents();
                 
                     Form form = new Form();
                     for (int i = 0; i < sf.NumFields; i++)
                     {
                         System.Windows.Forms.Label label = new System.Windows.Forms.Label();
                         label.Left = 15;
                         label.Top = i * 30 + 5;
                         label.Text = sf.Field[i].Name;
                         label.Width = 60;
                         form.Controls.Add(label);
                         TextBox box = new TextBox();
                         box.Left = 80;
                         box.Top = label.Top;
                         box.Width = 80;
                         box.Text = sf.CellValue[i, shapes[0]].ToString();
                         box.Name = sf.Field[i].Name;
                         form.Controls.Add(box);
                     }
                     form.Width = 180;
                     form.Height = sf.NumFields * 30 + 70;
                     Button btn = new Button
                                  {
                                      Text = "Ok",
                                      Top = sf.NumFields*30 + 10,
                                      Left = 20,
                                      Width = 70,
                                      Height = 25
                                  };
                     btn.Click += BtnClick;
                     form.Controls.Add(btn);
                     btn = new Button
                           {
                               Text = "Cancel",
                               Top = sf.NumFields*30 + 10,
                               Left = 100,
                               Width = 70,
                               Height = 25
                           };
                     btn.Click += BtnClick;
                     form.Controls.Add(btn);
                     form.FormClosed += FormFormClosed;
                     form.Text = "Shape: " + shapes[0];
                     form.ShowInTaskbar = false;
                     form.StartPosition = FormStartPosition.CenterParent;
                     form.FormBorderStyle = FormBorderStyle.FixedDialog;
                     form.MaximizeBox = false;
                     form.MinimizeBox = false;
                     form.ShowDialog(axMap1.Parent);
                 }
             }
         }
         // Execute this code if you want to save the results.
         // sf.StopEditingShapes(true, true, null);
    

Point Edited Successfully. The Attribute Table is Updated Successfully.
PROBLEM: Updated Values in the Attribute Table NOT Saved when Page is Refreshed.

  1. Delete Point Works without a hitch, following the MapWinGIS Example(https://www.mapwindow.org/documentation/mapwingis4.9/_remove_shape_8cs-example.html)
    The Point is Deleted immediately, and the Refreshed Page indicates clearly that the Point was Deleted.

But Here is something interesting.
If the Point Shape is Added successfully using cmAddShape( which does not save) but, when I Select the EditShape Button and Click on the Newly Added Point, or move the point around, the Newly Created Point is SAVED! But the EditShape interactive Editing position of the New Point is NOT Saved. The Point goes back to the point where it was created.
Why is New Point Saved ONLY after cmEdit tool is used to select the new point?

All right folks, sorry for taking up so much of your reading time. Please I need some one kind with a little extra time on their hand to take a look at this and let me know what is I’m doing not right.
All I want done is Save.

I’ve tried
“sf.Save()” and
sf.SaveAs(@" c:\ point.shp") and
sf.StopEditingShapes(true, true, null);

Hello @sw4web

At first glance (perhaps not in depth), is your call to _shpfile.Save() in the BtnEdit_Click method. That was not in the original example, and I don’t think you want to do that.

private void BtnEdit_Click(object sender, EventArgs e)
{
    _shpfile.InteractiveEditing = true;
    axMap1.CursorMode = tkCursorMode.cmEditShape;
    axMap1.MapCursor = tkCursor.crsrMapDefault;
    _shpfile.Save();  // <- this was not in the original example
}

What you are doing is Saving prior to actually making the edit. This event handler sets up the edit, then saves the shapefile (to disk), after which you are then making your edit. So if at that point, you say you are refreshing the map (do you mean that you reload from disk?) If so, I believe that you are reloading the pre-edited shapefile.

You should perhaps have a separate Save button, in which you call _shpfile.Save(). All adds, edits and deletes are taking place in memory, and you can also undo; and you can click the Save button occasionally when you want to save your changes to disk.

Respectfully,
Jerry.

Hi, Jerry thanks. My mistake, adding the Save bit. Will create a separate Save button and let you know how it goes.
Thank you for your time.

Hello, @jerryfaust Everything works like a charm with the separate Save button. However, the Save Edit Attributes does not overwrite the Shapefile being editied, but saves smoothly in a different location. I could live with that. Once more thank you very much.

Hello @sw4web

You’re welcome. Glad that it’s (mostly) working. It’s strange that you have to do a SaveAs…

Are you still in Edit mode when you try to save? That is a prerequisite. (see the Save documentation here)

Also, you could use the Callback function of the Save method (or better yet, the global callback to receive all errors and warnings). It would be interesting to know if the OCX is telling you why it won’t save.

Regards,
Jerry.

Hi, @jerryfaust thanks for your enlightenment. I really can’t say if I’m still in Edit mode or not. As I said, I tried “sf. StopEditingShapes(true, true, null)” hoping that should exit Edit mode. Shapefile still not Saved when I close the window and open it again, I notice that changes I made didn’t save.

I would love to use the Global Callback function so I can see what type of errors and where they are coming from, but I don’t know how to apply this function. A simple guide would do.

Thanks once more. You’ve been of great help in this matter. Keep it up.

Regards.

Hello @sw4web

The easiest way to set up the callback is within your main Form, in c#, something like

public partial class Form1 : Form, ICallback
{
    public Form1()
    {
        InitializeComponent();
    }

    public void Progress(string KeyOfSender, int Percent, string Message)
    {
        //
    }

    public void Error(string KeyOfSender, string ErrorMsg)
    {
        Console.WriteLine(string.Format("{0}: {1}", KeyOfSender, ErrorMsg));
    }
}

Give that a try.
Jerry.

Will do. Let you know the result.
Thanks.

Hi, @jerryfaust
Gave it a shot. Same results. Couldn’t figure it out. I’ve settled for “SaveAs”, although “Save” would have been more appropriate, to avoid having so many updated files. No need to border you further on the subject, seeing as we are quite busy.
Thanks for your support all the same. My project is almost completed. If I run into further issues I’ll let you know. If you would like a private audience of my Windows Desktop Application (Due to security reasons I can’t make it public), I will zip the package to you privately for comment - If you have the time.
Thanks.
Regards.