draw a layer for roads on sharpmap

Topics: SharpMap Project
Sep 16, 2009 at 8:40 PM

Hi Everybody,

 

I am new for sharpmap and i want to draw a layer on sharpmap for roads (for whole world or for specific country). 

 

I am not getting any way how to do it. I know very well how to add layer on sharpmap. 

 

please help me to draw a layer on sharpmap for roads.

 

Thanks in advance

Coordinator
Sep 17, 2009 at 1:54 PM

Hi Arminder, please see http://sharpmap.codeplex.com/Wiki/View.aspx?title=Add%20layers&referringTitle=Home - it doesn't make a difference if the layer displays roads , boundaries or important points - you use a VectorLayer configure and assign an IProvider implementation and away you go.. hth jd

Sep 17, 2009 at 7:38 PM

Thanks for reply Johndiss,

 

if i will use following code

This example is derivied from the Add a PostGIS layer example.

string tablename = "Roads"; //Name of table in database
string idColumn = "gid"; //Name of object ID column - MUST be integer!

SharpMap.Map myMap = new SharpMap.Map(new System.Drawing.Size(500,250)); //Initialize map object
SharpMap.Layers.VectorLayer layRoads= new SharpMap.Layers.VectorLayer("Roads"); //Create layer
layRoads.DataSource = new SharpMap.Providers.OleDbPoint(connStr, tablename, gid); //Set the datasource to the PostGreSQL table


//Set up a road label layer
SharpMap.Layers.LabelLayer layRoadLabel = new SharpMap.Layers.LabelLayer("Road labels");
layRoadLabel.DataSource = layRoads.DataSource;
layRoadLabel.Enabled = true;
layRoadLabel.LabelColumn = "RoadOfName";
layRoadLabel.Style = new SharpMap.Styles.LabelStyle();
layRoadLabel.Style.ForeColor = Color.Black;
layRoadLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);

layRoadLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
layRoadLabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
layRoadLabel.Style.Offset = new PointF(3, 3);
layRoadLabel.Style.Halo = new Pen(Color.Yellow, 2);
layRoadLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layRoadLabel.SmoothingMode = SmoothingMode.AntiAlias;
layRoadLabel.SRID = 4326;

myMap.Layers.Add(layRoads); //Add layer to map
myMap.Layers.Add(layRoadLabel); //Add label layer to map

myMap.ZoomToExtents(); //Zoom to extents
Image img = myMap.GetMap(); //Render map
what will be value for connStr? i have road.shp file for road database placed 
in same project app_data folder.

thanks,

Coordinator
Sep 17, 2009 at 8:11 PM

You need to use the Shapefile provider instead of the OledbPointProvider , and pass in the path to the shapefile in the constructor hth jd

Sep 22, 2009 at 8:11 PM

thanks again,

 

I i trying the way you have explained. But it is not showing road on map. if i change datasource file pate from road file to river file. it is showing information correctly but roads are not showing. 

Please suggest. Following is my code

 

protected void Button1_Click(object sender, EventArgs e)
    {
        
        ////Set up the countries layer
        //SharpMap.Layers.VectorLayer RailRoad = new SharpMap.Layers.VectorLayer("Rail");
        ////Set the datasource to a shapefile in the App_data folder
        //RailRoad.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\rail.shp"), true);
        ////Set fill-style to green
        //RailRoad.Style.Fill = new SolidBrush(Color.Red);
        ////Set the polygons to have a black outline
        //RailRoad.Style.Outline = System.Drawing.Pens.Orange;
        //RailRoad.Style.EnableOutline = true;
        //RailRoad.SRID = 4326;
        //RailRoad.Style.Line.DashPattern=new float[]{4.0f,2.0f};
        //RailRoad.Enabled = true;
        //SharpMap.Layers.LabelLayer RailRoadLables = new SharpMap.Layers.LabelLayer("Road lables");
        //RailRoadLables.DataSource = RailRoad.DataSource;
        //RailRoadLables.Enabled = true;
        //RailRoadLables.LabelColumn = "RoadOfName";
        //RailRoadLables.Style = new SharpMap.Styles.LabelStyle();
        ////myMap.Layers.Add(RailRoadLables);
        //myMap.Layers.Add(RailRoad);
        //System.Drawing.Image img = myMap.GetMap();
        //string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
        //imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
        
        SharpMap.Layers.VectorLayer layRoads = new SharpMap.Layers.VectorLayer("Roads");
        //Set the datasource to a shapefile
        layRoads.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\road1ln.shp"), true);
        SharpMap.Rendering.Thematics.CustomTheme roadTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetRoadStyle);
        SharpMap.Styles.VectorStyle defaultstyle = new SharpMap.Styles.VectorStyle();
        defaultstyle.Fill = Brushes.Gray;
        roadTheme.DefaultStyle = defaultstyle;
        layRoads.Theme = roadTheme;
        layRoads.Style.SymbolScale = 0.8f;
        layRoads.MaxVisible = 2;
        layRoads.SRID  = 4326;
        myMap.Layers.Add(layRoads);
        System.Drawing.Image img = myMap.GetMap();
        string imgid = SharpMap.Web.Caching.InsertIntoCache(1, img);
        imgMap.ImageUrl  = "getmap.aspx?id=" + HttpUtility.UrlEncode(imgid);
        
        Response.Write(i);
    }

  SharpMap.Layers.VectorLayer layRoads = new SharpMap.Layers.VectorLayer("Roads");

        //Set the datasource to a shapefile

        layRoads.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\road1ln.shp"), true);

        SharpMap.Rendering.Thematics.CustomTheme roadTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetRoadStyle);

        SharpMap.Styles.VectorStyle defaultstyle = new SharpMap.Styles.VectorStyle();

        defaultstyle.Fill = Brushes.Gray;

        roadTheme.DefaultStyle = defaultstyle;

        layRoads.Theme = roadTheme;

        layRoads.Style.SymbolScale = 0.8f;

        layRoads.MaxVisible = 2;

        layRoads.SRID  = 4326;

        myMap.Layers.Add(layRoads);

        System.Drawing.Image img = myMap.GetMap();

        string imgid = SharpMap.Web.Caching.InsertIntoCache(1, img);

        imgMap.ImageUrl  = "getmap.aspx?id=" + HttpUtility.UrlEncode(imgid);

if i change 

        layRoads.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\road1ln.shp"), true);

then it is showing rivers on map/

 

thanks in advance

 

Coordinator
Sep 23, 2009 at 10:39 AM

Hi arminder - from the code you posted I am unclear if you are ever calling myMap.ZoomToExtents() before calling myMap.GetMap(). Try explicity calling it before calling myMap.GetMap(). Other things to check might be that the roads shapefile is in the spatial reference system you set it to, that the prj file exists for the shapefile, the shapefile is not corrupt etc.. hth jd 

Sep 23, 2009 at 10:18 PM

johndiss, i tried with myMap.ZoomToExtents() methods also it is not showing data. Can you please let me know from where i can get sharpfile for roads or if possible can you please provide me sharp data file for road.

 

Thanks

Coordinator
Sep 24, 2009 at 10:24 AM
Edited Sep 24, 2009 at 12:02 PM

You can try http://downloads.cloudmade.com/ but how useful the data is will depend on how active the crowd source community are in the area you are interested in.. Failing that Google.. cheers jd