Leaflet turp.js pointsWithinPolygon-创建可用数组
我已经阅读了turp.js pointsWithinPolygon上的docu,并理解它需要一个数组。我知道我想要完成什么,但我不确定如何正确地转换我的L.geoJSON层以满足数组条件。请原谅这种奇怪的格式,因为我最近一直在玩很多,已经失去了一些结构 我的观点是:Leaflet turp.js pointsWithinPolygon-创建可用数组,leaflet,buffer,turfjs,Leaflet,Buffer,Turfjs,我已经阅读了turp.js pointsWithinPolygon上的docu,并理解它需要一个数组。我知道我想要完成什么,但我不确定如何正确地转换我的L.geoJSON层以满足数组条件。请原谅这种奇怪的格式,因为我最近一直在玩很多,已经失去了一些结构 我的观点是: var employees2 = L.geoJSON(); Papa.parse('src/data1/Employees.csv', { h
var employees2 = L.geoJSON();
Papa.parse('src/data1/Employees.csv', {
header: true,
download: true,
dynamicTyping: true,
skipEmptyLines: true,
complete: function(results) {
results.data.forEach((employee) => {
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [employee.Long, employee.Lat]
},
"properties": {
"Postal Code": employee.Pcode
}
}
mrkEmployees = L.geoJSON(feature, {
pointToLayer: function (feature, latlng){
return L.marker(latlng, {icon: redcircle});
}
}).addTo(employees2)
mrkEmployees.bindPopup(employee.Pcode)
})
}
});
我的多边形(即草皮缓冲区)是:
让我困惑的是如何提取数组,因为我的许多属性都是在解析的CSV代码块中定义的,因此在尝试调用坐标时会产生错误
边走边学。在成功定义pointsWithinPolygon之后,我将尝试将所述点导出(外部保存)为层,以防上下文有所帮助
一如既往地感谢——这个社区非常慷慨,非常乐于助人
编辑-参见文件
不要将该功能添加到GeoJSON组,而是将该组再次添加到GeoJSON组。一次就够了
var employeesData = L.geoJSON(null,{
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {icon: redcircle});
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties["Postal Code"])
}
}).addTo(map);
...
...Papa loop
results.data.forEach((employee) => {
var feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [employee.Long, employee.Lat]
},
"properties": {
"Postal Code": employee.Pcode
}
};
employeesData.addData(feature);
})
缓冲区也是如此(我将数据和缓冲区分成了不同的组):
然后可以检查缓冲区中的点是否:
(我不知道第一个函数是否工作,如果不工作,您必须检查每个缓冲区中是否有点getPointsInPolygonForEachBuffer
)
完整代码:
var employeesData=L.geoJSON(null{
pointToLayer:功能(特性、latlng){
返回L.marker(latlng,{icon:redcircle});
},
onEachFeature:功能(功能,图层){
layer.bindPopup(feature.properties[“邮政编码”])
}
}).addTo(地图);
var housesData=L.geoJSON(空{
onEachFeature:功能(功能,图层){
layer.bindPopup(feature.properties.Location)
}
}).addTo(地图);
var buffersData=L.geoJSON(null{
风格:功能(特征){
返回feature.properties.Type=='Duplex'?{color:“blue”}:feature.properties.Type=='Quadplex'?{color:“yellow”}:{color:“red”}
},
onEachFeature:功能(功能,图层){
层绑定弹出窗口(“5km缓冲区”)
}
}).addTo(地图);
parse('src/data1/Employees.csv'{
标题:对,
下载:对,
动态打字:对,
Skipeptylines:没错,
完成:功能(结果){
results.data.forEach((员工)=>{
变量特征={
“类型”:“功能”,
“几何学”:{
“类型”:“点”,
“坐标”:[employee.Long,employee.Lat]
},
“财产”:{
“邮政编码”:employee.Pcode
}
};
employeesData.addData(功能);
})
}
});
//解析本地CSV文件
parse('src/data1/Houses.csv'{
标题:对,
下载:对,
动态打字:对,
Skipeptylines:没错,
完成:功能(结果){
结果.数据.forEach((房屋)=>{
变量特征={
“类型”:“功能”,
“几何学”:{
“类型”:“点”,
“坐标”:[房屋.经度,房屋.纬度]
},
“财产”:{
“地点”:房子,地点,
“类型”:房子。类型
}
};
添加数据(特征);
addData(turf.buffer(特征,5,{units:'km'}));
})
}
});
var点缓冲区;
函数getPointsInPolygon(){
var ptsWithin=turp.pointsWithinPolygon(employeesData.toGeoJSON(),buffersData.toGeoJSON());
pointsInBuffer=L.geoJSON(ptsWithin).addTo(map);
}
函数getPointsInPolygonForEachBuffer(){
pointsInBuffer=L.geoJSON().addTo(map);
buffersData.eachLayer((层)=>{
var ptsWithin=turp.pointsWithinPolygon(employeesData.toGeoJSON(),layer.toGeoJSON());
pointsInBuffer.addData(ptsWithin);
})
}
在房屋加载功能中,您说要加载多边形,但创建了点
功能,这将不起作用。你能分享你的数据和结果吗?我试着创建一个jfiddle,但是有太多的插件和数据被解析。不过,我可以将js代码的主要部分包括在内。@FalkeDesign-我已经更新了文章的底部,添加了脚本链接和它的屏幕截图。解析的CSV只是lat/long+一个标识符列。我只是想弄清楚如何从这些数据中定义一个数组,并将其输入到turp.js中,以创建属于每个缓冲区的点列表。非常感谢@falke design。我完全按照您的布局重新安排了代码,当划分为多个部分时,所有内容都更加清晰。不幸的是,我认为pointsInPolygon函数不起作用。我尝试了console.log以查看这些变量是否填充了任何数据,但始终没有定义。我还尝试对“pointsInBuffer”应用不同的样式,看看它们是否出现,但不起作用,也没有错误。在演示中,它的工作原理与我描述的完全相同。我最初没有看到代码中的按钮部分。这很有魅力。再次感谢大家,,
var employeesData = L.geoJSON(null,{
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {icon: redcircle});
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties["Postal Code"])
}
}).addTo(map);
...
...Papa loop
results.data.forEach((employee) => {
var feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [employee.Long, employee.Lat]
},
"properties": {
"Postal Code": employee.Pcode
}
};
employeesData.addData(feature);
})
var housesData = L.geoJSON(null,{
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.Location)
}
}).addTo(map);
var buffersData = L.geoJSON(null,{
style: function (feature) {
return feature.properties.Type === 'Duplex' ? { color: "blue" } : feature.properties.Type === 'Quadplex' ? { color: "yellow" } : { color: "red"}
},
onEachFeature: function (feature, layer) {
layer.bindPopup("5km Buffer")
}
}).addTo(map);
... Papa loop
housesData.addData(feature);
buffersData.addData(turf.buffer(feature, 5, {units: 'kilometers'}));
var pointsInBuffer;
function getPointsInPolygon(){
var ptsWithin = turf.pointsWithinPolygon(employeesData.toGeoJSON(), buffersData.toGeoJSON());
pointsInBuffer = L.geoJSON(ptsWithin).addTo(map);
}
function getPointsInPolygonForEachBuffer(){
pointsInBuffer = L.geoJSON().addTo(map);
buffersData.eachLayer((layer)=>{
var ptsWithin = turf.pointsWithinPolygon(employeesData.toGeoJSON(), layer.toGeoJSON());
pointsInBuffer.addData(ptsWithin);
})
}