0

I try to create a shapefile of the FAO/GAUL/2015/leve1 country regions of the LMIC's countries. For that I decided to use Python as it gave me access to the earthengine API and the geopandas librairy.

To create the geodataframe I proceed as such :

import ee 
import geemap
import geopandas as gpd

ee.Initialize()

for i, row in df.iterrows():
    # select the country
    ee_country = ee.FeatureCollection("FAO/GAUL/2015/level1").filter(ee.Filter.eq('ADM0_CODE', row.GAUL))
    country_json = geemap.ee_to_geojson(ee_country)
    tmp = gpd.GeoDataFrame.from_features(country_json)
    
    # add the country to the global gdf 
    gdf = tmp if i == 0 else pd.concat([gdf, tmp])
 
gdf.crs = "EPSG:4326"
gdf

(the df is containing the FAO GAUL ADM0_CODE)

Then I try to transform this GDF into a shapefile using

gdf.to_file('FAO_GAUL_2015_level1.shp')

But I get an error because I have shapely.geometry.collection.GeometryCollection in my gdf (with linestring embed so not shapelyfiable).

Is there a way to only kee the Polygon for revery lin that contain a Geometry collection ? or Is it possible to drop all the linestring from the geometry column ?

0

I created a workaround that replace each line of the gdf containing a geometryCollection (a linestring + a polygon) by its first polygon

You can customize it to better fit your requirements

gdf_filtered = gdf.copy()

for i, row in gdf.iterrows():
    if type(row.geometry) == shapely.geometry.collection.GeometryCollection:

        # get the polygon and only keep the polygon 
        for shape in row.geometry:
            if type(shape) == shapely.geometry.polygon.Polygon:
                gdf_filtered.at[i, 'geometry'] = shape
                break

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.