AttributeError: 'Polygon' object has no attribute 'to_wkt'

Hi, i’m trying to get NDVI value for polygone ,
Please any solution .
i get this message :
thanx


AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_23220/2673414790.py in <module>
     12     dict_results.update({str(gdf['Name'][j]):[]})
     13     for index in indices:
---> 14         result=get_data_sentinelhub(tile_date,gdf.iloc[j:j+1],index=index,i=0)
     15         dict_results[str(gdf['Name'][j])].append(result)

~\AppData\Local\Temp/ipykernel_23220/3451574871.py in get_data_sentinelhub(date, gdf, index, i)
     82     )
     83     images=request.get_data()
---> 84     mask=rasterize_vector(gdf=gdf)
     85     first_band=images[i]['default.tif'][...,0]
     86     second_band=images[i]['default.tif'][...,1]

~\AppData\Local\Temp/ipykernel_23220/3451574871.py in rasterize_vector(gdf)
     15         raster_shape=(bbox_size[1],bbox_size[0]),
     16         no_data_value=np.nan)
---> 17     output_patch = rasterize_task.execute(input_patch)
     18     mask=output_patch.mask_timeless['ROI'][...,0]
     19     mask=np.where(mask==0,np.nan,mask)

~\anaconda3\envs\prod\lib\site-packages\eo_learn_geometry-0.9.0-py3.9.egg\eolearn\geometry\transformations.py in execute(self, eopatch)
    264 
    265         group_classes = self.overlap_value is not None
--> 266         rasterization_shapes = self._get_rasterization_shapes(eopatch, group_classes=group_classes)
    267 
    268         if not rasterization_shapes:

~\anaconda3\envs\prod\lib\site-packages\eo_learn_geometry-0.9.0-py3.9.egg\eolearn\geometry\transformations.py in _get_rasterization_shapes(self, eopatch, group_classes)
    177             warnings.warn('Given vector polygons contain some 3D geometries, they will be projected to 2D',
    178                           RuntimeWarning)
--> 179             vector_data.geometry = vector_data.geometry.apply(lambda geo: shapely.wkt.loads(geo.to_wkt()))
    180 
    181         if self.values_column is None:

~\anaconda3\envs\prod\lib\site-packages\geopandas\geoseries.py in apply(self, func, convert_dtype, args, **kwargs)
    572     @inherit_doc(pd.Series)
    573     def apply(self, func, convert_dtype=True, args=(), **kwargs):
--> 574         result = super().apply(func, convert_dtype=convert_dtype, args=args, **kwargs)
    575         if isinstance(result, GeoSeries):
    576             if self.crs is not None:

~\anaconda3\envs\prod\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
   4355         dtype: float64
   4356         
-> 4357         return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
   4358 
   4359     def _reduce(

~\anaconda3\envs\prod\lib\site-packages\pandas\core\apply.py in apply(self)
   1041             return self.apply_str()
   1042 
-> 1043         return self.apply_standard()
   1044 
   1045     def agg(self):

~\anaconda3\envs\prod\lib\site-packages\pandas\core\apply.py in apply_standard(self)
   1096                 # List[Union[Callable[..., Any], str]]]]]"; expected
   1097                 # "Callable[[Any], Any]"
-> 1098                 mapped = lib.map_infer(
   1099                     values,
   1100                     f,  # type: ignore[arg-type]

~\anaconda3\envs\prod\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

~\anaconda3\envs\prod\lib\site-packages\eo_learn_geometry-0.9.0-py3.9.egg\eolearn\geometry\transformations.py in <lambda>(geo)
    177             warnings.warn('Given vector polygons contain some 3D geometries, they will be projected to 2D',
    178                           RuntimeWarning)
--> 179             vector_data.geometry = vector_data.geometry.apply(lambda geo: shapely.wkt.loads(geo.to_wkt()))
    180 
    181         if self.values_column is None:

AttributeError: 'Polygon' object has no attribute 'to_wkt'

Hi @SAMED

Could you please let us know where this happens? Is this an example from eo-learn?

Additional comment: your eo-learn version seems to be a bit outdated: could you try with the latest (0.10.1)?

1 Like

Hi @batic Thank you for your quick response !

yes it’s from eo learn
the polygon is a Polygon Z (3D)

Honestly, I’ve never worked with a Polygon Z before, so it might be there is an issue there… Could you try something along the lines of this:

# poly is your polygon, e.g. poly = geodataframe.iloc[0].geometry
poly_wkt = poly.to_wkt()
shapely.wkt.loads(poly_wkt)