在OpenLayers 5.3.0中,我使用turf.js中的“difference”工具创建了一个MultiPolygon。当我检查JSON时,turf.js MultiPolygon看起来很好,但是当我尝试用它在OpenLayers中创建一个特性时,我得到了“未捕获TypeError: t.addEventListener不是一个函数”。
我尝试过许多JSON.stringify、JSON.parse、GeoJSON.readFeatures、.getCoordinates()的组合...我试着直接通过source.addFeature(multiPolygonGeometry)将turf.js MultiPolygon添加为一个功能,但是我得到了'Uncaught : e.getId不是一个函数‘。我也尝试了source.addFeatures(multiPolygonGeometry) (注意复数“addFeatures”),这并没有给我带来任何错误,但似乎也没有给源代码增加任何东西。
我的代码中的相关行如下:
multiPolygonGeometry = turf.difference(largeArea,maskAreas);
multiPolygonFeature = new ol.Feature({
geometry: multiPolygonGeometry,
id: 'multiPolygonFeature1'
});控制台中的multiPolygonGeometry如下所示:
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
140.9716711384525,
-36.97645228850101
],
[
140.97418321565786,
-36.97679331852701
],
[
140.9741163253784,
-36.97713531664132
],
[
140.9740304946899,
-36.97903805606076
],
[
140.97437381744382,
-36.98025509866784
],
[
140.97594864874696,
-36.98127512642501
],
[
140.9714880598484,
-36.9804459718428
],
[
140.9714500775476,
-36.97642733756345
],
[
140.9716711384525,
-36.97645228850101
]
]
],
[
[
[
140.97455248763328,
-36.97684309230892
],
[
140.97751071844857,
-36.97723786980259
],
[
140.97749308140382,
-36.977304276099005
],
[
140.97715289421623,
-36.97770848336402
],
[
140.97661807025068,
-36.97969050789806
],
[
140.97628355026242,
-36.97958658471583
],
[
140.97634792327878,
-36.97900377288852
],
[
140.9764981269836,
-36.97866094031662
],
[
140.97510337829587,
-36.97727245260485
],
[
140.97455248763328,
-36.97684309230892
]
]
],
[
[
[
140.97628420893903,
-36.98092777726751
],
[
140.97617893060388,
-36.98131793226549
],
[
140.97596635572492,
-36.98127841787872
],
[
140.97628420893903,
-36.98092777726751
]
]
]
]
},
"ol_lm": {
"change": []
}
}然后我得到了这样的信息:
events.js:174 Uncaught TypeError: t.addEventListener is not a function
at v (events.js:174)
at e.handleGeometryChanged_ (Feature.js:210)
at e (events.js:41)
at e.dispatchEvent (Target.js:101)
at e.notify (Object.js:151)
at e.set (Object.js:170)
at e.setProperties (Object.js:186)
at new e (Feature.js:108)
at getPolygon (maskedPolygon.js:319) <-- this is the second line in my code sample above
at <anonymous>:1:1我在这里做错了什么?我相信这是很简单的东西,但我就是不能破解它。
发布于 2019-10-13 19:49:25
Turf使用GeoJSON特性,因此您的"multiPolygonGeometry“是一个GeoJSON特性,可以由OpenLayers解析,然后给出一个Id:
multiPolygonFeature = new ol.format.GeoJSON().readFeature(multiPolygonGeometry);
multiPolygonFeature.setId('multiPolygonFeature1');https://stackoverflow.com/questions/58363062
复制相似问题