gpt4 book ai didi

javascript - 如何使用一个 L.Shapefile/zip 文件对象但更改每一层的 onEachFeature?

转载 作者:行者123 更新时间:2023-11-28 16:52:39 25 4
gpt4 key购买 nike

我当前遇到的问题是我有多个包含形状文件的tileLayer。每个图 block 层代表基于某些变量的不同数据集,并相应地改变颜色。

如果我创建三个单独的对象(L.shapefile(“”,{})...)并将每个对象放入其相应的图层中,我就可以使其工作。问题是我的 shapefile 相当大,有 12MB。当我这样做时,它会下载文件 3 次。

我如何下载一次 zip 文件,然后从该下载中创建 3 个实例?

    shpfile = new L.Shapefile('/mydata/shpfile.zip', {
onEachFeature: function (feature, layer) {
console.log(feature, layer)
if (feature.properties) {
var data_name = feature.properties.Name;
val = -1
if (data)
val = data[days+'DAY']
if (val==-1)
val="No Data";
var tooltip_Html = data_name + "<hr>" + days + " " + val;
layer.bindPopup(tooltip_Html, {
maxHeight: 200
});
layer.prop = {data_name , days};
layer.setStyle({fillColor : getColor(val),
color: "black",
weight: 1,
opacity: 1,
fillOpacity: 0.3
});
layer.on("click", layerClick);
if (vw>768) {
layer.on("mouseover", function () {
info.update(layer.prop);
layer.bindTooltip('<b>'+layer.prop.loc + '</b> Name<br>' + layer.prop.val);
});
layer.on("mouseout", function () {
info.update();
});
}
}
}});

shpfile.once("data:loaded", function() {
console.log("Shape File Downloaded! "+ days + ' days');
/* Tried this method of creating oneachfeature dynamically
shpfile.onEachFeature(function(feature, layer) {
var layerName;
});

*/
});
L.tileLayer(mapboxUrl, {id: 'mapbox.streets', attribution: mbAttr}).addTo(shpfile)```

最佳答案

让我引用Leaflet.shapefile readme :

usage:

new L.Shapefile(arrayBuffer or url[,options][,importUrl]);

L.shapefile(arrayBuffer or url[,options][,importUrl]);

因此,人们可以提供 shapefile 的 URL, ArrayBuffer包含压缩 shapefile 的内容。

现在的问题是:如何创建外部文件的arrayBuffer?有几种方法,但我偏向 fetch APIResponse.arrayBuffer()功能,例如

fetch(url)
.then(function(response){
return response.arrayBuffer();
}).then(function(buf){
L.shapefile(buf, options1).addTo(map);
L.shapefile(buf, options2).addTo(map);
L.shapefile(buf, options3).addTo(map);
});

关于javascript - 如何使用一个 L.Shapefile/zip 文件对象但更改每一层的 onEachFeature?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com