TransWikia.com

How to show only some labels sharpmap

Geographic Information Systems Asked by abhithakur88 on April 19, 2021

I am new to Sharpmap and GIS. Going through the sample code and building simple programs for better understanding. I have a water bodies layer. I want to display only a particular river. I used CustomTheme. How to show label for only this river.

To display a river (named Ganga), I have written following code.

SharpMap.Data.Providers.ShapeFile shapeFileProvider = new SharpMap.Data.Providers.ShapeFile(@"C:water.shp", false);
SharpMap.Layers.VectorLayer waterLayer = new SharpMap.Layers.VectorLayer("Water", shapeFileProvider);

SharpMap.Rendering.Thematics.CustomTheme waterTheme = new  SharpMap.Rendering.Thematics.CustomTheme(GetWaterStyle);
waterLayer.Theme = waterTheme;
mapBox1.Map.Layers.Add(waterLayer);

The GetWaterStyle method:

private SharpMap.Styles.VectorStyle GetWaterStyle(SharpMap.Data.FeatureDataRow row)
{
    SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
    if (row["NAME"].ToString().Equals("Ganga"))
    {
        style.Fill = Brushes.Aqua;
        return style;
    }
    else
        return null;
}

Now I want to show a label “Ganga” (the river name) as well. How to approach?

Please help.

Thanks and regards,
Abhi

One Answer

You can do something like this:

    //Proivder for labels only fetching one river
    SharpMap.Data.Providers.ShapeFile shapeFileProviderLabels = new SharpMap.Data.Providers.ShapeFile(@"rivers.shp", false);
    shapeFileProviderLabels.FilterDelegate += new SharpMap.Data.Providers.FilterProvider.FilterMethod(delegate(FeatureDataRow row) { return row["NAME"] as string == "Ganga"; });
    int cnt = shapeFileProviderLabels.GetFeatureCount();

    SharpMap.Layers.VectorLayer waterLayer = new SharpMap.Layers.VectorLayer("Water", shapeFileProvider);

    //Layer for labels
    SharpMap.Layers.LabelLayer waterLabels = new SharpMap.Layers.LabelLayer("Water_Labels");
    waterLabels.LabelColumn = "NAME";
    waterLabels.DataSource = shapeFileProviderLabels;

    mapBox1.Map.Layers.Add(waterLayer);
    mapBox1.Map.Layers.Add(waterLabels);

There has been an bug/issue with the FilterDelegate of ShapeFile provider in SharpMap, this is fixed and included in SharpMap 1.0 RC1 that is newly released

Answered by Peter on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP