Moving of Labels when panning

Topics: SharpMap Project, SharpMap v2.0, WinForms Controls
Oct 14, 2014 at 3:38 PM
Hello,

I just red a comment about the labellayer (http://sharpmap.codeplex.com/workitem/17910). I have a problem with those moving labels - users adjust the labels in my map and i am saving for example the new position. If the labels are moving with panning or zooming then they can edit the labels just at one scale - which is quite difficult!

Any idea is very welcome!

Anna
Coordinator
Oct 15, 2014 at 3:57 PM
I'm sorry Anna, I don't understand what you are trying do achieve and what it has to do with the referenced work item.
Could you try to elaborate the problem a bit?
Oct 15, 2014 at 4:18 PM
Ok, of course - i am sorry...

I have a map and I added a LabelLayer to it. The map will be exported to a pdf later. Sometimes the labels overlap each other, so I wanted to give the user the possibility to move the labels, to add new labels and to rotate them. Now I am also allowing to zoom and to pan the map. When the user zooms or pans the position of the labels is moving. Then the work to adjust the labels is just valid in a certain scale and not if someone panned the map, because in the next view the labels are shifted again.

Is it clearer now?
Coordinator
Oct 15, 2014 at 9:12 PM
Edited Oct 15, 2014 at 9:23 PM
You have several options:
  • enable collision detection.
var ls = new LabelStyle();
ls.CollisionDetection = true;
ls.CollisionBuffer = new Size(3, 3);
var ll = new LabelLayer("ll");
ll.LabelFilter = LabelFilter.ThoroughLabelCollision;
  • create a ITheme implementation that modifies LabelStyle.Offset based on attribute data. Apply it to ll;
Oct 15, 2014 at 9:56 PM
I will try to improve the position of the labels with the LabelFilter. Usually the Offset is set to 0 - so why is it necessary to calculate it based on the pan??? I created a video to make it clearer:

https://dl.dropboxusercontent.com/u/8118260/labels.mov
Coordinator
Oct 16, 2014 at 3:12 PM
Ok, could you tell me how your map is set up, especially I'd like to know what kind of layers you use and what they datasources are, maybe you can provide the sample data for the video.
Oct 16, 2014 at 3:25 PM
The data is generated in the code - I extract the lines of a polygon and save it to a postgres table. I just have the problems with the line objects - for the points its not a problem. If the line for the label is cut within the extent of the map, the center-position is recalculated and the label moves. The question is if I can avoid that somehow without calculating the center position or cutting the line on my own around the point.
Coordinator
Oct 16, 2014 at 4:28 PM
In this case you can achieve that with a theme:
public class LineSegmentLabelTheme : ITheme
{
    public LabelStyle DefaultStyle { get; set; }
    public IStyle GetStyle(FeatureDataRow attribute)
    {
        if (attribute != null && attribute.Geometry != null &&
            attribute.Geometry.OgcGeometryType == OgcGeometryType.LineString &&
            attribute.Geometry.NumPoints == 2)
        {
            attribute.Geometry = attribute.Geometry.Factory.CreatePoint(attribute.Geometry.EnvelopeInternal.Centre);
        }
        return DefaultStyle;
    }
}
Oct 17, 2014 at 8:08 AM
Edited Oct 17, 2014 at 8:09 AM
ok, thought there is something within the library to avoid the conversion - then i am coming to the next problem :)... When I am applying a rotation column to the points - the offset is somehow sometimes very different:

https://dl.dropboxusercontent.com/u/8118260/Screen%20Shot%202014-10-17%20at%2008.59.49.png

Thats actually the reason why i am saving the line and not the dot. Maybe its a matter of my implementation - but I disabled all the styles and I took the calculation of the angle from your source code.

Maybe another question which could solve the problem from another side - is there an automatic solution to label the dimensions of a polygon outside or inside the polygon?
Coordinator
Oct 17, 2014 at 10:36 AM
aleho wrote:
ok, thought there is something within the library to avoid the conversion
Not sure if I understand it right, is it working or not?
then i am coming to the next problem :)... When I am applying a rotation column to the points - the offset is somehow sometimes very different:
https://dl.dropboxusercontent.com/u/8118260/Screen%20Shot%202014-10-17%20at%2008.59.49.png
For labeling points, the order of transformations is Translate, Rotate (on the translated point). The result may look awkward if you don't take the alignment of the label (horizontally/vertically) into account.
Maybe another question which could solve the problem from another side - is there an automatic solution to label the dimensions of a polygon outside or inside the polygon?
No, I'd have to think about that.
Oct 17, 2014 at 10:49 AM
Well its not a solution for me, because the offset is always different. Indeed it looks really awkward... What do you mean with take it into account? I have to place the labels sometimes on the bottom and sometimes on the top depending if the line is above the center, right, left and which direction it has. Its not very nice to calculate and to identify. At the moment I am having always some wrong labels. So if you have an idea of improvement you are really, really welcome :)...

My solution for the offset problem was to calculate a really small line of 1 cm length - then everything looks nice and the moving of the labels while panning is alright.
Oct 17, 2014 at 2:52 PM
It would be good to know how the "Bottom" and "Top" is defined - does it depend on the order of the coordinates?
Oct 28, 2014 at 8:42 AM
Any news on this issue?
Coordinator
Oct 29, 2014 at 9:28 AM
aleho wrote:
It would be good to know how the "Bottom" and "Top" is defined - does it depend on the order of the coordinates?
Browsing the code, I think it depends on the overall orientation of the lineal geometry. If the starting point is left of the ending point, TOP defines the position closest to the line, otherwise it is the other way around.
Oct 30, 2014 at 1:38 PM
Thanks a lot! I added the angle to the LabelStyle to set the Bottom or Top attribute correctly and... it looks good now!