Add datatable to DataSource of VectorLayer

Jan 16, 2011 at 3:37 PM

Hi guys,

I am trying to create a new VectorLayer like this:

VectorLayer myLayer = new VectorLayer("myLayer");
myLayer.DataSource = myDataSource; //here I want to add a datatable to myDataSource

Anyone help me or give me an idea. Thanks a lot. 

Coordinator
Jan 16, 2011 at 4:25 PM

If you have a System.DataTable as a data source, you need to reference SharpMap.Extensions project. Your Datasource would then be of SharpMap.Data.Providers DataTablePointProvider.

On construction of this instance you need to choose which columns contain your x, y or long, lat ordinates.

You can assign that to your VectorLayer.

Hth FObermaier

Jan 17, 2011 at 3:25 AM

Hi FObermaier, I have been used your code at thread http://sharpmap.codeplex.com/Thread/View.aspx?ThreadId=229940. But my layer still doesn't appear. This is my code:

 

//get object we are searching into a datatable
public
static DataTable GetSearchingObj() { string strConnect = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString; SqlConnection sqlConnect; SqlCommand sqlCmd; SqlDataAdapter adapter; DataTable dt; DataSet ds; //double[] coordinates; string strQuery = "Select * From " + "cities" + " WHERE NAME ='" + "Tokyo" + "'"; sqlConnect = new SqlConnection(strConnect); sqlCmd = new SqlCommand(strQuery, sqlConnect); adapter = new SqlDataAdapter(); dt = new DataTable(); //ds = new DataSet(); try { sqlConnect.Open(); adapter.SelectCommand = sqlCmd; adapter.Fill(dt); //adapter.Fill(ds); }//end try catch (Exception ex) { }//end catch finally { sqlConnect.Close(); }//end end finally return dt; //return ds; }//end method GetSearchingObj //public static Map MyInitializeMap(Size size, ref string searchingObj) public static Map MyInitializeMap(Size size, ref string searchingObj) { searchingObj = String.Empty; //HttpContext.Current.Trace.Write("Initializing map..."); //Initialize a new map of size 'imagesize' Map map = new Map(size); string connectionString = ConfigurationManager.ConnectionStrings["GeoDatabase"].ConnectionString; //---------------------------------------------------- //load country layer from database VectorLayer countryLayer = new VectorLayer("countries"); //VectorLayer countryLayer = new VectorLayer("Diagioi_HP"); countryLayer.DataSource = new SharpMap.Data.Providers.MsSql(connectionString, "countries", "WKB_Geometry", "oid"); //countryLayer.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\rivers.shp"), true); countryLayer.Style.Fill = new SolidBrush(Color.Brown); countryLayer.Style.Outline = System.Drawing.Pens.Yellow; //countryLayer.Style.Fill = new SolidBrush(Color.Brown); //countryLayer.Style.Outline = System.Drawing.Pens.Yellow; countryLayer.Style.EnableOutline = true; //--------------------------------------------------- //Set up a river layer DataTable dtCities = getCitiesDataSource(); VectorLayer layCities = new VectorLayer("Cities"); //VectorLayer layCities = new VectorLayer("Cities", dtCities); //Set the datasource to a shapefile in the App_data folder layCities.DataSource = new SharpMap.Data.Providers.MsSql(connectionString, "cities", "WKB_Geometry", "oid"); //Define a blue 1px wide pen //layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png")); //==============================================================
//this is the layer that I want to insert, layer cities only contain 'Tokyo'  DataTable dt = GetSearchingObj(); //DataSet ds = GetSearchingObj(); //DataSet ds = GetSearchingObj(); //DataTablePoint xlsProvider = new SharpMap.Data.Providers.DataTablePoint(ds.Tables[0], "OID", "Envelope_MinX", "Envelope_MinY"); DataTablePoint dtPoint = new SharpMap.Data.Providers.DataTablePoint(dt, "OID", "Envelope_MinX", "Envelope_MinY"); //DataTablePoint dtPoint = new SharpMap.Data.Providers.DataTablePoint(dt, dt.Rows[0]["OID"], dt.Rows[0]["Envelope_MinX"], dt.Rows[0]["Envelope_MinY"]); VectorLayer searchingLayer = new VectorLayer("Tokyo", dtPoint); //VectorLayer searchingLayer = new VectorLayer("Hanoi", dt); searchingLayer.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\Tokyo.PNG")); //new System.Drawing.Bitmap(@"~\App_Data\here.PNG"); searchingLayer.SRID = 4326; //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); ProjNet.CoordinateSystems.CoordinateSystemFactory cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); ICoordinateSystem epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); ICoordinateSystem epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3785\"]]"); searchingLayer.CoordinateTransformation = ctf.CreateFromCoordinateSystems(epsg4326, epsg3785); /* FeatureDataSet searchingDataSet = new FeatureDataSet(); //searchingDataSet. VectorLayer searchingLayer = new VectorLayer("Hanoi"); //searchingLayer.DataSource = SharpMap.Data.Providers.DbaseReader(91, dt); searchingLayer.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\here.PNG")); */ map.Layers.Add(searchingLayer); //================================================================ layCities.Style.SymbolScale = 0.8f; layCities.MaxVisible = 40; layCities.SRID = 4326; //Set up a country label layer LabelLayer layLabel = new LabelLayer("Country labels"); layLabel.DataSource = countryLayer.DataSource; layLabel.Enabled = true; layLabel.LabelColumn = "Name"; layLabel.Style = new LabelStyle(); layLabel.Style.ForeColor = Color.White; layLabel.Style.Font = new Font(FontFamily.GenericSansSerif, 14); layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center; layLabel.SRID = 4326; layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest; //Set up a city label layer LabelLayer layCityLabel = new LabelLayer("City labels"); layCityLabel.DataSource = layCities.DataSource; layCityLabel.Enabled = true; layCityLabel.LabelColumn = "Name"; layCityLabel.Style = new LabelStyle(); layCityLabel.Style.ForeColor = Color.Black; layCityLabel.Style.Font = new Font(FontFamily.GenericSansSerif, 11); //layCityLabel.MaxVisible = layLabel.MinVisible; //layCityLabel.MinVisible = layLabel.MaxVisible; layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left; layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom; layCityLabel.Style.Offset = new PointF(3, 3); //layCityLabel.Style.Halo = new Pen(Color.Yellow, 2); layCityLabel.Style.ForeColor = Color.White; layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias; layCityLabel.SmoothingMode = SmoothingMode.AntiAlias; layCityLabel.SRID = 4326; layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection; layCityLabel.Style.CollisionDetection = true; //Add the layers to the map object. //The order we add them in are the order they are drawn, so we add the rivers last to put them on top //map.Layers.Add(layCountries); map.Layers.Add(countryLayer); //map.Layers.Add(layRivers); map.Layers.Add(layCities); map.Layers.Add(layLabel); map.Layers.Add(layCityLabel); //limit the zoom to 360 degrees width map.MaximumZoom = 360; map.BackColor = Color.LightBlue; map.Zoom = 360; map.Center = new Point(0, 0); HttpContext.Current.Trace.Write("Map initialized"); return map; }//end method MyInitializeMap

 

Map appear normally, but my layer doesn't. Because it doesn't display my image Tokyo.png.

My purpose is to mark a point in map with a difference icon. Please help me !

Coordinator
Jan 17, 2011 at 10:04 AM

My first guess is that you have to leave out the coordinate transformation part (the lines beginning with ProjNET...).

Hth FObermaier

Jan 17, 2011 at 12:58 PM

When I tried to leave out these lines

ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
        ProjNet.CoordinateSystems.CoordinateSystemFactory cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
        ICoordinateSystem epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
        ICoordinateSystem epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3785\"]]");
         
        searchingLayer.CoordinateTransformation = ctf.CreateFromCoordinateSystems(epsg4326, epsg3785);

It appears error in class DataTablePoint.cs http://www.flickr.com/photos/56362292@N03/5363722038/

My map also "hide" :D

Coordinator
Jan 17, 2011 at 2:09 PM

What kind of data is in columns 1 and 2 of your datarow?

Jan 17, 2011 at 3:01 PM

You mean "Envelope_MinX" and "Envelope_MinY", They are all real type

Coordinator
Jan 17, 2011 at 3:33 PM

real or double?

maybe you can try changing the row that fails to

features.Add(new Point(Convert.ToDouble(dr[2]), Convert.ToDouble(dr[1]));

Jan 17, 2011 at 4:28 PM

They are absolutely real type. I converted according to you but nothing changed. :(

Coordinator
Jan 18, 2011 at 8:16 AM

are there any DBNull values in the columns?

Jan 18, 2011 at 3:50 PM

No, I am sure because when I use the query "SELECT * FROM cities where NAME='Tokyo'" in SQL Server 2008 it display result without any null value.

Editor
Jan 18, 2011 at 8:07 PM

namserious,

I had the same issue with OleDbPoint.cs, dont know why.

I solved it like this:

 features.Add(new Point(double.Parse( dr[0].ToString()), double.Parse( dr[1].ToString())));
Jan 19, 2011 at 2:14 AM

I got it, hah hah ......................

Thank you, carjona ! thank you, FObermaier.

Well, I use the query "SELECT oid, WKB_Geometry, Envelope_MinX, Envelope_MinY, NAME FROM cities where NAME='Tokyo'".

And I modified:  

features.Add(new Point(double.Parse(dr[2].ToString()), double.Parse(dr[3].ToString())));//dr[2] is Envelope_MinX, dr[3] is Envelope_MinY
Again, thanks everyone.

Jun 17, 2011 at 12:16 PM
Edited Jun 20, 2011 at 9:32 AM

Thank!!!