2D Text

2D text is a text label at a given location that maintains the same size and orientation regardless of the zoom level and camera orientation of the 3D view.

The Text2D class represents a single 2D text label.

Text2D properties

Use the following properties to customise your Text2D object:

text

Set the text to display in the label.

font_style

Set the font style of the label to one of the following values:

size

Set the point size of the label text.

location

Set the location of the label as an XYZ coordinate.

colour

Set the colour of the text.

horizontal_alignment

Use this enum property to set the horizontal alignment of your 2D text object. You can set this property to the following values:

vertical_alignment

Use this enum property to set the vertical alignment of your 2D text object. You can set this property to the following values:

Text2D examples

Creating a Text2D object

from mapteksdk.project import Project
from mapteksdk.data import Text2D
proj = Project() # Connect to default project

object_path = "scrapbook/text_example"

# Create Text
with proj.new(object_path, Text2D, overwrite=True) as text:
    text.text = 'hello world'
    text.location = [0, 0, 0]
    # Colours are R,G,B,A, where A is Alpha (255 = opaque)
    text.colour = [38, 128, 0, 255] # Light Green
    text.size = 100

Editing Text2D properties

The following example will modify the colour, size, text, and position of the text object created in the last example.

from mapteksdk.project import Project
from mapteksdk.data import Text2D
proj = Project() # Connect to default project

object_path = "scrapbook/text_example"

# Create Text
with proj.edit(object_path) as text:
    text.text = 'new text'
    text.location = [50, 50, 50]
    # Colours are R,G,B,A, where A is Alpha (255 = opaque)
    text.colour = [255, 50, 50, 255]
    text.size = 150

Labelling point heights along a line with text

The following example will add 2D text at each point in a 3D Polyline and label it with the height of the point.

from mapteksdk.project import Project
from mapteksdk.data import Text2D, Polyline
proj = Project() # Connect to default project

# Create a swirl line that rises in height
line_path = "scrapbook/label_line_example"
labels_path = "scrapbook/line_labels"

with proj.new(line_path, Polyline, overwrite=True) as line:
    line.points = [[10, 0, 0], [7, 5, 5], [9, 10, 10], [15, 12, 15],
                   [20, 10, 20], [20, 5, 25], [15, 5, 30], [12, 8, 35]]

# Delete labels container if it exists
proj.delete(labels_path)

with proj.read(line.id) as line:
    for label_id, point in enumerate(line.points):
        # Create text at point
        with proj.new(
          "{}/{}".format(labels_path, label_id),
          Text2D,
          overwrite=True) as text:
            text.text = "{}m".format(point[2])
            text.location = point
            text.colour = [point[2], 255, 255, 255]
            text.size = 40