How to Delete?

Mar 24, 2011 at 11:13 AM

I'm having trouble deleting a couple selected points from a point layer (that does have an attribute dbf table).  I've tried several methods but not had any luck yet.  I've tried:

ptLyr.RemoveSelectedFeatures();  - this works visually but then executing a save crashes

ptLyr.DataSet.RemoveShapeAt(ftr.Fid); - This crashes once a new selection is made

ptLyr.DataSet.DataTable.Rows.RemoveAt(iRow); - I think this one didn't appear to do anything

ptLyr.DataSet.RemoveShapesAt(delThese); - following this up with a ptLyr.DataSet.Save();, the save errors index out of bounds

I can post longer examples and even a data set if that would help, I'm assuming I'm just overlooking some obvious method to delete the selected features and save (so they are no longer in the shape or dbf files), the RemoveSelectedFeatures looks real promising just can't get save to work with it.

May 10, 2011 at 9:44 AM

 

I AM HAVING SIMILAR ISSUES: please see detailed description below:

Here is my trouble:

Selecting a Feature and return it's FID or other fields to code for further processing - HELP NEEDED!

I am developing a custom GIS application using MapWindow in .net framework C#.

I have been able to link up everything including map, toolstrip, statusbar etc. Everything works as expected when I run the program and load my shape file to the map.

My shape file has fields like FID, longitude, latitude, address, name, population etc.

My challenge now is to read this data everytime I click on a particular feature in that shape.

I have tried an event of the map control: map1_SelectionChanged(object sender, EventArgs e) but this does not return the item or feature selected.

Any help o this will be greatly appreciated.

_________________________

In case you don't understand the picture, I will rephrase it here:

I am trying to read the attributes of features whenever I click on them (by code in C# or VB). Here is my code:

private void map1_SelectionChanged(object sender, EventArgs e)

{

MapWindow.Data.IFeature feat =

map1.GetFeatureLayers()[0].DataSet.GetFeature(index)

}

the problem is that the eventArgs - e, does not give me the selected index of the feature that was clicked upon. If I change the value of 'index' above to a number, say 21, I actually get the feature with index of 21 and from them I could get the recordset to display on a form; which is really what I am interested in. PLZ help. I am stuck.

 
May 10, 2011 at 4:21 PM

Doing what you suggest in SelectionChanged seems like the best way, but since that's not working for you perhaps this snippet will be helpful (note snippet is currently setup to handle point layers only).  You could place this in it's own toolbar Edit button or perhaps in your Selectionchanged event or even on map click event:

foreach (IMapLayer lyr in map.Layers)
{
    if (lyr is IMapPointLayer)
    {
        IMapPointLayer ptLyr = lyr as IMapPointLayer;
        if (ptLyr.IsVisible && ptLyr.DrawnStatesNeeded)
        {
            DotSpatial.Symbology.ISelection sels = ptLyr.Selection;
            if (sels != null && sels.Count > 0)
            {
                // Do what you want with selected features
            }
        }
    }
}

May 12, 2011 at 12:53 PM

 

Whenever I click on a feature on the map (or select a number of features - using rectangle) using the Selection tool in the toolbar, the SelectionChange event of the map gets fired, but then I see Count = 0 for the ISelection object when I step into in Debug mode. Nothing seems to be selected. If nothing is selected or count of selected features is 0, then I can't get my features. Why is this happening??. I have been looking into this for days now. Please help; and thanks for your previous help. Look forward to hearing from you...

May 21, 2011 at 1:59 AM

Hi, I am new with the mapwindow, use vb.net 2008, and hope you can help me, I having some troubles adding and deleting point features, with the RemoveAt(iRow), I try to delete selected points, as Bigger says, looks like didn't happend, but when I close my app and star again, the points was deleted, then when I remove some selected points, do the save part, remove the point layer, and reload it, I know it is not de best way, but is working, I can add points and consulting data from the selected, but when I remove all the points of the layer, and after try to add a point, save crash. I try to close layer, clear features, clear datatable. rows, without success. I don't know where is my error or if I am missing something. This is the code to add and delete points.

Private Sub Map1_MouseDown(ByVal sender as Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Map1.MouseDown

        If ToolStripButton5.CheckState Then
            Dim lyr As MapWindow.Data.FeatureSet = BuscaLayer("puntosc").DataSet

            Dim cp As New MapWindow.Geometries.Coordinate
            cp = Map1.PixelToProj(e.Location)
            Cursor = Cursors.WaitCursor

            Dim punto As New MapWindow.Data.Feature(cp)

            Dim coment As String = InputBox("Comentario", "APP")
            If comen = "" Then
                Cursor = Cursors.Default
                Exit Sub
            End If

                lyr.AddFeature(punto)
                lyr.DataTable.Rows(lyr.DataTable.Rows.Count - 1).Item(1) = coment
                lyr.DataTable.Rows(lyr.DataTable.Rows.Count - 1).Item(2) = 0
                lyr.Save()
                lyr.Close()

  Cursor = Cursors.Default
End Sub

Private Sub Map1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Map1.MouseUp

       If  ToolStripButton7.CheckState Then

            Cursor = Cursors.WaitCursor
         
            Dim ly As MapWindow.Drawing.FeatureLayer = BuscaLayer("puntosc")
            Dim lys As MapWindow.Drawing.ISelection
            lys = ly.Selection

            Dim idx As Integer
            Dim ban As Boolean = False

            For Each sel In lys.ToFeatureList

                If 6 = MsgBox("Delete: " & sel.DataRow.Item("COMENTARIO"), MsgBoxStyle.YesNo, "APP") Then

                    idx = ly.DataSet.Features.IndexOf(sel)
                    ly.DataSet.Features.RemoveAt(idx)
                    ly.DataSet.Save()
                    ly.DataSet.Close()

             ban = True
                End If

            Next
            If ban Then
                Map1.Layers.Remove(BuscaLayer("puntosc"))
                CargaLayers("puntosc", mnuComentarios, Color.Red, Color.DarkGray, 1, 0, Color.Black, ContentAlignment.TopRight, 10, True, , )

                Dim lyps As MapWindow.Map.MapPointLayer = BuscaLayer("puntosc")
                lyps.SelectionSymbolizer = New MapWindow.Drawing.PointSymbolizer(Color.Gold, MapWindow.Drawing.PointShapes.Ellipse, 12)
                lyps.SelectionSymbolizer.SetOutline(Color.Red, 1)
                Map1.Refresh()
            End If
            Cursor = Cursors.Default

        End If
    End Sub

Jun 22, 2011 at 10:59 AM
Hobbesone wrote:

Hi, I am new with the mapwindow, use vb.net 2008, and hope you can help me, I having some troubles adding and deleting point features, with the RemoveAt(iRow), I try to delete selected points, as Bigger says, looks like didn't happend, but when I close my app and star again, the points was deleted, then when I remove some selected points, do the save part, remove the point layer, and reload it, I know it is not de best way, but is working, I can add points and consulting data from the selected, but when I remove all the points of the layer, and after try to add a point, save crash. I try to close layer, clear features, clear datatable. rows, without success. I don't know where is my error or if I am missing something. This is the code to add and delete points.

Private Sub Map1_MouseDown(ByVal sender as Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Map1.MouseDown

        If ToolStripButton5.CheckState Then
            Dim lyr As MapWindow.Data.FeatureSet = BuscaLayer("puntosc").DataSet

            Dim cp As New MapWindow.Geometries.Coordinate
            cp = Map1.PixelToProj(e.Location)
            Cursor = Cursors.WaitCursor

            Dim punto As New MapWindow.Data.Feature(cp)

            Dim coment As String = InputBox("Comentario", "APP")
            If comen = "" Then
                Cursor = Cursors.Default
                Exit Sub
            End If

                lyr.AddFeature(punto)
                lyr.DataTable.Rows(lyr.DataTable.Rows.Count - 1).Item(1) = coment
                lyr.DataTable.Rows(lyr.DataTable.Rows.Count - 1).Item(2) = 0
                lyr.Save()
                lyr.Close()

  Cursor = Cursors.Default
End Sub

Private Sub Map1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Map1.MouseUp

       If  ToolStripButton7.CheckState Then

            Cursor = Cursors.WaitCursor
         
            Dim ly As MapWindow.Drawing.FeatureLayer = BuscaLayer("puntosc")
            Dim lys As MapWindow.Drawing.ISelection
            lys = ly.Selection

            Dim idx As Integer
            Dim ban As Boolean = False

            For Each sel In lys.ToFeatureList

                If 6 = MsgBox("Delete: " & sel.DataRow.Item("COMENTARIO"), MsgBoxStyle.YesNo, "APP") Then

                    idx = ly.DataSet.Features.IndexOf(sel)
                    ly.DataSet.Features.RemoveAt(idx)
                    ly.DataSet.Save()
                    ly.DataSet.Close()

             ban = True
                End If

            Next
            If ban Then
                Map1.Layers.Remove(BuscaLayer("puntosc"))
                CargaLayers("puntosc", mnuComentarios, Color.Red, Color.DarkGray, 1, 0, Color.Black, ContentAlignment.TopRight, 10, True, , )

                Dim lyps As MapWindow.Map.MapPointLayer = BuscaLayer("puntosc")
                lyps.SelectionSymbolizer = New MapWindow.Drawing.PointSymbolizer(Color.Gold, MapWindow.Drawing.PointShapes.Ellipse, 12)
                lyps.SelectionSymbolizer.SetOutline(Color.Red, 1)
                Map1.Refresh()
            End If
            Cursor = Cursors.Default

        End If
    End Sub


the function add point is work,but it can't show the point i added live,is there still need something to control? can anybody tell me how?