transform_geom_point_type

pyhelpers.geom.transform_geom_point_type(*pts, as_geom=True)

Transform iterable to shapely.geometry.Point type, or the other way round.

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

  • as_geom (bool) – whether to return point(s) as shapely.geometry.Point, defaults to True

Returns

a sequence of points (incl. None if errors occur)

Return type

Generator[shapely.geometry.Point, list, tuple, numpy.ndarray]

Examples:

>>> from pyhelpers.geom import transform_geom_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_geom_point_type(pt1, pt2)
>>> for x in geom_points:
...     print(x)
POINT (-0.1276474 51.5073219)
POINT (-1.9026911 52.4796992)

>>> geom_points = transform_geom_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_geom_point_type(pt1, pt2)
>>> for x in geom_points:
...     print(x)
POINT (-0.1276474 51.5073219)
POINT (-1.9026911 52.4796992)

>>> geom_points = transform_geom_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_geom_point_type(Point([1, 2, 3]), as_geom=False)
>>> for x in geom_points_:
...     print(x)
(1.0, 2.0, 3.0)