Search code examples
pythongisgeopandasshapely

How to extract individual points from the shapely Multipoint data type?


I am using shapely2.0; somehow, I can't iterate through individual points in the MULTIPOINT data type in this version.

I wanted to extract and plot individual points from the MULTIPOINT. The MULTIPOINT is obtained from line.intersection(circle_boundary), where I tried to get the intersection points between line and circle geometry.

Is there any way to access the Individual points in the MULTIPOINT or get the intersecting points as individual shapely Points rather than as MULTIPOINT?


Solution

  • MultiPoint geometry object can be exploded to become basic Point objects.

    Here is a demo code:-

    import os
    os.environ['USE_PYGEOS'] = '0'
    import geopandas as gpd
    from shapely.geometry import MultiPoint
    
    # Create a geodataframe containing 2 rows, each row has a MultiPoint geometry
    s = gpd.GeoSeries(
        [MultiPoint([(0, 0), (1, 1)]), 
         MultiPoint([(2, 2), (3, 3), (4, 4)])]
    )
    
    # Create a new geodataframe from "s" by `s.explode()`
    # This geodataframe will have 5 rows, each has simple Point geometry derived from the original geodataframe 
    exploded_s = s.explode(index_parts=True)
    exploded_s
    
    0  0    POINT (0.00000 0.00000)
       1    POINT (1.00000 1.00000)
    1  0    POINT (2.00000 2.00000)
       1    POINT (3.00000 3.00000)
       2    POINT (4.00000 4.00000)
    dtype: geometry