如何使用 Leaflet 分别设置 GeoJSON GeometryCollection 的每个几何图形的样式
How to style each Geometry of a GeoJSON GeometryCollection individually using Leaflet
我正在使用 Leaflet 和一些 GeoJSON 数据制作交互式地图,显示城市的铁路运输历史。我的 GeoJSON 数据如下所示:
var lines = [
{
"type": "FeatureCollection",
"name": "stadtmitte_gerresheim",
"startYear": "1902",
"endYear": "2019",
"lineColor": "#DD0000",
"trainID": "001",
"features": [
{
"type": "Feature",
"properties": {
"lineName": "U73",
"station1": "Universität Ost/Botanischer Garten",
"station2": "Gerresheim S",
"startYear": "2016",
"endYear": "2019",
"lineColor": "#DD0000",
},
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"lineID": "001",
"lineDescription": "Schleife am S-Bahnhof Gerresheim",
"coordinates": [...
]
},
{
"type": "LineString",
"lineID": "002",
"lineDescription": "Gerresheim S bis Ecke Schönaustraße/Heyestraße",
"coordinates": [...
]
}
....
我正在使用几个 FeatureCollections,它们包含一些 Features。如您所见,每个要素的几何形状均由 GeometryCollection(主要由 LineString 组成)定义。
我想使用 Leaflet 根据其成员来设置每个几何图形(来自 GeometryCollections 的 LineStrings)的样式。目前我只能通过功能设置整个 GeometryCollection 的样式:
L.geoJSON(route, {
filter: function(feature) {
if (checkYear(feature)) {
return true;
} else {
return false;
}
},
style: function(feature) {
switch (feature.properties.tunnel) {
case 'yes': return {color: "#ff0000", dashArray: "1,6"};
default: return {color: "#ff0000"}
}
},
pointToLayer: function (feature, latlng) {
getIconSize();
switch (feature.properties.underground) {
case 'yes': return L.marker(latlng, {icon: stadtbahnIcon});
default: return L.marker(latlng, {icon: strassenbahnIcon});
}
}
}).addTo(mymap);
}
我的问题:是否可以将单独的样式函数传递给同一特征的不同几何体,而不是将相同的样式函数传递给该特征中的所有几何体?如果是这样,怎么办?我想这应该是可能的,因为 Leaflet 为每个 Geometry 创建了一个路径。
干杯
Is it possible to hand individual style functions to different Geometries of the same Feature?
没有
Leaflet 在 GeoJSON Feature
中创建一个 L.Layer
的实例,right over here; and the application of the style
callback function just loops over the L.Layer
s corresponding to each feature,它不会向下钻取嵌套的 L.LayerGroup
s(另外,L.Polyline
s 和 L.Polygon
s 作为 GeometryCollection
的结果创建,没有对原始 GeoJSON 特征及其属性的引用)。
我想您会想要使用 TurfJS' geomEach
(或类似的东西)来预处理您的特征和几何图形。
我正在使用 Leaflet 和一些 GeoJSON 数据制作交互式地图,显示城市的铁路运输历史。我的 GeoJSON 数据如下所示:
var lines = [
{
"type": "FeatureCollection",
"name": "stadtmitte_gerresheim",
"startYear": "1902",
"endYear": "2019",
"lineColor": "#DD0000",
"trainID": "001",
"features": [
{
"type": "Feature",
"properties": {
"lineName": "U73",
"station1": "Universität Ost/Botanischer Garten",
"station2": "Gerresheim S",
"startYear": "2016",
"endYear": "2019",
"lineColor": "#DD0000",
},
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"lineID": "001",
"lineDescription": "Schleife am S-Bahnhof Gerresheim",
"coordinates": [...
]
},
{
"type": "LineString",
"lineID": "002",
"lineDescription": "Gerresheim S bis Ecke Schönaustraße/Heyestraße",
"coordinates": [...
]
}
....
我正在使用几个 FeatureCollections,它们包含一些 Features。如您所见,每个要素的几何形状均由 GeometryCollection(主要由 LineString 组成)定义。
我想使用 Leaflet 根据其成员来设置每个几何图形(来自 GeometryCollections 的 LineStrings)的样式。目前我只能通过功能设置整个 GeometryCollection 的样式:
L.geoJSON(route, {
filter: function(feature) {
if (checkYear(feature)) {
return true;
} else {
return false;
}
},
style: function(feature) {
switch (feature.properties.tunnel) {
case 'yes': return {color: "#ff0000", dashArray: "1,6"};
default: return {color: "#ff0000"}
}
},
pointToLayer: function (feature, latlng) {
getIconSize();
switch (feature.properties.underground) {
case 'yes': return L.marker(latlng, {icon: stadtbahnIcon});
default: return L.marker(latlng, {icon: strassenbahnIcon});
}
}
}).addTo(mymap);
}
我的问题:是否可以将单独的样式函数传递给同一特征的不同几何体,而不是将相同的样式函数传递给该特征中的所有几何体?如果是这样,怎么办?我想这应该是可能的,因为 Leaflet 为每个 Geometry 创建了一个路径。
干杯
Is it possible to hand individual style functions to different Geometries of the same Feature?
没有
Leaflet 在 GeoJSON Feature
中创建一个 L.Layer
的实例,right over here; and the application of the style
callback function just loops over the L.Layer
s corresponding to each feature,它不会向下钻取嵌套的 L.LayerGroup
s(另外,L.Polyline
s 和 L.Polygon
s 作为 GeometryCollection
的结果创建,没有对原始 GeoJSON 特征及其属性的引用)。
我想您会想要使用 TurfJS' geomEach
(或类似的东西)来预处理您的特征和几何图形。