Quantcast
Channel: DotSpatial
Viewing all articles
Browse latest Browse all 3973

New Post: How to create labels on a layer with version 1.9

$
0
0
The way that I created labels for a layer in version 1.7 is not working for version 1.8 or 1.9. What am I doing wrong? I get an index out of bounds error when I try to set the label layer on the feature layer.
   private void AddLayer()
    {
        Dictionary<string, Color> uniqueKeys;

        DataTable _assetTable = new DataTable();
        _assetTable.Columns.Add("Name", typeof(string));
        _assetTable.Columns.Add("Team", typeof(string));
        _assetTable.Columns.Add("Lat", typeof(double));
        _assetTable.Columns.Add("Lon", typeof(double));

        IMapFeatureLayer _assetPositionLayer = CreateLayer(DotSpatial.Topology.FeatureType.Point, "Asset Positions", _assetTable.Columns, true);

        for (int i = 0; i < 5; ++i)
        {
            double lat = 0.0;
            double lon = i * 5;
            DataRow row = _assetTable.NewRow();
            row["Name"] = "Test" + i.ToString();
            row["Team"] = "BLUE";
            row["Lat"] = lat;
            row["Lon"] = lon;

            Coordinate c = new Coordinate(lon, lat);
            Feature geometry = new Feature(c);
            IFeature f = _assetPositionLayer.DataSet.AddFeature(geometry);
            f.DataRow.BeginEdit();
               f.DataRow["Name"] = row["Name"];
               f.DataRow["Team"] = row["Team"];
               f.DataRow["Lat"] = row["Lat"];
               f.DataRow["Lon"] = row["Lon"];
            f.DataRow.EndEdit();
        }

        Coordinate c1 = new Coordinate(10, 10);
        Feature g = new Feature(c1);
        IFeature f1 = _assetPositionLayer.DataSet.AddFeature(g);
        f1.DataRow.BeginEdit();
        f1.DataRow["Name"] = "RedOne";
        f1.DataRow["Team"] = "RED";
        f1.DataRow["Lat"] = 10;
        f1.DataRow["Lon"] = 10;
        f1.DataRow.EndEdit();

        uniqueKeys = new Dictionary<string, Color>();
        uniqueKeys.Add("BLUE", Color.Blue);
        uniqueKeys.Add("RED", Color.Red);

        // Add the scheme for the layer
        PointScheme scheme = new PointScheme();
        scheme.Categories.Clear();

        foreach (string key in uniqueKeys.Keys)
        {
            Color color = uniqueKeys[key];
            PointSymbolizer ps = new PointSymbolizer(color, DotSpatial.Symbology.PointShape.Ellipse, 10);
            ps.SetOutline(Color.Black, 1);
            PointCategory pc = new PointCategory(ps);
            pc.FilterExpression = "[" + "Team" + "] = '" + key + "'";
            pc.LegendText = key;
            scheme.AddCategory(pc);
        }
        _assetPositionLayer.Symbology = scheme;

        // Add label layer
        IMapLabelLayer labelLayer = new MapLabelLayer();
        ILabelCategory category = labelLayer.Symbology.Categories[0];

        category.Expression = "[Name]";
        category.Symbolizer = new LabelSymbolizer()
        {
            BackColorEnabled = true,
            BackColor = Color.AliceBlue,
            BackColorOpacity = 0.4F,
            BorderVisible = true,
            BorderColor = Color.Black,
            FontColor = Color.Black,
            FontSize = 11,
            FontStyle = System.Drawing.FontStyle.Bold,
            OffsetY = -10,
            Orientation = ContentAlignment.BottomCenter,
            HaloColor = Color.Green,
            HaloEnabled = false
        };

        _assetPositionLayer.ShowLabels = true;
        _assetPositionLayer.LabelLayer = labelLayer;
    }

    public IMapFeatureLayer CreateLayer(FeatureType type, String name, DataColumnCollection columns, bool selectionEnabled = true)
    {
        FeatureSet fs = new FeatureSet(type);
        fs.Name = name;
        fs.Projection = map.Projection;

        if (columns != null)
        {
            foreach (DataColumn column in columns)
            {
                String attName = column.ColumnName;
                Type attType = column.DataType;
                fs.DataTable.Columns.Add(new DataColumn(attName, attType));
            }
        }

        IMapFeatureLayer layer = map.Layers.Add(fs);
        layer.SelectionEnabled = selectionEnabled;
        layer.IsSelected = true;

        return layer;
    }

Viewing all articles
Browse latest Browse all 3973

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>