c# - 在 SHARPMAP 中创建新图层

标签 c# maps sharpmap

我正在处理锐化 map ,假设我有一张美国 map ,如何在 map 上添加新图层?

因为我需要在基本层上写下状态的名称,并用不同的颜色为每个状态着色。 是否有可能在 Sharpmaps 中实现这个目标...?

最佳答案

您必须为每个州使用“自定义它们”颜色,为州名称使用“标签层”

您必须首先定义一个委托(delegate)方法。该委托(delegate)将 FeatureDataRow 作为参数,您可以处理自定义样式

例如

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;
}

然后将图层的 Theme 属性设置为此方法

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

对于标签图层,您必须按照 LabelLayer 定义的方式新建图层 例如

//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); 

告诉所有here

关于c# - 在 SHARPMAP 中创建新图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595681/

相关文章:

c# - 使用/p :PublishSingleFile=true Flag 发布时出错

javascript - 使用两个值作为一个值的键

c# - 如何使用 sharpmap 在地球上渲染一个国家的图像

c# - 使用 SharpMap 进行地理信息系统编程。最后一层覆盖其他层

c# - 将 datauri pdf 转换为文件以附加到电子邮件

c# - 有没有一种方法可以在 C# 中一个接一个地进行多个替换?

c# - 使用 Sqlite.dll 错误

c++ - route-me 与 libosmscout(适用于 iOS 的离线 vector map )

ios - 如何摆脱 iOS 版 Google map 上的兴趣点

c# - 转换 EPSG :4326 projection to EPSG:3857 mercator