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

New Post: How do I create static labels around map1 for grid features?

$
0
0
I have the code below that generates a gridline X and gridline Y features and adds them to the map and then adds the field 'x' and field 'y' for labeling.

I am able to label these two grid features but its placed on the line.
I would want the labels to be static around the map1 like having tick marks with labels so that when I zoomin or zoomOut, the grid labels would still be at that same place.

Please, I would be very happy if anyone can help me with this.

The codes are below:

Feature f = new Feature();
        FeatureSet fs = new FeatureSet(f.FeatureType);

        Feature fz = new Feature();
        FeatureSet fsz = new FeatureSet(f.FeatureType);


        double minX, minY, maxX, maxY;
        minX = 400000.000;

        double stepX, stepY;
        stepX = 2000;
        stepY = 2000;
        minY = 0.00;
        maxY = 4000000;
        //maxY = map1.ClientSize.Height - 70;
        maxX = 4000000;
        //maxX = map1.ClientSize.Width - 10;

        List<DotSpatial.Topology.LineString> lines = new List<DotSpatial.Topology.LineString>();

        fs.DataTable.Columns.Add("y");


        //create the lines running from top to bottom
        for (double currX = minX; currX < maxX + stepX; currX += stepX)
        {
            DotSpatial.Topology.Point point1, point2;
            point1 = new DotSpatial.Topology.Point(currX, minY);
            point2 = new DotSpatial.Topology.Point(currX, maxY);
            LineString line = new LineString(new List<DotSpatial.Topology.Point>(new DotSpatial.Topology.Point[] { point1, point2 }));
            lines.Add(line);

            //dr["id"] = shapeIndex;

            f = new Feature(line);
            fs.Features.Add(f);





        }

        double currX_value = minX;
        foreach (DataRow dr in fs.DataTable.Rows)
        {
            dr["y"] = currX_value += stepX;


        }
        //map1.Layers.Add(fs);
        MapLineLayer lineLayery = (MapLineLayer)map1.Layers.Add(fs);   //add the featureSet as map layer
        lineLayery.LegendText = "GridlineY";
        lineLayery.Symbolizer = new LineSymbolizer(Color.Red, 1);

        IMapLabelLayer labelLayer = new MapLabelLayer(); ILabelCategory category = labelLayer.Symbology.Categories[0]; category.Expression = "[x]";
        category.Symbolizer.Orientation = ContentAlignment.MiddleCenter; lineLayery.ShowLabels = true;
        lineLayery.LabelLayer = labelLayer;


        fsz.DataTable.Columns.Add("x");
        //create the lines running from left to right
        for (double currY = minY; currY < maxY + stepY; currY += stepY)
        {
            DotSpatial.Topology.Point point1, point2;
            point1 = new DotSpatial.Topology.Point(minX, currY);
            point2 = new DotSpatial.Topology.Point(maxX, currY);
            LineString line = new LineString(new List<DotSpatial.Topology.Point>(new DotSpatial.Topology.Point[] { point1, point2 }));
            lines.Add(line);



            fz = new Feature(line);
            fsz.Features.Add(fz);


        }

        double currY_value = minY;
        foreach (DataRow dr in fsz.DataTable.Rows)
        {
            dr["x"] = currY_value += stepY;


        }
        ProjectionInfo dest = default(ProjectionInfo);
        //add the line to the feature set
        MapLineLayer lineLayer = (MapLineLayer)map1.Layers.Add(fsz);   //add the featureSet as map layer
        lineLayer.LegendText = "GridlineX";
        lineLayer.Symbolizer = new LineSymbolizer(Color.Blue, 1);
        //dest = KnownCoordinateSystems.Projected.NationalGrids.AccraGhanaGrid;
        //fsz.Projection = dest;
        //fs.Projection = dest;
        IMapLabelLayer labelLayer1 = new MapLabelLayer(); ILabelCategory category1 = labelLayer.Symbology.Categories[0]; category.Expression = "[y]";
        category.Symbolizer.Orientation = ContentAlignment.MiddleCenter; lineLayer.ShowLabels = true;
        lineLayer.LabelLayer = labelLayer1;

Viewing all articles
Browse latest Browse all 3973

Trending Articles