create label for LineString

Mar 11, 2009 at 10:36 AM

I can not display a label for LineString.
I created two points, and a linestring, I put it on a vector layer and then i create a label layer,
I have read the discussions, and tried the LabelStringDelegate but it says Attribute "data is not supported by the GeometryProvider"
I try open the datasource but nothing? 

(I want to display the distance between these to points in the line, the points will be created on the fly)

Mar 11, 2009 at 11:31 AM
hi visar see hth jd
Mar 13, 2009 at 10:54 AM
Thank you very much John Diss for the hint.

I can display properly now the label above the line string , but the thing is
if I want to display another distance between to points, than it displays in the two label's
the latest distance in two linestring's.

result = p1.Distance(p2);
LabelString = (result / 1000).ToString("F2") + " km";
 lbl.LabelStringDelegate =new LabelLayer.GetLabelMethod(this.getString);

private string getString(FeatureDataRow row)
            return LabelString;

and I get like

P1--------------------------32 km----------------------------------P2

P3-------------32 km----------------P4

again thank you very very much for your support
Mar 13, 2009 at 12:12 PM
Hi visar, I think you will have to add a new point layer adding the mid point between every pair of points in the Line Layer, and precompute the relevant distances for display
hth jd 
Mar 13, 2009 at 2:12 PM
Hello John

I have done some math in a paper and can find the mid point between two points, but
to translate it into an algorithm would take me one or two days, because it's not a simple
formula but a system with two unknown variables ...

Do you know a simpler way through SharpMap to get this mid Point?

Mar 13, 2009 at 2:47 PM
Edited Mar 13, 2009 at 2:49 PM
Oh my God

I have been going the other way around doing unecessary math whereas the result was so simple

Given two coordinates (x1,y1) and (x2,y2)
The midpoint is ( ((x2+x1)/2) , ((y2+y1)/2) )

Sorry, I've got it now thanks for your help

Mar 13, 2009 at 3:12 PM
Sorry for bothering with all these questions but do you know how to turn off the symbol in the point.
because when displaying the middle point it displays also the symbol and it gets ugly point + label
it was supposed to show just the label

Mar 13, 2009 at 3:16 PM
Edited Mar 13, 2009 at 3:18 PM
One way would be to change the Symbol property of the style to a transparent image
style.Symbol = Bitmap.FromFile('//path to transparent image.png')

edit: though it sounds like you have added an unnecessary VectorLayer - you only need to add the label layer
Mar 13, 2009 at 4:20 PM
ok, I try to avoid that unecessary VectorLayer, and add just a LabelLayer but I cannot algin it in the LineString
Do you now how to align it? Or is there a DataTableLine or something like DataTablePoint, that can have label column's...

distance= -1;
distance= p1.Distance(p2);


VectorLayer vl = new VectorLayer("linestring_"+DateTime.Now.GetHashCode());
Collection<SharpMap.Geometries.Point> points = new Collection<SharpMap.Geometries.Point>();




LineString ls = new SharpMap.Geometries.LineString(points);
vl.DataSource =
new SharpMap.Data.Providers.GeometryProvider(ls);



LabelLayer lbl = new LabelLayer("distanca_"+DateTime.Now.GetHashCode());
LabelString = (distance/ 1000).ToString("F2") + " km";



DataTable dt = new DataTable("Distance table");



DataRow dr= dt.NewRow();
dr.ItemArray =
new object[]{1,((p1.X+p2.X)/2),((p1.Y+p2.Y)/2),LabelString};



DataTablePoint dtp = new DataTablePoint(dt, "FID", "X", "Y");





lbl.Enabled =
lbl.DataSource = dtp;
lbl.Style =
new SharpMap.Styles.LabelStyle();
lbl.Style.Font =
new Font("Arial", 12);
lbl.Style.ForeColor =




lbl.LabelColumn = "LblCol";

lbl.Style.HorizontalAlignment = SharpMap.Styles.
lbl.MultipartGeometryBehaviour = SharpMap.Layers.
lbl.Style.HorizontalAlignment = SharpMap.Styles.
lbl.Style.VerticalAlignment = SharpMap.Styles.













Mar 13, 2009 at 4:56 PM
Ok, instead of creating one point per link you probably need to create a 2 point line string for each pair of points in the original dataset (i.e split the original lines into smaller linestrings)
then you shoudl be able to label on the line.. You are likely to have issues with the labels being culled though..