Doing DotSpatial Programming: To Create Random Points in Visual C# 2010 Express - No errors & No result!!

Feb 8, 2011 at 5:22 PM
Edited Feb 8, 2011 at 5:27 PM

Hi all,

I tried to do my first DotSpatial programming: To Create Randon Points in Visual C# 2010 Express.  I managed to get the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Projections;



namespace scDotSpatial_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void BuildPoints()
        {
            // Setup a new "shapefile" by using the featureset object
            FeatureSet fs = new FeatureSet(FeatureTypes.Point);
            // You can control things like the projection with this object
            fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
            // The DataTable is a standard .Net DataTable, so you can add columns the normal way and use with DataGrid controls
            fs.DataTable.Columns.Add("Elevation", typeof(int));

            // Set up the specs for creating random points.  If you already know the point values from a file this is not necessary.
            Random rnd = new Random(DateTime.Now.Millisecond);
            const int YMIN = 0;
            const int YSPAN = 1000;
            const int XMIN = 0;
            const int XSPAN = 1000;

            // In a loop we are creating 100 different points using the box established above.
            for (int i = 0; i < 100; i++)
            {
                // A coordinate is the simple X and Y location
                Coordinate c = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);
                // A point has geoemtry capabilities like testing intersection with polygons etc.
                Point pt = new Point(c);
                // A feature also has attributes related to the featureset
                // Features can be created directly, passing the point into the constructor, but there is a glitch
                // right now that may not update the DataRow property of the feature correctly once it is added.
                IFeature currentFeature = fs.AddFeature(pt);

                // Working with the current feature allows you to control attribute content as well as the feature content.
                currentFeature.DataRow["Elevation"] = rnd.Next(0, 100);
            }
            fs.SaveAs(@"C:\test.shp", true);
        }

        
       
    }
}

I got the following 7 errors:

Error 1 The name 'FeatureTypes' does not exist in the current context C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 26 44 scDotSpatial-1
Error 2 'Point' is an ambiguous reference between 'System.Drawing.Point' and 'DotSpatial.Topology.Point' C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 45 17 scDotSpatial-1
Error 3 'Point' is an ambiguous reference between 'System.Drawing.Point' and 'DotSpatial.Topology.Point' C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 45 32 scDotSpatial-1
Error 4 The best overloaded method match for 'System.Drawing.Point.Point(int)' has some invalid arguments C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 45 28 scDotSpatial-1
Error 5 Argument 1: cannot convert from 'DotSpatial.Topology.Coordinate' to 'int' C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 45 38 scDotSpatial-1
Error 6 The best overloaded method match for 'DotSpatial.Data.FeatureSet.AddFeature(DotSpatial.Topology.IBasicGeometry)' has some invalid arguments C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 49 43 scDotSpatial-1
Error 7 Argument 1: cannot convert from 'System.Drawing.Point' to 'DotSpatial.Topology.IBasicGeometry' C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial-1\scDotSpatial-1\Form1.cs 49 57 scDotSpatial-1

Please kindly help, enlighten me where I can get the guidances to use methods, commands, etc. and advise me how I can correct the errors.

Thanks in advance,

Scott Chang 

Developer
Feb 8, 2011 at 6:20 PM

When constructing your feature set, you should use FeatureType (singular) not FeatureTypes.  Earlier docs may have used the plural version, but the change was made to singular to differentiate between bit-wise flag enums that can be combined vs. normal enums that can have just a single value.

Both the System.Drawing and DotSpatial.Topology namespaces define Point.  You will need to fully qualify the useage of Point (e.g. DotSpatial.Topology.Point) or add a #using statement such as:

using Point = DotSpatial.Topology.Point;

Feb 8, 2011 at 7:37 PM
Edited Feb 10, 2011 at 2:33 PM

Hi kellison,  Thanks for your nice response.

I made some changes according to your instructions.  The new code is listed below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Projections;
using Point = DotSpatial.Topology.Point;



namespace scDotSpatial_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void BuildPoints()
        {
            // Setup a new "shapefile" by using the featureset object
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            // You can control things like the projection with this object
            fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
            // The DataTable is a standard .Net DataTable, so you can add columns the normal way and use with DataGrid controls
            fs.DataTable.Columns.Add("Elevation", typeof(int));

            // Set up the specs for creating random points.  If you already know the point values from a file this is not necessary.
            Random rnd = new Random(DateTime.Now.Millisecond);
            const int YMIN = 0;
            const int YSPAN = 1000;
            const int XMIN = 0;
            const int XSPAN = 1000;

            // In a loop we are creating 100 different points using the box established above.
            for (int i = 0; i < 100; i++)
            {
                // A coordinate is the simple X and Y location
                Coordinate c = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);
                // A point has geoemtry capabilities like testing intersection with polygons etc.
                Point pt = new Point(c);
                // A feature also has attributes related to the featureset
                // Features can be created directly, passing the point into the constructor, but there is a glitch
                // right now that may not update the DataRow property of the feature correctly once it is added.
                IFeature currentFeature = fs.AddFeature(pt);

                // Working with the current feature allows you to control attribute content as well as the feature content.
                currentFeature.DataRow["Elevation"] = rnd.Next(0, 100);
            }
            fs.SaveAs(@"C:\Test-DotSpatial\test-RandomPts.shp", true);
        }

        
       
    }
}

It worked without any error.  I checked my C:\Test-DotSpatial folder and I did not see the test-RandonPts.shp file in that folder.  Why was the test-RandonPts.shp file not saved in my C:\Test-DotSpatial folder?   Please tell me (1) any thing is still not right in my coding, and (2) how I can get the test-RandonPts.shp file saved in my C:\Test-DotSpatial folder.

Thanks,

Scott Chang   

Developer
Feb 8, 2011 at 7:43 PM

Just use the Visual Studio debugger and step into SaveAs and see what is going on.

Feb 8, 2011 at 8:24 PM

Hi bellison,

I tried what you suggested and I think the following thing is what you wanted:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace scDotSpatial_1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

I think the last code statement is not functioning properly.   By the way, I can not get the code statement: Application.Run(new Form1()); in creating the Items of the "Toolbox" on tabPage2 - please see the other thread "...the Items of the Toolbox..." I posted and mudnug tried to help me.  Do you have any thoughts on solving this problem of not getting the shp file?   Please respond.

Thanks,

Scott Chang

Developer
Feb 8, 2011 at 8:31 PM

Scott,

What I meant was, try stepping into fs.SaveAs() with your debugger and see where it takes you.  You can probably find out what is going wrong down in there.

Good luck.

Kyle

Feb 10, 2011 at 2:29 PM
Edited Feb 10, 2011 at 4:51 PM

Hi kellison,

I did try stepping into fs.SaveAs() with my debugger and I got the thing I presented in my last post.  I continued to debug the following 3 code statements:

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

I did not see any error!!!???

Any more thoughts for solving the problem of this project?

Please help me again.

Thanks,

Scott Chang

Feb 10, 2011 at 6:31 PM
Edited Feb 10, 2011 at 6:53 PM

Per jirikadlec2 - DotSpatial.Topology.Point class,  I changed  "Point pt = new Point (c);"  to  "DotSpatial.Topoly.Point pt = new DotSpatial.Topology.Point(c);" and I executed the following modified code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Projections;
using Point = DotSpatial.Topology.Point;



namespace scDotSpatial_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void BuildPoints()
        {
            // Setup a new "shapefile" by using the featureset object
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            // You can control things like the projection with this object
            fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
            // The DataTable is a standard .Net DataTable, so you can add columns the normal way and use with DataGrid controls
            fs.DataTable.Columns.Add("Elevation", typeof(int));

            // Set up the specs for creating random points.  If you already know the point values from a file this is not necessary.
            Random rnd = new Random(DateTime.Now.Millisecond);
            const int YMIN = 0;
            const int YSPAN = 1000;
            const int XMIN = 0;
            const int XSPAN = 1000;

            // In a loop we are creating 100 different points using the box established above.
            for (int i = 0; i < 100; i++)
            {
                // A coordinate is the simple X and Y location
                Coordinate c = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);
                // A point has geoemtry capabilities like testing intersection with polygons etc.
                DotSpatial.Topology.Point pt = new DotSpatial.Topology.Point(c);
                // A feature also has attributes related to the featureset
                // Features can be created directly, passing the point into the constructor, but there is a glitch
                // right now that may not update the DataRow property of the feature correctly once it is added.
                IFeature currentFeature = fs.AddFeature(pt);

                // Working with the current feature allows you to control attribute content as well as the feature content.
                currentFeature.DataRow["Elevation"] = rnd.Next(0, 100);
            }
            fs.SaveAs(@"C:\TEMP\test-RandomPts.shp", true);
        }
      
    }
}

This project ran and gave no errors.  But I still did not see/have the results "test-RandomPts.shp" in my C:\TEMP\ folder that is only folder I am allowed to save my programming results into that folder in our Microsoft NT4 LAN System (this is informed by my Computer Team in our office).  Jiri and other DotSpatial Experts, please help and advise me in solving this problem.

Many Thanks in advance,

Scott Chang

Developer
Feb 10, 2011 at 6:56 PM

The code you show is not what is inside DotSpatial.Data.FeatureSet.SaveAs(string filename, bool overwrite)

From: Scott_H_Chang [email removed]
Sent: Thursday, February 10, 2011 9:30 AM
To: kellison@geocue.com
Subject: Re: Doing DotSpatial Programming: To Create Randon Points in Visual C# 2010 Express - 7 errors [DotSpatial:245154]

From: Scott_H_Chang

Hi kellison,

I did try stepping into fs.SaveAs() with my degger and I got the thing I presented in my last post. I continued to debug the following 3 code statements:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

I did not see any error!!!???

Any more thoughts for solving the problem of this project?

Please help me again.

Thanks,

Scott Chang

Developer
Feb 10, 2011 at 8:43 PM

I don't know if it is related, but I have been told that a new OGR feature provider has been activated with regards to shapefiles,.  This could be throwing a wrench in your process.  You can try to remove any plug-ins from your DataExtensions folder and see if the save code still works.  However, I don't think this existed on the December release.  I'd recommend building from source and trying that since I think there are a few bug-fixes in the latest source code.

To step into the code, you don't just press F11, but rather you must first left click on the very left side of the source code window of the IDE in the gray bar on the line of code that has the fs.SaveAs method.  It should add a red dot.  Then run your application and take the steps that would reach that line of code.  The application will halt and open up the source code window with a highlight on the current line.  Then you can use F11 to step into the code.  You will likely only be able to do this if you have built from source code on your machine and are referencing those files.  If you are using dll files built on someone elses machine, it will either skip to the next line or else pop up a message saying it couldn't find the source file or something.  In some cases I can't even get it to work on my own machine unless I have a project reference (instead of simply referencing the dll).

Ted

 

Feb 15, 2011 at 6:25 PM

Hi Ted,  Thanks for your nice response.

I studied OGR thing and I am not sure I understand OGR well enough to act on the OGR feature provider.

1) I just tried to avoid it and I did the following thing:  After I read your response, I downloaded the latest DotSpatial 2-12-2011 and re-started a project for the "Create a Random Points" programming by using the new "DotSpatial" Controls and the References: DotSpatial.Data, DotSpatial.Data.Forms, DotSpatial.Projections, DotSpatial.Symbology, and DotSpatial.Topology.  I executed the new project and I got the result of "No Error, No Test Result" in my C:\TEMP folder!!!   Did I use the right References this time? Should I add more dll files to the References of my project?

2) I also tried to do the "fs.SaveAs" thing for the "Create a point" project - see the following code:

System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Projections;

namespace scDotSpatial_NewPoint_btnCoord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCoord_Click(object sender, EventArgs e)
        {
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
            fs.DataTable.Columns.Add("Elevation", typeof(int));
            
            //creates a new coordinate 
            Coordinate c = new Coordinate(2.4, 7.9);
            //passes the coordinate to a new point
            DotSpatial.Topology.Point p = new DotSpatial.Topology.Point(c);
            //displayes the new point's x and y coordiantes
            MessageBox.Show("Point p is: x= " + p.X + " & y= " + p.Y);
            
            IFeature currentFeature = fs.AddFeature(p);
            fs.SaveAs(@"C:\Temp\test-onePt.shp);
        }     
    }
}

I got the following 6 errors:

Error 1 Unterminated string literal C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 36 23 scDotSpatial_NewPoint-btnCoord
Error 2 } expected C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 40 1 scDotSpatial_NewPoint-btnCoord
Error 3 } expected C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 40 1 scDotSpatial_NewPoint-btnCoord
Error 4 } expected C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 40 1 scDotSpatial_NewPoint-btnCoord
Error 5 ; expected C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 40 1 scDotSpatial_NewPoint-btnCoord
Error 6 ) expected C:\Documents and Settings\e1enxshc\my documents\visual studio 2010\Projects\scDotSpatial_NewPoint-btnCoord\scDotSpatial_NewPoint-btnCoord\Form1.cs 40 1 scDotSpatial_NewPoint-btnCoord

I think this project in 2) has nothing to do with "OGR".  I do not know how to save this "One Point with XY-Coordinates" project in my C:\TEMP folder.  Please kindly help and advise me how to get the "One-Point with XY-Coordinates" project programmed and the results/output saved in my C:\TEMP folder.

Many Thanks in advance,

Scott Chang   

Developer
Feb 15, 2011 at 11:06 PM
Edited Feb 15, 2011 at 11:18 PM

Hi Scott,

You have some syntax errors in your code. In your first line you need to have using System; instead of just System;

Instead of fs.SaveAs(@"c:\Temp\test-onePt.shp); you need to use fs.SaveAs(@"C:\Temp\test-onePt.shp", true);

You have to use the SaveAs method with two parameters. First parameter is the file name and second parameter is true if you want to overwrite an existing file. The file name string has to be enclosed by quotes (in your code you were missing the closing quote).

The errors you were getting in your code are usually visual studio syntax errors such as missing quote, missing parenthesis or missing semicolon. You can see the "error list" in Visual studio and double-click on the error message which will show you the line and the underlined syntax error.

 

After I slightly modified your code, it works correctly:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Projections;

namespace scDotSpatial_NewPoint_btnCoord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCoord_Click(object sender, EventArgs e)
        {
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
            fs.DataTable.Columns.Add("Elevation", typeof(int));

            //creates a new coordinate 
            Coordinate c = new Coordinate(2.4, 7.9);
            //passes the coordinate to a new point
            DotSpatial.Topology.Point p = new DotSpatial.Topology.Point(c);
            //displayes the new point's x and y coordiantes
            MessageBox.Show("Point p is: x= " + p.X + " & y= " + p.Y);

            IFeature currentFeature = fs.AddFeature(p);
            fs.SaveAs(@"C:\Temp\test-onePt.shp", true);
            MessageBox.Show("Shapefile created successfully.");
} } }

 


The shapefile was correctly created on my disk and I could open it in a GIS program.

When you test your code, you should also check that you have write access to your c:\Temp folder and that your "btnCoord" button is properly linked with the "btnCoord_Click" event.

Hope this helps,

Jiri

Feb 16, 2011 at 12:23 PM

Hi Jiri,  Thanks for your nice response.

I corrected my mistakes and executed the corrected code.  It worked nicely in my Visual C# 2010 Express that is in our Microsoft NT4 LAN System. 

I got the following 4 files: test-onePt.dbf, test-onePt.prj, test-onePt.shp, and test-onePt.shx, in my C:\TEMP folder.  Wonderful!!!!

I will go on to create the multi-pointspolylines (by connecting the multi-points) and polygons (by connecting the polylines) of vectors in my Visual C# 2010 Express. 

Thanks again,

Scott Chang