I'm working on a python script whose goal is to detect if a point is out of a row of points (gps statement from an agricultural machine). To do this, my idea is to make a buffer around the 10 points around considered point (five before and five after). After that detect if my point is in the buffer. I'm stuck with 'GeoSeries' object has no attribute '_geom'
Here is my code for this part:
for i, row in src.iterrows():
# Tague les points trop éloignés du rang en cours
imin, imax = getMinMax(i, nl, 5) # Retourne les 5eme voisins avant et apres
buddies = src.loc[imin:imax, 'geometry'].drop([i], axis=0) # DF des voisins de i avec uniquement la geometrie
buddies_buf = buddies.buffer(ird*1.2) # Tampon avec un rayon egal a la distance inter rang + 20%
buddies_buf = buddies_buf.unary_union # Fusionne tous les tampons
buddies_buf = gpd.GeoDataFrame(gpd.GeoSeries(buddies_buf)).rename(columns={0: 'geometry'}).set_geometry('geometry') # Formate le tampon en GDF
buddies_buf.crs = crs_work # Attribut le crs
print((row.geometry).within(buddies_buf.geometry))
Somebody can help me?
I don't know if my idea is the best for this kind of detection, so if you have another best idea…
Best Answer
When you are at the following line of your script:
then
row.geometry
is a single shapely Point (row
is a Series representing a single row, sorow.geometry
is the single geometry from that row), whilebuddies_buf.geometry
is a GeoSeries.So the
within
method you are calling is the shapely's Point.within, which can only work with other scalar shapely geometries, and does not recognize a GeoSeries (and it is here that the error is coming from).The other way around, GeoSeries methods can work with both GeoSeries or shapely objects as argument. So you can invert the operation: