Converting 2D points to 3D points
In the Maptek Python SDK, points are typically represented using three-dimensional points (x, y and z). However, when importing data from external files or other packages, points may only include two dimensions (x and y). The following script demonstrates an efficient method for converting an array of two-dimensional points to three-dimensional points by adding a specified z-coordinate.
from __future__ import annotations
import typing
from mapteksdk.project import Project
from mapteksdk.data import PointSet
import numpy as np
def convert_2d_points_to_3d(points_2d, z_coordinate):
"""Convert 2D coordinates to 3D coordinates.
Parameters
----------
points_2d
Array like containing two dimensional (x, y) coordinates.
z_coordinate
Z coordinate to add to the 2D coordinates.
Returns
-------
npt.ArrayLike
An array of points containing the 2D points in points_2d
with z_coordinate added to make the coordinates three
dimensional.
"""
points_3d = np.empty(
(len(points_2d), 3), dtype=np.float64
)
points_3d[:, :2] = points_2d
points_3d[:, 2] = z_coordinate
return points_3d
if __name__ == "__main__":
points = [
(-1, -1), (2, -1), (-1, 2), (2, 2)
]
with Project() as project:
with project.new("cad/2d_point_set", PointSet) as point_set:
point_set.points = convert_2d_points_to_3d(points, 0.0)
print(point_set.points)
# Expected output:
# [[-1. -1. 0.]
# [ 2. -1. 0.]
# [-1. 2. 0.]
# [ 2. 2. 0.]]
Alternative approaches
If you have a list of tuples (or lists) with two elements in each tuple, such as [(x0, y0), (x1, y1), ..., (xN, yN)], you can convert it into a 3D array of triples (x, y, z) where z is always zero using the following approach:
import numpy as np points_2d = [(-1, -1), (2, -1), (-1, 2), (2, 2)] points_3d = np.hstack((points_2d, np.zeros((len(points_2d), 1))))
If you have a NumPy array of shape (N, 2), then the following snippet can convert it into a 3D array with (x, y, z) where z is always zero.
import numpy as np points_3d = np.hstack((points_2d, np.zeros((points_2d.shape[0], 1))))
The resulting points_3d in both cases will be:
array([[-1., -1., 0.],
[ 2., -1., 0.],
[-1., 2., 0.],
[ 2., 2., 0.]])