2

I have a creating a list of shapely polygons. While I am trying to create a geodataframe from that list it gives an error:

intersec_ls = [poly1, poly2, poly3]
cu = cascaded_union(intersec_ls)
differ = buffer.difference(cu)

fin_ls.append(differ)
fin_ls.append(intersec_ls)

geom = gpd.GeoSeries(fin_ls)
gdf = gdf( geometry= geom)

TypeError: Input must be valid geometry objects: [<shapely.geometry.polygon.Polygon object at 0x00000213702BEDA0>]

How can I deal with invalid geometry in the list?

3
  • How many rows gdf have? – Kadir Şahbaz Jun 9 '20 at 9:30
  • gdf has 30000 rows. – FJ_Abbasi Jun 9 '20 at 9:39
  • Is gdf defined somewhere before gdf=dgf(geometry=geom)? – Kadir Şahbaz Jun 9 '20 at 9:40
4

There are two mistakes in your script.

  1. Since intersect_ls is a list, use extend instead of append. The reason of getting error is that. Because the first item in fin_ls is a geometry, the other is a list, but it should have been a geometry too.

     fin_ls.append(differ)
     fin_ls.extend(intersec_ls)
    
  2. Since gdf is a GeoDataFrame instance, you cannot use parenthesis with gdf as in gdf( geometry= geom). Instead, use

     gdf = gpd.GeoDataFrame(gdf, geometry=geom) 
    

Or just use gdf = gpd.GeoDataFrame(geometry=geom) if you didn't define gdf before. In this case gdf includes only geometries.

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.