3

我正在研究Sharpmaps,假设我有一张美国地图,如何在地图上添加新图层?

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

4

2 回答 2

5

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

您必须首先定义一个委托方法。委托将 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); 

在这里告诉一切

于 2012-11-29T09:26:30.913 回答
-1

我认为这在sharpmaps中是不可能的。

但是,在 c# 中,它可以通过映射状态边界的坐标来实现。

于 2012-04-17T03:52:01.190 回答