3

I would like to iterate over each row in a GeoPandas multipoint dataframe to translate each point by different x, y values as such:

x = [numpy array of x translations of length of dataframe] ex: [5,10,6,8,...]
y = [numpy array of y translations of length of dataframe]

for index, poi in test1.iterrows():
    test1.loc[index, 'geometry'] = test1.loc[index, 'geometry'].translate(xoff=x[index], yoff=y[index])

I'm getting the error: AttributeError: 'MultiPoint' object has no attribute 'translate'

As a note: it will run using

for index, poi in test1.iterrows():
    test1['geometry'].translate(xoff=x[index], yoff=y[index])

but ultimately moves all the points over and over.

Any ideas?

1

In GeoPandas translate is a method of GeoSeries. In your case you must import shapely and then use its translate function on the geometry. This should work:

from shapely.affinity import translate

tmp = []
for index, poi in test1.iterrows():
    new_point = translate(test1.loc[index, 'geometry'], xoff=x[index], yoff=y[index])
    tmp.append({'geometry':new_point, 'other_stuff':test1.loc[index, 'other_col']...})

test2 = gpd.GeoDataFrame(tmp)
4

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.