This project is read-only.

IndexOutOfRangeException

Jun 9, 2012 at 3:34 AM
Edited Jun 9, 2012 at 3:35 AM

Hi all ,

    Is someone meet the same exception? I use the follow code to create a shp file but an exception happen!

            FeatureSet fs = new FeatureSet(FeatureType.Point);
            string filename = Path.Combine(Application.StartupPath , "testPoint.shp");
            fs.SaveAs(filename, true);
            MapPointLayer layer = new MapPointLayer(fs);
            layer.Symbolizer = new PointSymbolizer(Color.Black, DotSpatial.Symbology.PointShape.Ellipse, 5);
            Random rnd=new Random(this.Width);
            IMapLayer player= map1.AddLayer(filename);
            fs = player.DataSet as FeatureSet;
            fs.DataTable.Columns.Add("ID");
            for (int i = 0; i < 10000;i++ )
            {
                double x = rnd.NextDouble() * 1000;
                double y = rnd.NextDouble() * 10000;
                DotSpatial.Topology.Point p = new DotSpatial.Topology.Point(x, y);
                IFeature f = fs.AddFeature(p);
                f.DataRow["ID"] = i;
            }
            fs.InitializeVertices();
            map1.Refresh();  //here the exception occur!

 And here is the Calling stack :

   At DotSpatial.Controls.MapPointLayer.DrawFeatures(MapArgs e, IEnumerable`1 indices) Location G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\MapPointLayer.cs:Rownumber 478
   At DotSpatial.Controls.MapPointLayer.DrawFeatures(MapArgs args, List`1 indices, List`1 clipRectangles, Boolean useChunks) Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\MapPointLayer.cs:Rownumber 219
   At DotSpatial.Controls.MapPointLayer.DrawRegions(MapArgs args, List`1 regions) Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\MapPointLayer.cs:Rownumber 286
   At DotSpatial.Controls.MapFrame.Initialize(List`1 regions) Location G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\MapFrame.cs:Rownumber 325
   At DotSpatial.Controls.MapFrame.Initialize() Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\MapFrame.cs:Rownumber 388
   At DotSpatial.Controls.Map.Refresh() Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Controls\Map.cs:Rownumber 1611
   At DemoMap.MainForm.button2_Click(Object sender, EventArgs e) Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Examples.DemoMap\MainForm.cs:Rownumber 541
   At System.Windows.Forms.Control.OnClick(EventArgs e)
   At System.Windows.Forms.Button.OnClick(EventArgs e)
   At System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   At System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   At System.Windows.Forms.Control.WndProc(Message& m)
   At System.Windows.Forms.ButtonBase.WndProc(Message& m)
   At System.Windows.Forms.Button.WndProc(Message& m)
   At System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   At System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   At System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   At System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   At System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   At System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   At System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   At System.Windows.Forms.Application.Run(Form mainForm)
   At DemoMap.Program.Main() Location  G:\dotspatial1213\DotSpatial_c353d1c0f846\DotSpatial.Examples.DemoMap\Program.cs:Rownumber  35
   At System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   At System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   At Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   At System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   At System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   At System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   At System.Threading.ThreadHelper.ThreadStart()



Who can help me to sovle this problem?

Thanks!

Sep 12, 2012 at 6:19 AM

Getting the same sort of error, did you ever get to the bottom of this?

Regards

Sep 12, 2012 at 8:15 AM

try this code please! 

            Random rnd = new Random();
            string file = Path.Combine(Application.StartupPath, "testPoint.shp");
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.Projection = map1.Projection;
            for (int i = 0; i < 1000; i++)
            {
                int x = rnd.Next(map1.Width);
                int y = rnd.Next(map1.Height);
                System.Drawing.Point p = new System.Drawing.Point(x, y);
                Coordinate c = map1.PixelToProj(p);
                IFeature f = new Feature(c);
                fs.AddFeature(f);
            }
            fs.SaveAs(file, true);
Sep 12, 2012 at 10:57 AM

try the for loop starting from 1 instead of 0. it does not like ID 0..... i raised a similar query some time back.

Sep 13, 2012 at 3:33 AM

well ,i don't know what you mean? can you tell more?

Sep 17, 2012 at 10:13 AM

Okie, ignore my message above. I have tried the above code and it works fine for me.

Sep 20, 2012 at 12:57 AM

the last example misses the part of adding a column and to fill it.....  the code below doesnt work   NullReferenceException was unhandled.... How to solve this then?

        Dim rnd As Random = New Random
        Dim file As String = Path.Combine(Application.StartupPath, "c:\testPoint.shp")
        Dim fs As FeatureSet = New FeatureSet(FeatureType.Point)
        fs.Projection = myVenster.Projection
        fs.DataTable.Columns.Add("ID")
        Dim i As Integer = 0
        Do While (i < 1000)
            Dim x As Integer = rnd.Next(myVenster.Width)
            Dim y As Integer = rnd.Next(myVenster.Height)
            Dim p As System.Drawing.Point = New System.Drawing.Point(x, y)
            Dim c As Coordinate = myVenster.PixelToProj(p)
            Dim f As IFeature = New Feature(c)
            fs.AddFeature(f)
            f.DataRow("ID") = i
            i = (i + 1)
        Loop
        fs.SaveAs("c:\test.shp", True)
        myVenster.AddLayer("c:\test.shp")

Sep 20, 2012 at 2:31 AM

well the feature f is not in the featureset fs ,so it has an exception, you should do like this

Dim f As IFeature = New Feature(c) 
 dim newfeature as ifeature;
newfeature= fs.AddFeature(f) 
newfeature.DataRow("ID") = i
go ahead!
Sep 20, 2012 at 2:32 AM

thx   at your service to try it immediatly!  :o)

Sep 20, 2012 at 2:59 AM

It works thx,.....    but  in my own programm it wont so im sure i make some nooby mistake  but which one :o)

I have a list of coordinates GPPlijst, where i add a few columns...

Then for each corodinate from the GPPlijst-list I want to add some values and strangly the columns are made but there is no data transferred into it....

You have any idea why not?

 

            Dim count As Long
            Dim coord As Coordinate() = New Coordinate(GPPlijst.Count - 1) {}
            Dim filename As String = Form1.txtPadFileNaam.Text & Form1.lblGPPverschilNaam.Text & ".shp"
            Dim f As New Point
            Dim fs As New FeatureSet(f.FeatureType)
            fs.Projection = Form1.myVenster.Projection
            'Voeg kolommen toe
            fs.DataTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
            fs.DataTable.Columns.Add(New DataColumn("Xgpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("Ygpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("dBref", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBvar", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBverschil", GetType(System.Single)))
            'Aantal GPPS 
            For count = 0 To GPPlijst.Count - 1
                'Per adres
                coord(count) = New Coordinate(GPPlijst(count).GPPx, GPPlijst(count).GPPy)
                fs.Features.Add(coord(count))
                'Ken waarden aan de kolommen toe
                Dim feature As IFeature = fs.AddFeature(f)

                'Dim f As IFeature = New Feature(coor)
                Dim newfeature As IFeature
                newfeature = fs.AddFeature(f)

                feature.DataRow.BeginEdit()
                feature.DataRow("ID") = count + 1
                feature.DataRow("Xgpp") = GPPlijst(count).GPPx
                feature.DataRow("Ygpp") = GPPlijst(count).GPPy
                feature.DataRow("dBref") = GPPlijst(count).GPP_dBref
                feature.DataRow("dBvar") = GPPlijst(count).GPP_dBvar
                feature.DataRow("dBverschil") = GPPlijst(count).GPP_dBverschil
                feature.DataRow.EndEdit()

            Next
            fs.SaveAs(filename, True)
            Form1.myVenster.AddLayer(filename)

Sep 20, 2012 at 8:06 AM

I think the problem is here:

                fs.Features.Add(coord(count)) //well this line code has add a new feature into the featureset,but without column value
                'Ken waarden aan de kolommen toe
                Dim feature As IFeature = fs.AddFeature(f)  //the param f i found from your code is null
                'Dim f As IFeature = New Feature(coor)
                Dim newfeature As IFeature
                newfeature = fs.AddFeature(f)
I  think that you should change your code like this:
                coord(count) = New Coordinate(GPPlijst(count).GPPx, GPPlijst(count).GPPy)
               
                Dim f As IFeature = New Feature(coor)
                Dim newfeature As IFeature
                newfeature = fs.AddFeature(f)

                feature.DataRow.BeginEdit()
                feature.DataRow("ID") = count + 1
                feature.DataRow("Xgpp") = GPPlijst(count).GPPx
                feature.DataRow("Ygpp") = GPPlijst(count).GPPy
                feature.DataRow("dBref") = GPPlijst(count).GPP_dBref
                feature.DataRow("dBvar") = GPPlijst(count).GPP_dBvar
                feature.DataRow("dBverschil") = GPPlijst(count).GPP_dBverschil
                feature.DataRow.EndEdit()
Sep 20, 2012 at 9:18 PM

sorry but i really cant get it working.......   

Sep 20, 2012 at 9:40 PM

I tried a few different approaches... This example still contains an error.... the specific filetype is not supported... I admit i just compose these code from different other examples....  maybe u can help me to clear this step by step?

 Dim count As Long
            Dim coord As Coordinate() = New Coordinate(GPPlijst.Count - 1) {}
            Dim filename As String = Form1.txtPadFileNaam.Text & Form1.lblGPPverschilNaam.Text & ".shp"
            Dim f As New Feature
            Dim fs As New FeatureSet(f.FeatureType)
            'Voeg kolommen toe
            fs.DataTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
            fs.DataTable.Columns.Add(New DataColumn("Xgpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("Ygpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("dBref", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBvar", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBverschil", GetType(System.Single)))
            'Set projection
            fs.Projection = Form1.myVenster.Projection
            'Aantal GPPs 
            For count = 0 To GPPlijst.Count - 1

                'Per GPP
                coord(count) = New Coordinate(GPPlijst(count).GPPx, GPPlijst(count).GPPy)
                'Ken waarden aan de kolommen toe

                Dim newfeature As IFeature
                newfeature = fs.AddFeature(f)
                ' now the resulting features knows what columns it has
                ' add values for the columns
                newfeature.DataRow.BeginEdit()
                newfeature.DataRow("ID") = count + 1
                newfeature.DataRow("Xgpp") = GPPlijst(count).GPPx
                newfeature.DataRow("Ygpp") = GPPlijst(count).GPPy
                newfeature.DataRow("dBref") = GPPlijst(count).GPP_dBref
                newfeature.DataRow("dBvar") = GPPlijst(count).GPP_dBvar
                newfeature.DataRow("dBverschil") = GPPlijst(count).GPP_dBverschil
                newfeature.DataRow.EndEdit()

            Next
            fs.SaveAs(filename, True)
            Form1.myVenster.AddLayer(filename)
Sep 21, 2012 at 1:45 AM

well ,you miss the code: Dim f As IFeature = New Feature(coor)

Sep 21, 2012 at 2:24 AM
Edited Sep 21, 2012 at 2:29 AM

thx izz but still it wont work, I challenge you to point me where to put it without errors :o)

all i want is to create a point shape from a list of coordinates and add columns to it with values....

Sep 21, 2012 at 3:35 AM
Edited Sep 21, 2012 at 3:54 AM

what is the exception? please read the code below more seriously!

Dim rnd As Random = New Random
        Dim file As String = Path.Combine(Application.StartupPath, "testPoint.shp")
        Dim fs As FeatureSet = New FeatureSet(FeatureType.Point)
        fs.Projection = myVenster.Projection
        fs.DataTable.Columns.Add("ID")
        Dim i As Integer = 0
        Do While (i < 1000)
            Dim x As Integer = rnd.Next(myVenster.Width)
            Dim y As Integer = rnd.Next(myVenster.Height)
            Dim p As System.Drawing.Point = New System.Drawing.Point(x, y)
            Dim c As Coordinate = myVenster.PixelToProj(p)
            Dim f As IFeature = New Feature(c)
            Dim ff As IFeature = fs.AddFeature(f)
            ff.DataRow("ID") = i
            i = (i + 1)
        Loop
        fs.SaveAs(file,True)
        myVenster.AddLayer(file)

Sep 21, 2012 at 6:52 AM

The code below worked, at least it didnt gave any errors..... thx for helping but tbh its hard to understand what happends with all the sets.....

 Dim count As Long
            Dim coor As Coordinate() = New Coordinate(GPPlijst.Count - 1) {}
            Dim filename As String = Form1.txtPadFileNaam.Text & Form1.lblGPPverschilNaam.Text & "_GPPs.shp"
            Dim fs As New FeatureSet(FeatureType.Point)
            'Voeg kolommen toe
            fs.DataTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
            fs.DataTable.Columns.Add(New DataColumn("Xgpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("Ygpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("dBref", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBvar", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBverschil", GetType(System.Single)))
            'Set projection
            fs.Projection = Form1.myVenster.Projection
            'Aantal GPPs 
            For count = 0 To GPPlijst.Count - 1

                'Per GPP
                Dim c As New Coordinate(GPPlijst(count).GPPx, GPPlijst(count).GPPy)
                Dim f As IFeature = New Feature(c)
                Dim ff As IFeature = fs.AddFeature(f)
                ' now the resulting features knows what columns it has
                ' add values for the columns
                ff.DataRow.BeginEdit()
                ff.DataRow("ID") = count + 1
                ff.DataRow("Xgpp") = GPPlijst(count).GPPx
                ff.DataRow("Ygpp") = GPPlijst(count).GPPy
                ff.DataRow("dBref") = GPPlijst(count).GPP_dBref
                ff.DataRow("dBvar") = GPPlijst(count).GPP_dBvar
                ff.DataRow("dBverschil") = GPPlijst(count).GPP_dBverschil
                ff.DataRow.EndEdit()

            Next
Sep 21, 2012 at 6:57 AM

well ,you dont know why? why dont you compare the two code snippet and found out the difference!

Sep 22, 2012 at 3:51 AM

this code works...... i think its a nice solution.... any comments on it?

 

        Dim count As Long
        Dim filename As String = Form1.txtPadFileNaam.Text & Form1.txtGPPverschilNaam.Text & "_GPPs.shp"
        Dim fs As New FeatureSet(FeatureType.Point)
        'Voeg kolommen toe
        Try
            fs.DataTable.Columns.Add(New DataColumn("ID", GetType(System.Int64)))
            fs.DataTable.Columns.Add(New DataColumn("Xgpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("Ygpp", GetType(System.Double)))
            fs.DataTable.Columns.Add(New DataColumn("dBref", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBvar", GetType(System.Single)))
            fs.DataTable.Columns.Add(New DataColumn("dBverschil", GetType(System.Single)))
            'Set projection
            fs.Projection = Form1.myVenster.Projection
            'Aantal GPPs 
            For count = 0 To GPPlijst.Count - 1
                'Per GPP
                Dim c As New Coordinate(GPPlijst(count).GPPx, GPPlijst(count).GPPy)
                Dim ff As IFeature = New Feature(c)
                ff = fs.AddFeature(ff)
                ff.DataRow.BeginEdit()
                ff.DataRow("ID") = count + 1
                ff.DataRow("Xgpp") = GPPlijst(count).GPPx
                ff.DataRow("Ygpp") = GPPlijst(count).GPPy
                ff.DataRow("dBref") = GPPlijst(count).GPP_dBref
                ff.DataRow("dBvar") = GPPlijst(count).GPP_dBvar
                ff.DataRow("dBverschil") = GPPlijst(count).GPP_dBverschil
                ff.DataRow.EndEdit()
                ff = Nothing
            Next
            fs.SaveAs(filename, True)
            Form1.myVenster.AddLayer(filename)