[GIS] Folium and timestamped GeoJSON issue , not reading the data correctly

foliumpythonvisualisation

I am having issues with Folium and using TimestampedGeoJson. I have the following dataframe structure below. I am trying to display this data with Folium and a time slider to be used on the date field:

    Date        Geometry
0   2016-03-09  POLYGON ((10.00000 25.00000, 10.00000 25.00000, 10.00000 
                          26.00000, 12.00000 26.00000, 18.00000 25.00000,
                          17.00000 25.00000))

I am trying to follow this tutorial below and still not working:
https://towardsdatascience.com/visualizing-nyc-bike-data-on-interactive-and-animated-maps-with-folium-plugins-c2d7645cd19b

def create_geojson_features(df):

    features = []   
    for _, row in df.iterrows():
        features = []
        feature = {
            'type': 'Features',
            'geometry': {
                'type':'MultiPolygon',
                'coordinates':(row['Geometry'])
            },
            'properties': {
                'times':(pd.to_datetime(row['Date'])),
            }
        }
        features.append(feature)

    return features

df1 = create_geojson_features(df)
world_map = folium.Map(location=[10,0],tiles="cartodbposition",zoom_start=2,max_zoom=6,min_zoom=3)

    TimestampedGeoJson(
          df1
        , period='P1D'
        , add_last_point=True
        , auto_play=False
        , loop=False
        , max_speed=1
        , loop_button=True
        , date_options='YYYY-MM-DD'
        , time_slider_drag_update=True
    ).add_to(world_map)

The geometry and times are not being read correctly via the function and/or the TimestampedGeoJson. Also, in the coordinates field is a shapely object once the function create_geojson_features is executed.

Best Answer

I think the best way to transform geodataframe to geojson is using this function:

enter image description here

# list
polygon=[]

for i in shapes_gpd.index:
    geom = str(shapes_gpd[shapes_gpd.index==i].unary_union)
    positi = geom.find("(")
    coord1 = geom[positi:]
    coord2 = coord1.replace(")), ((",")),((")
    coord3 = coord2.replace(", ","),(")
    coord4 = str([coord3.replace(" ", ", ")]).replace("'(","(").replace(")'", ")")
    coord5 = coord4.replace("[(((", "[((").replace(")))]", "))]")
    coord6 = ast.literal_eval(coord5.replace("[((", "[(((").replace("))]",")))]"))
    coordgrup = {
        'coordinates': coord6,
        'group': shapes_gpd['group'].iloc[i], 
        'time': shapes_gpd['time'].iloc[i],
        'color': shapes_gpd['color'].iloc[i], 
    }
    polygon.append(coordgrup)

Source