get_midpoint

pyhelpers.geom.get_midpoint(x1, y1, x2, y2, as_geom=False)[source]

Get the midpoint between two points (applicable for vectorized computation).

Parameters:
  • x1 (float | int | Iterable | numpy.ndarray) – longitude(s) or easting(s) of a point (an array of points)

  • y1 (float | int | Iterable | numpy.ndarray) – latitude(s) or northing(s) of a point (an array of points)

  • x2 (float | int | Iterable | numpy.ndarray) – longitude(s) or easting(s) of another point (another array of points)

  • y2 (float | int | Iterable | numpy.ndarray) – latitude(s) or northing(s) of another point (another array of points)

  • as_geom (bool) – whether to return shapely.geometry.Point, defaults to False

Returns:

the midpoint between (x1, y1) and (x2, y2) (or midpoints between two sequences of points)

Return type:

numpy.ndarray | shapely.geometry.Point | shapely.geometry.MultiPoint

Examples:

>>> from pyhelpers.geom import get_midpoint

>>> x_1, y_1 = 1.5429, 52.6347
>>> x_2, y_2 = 1.4909, 52.6271

>>> midpt = get_midpoint(x_1, y_1, x_2, y_2)
>>> midpt
array([ 1.5169, 52.6309])

>>> midpt = get_midpoint(x_1, y_1, x_2, y_2, as_geom=True)
>>> midpt.wkt
'POINT (1.5169 52.6309)'

>>> x_1, y_1 = (1.5429, 1.4909), (52.6347, 52.6271)
>>> x_2, y_2 = [2.5429, 2.4909], [53.6347, 53.6271]

>>> midpt = get_midpoint(x_1, y_1, x_2, y_2)
>>> midpt
array([[ 2.0429, 53.1347],
       [ 1.9909, 53.1271]])

>>> midpt = get_midpoint(x_1, y_1, x_2, y_2, as_geom=True)
>>> midpt.wkt
'MULTIPOINT (2.0429 53.1347, 1.9909 53.1271)'