Skip to content
Advertisement

Converting a list of coordinates to point geometry

My geopandas df looks like this:

   events   datum   pz  ...  dst        Aktenzahl              geometry
0      22  2015.0  204  ...  BSm  BSm/204/30/2015  [16.34203, 48.21717]
1      22  2015.0  202  ...  BSm   BSm/202/8/2015  [16.34189, 48.21719]

As you can see, the geometry column is a list containing longitude and latitude values. Is there an efficient way to convert them to points?

This works but is very inefficient:

pts = gpd.GeoDataFrame(geometry=gpd.points_from_xy(
[df['geometry'][0][0], df['geometry'][1][0]], 
[df['geometry'][0][1], df['geometry'][1][1]]))

df["geometry"] = pts
df = gpd.GeoDataFrame(df, crs='epsg:4326').set_geometry('geometry')
df

   events   datum   pz  ...  dst        Aktenzahl                   geometry
0      22  2015.0  204  ...  BSm  BSm/204/30/2015  POINT (16.34203 48.21717)
1      22  2015.0  202  ...  BSm   BSm/202/8/2015  POINT (16.34189 48.21719)

When I put this in a loop I get TypeError: len() of unsized object

for i in range(len(df['geometry'])):
  print(df['geometry'][i][0], df['geometry'][i][1]) # works
  print(gpd.GeoDataFrame(geometry=gpd.points_from_xy(df['geometry'][i][0], df['geometry'][i][1]))) # TypeError: len() of unsized object

Basically I need the inverse of this question.

Reproducible data here:

df={'events': {0: 22, 1: 22}, 'datum': {0: 2015.0, 1: 2015.0}, 'pz': {0: 204, 1: 202}, 'strasse': {0: 'Hernalser Gürtel', 1: 'Hernalser Gürtel'}, 'hnr': {0: 'unter U6', 1: 'unter U6'}, 'dst': {0: 'BSm', 1: 'BSm'}, 'Aktenzahl': {0: 'BSm/204/30/2015', 1: 'BSm/202/8/2015'}, 'geometry': {0: [16.34203, 48.21717], 1: [16.34189, 48.21719]}}

Advertisement

Answer

I don’t know why you want a loop.

df={'events': {0: 22, 1: 22}, 'datum': {0: 2015.0, 1: 2015.0}, 'pz': {0: 204, 1: 202}, 'strasse': {0: 'Hernalser Gürtel', 1: 'Hernalser Gürtel'}, 'hnr': {0: 'unter U6', 1: 'unter U6'}, 'dst': {0: 'BSm', 1: 'BSm'}, 'Aktenzahl': {0: 'BSm/204/30/2015', 1: 'BSm/202/8/2015'}, 'geometry': {0: [16.34203, 48.21717], 1: [16.34189, 48.21719]}}
df = pd.DataFrame(df)
df.geometry = gpd.points_from_xy(df.geometry[0], df.geomety[1])
gdf = gpd.GeoDataFrame(df, geometry='geometry', crs = 'epsg:4326)

All coordinates have been changed to points.
Do you think this is really inefficient?


Update

sorry, I have a mistake. I think the solution you want is this.

df.geometry = gpd.points_from_xy(pd.DataFrame(df.geometry.to_list())[0], pd.DataFrame(df.geometry.to_list())[1])
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement