1
from shapely.geometry import MultiPoint, Point
a=Point(1, 1.5)
b=Point(2, 1.5)
c=MultiPoint([a,b])

I am trying to create a MultiPoint column from two Point columns. Using the example code above, I can obtain a multipoint c but the data length is doubled, which I cannot use for an entire dataset. I tried different brackets, none has worked.

My dataset has 42 rows and contains columns of centroid_x (Point(a,b)) and centroid_y (Point(a,b)), which are original and destination locations. When I creating a MultiPoint column that combining (centroid_x and centroid_y), it comes out with 84 rows. I tried list(zip(tuple(a), tuple(b))). It solved the row number issues, but couldn't work for MultiPoint. PS: the method I used works fine with LineString.

5
  • Delete a and b at the end?
    – user30184
    May 28 at 10:07
  • Could you elaborate on what you meant by "data length"?
    – addcolor
    May 28 at 11:20
  • A multipoint will always be larger than two points, since it needs the count in addition to the vertices. In shapefile it will also contain the bounding box (for two points, effectively doubling the storage).
    – Vince
    May 28 at 11:23
  • My dataset has 42 rows and contains columns of centroid_x (Point(a,b)) and centroid_y (Point(a,b)), which are original and destination locations. When I create a MultiPoint column that combining (centroid_x and centroid_y), it comes out with 84 rows. I am following residentmario.github.io/geoplot/plot_references/…, to create a sankey plot for my data. The example uses multiPoint with both original and destination in one geometry column. May 28 at 11:40
  • 2
    Unclear what "your dataset" is? It's a CSV, a Python list you constructed with 2 columns (one for origin, other for destination), a geopandas dataframe, something else? Thanks
    – ThomasG77
    May 28 at 13:45
0

Below a solution with Geopandas

from shapely.geometry import MultiPoint, Point
import pandas as pd
import geopandas as gpd

# Get a dataframe
df = pd.DataFrame(
    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
     'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],
     'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86],
     'Latitude1': [-33.58, -16.78, -32.45, 3.60, 11.48],
     'Longitude1': [-59.66, -45.91, -68.66, -70.08, -64.86]})

# Create gdf using Latitude, Longitude (hence a first column of Point)
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
# Create second column of Point
gdf['geom2'] = [Point(x, y) for x, y in zip(df.Longitude1, df.Latitude1)]
# Take both previous Point column and convert to MultiPoint
gdf['multi'] = [MultiPoint([x, y]) for x, y in zip(df.geometry, df.geom2)]

# Create new geodataframe with multi colum set as the geopandas geometry column and intermediate Point columns removed
cleaned_gdf = gdf.set_geometry('multi').drop(['geometry', 'geom2'], axis=1)
# If you don't want to drop colum as above, comment above statement and do only the following (commented)
# cleaned_gdf = gdf.set_geometry('multi')
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.