有没有人有使用leaflet.js,turf.js和mapbox进行JavaScript映射的经验?我正在尝试使用草皮的并集函数合并从geoJSON文件提取的多个多边形系列。任何帮助将不胜感激!
turf.js联合命令:https://turfjs.org/docs/#union
我正在从GeoJSON文件中调用德克萨斯州的县数据,如下所示。我已经有代码以相同的颜色标记大城市中的县集-参见下文。我正在尝试使用turf.js的并集函数来转换每个集合(例如如下所示的Abilene的3个县和Amarillo的5个县)以为每组县创建统一的(多)多边形,包括消除内部边界和允许每个都被选择为一个单元来显示弹出窗口。
// Creating map object
var map = L.map("map", {
center: [31, -97],
zoom: 6
});
// Adding tile layer
L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", {
attribution: "Map data © <a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a> contributors, <a href=\"https://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, Imagery © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.streets",
accessToken: API_KEY
}).addTo(map);
var link = "https://raw.githubusercontent.com/TNRIS/tx.geojson/master/counties/tx_counties.geojson";
// Function that will determine the color of a metropolitan area based on the county it belongs to
function chooseColor(COUNTY) {
switch (COUNTY) {
//Abilene
case "Callahan County":
return "pink";
case "Jones County":
return "pink";
case "Taylor County":
return "pink";
//Amarillo
case "Armstrong County":
return "lightgreen";
case "Carson County":
return "lightgreen";
case "Oldham County":
return "lightgreen";
case "Potter County":
return "lightgreen";
case "Randall County":
return "lightgreen";
default:
return "black";
}
}
// Grabbing our GeoJSON data..
d3.json(link, function(data) {
// Creating a geoJSON layer with the retrieved data
L.geoJson(data, {
style: function(feature) {
return {
color: "white",
fillColor: chooseColor(feature.properties.COUNTY),
fillOpacity: 1,
weight: 1.5
};
}
}).addTo(map);
});
无法弄清楚turf.union命令的结构,希望获得帮助!