folium 中线条的动态样式

San*_*era 3 python leaflet folium

我一直试图TimestampedGeoJson从 folium 中理解插件。

我想绘制随时间改变颜色的线条。目前,我所做的是每次需要更改颜色时都完全重画一条线,这会带来大量开销。

另一个问题是如何在要素中指定时间。目前,我有这个例子:

import folium
from folium.plugins import TimestampedGeoJson

m = folium.Map(
    location=[42.80491692, -4.62577249],
    zoom_start=10
)

data = [
{
    'coordinates': [
        [-4.018876661, 43.11843382],
        [-4.856537491, 42.82202193],
    ],
    'dates': [
        '2017-06-02T00:00:00',
        '2017-06-02T00:10:00'
    ],
    'color': 'red'
},
{
    'coordinates': [
        [-4.018876661, 43.11843382],
        [-4.856537491, 42.82202193],
    ],
    'dates': [
        '2017-06-02T00:00:00',
        '2017-06-02T00:20:00'
    ],
    'color': 'blue'
},
]

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

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

m.save('dynamic4.html')
Run Code Online (Sandbox Code Playgroud)

对我来说,第一次约会没有任何意义,但显然这是必需的,否则浏览器将不会绘制任何内容。

所以:

a) 如何在不重新绘制线条的情况下更改样式?b) 时间是什么意思?如何指定一致的时间序列?

Ber*_*sen 5

我将首先尝试单独解决您的问题,然后我将给出我最终会做什么的完整解决方案。本质上:

  1. 更改 TimestampedGeoJson _template 变量以更改 style_function 并使其能够使样式动态化
  2. 确保 TimestampedGeoJson 数据中的每个坐标都有一个时间步长
  3. 为避免混淆,尽量不要重叠特征或在特定时间步长内使特征缺失数据
  4. 我相信在你的场景中你只有一个功能,但在不同的时间步长改变颜色

解决您的问题:

a) 如何在不重新绘制线条的情况下更改样式?

我认为从 folium 本身是不可能的,有必要将 style_function 传递给 TimestampedGeoJson,它目前甚至不是类 init 的参数。这样做似乎很难,因为您需要将 python style_function 转换为 javascript style_function。

会有一个简单的解决方法。在TimestampedGeoJson类定义中,它使用 _template 变量作为 javascript 代码的字符串模板,因此您可以根据需要修改此模板,但使用 javascript。

class TimestampedGeoJson(MacroElement):
         .... hidding lines to save space
    _template = Template("""
        {% macro script(this, kwargs) %}
                 .... hidding lines to save space
                    style: function (feature) {
                        return feature.properties.style;
                    },
                    onEachFeature: function(feature, layer) {
                        if (feature.properties.popup) {
                        layer.bindPopup(feature.properties.popup);
                        }
                    }
                })
        {% endmacro %}
        """)  # noqa
     ..... hidding lines to save space
Run Code Online (Sandbox Code Playgroud)

因此,要在每个时间步更改线条颜色,您可以更改模板的这一部分:

style: function (feature) {
                        return feature.properties.style;
                    },
Run Code Online (Sandbox Code Playgroud)

通过这样:循环遍历颜色数组

                    style: function(feature) {
                        lastIdx=feature.properties.colors.length-1
                        currIdx=feature.properties.colors.indexOf(feature.properties.color);
                        if(currIdx==lastIdx){
                            feature.properties.color = feature.properties.colors[0]  
                        }
                        else{
                            feature.properties.color =feature.properties.colors[currIdx+1] 
                        }
                        return {color: feature.properties.color}
                    },

Run Code Online (Sandbox Code Playgroud)

更改它,以便您在每个时间步更新 properties.style 中的颜色。

b) 时间是什么意思?如何指定一致的时间序列?

TimestampedGeoJson 使用的是 Leaflet.TimeDimension 库,因此 TimestampedGeoJson 对应于L.TimeDimension.Layer.GeoJSON

从那个文件你得到

“coordTimes、times 或 linestringTimestamps:可以与几何(日期字符串或毫秒)相关联的时间数组。在 LineString 的情况下,它必须具有与 LineString 中的坐标一样多的项目。

所以基本上要保持一致,只需确保 1. 对于每个特征,时间大小与坐标相同,2. 使用有效的日期字符串或 ms 格式 3. 如果您的日期增加一个常数,则将您的时间段设置为该值

总而言之,我主要在您之前的示例中进行了更改:1)添加了一个带有新 style_function 的 _template 变量并更改了 TimestampedGeoJson 默认模板

2)稍微改变了两个特征的坐标,以显示您设置的两个特征是重叠的和一些时间步长,在某些时间步长中只定义了第一个特征,后来只定义了第二个特征,因此每次都会发生混淆步。

3)为每个功能添加了一个颜色列表以循环使用

from jinja2 import Template
_template = Template("""
    {% macro script(this, kwargs) %}
        L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
            _getDisplayDateFormat: function(date){
                var newdate = new moment(date);
                console.log(newdate)
                return newdate.format("{{this.date_options}}");
            }
        });
        {{this._parent.get_name()}}.timeDimension = L.timeDimension(
            {
                period: {{ this.period|tojson }},
            }
        );
        var timeDimensionControl = new L.Control.TimeDimensionCustom(
            {{ this.options|tojson }}
        );
        {{this._parent.get_name()}}.addControl(this.timeDimensionControl);
        var geoJsonLayer = L.geoJson({{this.data}}, {
                pointToLayer: function (feature, latLng) {
                    if (feature.properties.icon == 'marker') {
                        if(feature.properties.iconstyle){
                            return new L.Marker(latLng, {
                                icon: L.icon(feature.properties.iconstyle)});
                        }
                        //else
                        return new L.Marker(latLng);
                    }
                    if (feature.properties.icon == 'circle') {
                        if (feature.properties.iconstyle) {
                            return new L.circleMarker(latLng, feature.properties.iconstyle)
                            };
                        //else
                        return new L.circleMarker(latLng);
                    }
                    //else
                    return new L.Marker(latLng);
                },
                style: function(feature) {
                    lastIdx=feature.properties.colors.length-1
                    currIdx=feature.properties.colors.indexOf(feature.properties.color);
                    if(currIdx==lastIdx){
                        feature.properties.color = feature.properties.colors[currIdx+1] 
                    }
                    else{
                        feature.properties.color =feature.properties.colors[currIdx+1] 
                    }
                    return {color: feature.properties.color}
                },
                onEachFeature: function(feature, layer) {
                    if (feature.properties.popup) {
                    layer.bindPopup(feature.properties.popup);
                    }
                }
            })
        var {{this.get_name()}} = L.timeDimension.layer.geoJson(
            geoJsonLayer,
            {
                updateTimeDimension: true,
                addlastPoint: {{ this.add_last_point|tojson }},
                duration: {{ this.duration }},
            }
        ).addTo({{this._parent.get_name()}});
    {% endmacro %}
    """)
import folium
from folium.plugins import TimestampedGeoJson

m = folium.Map(
    location=[42.80491692, -4.62577249],
    zoom_start=9
)

data = [
{
    'coordinates': [
        [-4.018876661, 43.11843382],
        [-4.856537491, 42.82202193],
    ],
    'dates': [
        '2017-06-02T00:00:00',
        '2017-06-02T00:10:00'
    ],
    'color': 'brown',
    'colors':["black","orange","pink"],
},
{
    'coordinates': [
        [-4.058876661, 43.11843382],
        [-4.936537491, 42.82202193],
    ],
    'dates': [
        '2017-06-02T00:00:00',
        '2017-06-02T00:10:00'
    ],
    'color': 'blue',
    'colors':["red","yellow","green"],
},
]

features = [
    {
        'type': 'Feature',
        'geometry': {
            'type': 'LineString',
            'coordinates': d['coordinates'],
        },
        'properties': {
            'times': d['dates'],
            'color': d["color"],
            'colors':d["colors"]
        }
    }
    for d in data
]

t=TimestampedGeoJson({
    'type': 'FeatureCollection',
    'features': features,
}, period='PT10H', add_last_point=True)
t._template=_template
t.add_to(m)

m.save('original.html')
Run Code Online (Sandbox Code Playgroud)