Labeling in Sharpmap

Jul 1, 2011 at 12:53 PM

Hello,

i am labeling a vectorlayer with the length of the segments of the polygons. It looks quite nice, but i faced a problem, that labels do not appear when the line is very short.

Is there any possibility to control it? I had a look at the Label collision detection, but actually I don´t know if i can use that. When my length of the numbers is longer than the line, i still want it to be drawn.

Thanks for helping!

Coordinator
Jul 1, 2011 at 1:36 PM

That is currently not possible.

You would need to add a boolean IgnoreLength property to the LabelStyle class and test that for that in the LabelLayer class' CreateLabel function, right above the call to CalcLabelForLineString.
If you want it badly, please raise an issue (along with your patchfile)

Hth FObermaier.

Coordinator
Jul 1, 2011 at 2:00 PM

Something like this

Index: SharpMap/Layers/LabelLayer.cs
===================================================================
--- SharpMap/Layers/LabelLayer.cs    (revision 90576)
+++ SharpMap/Layers/LabelLayer.cs    (working copy)
@@ -566,7 +566,7 @@
             if (feature is LineString)
             {
                 LineString line = feature as LineString;
-                if (line.Length/map.PixelSize > size.Width) //Only label feature if it is long enough
+                if (line.Length/map.PixelSize > size.Width || style.IgnoreLength) //Only label feature if it is long enough or definately wanted
                     CalculateLabelOnLinestring(line, ref lbl, map);
                 else
                     return null;
Index: SharpMap/Styles/LabelStyle.cs
===================================================================
--- SharpMap/Styles/LabelStyle.cs    (revision 90576)
+++ SharpMap/Styles/LabelStyle.cs    (working copy)
@@ -15,6 +15,7 @@
 // along with SharpMap; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+using System;
 using System.Drawing;
 
 namespace SharpMap.Styles
@@ -82,6 +83,7 @@
         private PointF _Offset;
         private VerticalAlignmentEnum _VerticalAlignment;
         private float _rotation;
+        private bool _ignoreLength;
 
         /// <summary>
         /// Initializes a new LabelStyle
@@ -197,5 +199,14 @@
             set { _rotation = value % 360f; }
         }
 
+        /// <summary>
+        /// Gets or sets if length of linestring should be ignored
+        /// </summary>
+        [System.ComponentModel.Category("Alignment")]
+        public bool IgnoreLength
+        {
+            get { return _ignoreLength; }
+            set { _ignoreLength = value; }
+        }
     }
 }
\ No newline at end of file