Create layer from DataTable

Nov 4, 2010 at 2:31 PM

I need to create a new layer using a datatable, and I noticed there's a ctor of FeatureSet:  FeatureSet(DataTable wkbTable, int wkbColumnIndex, bool indexed, FeatureType type).

Here's my code:

                string sql = "select * from tgr32017lka";
                NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn);
                DataTable table = new DataTable();

                FeatureSet fs = new FeatureSet(table, 21, false, DotSpatial.Topology.FeatureType.Line);

unfountionatly, nothing shows on the mapcontrol, and the variable 'fs' has no data.

Did I miss something? Can someone show me a sample on how to create a feature layer from datatable?

Jul 8, 2011 at 5:29 PM

I'd really like to do this also.  I'm trying to use a SQL Server 2008 table with a geography column as a FeatureSet. 

Jul 8, 2011 at 8:47 PM

Here is my code:

    Private Sub Test(ByVal ConnectionString As String)
        Dim Sql As String = "select top(100) CaribouID,LineGeography from CaribouMovements"
        Dim MyDataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(Sql, New  System.Data.SqlClient.SqlConnection(ConnectionString))
        Dim MyLinesDataTable As New DataTable
        Dim MyFeatureSet As New FeatureSet(MyLinesDataTable, 1, False, DotSpatial.Topology.FeatureType.Line)
    End Sub

When I run the sub nothing happens.  When I put a breakpoint in the sub I get a window asking where C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\FeatureSet.cs is.  The debugger leads to a No Source Available tab with the error:

Locating source for 'C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs'. Checksum: MD5 {7f aa 18 2e f1 8f a0 89 ea 8 25 33 c1 91 26 3f}
The file 'C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs' does not exist.
Looking in script documents for 'C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs'...
Looking in the projects for 'C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs'.
The file was not found in a project.
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debugger will ask the user to find the file: C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs.
The user pressed Cancel in the Find Source dialog. The debug source files settings for the active solution have been modified so that the debugger will not ask the user to find the file: C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs.
The debugger could not locate the source file 'C:\dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DataSet.cs'.

Mar 12, 2012 at 9:39 AM
Edited Mar 12, 2012 at 10:18 AM


The method you're calling isn't working because i'ts not implemented properly.  If you downloaded the source code, you would be able to browse to it instead of going to the 'No Source Available' tab.

If you look at the code below, you'll see that the FeatureSet overload is doing the following:

- The wkbTable variable is not being passed to the global _wkbTable class (this may have flow on effects, but is not causing your problem)

- The wkbColumnIndex is ignored, and the wkbColumn is assumed as row[0]

- A SqlGeometry or SqlGeography object is not castable to a byte[].  Provided the other problems are solved here, you would need to present a query as follows in order to be able to cast your wkb to a byte[]: "SELECT geom.AsBinary() as WKB".  To include your attributes in the query, you would need to add them after the WKB element since wkbColumnIndex is set to '0'.

- It looks like only polygons are supported at this point.

- On top of all that 'IndexMode' appears as if it will always be false, therefore nothing in this method will ever execute.

Solving these issues will allow the method to run as expected.  There is no guarantee that the application will behave as expected.  I will attempt to issue a patch immediately if I can solve the problem.


  public FeatureSet(DataTable wkbTable, int wkbColumnIndex, bool indexed, FeatureType type)
            : this()
            if (IndexMode)
                // Assume this DataTable has WKB in column[0] and the rest of the columns are attributes.
                FeatureSetPack result = new FeatureSetPack();
                foreach (DataRow row in wkbTable.Rows)
                    byte[] data = (byte[])row[0];
                    MemoryStream ms = new MemoryStream(data);
                    WkbFeatureReader.ReadFeature(ms, result);

                // convert lists of arrays into a single vertex array for each shape type.

                // Make sure all the same columns exist in the same order

                // Assume that all the features happened to be polygons
                foreach (DataRow row in wkbTable.Rows)
                    // Create a new row
                    DataRow dest = result.Polygons.DataTable.NewRow();
                    dest.ItemArray = row.ItemArray;
Mar 12, 2012 at 7:24 PM

As a note about 'No Source Available': We are looking toward including debugging symbols with each nuget release. There are some difficulties we have to work through, but this will allow everyone that is consuming the nuget packages to debug through DotSpatial after configuring Visual Studio to point at the symbol server.

Mar 12, 2012 at 9:41 PM

Thanks for working on this.