project_point_to_line
- pyhelpers.geom.project_point_to_line(point, line, drop_dimension=None)
Find the projected point from a known point to a line.
- Parameters
point (shapely.geometry.Point) – geometry object of a point
line (shapely.geometry.LineString) – geometry object of a line
drop_dimension (str or None) – which dimension to drop, defaults to
None
; options include'x'
,'y'
and'z'
- Returns
the original point (with all or partial dimensions, given
drop
) and the projected one- 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 Fig. 10):
>>> import matplotlib.pyplot as plt >>> from pyhelpers.settings import mpl_preferences >>> mpl_preferences(font_name='Times New Roman', 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.text(pos[0], pos[1], pos[2], str(pos)) >>> ax.legend(loc=3) >>> plt.tight_layout() >>> ax.set_xticklabels([]) >>> ax.set_yticklabels([]) >>> ax.set_zticklabels([]) >>> plt.show()
Fig. 10 An example of projecting a point onto a line.