0

I have a geodataframe with points, each with a scalar value. I also have a geodataframe with shapes. I want to add the value field of all points that intersect shapes to their respective shape. So if point 1 has value 10 and point 2 has value 3 and both intersect shape 1, shape 1 then has a field called value with the value 13.

I looked at using sjoin in geopandas, but I can only see it matching one point to one shape, not if there are multiple points in a shape. I suppose I could do a spatial inner join with both and have the separate entries for each point matched with a shape and then flatten it, but I don't know how to combine rows like that either.

J. Doe
  • 985
  • 2
  • 10
  • 15
  • 1
    I would use sjoin to solve this problem. I actually just posted a quick [intro](https://medium.com/@bobhaffner/spatial-joins-in-geopandas-c5e916a763f3) to sjoin the other day. You would do something like `sjoin(points, shapes, op='within')` the result would be a frame that contains the index of shapes (index_right) that you could groupby and sum to get your "13" – Bob Haffner Sep 19 '17 at 19:10
  • thanks, still having a little trouble with this. I'm able to get the table with each point and the column index_right that is the polygon that point falls in, but trying to aggregate them seems to get rid of the index_right column. I'm using `df.dissolve(by='index_right', aggfunc='sum')`. – J. Doe Sep 19 '17 at 22:53
  • Couldn't you just `df.groupby('index_right')['your_sum_col'].sum()`? – Bob Haffner Sep 19 '17 at 23:01
  • got it, thanks. then i just join that back to the polygon dataframe, but seems a bit roundabout, no? – J. Doe Sep 20 '17 at 01:43
  • I was thinking do `s = sjoin(shapes, points)` and then use a function like `s.dissolve(by='geometry', aggfunc='sum')` to add up all fields, since i actually want to do this for multiple fields later on, but i get `KeyError: 'geometry'`. Any idea why I can't dissolve by the polygon field? It seems like a much more straightforward way—only 2 steps. – J. Doe Sep 20 '17 at 01:47
  • perhaps `s.dissolve(by='your_shape_name', aggfunc='sum')` Also, it might be helpful to describe your two dataframes and desired output in your question – Bob Haffner Sep 20 '17 at 02:38
  • thanks, was too tired to think. the geoid column did me fine, but i'm sure name would've too. – J. Doe Sep 20 '17 at 04:51
  • Hello ! did you find a solution ? because have same issue with a join between a polygon grid and points. When I do sjoin between them, each square of my grid is multiplied by as many points that are contained... – Tim C. Jan 30 '18 at 14:42

0 Answers0