get_coordinates_as_array

pyhelpers.geom.get_coordinates_as_array(geom_obj, unique=False)[source]

Retrieve an array of coordinates from the input geometry object.

Parameters:
  • geom_obj (numpy.ndarray | list | tuple | Iterable | shapely.geometry.base.BaseGeometry) – Input geometry object.

  • unique (bool) – Whether to remove duplicated points; defaults to False.

Returns:

Array of coordinates extracted from the geometry object.

Return type:

numpy.ndarray

Examples:

>>> from pyhelpers.geom import get_coordinates_as_array
>>> from pyhelpers._cache import example_dataframe
>>> from shapely.geometry import Polygon, MultiPoint, MultiPolygon, GeometryCollection
>>> from numpy import array_equal
>>> 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
>>> geom_obj_1 = example_df.to_numpy()
>>> geom_coords_1 = get_coordinates_as_array(geom_obj=geom_obj_1)
>>> geom_coords_1
array([[-0.1276474, 51.5073219],
       [-1.9026911, 52.4796992],
       [-2.2451148, 53.4794892],
       [-1.5437941, 53.7974185]])
>>> geom_obj_2 = Polygon(example_df.to_numpy())
>>> geom_coords_2 = get_coordinates_as_array(geom_obj=geom_obj_2, unique=True)
>>> array_equal(geom_coords_2, geom_coords_1)
True
>>> geom_obj_3 = MultiPoint(example_df.to_numpy())
>>> geom_coords_3 = get_coordinates_as_array(geom_obj=geom_obj_3)
>>> array_equal(geom_coords_3, geom_coords_1)
True
>>> geom_obj_4 = MultiPolygon([geom_obj_2, geom_obj_2])
>>> geom_coords_4 = get_coordinates_as_array(geom_obj=geom_obj_4, unique=True)
>>> array_equal(geom_coords_4, geom_coords_1)
True
>>> geom_obj_5 = GeometryCollection([geom_obj_2, geom_obj_3, geom_obj_4])
>>> geom_coords_5 = get_coordinates_as_array(geom_obj=geom_obj_5, unique=True)
>>> array_equal(geom_coords_5, geom_coords_1)
True