This project is read-only.

add a point to a exsit point shapefile encounter exception

Jun 24, 2011 at 9:49 AM

Dear all:

i add a point to a exsit point shapefile use the code below,but it encounters a exception,i don't know why?

           FeatureLayer featureLayer = null;
            FeatureSet featureSet = null;
            if (map1.Layers.Count > 0)
                featureLayer = map1.Layers[0] as FeatureLayer;
                featureSet = featureLayer.DataSet as FeatureSet;
                Coordinate coordinate = map1.PixelToProj(e.Location);
                DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(coordinate);
                Feature feature = new Feature(point);
                featureSet.DataTable.Rows[featureSet.DataTable.Rows.Count - 1][0] = featureSet.DataTable.Rows.Count;
                //featureSet.DataTable.Rows[featureSet.DataTable.Rows.Count - 1][1] = "123";              

                map1.MapFrame.Invalidate();  ===================>this code will crash while running


i hope someone tell me how to solve the problem;

best wishes!

Jun 29, 2011 at 11:26 AM
Edited Jun 29, 2011 at 11:36 AM

Hi ,

I also met similar problems. My code follows the DotSpatial_Tutorial with some modifications. There are two parts of my codes, in which one is a menuItem_Click event and the other is under Map_MouseDown Event.

Here are my codes:

Part I: Menu_Click Event

'set the cursor mode 

Me.MapMain.Cursor = Cursors.Cross

Dim pFeatLyr As FeatureLayer

FeatureLayer = TryCast(Me.MapMain.Layers.SelectedLayer, MapPointLayer)


pFeatureSet = CType(pFeatLyr.DataSet, FeatureSet)

‘get the ID of the last feature in the current featureset

pShapeID = pFeatureSet.DataTable.Rows.Count

'set the mouse click, enable drawing 

pointmouseClick = True


PartII: Map_MouseDown Event


If e.Button = Windows.Forms.MouseButtons.Left And Me.MapMain.Cursor = Cursors.Cross Then

    If pointmouseClick = True Then

         Dim coord As Coordinate = Me.MapMain.PixelToProj(e.Location)

         Dim newPoint As New DotSpatial.Topology.Point(coord)

         Dim pFeature As DotSpatial.Data.IFeature = pFeatureSet.AddFeature(newPoint)

         pShapeID += 1

         pFeature.DataRow("ID") = pShapeID

         'Refresh the map

         Me.MapMain.ResetBuffer()    'Here came the errors


         Me.MapMain.Cursor = Cursors.Default

         pointmouseClick = False

     End If

End If




If I use the code “Me.MapMain.ResetBuffer()” , errors came with “System.IndexOutOfRangeException was unhandled: Index was outside the bounds of the array.” However, when I remove it, the newly added point couldn’t display on the map. But if I saved the featureset, remove the point layer and reload it again, the added points could now be displayed correctly.

What wrong with it? Could anyone tell me how to modify the code? Thank very much!


Jul 30, 2011 at 4:08 AM

I  met similar problems too.

Could anyone tell me how to modify?



Aug 2, 2011 at 3:29 AM

no one can solve this problem? I have the same problem.

Aug 2, 2011 at 4:55 AM
add the codes above before you use map.refresh();
Oct 19, 2012 at 6:07 PM

thanks that solved my problem..

Dim fLayerNodes As FeatureLayer