This project has moved. For the latest updates, please go here.

Example 4: Adding a Tiled layer as Background

In this step you will see how you can combine online tile-server data together with local data from shapefiles

1. Add references to BruTile.dll and ProjNet.dll

2. Modify your initialization code to add OpenStreetMap as a backgroundlayer in the map

You now need to set the CoordinateTransformation and RevereseCoordinateTransformation property on your vectorlayers for them to be reprojected and line up with the background tiles.

public Form1()
{
    InitializeComponent();

    SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
    vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"C:\Projekt\opensource\sharpmap-nts\UnitTests\TestData\states_ugl.shp", true);

    //Create the style for Land
    SharpMap.Styles.VectorStyle landStyle = new SharpMap.Styles.VectorStyle();
    landStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(232, 232, 232));

    //Create the style for Water
    SharpMap.Styles.VectorStyle waterStyle = new SharpMap.Styles.VectorStyle();
    waterStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(198,198,255));

    //Create the theme items
    Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, SharpMap.Styles.IStyle>();
    styles.Add("land", landStyle);
    styles.Add("water", waterStyle);

    //Assign the theme
    vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);

    mapBox1.Map.Layers.Add(vlay);

            ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
            vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
     
    mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
                new BruTile.Web.OsmTileSource(), "OSM"));

    mapBox1.Map.ZoomToExtents();
    mapBox1.Refresh();
    mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
}

3. Zoom in to nothern US and se how your ShapeFile data lines up with OpenStreetMap

osm.png

Last edited Mar 9, 2015 at 8:49 AM by petlof, version 5