Road Label

Topics: General Topics
Dec 10, 2010 at 11:16 AM

Hi,

I want to need following type of road label

====================== aaaa =================== aaa       =============== aaa

by using MultiPartGeometryLabel,

please give me a sample source code.

Sandip

 

Coordinator
Dec 13, 2010 at 1:48 PM

Hello sandipchk,

here you go:

private const string Wkt =
    "LINESTRING(2598267.71986402 5928651.08295893,2598294.75847761 5928628.26451746,"+
    "2598303.73499996 5928611.75105787,2598302.59219084 5928602.8207366,"+
    "2598294.23953931 5928589.28741285,2598272.13091031 5928566.56193923,"+
    "2598272.13091031 5928566.56193923,2598338.08935749 5928524.51198638,"+
    "2598338.08935749 5928524.51198638,2598377.72866056 5928566.52855469,"+
    "2598439.42006662 5928570.03762395,2598454.16353754 5928563.66427658,"+
    "2598471.3764063 5928470.50299995,2598488.9221027 5928425.22166806,"+
    "2598518.48729235 5928376.85052454,2598518.48729235 5928376.85052454,"+
    "2598599.15531301 5928394.11643866,2598639.67460965 5928393.8471714,"+
    "2598677.74719624 5928383.5073167,2598750.14176292 5928351.61700993,"+
    "2598750.14176292 5928351.61700993,2598711.33904821 5928269.53498864,"+
    "2598689.99628116 5928210.08377041,2598627.05402316 5927979.42717379,"+
    "2598556.62385625 5927820.9819144)";

public SharpMap.Geometries.MultiLineString SplitLineString(
    SharpMap.Geometries.LineString lineString,
    System.Double length)
{
    if (lineString == null || lineString.IsEmpty())
        throw new System.ArgumentException("Linestring is null or Empty", "lineString");

    var gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory();
    var ntsLine = (GisSharpBlog.NetTopologySuite.Geometries.LineString)
                    SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(lineString, gf);

    var ret = new SharpMap.Geometries.MultiLineString();
    var lil = new GisSharpBlog.NetTopologySuite.LinearReferencing.LengthIndexedLine(ntsLine);

    double currentLength = 0d;
    while (currentLength < ntsLine.Length)
    {
        var tmpLine = (GisSharpBlog.NetTopologySuite.Geometries.LineString)
            lil.ExtractLine(currentLength, currentLength + length);
        ret.LineStrings.Add((SharpMap.Geometries.LineString)
            SharpMap.Converters.NTS.GeometryConverter.ToSharpMapGeometry(tmpLine));
        currentLength += length;
    }
    return ret;
}

public void MultiLineStringLabelingSample()
{
    var map = new SharpMap.Map(new System.Drawing.Size(400, 300));
    var fdt = new SharpMap.Data.FeatureDataTable {TableName = "MLS"};
    fdt.Columns.Add("Name", typeof (string));
    var fdr = fdt.NewRow();
    fdr["Name"] = "A Test Name";
    fdr.Geometry = SplitLineString(
        (SharpMap.Geometries.LineString) SharpMap.Converters.WellKnownText.GeometryFromWKT.Parse(Wkt), 500d);
    fdt.AddRow(fdr);
    var prov = new SharpMap.Data.Providers.GeometryFeatureProvider(fdt);
    map.Layers.Add(new SharpMap.Layers.VectorLayer("MLS", prov));
    var ll = new SharpMap.Layers.LabelLayer("LMLS");
    ll.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.All;
    ll.LabelColumn = "Name";
    ll.DataSource = prov;
    map.Layers.Add(ll);
    map.ZoomToExtents();
    System.Drawing.Image mapImage = map.GetMap();
    mapImage.Save("mlsls.bmp");
}

You need to make sure that the length of the label is less than the LineString entity of MultiLineString that you want to draw the label to. Also avoid steep angles within a linestring, since the label will not be warped to the linestring.

Btw, I don't get to finishing up the code for warping text to linestrings. If you care for it, anyway, just yell

Hth FObemaier