This project is read-only.

map refresh problem ?

Sep 30, 2014 at 2:21 PM
Hi everybody, I've just started with DotSpatial so please excuse me if I'm missing something.

This is the issue: I would like to be able to add labeled features of type Point on a layer.

It doesn't seem to be so difficult and in fact at the moment I'm able to do it but I've got a problem with labels.

In my application I've got to use a couple of .shp and then I need a new layer to show some "user defined" symbols.

First of all, I create a specific layer in this way:
    public bool CreateCategory(string name, PointSymbol symbol, Color color, double size, out string errorDescription) {

        //  Initialize function's value
        bool bRes = false;

        errorDescription = string.Empty;

        try {

            if (_map != null) {

                //  Check for duplicates
                MapPointLayer layer = (MapPointLayer)_map.Layers.Where(e => e.DataSet.Name == name).FirstOrDefault();

                if (layer == null) {

                    //  The FeatureSet starts with no data
                    FeatureSet featureSet = new FeatureSet(FeatureType.Point);
                    featureSet.Projection = _map.Projection;
                    featureSet.Name = name;
                    featureSet.DataTable.Columns.Add("label", typeof(string));

                    //  The MapPointLayer controls the drawing of the marker features
                    MapPointLayer markerLayer = new MapPointLayer(featureSet);
                    markerLayer.Name = name;

                    //  The Symbolizer controls what the points look like
                    markerLayer.Symbolizer = new PointSymbolizer(color, decodePointShape(symbol), size);
                    //markerLayer.Symbolizer.ScaleMode = ScaleMode.Geographic;

                    //  Add a Label Layer 
                    MapLabelLayer markerLabelLayer = new MapLabelLayer(markerLayer);

                    //  This is the expression for the label drawing
                    markerLabelLayer.Symbology.Categories[0].Expression = "[label]";

                    //  The Symbolizer controls what the labels look like
                    markerLabelLayer.Symbolizer = new LabelSymbolizer {
                        BackColor = Color.White,
                        BackColorEnabled = true,
                        FontFamily = "Tahoma",
                        FontColor = Color.Black,
                        FontSize = 12,
                        Orientation = ContentAlignment.TopCenter,
                        OffsetX = 0,
                        OffsetY = 5,
                        PartsLabelingMethod = PartLabelingMethod.LabelAllParts
                    };

                    //  Set markers label layer
                    markerLayer.LabelLayer = markerLabelLayer;
                    markerLayer.ShowLabels = true;

                    //  Add the layer to the map
                    _map.Layers.Add(markerLayer);

                    //  Update function's value
                    bRes = true;

                } else
                    errorDescription = "Category already defined";

            } else
                errorDescription = "Invalid Map Handle";

        } catch (Exception ex) {
            errorDescription = ex.Message;
        }

        return (bRes);
    }

After this, I can add a new element in this way:
    public int AddElement(string category, Coordinate coords, string label, out string errorDescription) {

        //  Initialize function's value
        int nRes = -1;

        errorDescription = string.Empty;

        try {

            if (_map != null) {

                //  Check for specified layer
                MapPointLayer layer = (MapPointLayer)_map.Layers.Where(e => e.DataSet.Name == category).FirstOrDefault();

                if (layer != null) {

                    DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(coords);

                    /*******************  TAKE A LOOK AT THIS SECTION ********************
                    Coordinate c1 = new Coordinate(1017054.08021215, 4505730.70512603);
                    DotSpatial.Topology.Point point1 = new DotSpatial.Topology.Point(c1);
                    IFeature ff1 = layer.FeatureSet.AddFeature(point1);
                    ff1.DataRow["label"] = "one";

                    Coordinate c2 = new Coordinate(1035800.44289391, 4514978.91071471);
                    DotSpatial.Topology.Point point2 = new DotSpatial.Topology.Point(c2);
                    IFeature ff2 = layer.FeatureSet.AddFeature(point2);
                    ff2.DataRow["label"] = "two";
                    *************************************************************************/

                    //  Add the feature 
                    IFeature ff = layer.FeatureSet.AddFeature(point);
                    ff.DataRow["label"] = label;

                    //  Drawing will take place from a bitmap buffer, so if data is updated, we need to tell the map to refresh the buffer 
                    _map.Refresh();

                    //  Update function's value
                    nRes = ff.Fid;
                }

            } else
                errorDescription = "Invalid Map Handle";

        } catch (Exception ex) {
            errorDescription = ex.Message;
        }

        return (nRes);

    }
In order to test this procedure, I simply call my AddElement function when the user clicks on the map.

Now, when I do it for the first time I can see the new element correctly represented on the map but when I do it for the second time I can see only the symbol without its label.

I've also tested this situation: in the AddElement routine there's a code portion ("TAKE A LOOK AT THIS SECTION") that I've used to test what happens if I add more tahn one feature statically; in this case I can see all my Points with their labels correctly.

Anybody could please help me to understand ?

Thanks in advance