Issue with gtfs_functions gtfs.cut_gtfs python

169 Views Asked by At

Trying to use the gtfs_functions package with python, I am struggling with the cut_gtfs function. More specifically, this function returns the following error: TypeError: object of type 'GeometryCollection' has no len(). Thank you for your help!


routes, stops, stop_times, trips, shapes = gtfs.import_gtfs("C:/Users/charl/OneDrive/Bureau/car_dependency/Data/tisseo_gtfs_v2.zip")

cutoffs = [0, 6, 9, 15, 19, 22, 24]
stop_freq = gtfs.stops_freq(stop_times, stops, cutoffs=cutoffs)
line_freq = gtfs.lines_freq(stop_times, trips, shapes, routes, cutoffs=cutoffs)
segments_gdf = gtfs.cut_gtfs(stop_times, stops, shapes)
Full error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[79], line 1
----> 1 segments_gdf = gtfs.cut_gtfs(stop_times, stops, shapes)

File c:\Users\charl\anaconda3\envs\inequality2\Lib\site-packages\gtfs_functions\gtfs_funtions.py:343, in cut_gtfs(stop_times, stops, shapes)
    340         loops_direction_id.append(direction_id)
    341         loops_shape_id.append(shape_id)
--> 343 segments = [cut_shapes_(shape_trans_lines[i], shape_closest_points[i])  for i in range(0, len(shape_trans_lines))]
    345 # Remove None values
    346 segments = [i for i in segments if i] 

File c:\Users\charl\anaconda3\envs\inequality2\Lib\site-packages\gtfs_functions\gtfs_funtions.py:343, in (.0)
    340         loops_direction_id.append(direction_id)
    341         loops_shape_id.append(shape_id)
--> 343 segments = [cut_shapes_(shape_trans_lines[i], shape_closest_points[i])  for i in range(0, len(shape_trans_lines))]
    345 # Remove None values
    346 segments = [i for i in segments if i] 

File c:\Users\charl\anaconda3\envs\inequality2\Lib\site-packages\gtfs_functions\gtfs_funtions.py:330, in cut_gtfs..cut_shapes_(shape_trans_lines, shape_closest_points)
    327     cut_points = MultiPoint(cut_points[1:-1])
    328     result = split(line, cut_points)
--> 330 if len(result)==len(trans_lines_all)-1:
    331     d['segment'] = [s for s in result]
    333     return d

TypeError: object of type 'GeometryCollection' has no len()

1

There are 1 best solutions below

0
Md Hishamur Rahman On

Shapely GeometryCollection is an object, and not an array/sequence. As a result, GeometryCollection objects stored in result and trans_lines_all will not return lengths. However, based on Shapely documentation, list of geometry instances can be retrieved using geoms attribute. Therefore, line 330-331 in gtfs_funtions.py needs to be modified as follows:

if len(result.geoms)==len(trans_lines_all.geoms)-1:
    d['segment'] = [s for s in result.geoms]