如何使用一个 L.Shapefile/zip 文件对象,但更改每一层的 onEachFeature?
How to use one L.Shapefile/zip file Object but change the onEachFeature for each layer?
我目前遇到的问题是我有多个包含形状文件的 tileLayer。每个图块层代表一个基于某些变量的不同数据集,并相应地改变颜色。
如果我创建三个单独的对象 (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]);
因此可以将 URL 提供给 shapefile, 或 ArrayBuffer
以及压缩 shapefile 的内容。
那么现在的问题是:如何创建一个arrayBuffer
的外部文件?有几种方法,但我偏爱 fetch
API and the Response.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);
});
我目前遇到的问题是我有多个包含形状文件的 tileLayer。每个图块层代表一个基于某些变量的不同数据集,并相应地改变颜色。
如果我创建三个单独的对象 (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]);
因此可以将 URL 提供给 shapefile, 或 ArrayBuffer
以及压缩 shapefile 的内容。
那么现在的问题是:如何创建一个arrayBuffer
的外部文件?有几种方法,但我偏爱 fetch
API and the Response.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);
});