project_point_to_line

pyhelpers.geom.project_point_to_line(point, line, drop_dimension=None)[source]

Find the projected point from a given point to a line.

Parameters:
  • point (shapely.geometry.Point) – Point geometry object representing the starting point.

  • line (shapely.geometry.LineString) – Line geometry object to which the point is projected.

  • drop_dimension (str | None) – Dimension to drop during projection; options include 'x', 'y' and 'z'; defaults to None.

Returns:

Tuple containing the original point (with all or partial dimensions, based on drop_dimension) and the projected point on the line.

Return type:

tuple

Examples:

>>> from pyhelpers.geom import project_point_to_line
>>> from shapely.geometry import Point, LineString, MultiPoint
>>> pt = Point([399297, 655095, 43])
>>> ls = LineString([[399299, 655091, 42], [399295, 655099, 42]])
>>> _, pt_proj = project_point_to_line(point=pt, line=ls)
>>> pt_proj.wkt
'POINT Z (399297 655095 42)'

This example is illustrated below (see Figure 15):

>>> import matplotlib.pyplot as plt
>>> from pyhelpers.settings import mpl_preferences
>>> mpl_preferences(backend='TkAgg', font_size=12)
>>> fig = plt.figure()
>>> ax = fig.add_subplot(projection='3d')
>>> ls_zs = list(map(lambda c: c[2], ls.coords))
>>> ax.plot(ls.coords.xy[0], ls.coords.xy[1], ls_zs, label='Line')
>>> ax.scatter(pt.x, pt.y, pt.z, label='Point')
>>> ax.scatter(pt_proj.x, pt_proj.y, pt_proj.z, label='Projected point')
>>> for i in MultiPoint([*ls.coords, pt, pt_proj]).geoms:
...     pos = tuple(map(int, i.coords[0]))
...     ax.text3D(pos[0], pos[1], pos[2], str(pos))
>>> ax.legend(loc=3)
>>> fig.tight_layout()
>>> ax.set_xticklabels([])
>>> ax.set_yticklabels([])
>>> ax.set_zticklabels([])
>>> fig.show()
>>> # from pyhelpers.store import save_figure
>>> # path_to_fig_ = "docs/source/_images/geom-project_point_to_line-demo"
>>> # save_figure(fig, f"{path_to_fig_}.svg", verbose=True)
>>> # save_figure(fig, f"{path_to_fig_}.pdf", verbose=True)
../_images/geom-project_point_to_line-demo.svg

Figure 15 An example of projecting a point onto a line.