How to convert a DataSet to FeatureDataSet

Topics: SharpMap v0.9 / v1.x
Feb 27, 2014 at 3:25 PM
I am trying to get the geometry data from a dataset to a featuredataset:
   private void QueryCustomer(DataSet ds)
            {
    
                SharpMap.Data.FeatureDataSet ds_feature = new SharpMap.Data.FeatureDataSet();
    
                ds_feature = (SharpMap.Data.FeatureDataSet)ds;  // ERROR HERE
    ..
 
I am getting :

Unable to cast object of type 'System.Data.DataSet' to type 'SharpMap.Data.FeatureDataSet'

Any help would be appreciated. Thanks.
Coordinator
Feb 28, 2014 at 1:22 PM
Where does your dataset come from?

What is in it?

What are you trying to achieve?
Mar 1, 2014 at 4:31 AM
Basically I have a Spatialite database. I want to extract the x and y center cordinates of some polygons from the geometry column and then send those to another form to be displayed. Is there an easier way to extract the X and Y coordinates?
Coordinator
Mar 1, 2014 at 4:33 PM
If you have a SpatiaLite database file, you can use [Managed]SpatiaLite provider to get your data.
Mar 3, 2014 at 12:54 PM
Thanks do you have an example of how to retrieve the following data using the [Managed]SpatiaLite provider from a sqlite table:

double X = (bb.BottomLeft().X + bb.BottomRight().X) / 2;
double Y = (bb.BottomRight().Y + bb.TopRight().Y) / 2;

Thanks
Mar 4, 2014 at 2:39 PM
Ok this is what I tried so far but does not work, it comes back saying that the COL_ID already belongs to the datatable. I have no idea why it is trying to insert records if all I am doing is trying to get back the center x/y values of the polygons:
double x, y;
FeatureDataSet fds = new FeatureDataSet();
Envelope env = new Envelope();

SharpMap.Data.Providers.ManagedSpatiaLite p = new ManagedSpatiaLite(ConnectionString, Table, GeometryColumn, KeyColumn);
p.Open();
p.ExecuteIntersectionQuery(env, fds);    // <-- Error in this line is: A column named 'COL_ID' already belongs to this DataTable.
foreach (FeatureDataRow fdr in ((FeatureDataTable)fds.Tables[0]).Rows)
{
x = fdr.Geometry.Centroid.X;
y = fdr.Geometry.Centroid.Y;
}
p.Close();
p.Dispose();
Any help would be appreciated
Coordinator
Mar 4, 2014 at 2:53 PM
Do you have two columns with the same name in you sqlite-file?. Can you share the file with us (delete the data but keep the tables if it's sensitive data)
Mar 4, 2014 at 4:16 PM
Edited Mar 4, 2014 at 4:18 PM
petlof, thanks for your input. No, I do not have two columns with the same name in sqlite. Is that even possible?

As far as the code itself, is the code I am using correct?
Coordinator
Mar 4, 2014 at 8:26 PM
Could you post the schema of your table? Values for Table, GeometryColumn and KeyColumn.
Coordinator
Mar 5, 2014 at 5:42 AM
Oh, I see now that the KeyColumn is Case Sensitive, so you need to make sure your KeyColumn matches name in the database by case (Col_Id, col_id, col_ID,..)

I will fix this in the provider but in the mean time it should work for you if you use the right casing.
Coordinator
Mar 5, 2014 at 6:02 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Mar 5, 2014 at 6:59 AM
Edited Mar 5, 2014 at 6:59 AM
petlof wrote:
Oh, I see now that the KeyColumn is Case Sensitive, so you need to make sure your KeyColumn matches name in the database by case (Col_Id, col_id, col_ID,..)
I always thought that is a feature, at least on SQLite side
Mar 5, 2014 at 1:51 PM
That was the problem!

Changing:

SharpMap.Data.Providers.ManagedSpatiaLite p = new ManagedSpatiaLite(ConnectionString, Table, GeometryColumn, KeyColumn);

To:

SharpMap.Data.Providers.ManagedSpatiaLite p = new ManagedSpatiaLite(ConnectionString, Table, GeometryColumn.ToUpper(), KeyColumn.ToUpper());


Got rid of that error message!

But now, I am getting 0 records! There are 1854 records in that table, but it returns 0 records?
Editor
Mar 5, 2014 at 2:41 PM
you'll need to give an envelope with extents that cover your data otherwise the 'ExecuteIntersectionQuery' will return no rows.

you should be able to get this from the database quite easily by just getting the max and min values for the x and y columns and then parsing these values into the constructor of the envelope.

I dunno if it will work but you could also try - new Envelope(double.MinValue, double.MaxValue, double.MinValue, double.MaxValue);
Mar 5, 2014 at 3:44 PM
Edited Mar 5, 2014 at 3:46 PM
Robert thank you, that was the issue! Thank you very much for all that contributed. It is now working perfect. Here is the final code:
double x, y;
FeatureDataSet fds = new FeatureDataSet();
Envelope env = new Envelope(double.MinValue, double.MaxValue, double.MinValue, double.MaxValue); 

SharpMap.Data.Providers.ManagedSpatiaLite p = new ManagedSpatiaLite(ConnectionString, Table, GeometryColumn.ToUpper(), KeyColumn.ToUpper()); 
p.Open();
p.ExecuteIntersectionQuery(env, fds);
foreach (FeatureDataRow fdr in ((FeatureDataTable)fds.Tables[0]).Rows)
{
x = fdr.Geometry.Centroid.X;
y = fdr.Geometry.Centroid.Y;
//...process x and y here...
}
p.Close();
p.Dispose();