transform_point_type

pyhelpers.geom.transform_point_type(*pts, as_geom=True)[source]

Transform iterable data to geometric type or vice versa.

Parameters:
  • pts (list | tuple | shapely.geometry.Point) – Iterable data representing points (e.g. list of lists/tuples).

  • as_geom (bool) – Whether to return points as shapely.geometry.Point; defaults to True.

Returns:

A sequence of points, including None if errors occur.

Return type:

Generator

Examples:

>>> from pyhelpers.geom import transform_point_type
>>> from pyhelpers._cache import example_dataframe
>>> from shapely.geometry import Point
>>> example_df = example_dataframe()
>>> example_df
            Longitude   Latitude
City
London      -0.127647  51.507322
Birmingham  -1.902691  52.479699
Manchester  -2.245115  53.479489
Leeds       -1.543794  53.797418
>>> pt1 = example_df.loc['London'].values  # array([-0.1276474, 51.5073219])
>>> pt2 = example_df.loc['Birmingham'].values  # array([-1.9026911, 52.4796992])
>>> geom_points = transform_point_type(pt1, pt2)
>>> for x in geom_points:
...     print(x)
POINT (-0.1276474 51.5073219)
POINT (-1.9026911 52.4796992)
>>> geom_points = transform_point_type(pt1, pt2, as_geom=False)
>>> for x in geom_points:
...     print(x)
[-0.1276474 51.5073219]
[-1.9026911 52.4796992]
>>> pt1, pt2 = map(Point, (pt1, pt2))
>>> geom_points = transform_point_type(pt1, pt2)
>>> for x in geom_points:
...     print(x)
POINT (-0.1276474 51.5073219)
POINT (-1.9026911 52.4796992)
>>> geom_points = transform_point_type(pt1, pt2, as_geom=False)
>>> for x in geom_points:
...     print(x)
(-0.1276474, 51.5073219)
(-1.9026911, 52.4796992)
>>> geom_points_ = transform_point_type(Point([1, 2, 3]), as_geom=False)
>>> for x in geom_points_:
...     print(x)
(1.0, 2.0, 3.0)