0

I have a function that helps me read in large feature classes and them create a geodataframe:

line_feat = "S_Studies_Ln"
cnms_cols = ['REACH_ID', 'STUDY_ID', 'CO_FIPS', 'CID', 'FLD_ZONE',
         'VALIDATION_STATUS', 'MILES', 'STATUS_TYP','STATUS_DATE', 
         'STUDY_TYPE', 'TIER','LINE_TYPE', 'BS_ZONE', 'BS_STDYTYP', 
         'PRELIM_DATE']

def records(filename, cols):
    """DESCRIPTION HERE""" 
    with fiona.open(filename, layer=line_feat) as source:
        for feature in source:
            f = {k: feature[k] for k in ['id', 'geometry']}
            f['properties'] = {k: feature['properties'][k] for k in cols}
            yield f

cnms_df = gpd.GeoDataFrame.from_features(records(input_gdb, cnms_cols))

This works fine on all my feature except one. For a feature class called "FL", it throws the follwong error: IndexError: list index out of range. Any ideas as to what may be causing this in a feature class or how to debug it? Here is the full output:

> IndexError                                Traceback (most recent call
> last) <ipython-input-12-8059b0bcbbb8> in <module>
> ----> 1 cnms_df = gpd.GeoDataFrame.from_features(records(input_gdb, cnms_cols))
>       2 #cnms_df = pd.DataFrame(cnms_df.drop(columns='geometry'))
>       3 cnms_df.tail()
> 
> ~\AppData\Local\conda\conda\envs\geo_env\lib\site-packages\geopandas\geodataframe.py
> in from_features(cls, features, crs, columns)
>     231                 f = f
>     232 
> --> 233             d = {'geometry': shape(f['geometry']) if f['geometry'] else None}
>     234             d.update(f['properties'])
>     235             rows.append(d)
> 
> ~\AppData\Local\conda\conda\envs\geo_env\lib\site-packages\shapely\geometry\geo.py
> in shape(context)
>      37         return MultiPoint(ob["coordinates"])
>      38     elif geom_type == "multilinestring":
> ---> 39         return MultiLineString(ob["coordinates"])
>      40     elif geom_type == "multipolygon":
>      41         return MultiPolygon(ob["coordinates"], context_type='geojson')
> 
> ~\AppData\Local\conda\conda\envs\geo_env\lib\site-packages\shapely\geometry\multilinestring.py
> in __init__(self, lines)
>      50             pass
>      51         else:
> ---> 52             self._geom, self._ndim = geos_multilinestring_from_py(lines)
>      53 
>      54     def shape_factory(self, *args):
> 
> ~\AppData\Local\conda\conda\envs\geo_env\lib\site-packages\shapely\geometry\multilinestring.py
> in geos_multilinestring_from_py(ob)
>     121     exemplar = obs[0]
>     122     try:
> --> 123         N = len(exemplar[0])
>     124     except TypeError:
>     125         N = exemplar._ndim
> 
> IndexError: list index out of range

I have tried checking the attribute table for the feature class in ArcMap but can't see any difference between it and the other feature classes.

  • 1
    Check for null geometry – user2856 Oct 11 at 23:25
  • 1
    Do you have the same if you use geopandas.read_file(filename) ? – joris Oct 12 at 7:24
  • Yep. Same error. – gwydion93 Oct 12 at 16:01
  • OK @user2856, I removed the screenshot and added coding format to the actual output. Also, I tried doing a repair geometry and check geometry (produced an empty attribute table) and I still got the same error. Can you see anything from the geos_multilinestring_from_py output in the code above that would clue you into what the problem might be? – gwydion93 Oct 13 at 23:15
  • I can't reproduce, but there's (an unresolved) ticket from a couple of years ago with the same issue. Make sure shapely is up to date...??? – user2856 Oct 14 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.