GeoPandas Empty Geometry – Handling Empty Geometry Error in GeoPandas Dataframe

coordinate systemgeopandaspythonremote sensing

I want to reproject my ground truth data (land reference map) to UTM.

# Load Reference Data (reference map = ground truth data): labeled vector data in a shapefile (crops in Jendouba)
LULC_filename = "Jendouba.shp"     
land_use_ref= gpd.read_file(str(LULC_filename))


# Convert CRS (WGS 84) to UTM             
# CRS is the Coordinates Reference System
land_use_ref = land_use_ref.to_crs(Jendouba_crs.pyproj_crs()) 

#plot country
land_use_ref.plot()
plt.axis('off');
land_use_ref.crs

which raised the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-87-fe96bab05714> in <module>()
      6 # Convert CRS (WGS 84) to UTM
      7 # CRS is the Coordinates Reference System
----> 8 land_use_ref = land_use_ref.to_crs(Jendouba_crs.pyproj_crs())
      9 
     10 #plot country

4 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

/usr/local/lib/python3.6/dist-packages/shapely/ops.py in transform(func, geom)
    222     also satisfy the requirements for `func`.
    223     """
--> 224     if geom.is_empty:
    225         return geom
    226     if geom.type in ('Point', 'LineString', 'LinearRing', 'Polygon'):

AttributeError: 'NoneType' object has no attribute 'is_empty'

I checked the content of the dataframe:

print(land_use_ref.info())

And geometry had a missing value:

class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 249 entries, 0 to 248
Data columns (total 11 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   Type        249 non-null    object  
 1   Observatio  100 non-null    object  
 2   Crop_heigh  249 non-null    int64   
 3   Previous_y  7 non-null      object  
 4   Irrigation  28 non-null     object  
 5   Sewing_dat  249 non-null    object  
 6   Soil_type   99 non-null     object  
 7   Soil_col_1  95 non-null     object  
 8   FID222      249 non-null    int64   
 9   Label       249 non-null    int64   
 10  geometry    248 non-null    geometry
dtypes: geometry(1), int64(3), object(7)
memory usage: 21.5+ KB
None

So I tried to drop the row that has this missing value:

polygons_clean = Polygon[Polygon.geometry.type == 'POLYGON']

But I got the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-97-062acdbf0eeb> in <module>()
----> 1 polygons_clean = Polygon[Polygon.geometry.type == 'POLYGON']

AttributeError: type object 'Polygon' has no attribute 'geometry'

This is my dataframe

Edit: I used this:
land_use_ref['geometry']=land_use_ref['geometry'].dropna
and the problem persists and the weird thing is that the number of non-null samples become 249 (previously 248).

Best Answer

I was lucky to locate the "None object" in geometry (last row) and thus create a new dataframe from the original one minus the last row:

# Deleting the last row (Wheat) where geometry=None 
land_use_ref.shape    #(249,11)
land_use_ref = land_use_ref[:-1]
land_use_ref.shape    #(248,11)
Related Question