Search code examples
c#mapssharpmap

Creating New layers in SHARPMAP


I'm working on sharpmaps, Say i have a map of US , how to add new layers over the maps ?

As i need to write the name of the state over the basic layer and color each state with different colours. Is it possible to achive this goal in Sharpmaps...?


Solution

  • You must use "custom them" for color each state and "label layer" for name of the state

    You must first one delegate method define.The delegate takes a FeatureDataRow as parameter and you can handle the custom style

    for example

    private SharpMap.Styles.VectorStyle GetCountryStyle(SharpMap.Data.FeatureDataRow row)
    {
        SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
        switch (row["NAME"].ToString().ToLower())
        {
            case "denmark": //If country name is Danmark, fill it with green
                style.Fill = Brushes.Green;
                return style;
            case "united states": //If country name is USA, fill it with Blue and add a red outline
                style.Fill = Brushes.Blue;
                style.Outline = Pens.Red;
                return style;
            case "china": //If country name is China, fill it with red
                style.Fill = Brushes.Red;
                return style;
            default:
                break;
        }
        //If country name starts with S make it yellow
        if (row["NAME"].ToString().StartsWith("S"))
        {
            style.Fill = Brushes.Yellow;
            return style;
        }
        // If geometry is a (multi)polygon and the area of the polygon is less than 30, make it cyan
        else if (row.Geometry is GeoAPI.Geometries.IPolygonal && row.Geometry.Area < 30)
        {
            style.Fill = Brushes.Cyan;
            return style;
        }
        else //None of the above -> Use the default style
            return null;
    }
    

    and then set the Theme property of your layer to this method

    SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetCountryStyle);
    myVectorLayer.Theme = myTheme ;
    

    and for label Layer you must new layer as LabelLayer define for example

    //Name of table in database
    string tablename = "Roads"; 
    //Name of object ID column - MUST be integer!
    string idColumn = "gid";
    
    //Create layer
    SharpMap.Layers.VectorLayer layRoads= new SharpMap.Layers.VectorLayer("Roads");
    layRoads.DataSource = datasource;
    //Set up a road label layer
    SharpMap.Layers.LabelLayer layRoadLabel = new SharpMap.Layers.LabelLayer("Road labels");
    //Set the datasource to that of layRoads.
    layRoadLabel.DataSource = layRoads.DataSource;
    layRoadLabel.Enabled = true;
    //Specifiy field that contains the label string.
    layRoadLabel.LabelColumn = "RoadOfName";
    
    //Add layer to map
    myMap.Layers.Add(layRoads);
    //Add label layer to map
    myMap.Layers.Add(layRoadLabel); 
    

    tells all here