MapInfo Pro

MapInfo Monday: Rotating Labels for Point Objects

  • 1.  MapInfo Monday: Rotating Labels for Point Objects

    Employee
    Posted 11-01-2021 03:56
    Edited by Peter Møller 11-02-2021 03:48

    Happy #MapInfoMonday!

    Last week, @Mike Woodbury raised a question around rotating labels 45 degrees. He had some issues doing this through a tool. I'm not familiar with that specific tool but I'm guessing the tool would create ​​custom labels for all objects in the table.

    Personally, I'm not very fond of custom labels so I try to steer clear of these. So I have come up with an alternative solution using a query. This does however require MapInfo Pro v2021 as I'm creating new virtual objects through the query. You can of course get around this by creating a copy of the table and for this copy convert the objects.

    Creating Rotated Lines through a Query

    In this example, we have a set of points where we want to switch from horizontal labels to labels that are rotated a given number of degrees, in our example 45 degrees.

    This is our initial map with a point layer that has labels, house numbers to be precise, on top of the points.


    We now want to change this to labels that are rotated 45 degrees.

    The idea is to create a derived dataset where we create a line at a 45-degree angle from the current point. To do so we need a couple of MapBasic functions. We need to use the Offset() function, or CartesianOffset(), that can calculate a new point based on an input point, an angle, and a direction. And we need another function that can create a polyline between two objects. ConnectObjects(), or CartesianConnectObjects(), does this trick.

    Offset( object, angle, distance, units )
    • object is the object being offset.
    • angle is the angle to offset the object.
    • distance is a number representing the distance to offset the object.
    • units is a string representing the unit in which to measure distance.
    ConnectObjects( object1, object2, minimum )
    • object1 and object2 are object expressions.
    • minimum is a logical expression where TRUE calculates the minimum distance between the objects, and FALSE calculates the maximum distance between objects.
    The SQL Window that we added with MapInfo Pro v2021 allows us to build a query where the derived spatial object is used as the spatial object in the resulting query. We have to use the object keyword after the spatial expression to let MapInfo Pro know we want to use this expression instead of the spatial object from the base table.

    You can access the SQL Window from the SQL dropdown on the Table, Map, and Spatial tab. In the SQL Window write a query like this:
    Select t.*, ConnectObjects(t.obj, Offset(t.obj, 45, 100, "m") , 1) Object
    From Addresses As "t"
    Into Addresses_Line NoSelect

    I select into a named query, Addresses_Line, so that it is easy to recognize the query table afterward and I use the NoSelect keyword as I don't need the result to be highlighted.

    This expression creates the polyline: ConnectObjects(t.obj, Offset(t.obj, 45, 100, "m") , 1).

    It uses the base object as the start point of the polyline and as the endpoint, it creates a point that is offset 100 meters at a 45-degree angle. Remember that MapInfo Pro calculates angles with East being 0 and then counterclockwise.

    It will probably be a bit too much with 100 meters long lines. I used this distance so that you clearly could see the rotated lines.

    The result looks like this. You can of course change the angle and the length of the lines to meet your needs.


    You can also refer to a column in your table instead of using a fixed value. This allows you to create labels that are rotated at different angles. You can replace 45 with an expression like t.Angle.

    Another tip is to offset the start point as well as the endpoint of the polyline that would change the expression into something along these lines if we want to stick to a 100-meter long line: ConnectObjects(Offset(t.obj, 225, 50, "m"), Offset(t.obj, 45, 10, "m") , 1).

    And now we can add labels to the layer with the rotated lines and so control the angle of the labels.


    And as a final touch, we make the lines display with a "No Style" override so that we can't see these lines at all.


    And so we can control the rotation of labels by using a SQL query that creates rotated polylines.

    Also, note that there is an idea on Precisely Ideas to label points with rotated texts.



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------