Search code examples
pythongeojsonfolium

I have an error using TimestampedGeoJson in folium


I'm trying to use TimestampedGeoJson from Folium. But it doesn't work. My goal is to visualize the route by entering the latitude and longitude values in TYP_LOCATION_MATRIX_FLO. However, the following error occurred: I searched a lot of data in stackoverflow, but I don't think it fits my case. to be in need of assistance TYP_LOCATION_MATRIX_FLO is an array with a latitude value.


for n in geo_df.index:

lines = [
{
    'coordinates': [
        [TYP_LOCATION_MATRIX_FLO[0:,0], TYP_LOCATION_MATRIX_FLO[0,0:]],
        [TYP_LOCATION_MATRIX_FLO[1:,0], TYP_LOCATION_MATRIX_FLO[0,1:]],
    ],
    'dates': [
        '2017-06-02T00:00:00',
        '2017-06-02T00:10:00'
    ],
    'color': 'red'
},
]

features = [
    {
        'type': 'Feature',
        'geometry': {
            'type': 'LineString',
            'coordinates': line['coordinates'],
        },
        'properties': {
            'times': line['dates'],
            'style': {
                'color': line['color'],
                'weight': line['weight'] if 'weight' in line else 5
            }
        }
    }
    for line in lines
]

plugins.TimestampedGeoJson({
    'type': 'FeatureCollection',
    'features': features,
}, period='PT1M', add_last_point=True).add_to(map)

TypeError Traceback (most recent call last) in 40 'type': 'FeatureCollection', 41 'features': features, ---> 42 }, period='PT1M', add_last_point=True).add_to(map)

TypeError: Object of type ndarray is not JSON serializable


Solution

  • The problem is that you are trying to pass a numpy.ndarray as an input and, like the error message shows, this is not accepted. You can simply change the coordinate input to:

    'coordinates': [
        [TYP_LOCATION_MATRIX_FLO[0][0], TYP_LOCATION_MATRIX_FLO[0][1]],
        [TYP_LOCATION_MATRIX_FLO[1][0], TYP_LOCATION_MATRIX_FLO[1][1]],
    ],
    

    and this should do the trick as you are now passing values instead of arrays.