How can I display(show ) some line on my map from my postgis database ?

Topics: Data Access, SharpMap Project
Oct 22, 2014 at 3:12 PM
Hello everyone, I am new with sharpmap and postgis.
I want to display(show) some line on my map from my postgis database. I have some code.
ı can display my some line on datagridview. but I want to do it with layer on my map?
how can I do it ?
 private DataTable dt = new DataTable();
        private DataSet ds = new DataSet();
        private NpgsqlConnection conn = new NpgsqlConnection("server=localhost;port=5432;user=postgres;pwd=passwordmy;database=roaddb");
       

  conn.Open();
            string sql = "SELECT * FROM road WHERE roadtype = 'motorway' ;";
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
            ds.Reset();
            da.Fill(ds);
            dt = ds.Tables[0];
            dataGridView1.DataSource =dt;
Coordinator
Oct 22, 2014 at 6:00 PM
You need to use a IProvider implementation. In your case the one for PostGis. You can find it in the SharpMap.Extensions project.
Use that with a VectorLayer and add it to Map.
Oct 22, 2014 at 7:12 PM
Thanks your reply. But I dont understand how can I do it? could you please some sample code for this?
Coordinator
Oct 23, 2014 at 5:57 AM
No. There is a lot of sample code in the repository (Sources) and even a tutorial (Documentation).
Oct 23, 2014 at 6:12 PM
Edited Oct 23, 2014 at 6:13 PM
Hello, I made some code. I have 2 type road ( or more ) I want to show them different color.
I show them like İmage 2
But there is no outline on my layer.
  style.EnableOutline = true;
                        style.Outline = new Pen(Color.Magenta, 3f);
                        style.EnableOutline = true;
                        style.Outline.Width = 3;
this code not work ?
ı would like to show them image 3.
How can I do it ?
 private void loadmap()
        {
       SharpMap.Layers.VectorLayer VLyol = new SharpMap.Layers.VectorLayer("Lroads");
           VLyol.DataSource = new SharpMap.Data.Providers.PostGIS("server=localhost;port=5432;user=postgres;pwd=passs;database=roaddb", "roads", "geom", "id");
           VLyol.Theme = new SharpMap.Rendering.Thematics.CustomTheme(roadstyles);
           mapBox1.Map.Layers.Add(VLyol);
           mapBox1.Map.ZoomToExtents();
           mapBox1.Refresh();
        }
    private SharpMap.Styles.VectorStyle roadstyles(SharpMap.Data.FeatureDataRow row)
        {
            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            switch (row["roadtype"].ToString())
            {
                case "motorway":
                    {                 
                        style.Line.Color = Color.Green;
                        style.Line.Width = 3f;
                        style.EnableOutline = true;
                        style.Outline = new Pen(Color.Magenta, 3f);
                        style.EnableOutline = true;
                        style.Outline.Width = 3;
                   
                    }; break;
                case "mainroad":
                    {
                        style.Line.Color = Color.Yellow;
                        style.Line.Width = 2f;
                      
                    }; break;
                
            }
            return style;
        }
İMAGE 1

Image

İMAGE 2

Image

İMAGE 3

Image
Coordinator
Oct 24, 2014 at 6:25 AM
The Outline pen has to be thicker than the Line pen.