Hen*_*Zhu 5 javascript algorithm jquery json mapbox
我正在尝试更改图层中value某个功能(基于)的属性() 。id我无法使用 setFeatureState,因为我需要根据属性对功能进行聚类value,并且clusterProperties不支持功能状态聚合。我当前用于设置value具有特定功能的属性的方法id如下(每 2 秒,我迭代GeoJSON 中的每个功能并设置属性):
setTimeout(() => {\n const newGeojson = {\n ...geojson,\n features: geojson.features.map(feature => {\n if ([0, 1].includes(feature.id)) { // update features selectively\n feature.properties.value= 1;\n }\n return feature;\n })\n };\n map.getSource(\'geom\').setData(newGeojson);\n}, 2000);\nRun Code Online (Sandbox Code Playgroud)\n\n这是非常低效的,时间复杂度为 O(nk),k 是我想要更改的特征数量\xe2\x80\x94我有大约130,000 个特征。事实上,它导致我的浏览器崩溃。我尝试使用mapbox-gl-js的MapboxDraw(mapbox-gl-js有一个setFeatureProperty方法),但是加载130,000个功能需要太长时间(使浏览器崩溃),所以这不能作为一个选项。是否有更可行的选择,我可以在运行时设置功能的属性,而无需花费很长时间?
我的 GeoJSON (geojson变量)具有以下格式(Mapbox需要 GeoJSON 数据):
{ \n "type":"FeatureCollection",\n "features":[ \n { \n "type":"Feature",\n "id":0,\n "geometry":{ \n "type":"Point",\n "coordinates":[ \n 1.49129,\n 42.46372\n ]\n },\n "properties":{ \n "country":"AD",\n "city":"Sant Juli\xc3\xa0 de L\xc3\xb2ria-0",\n "value":0\n }\n },\n { \n "type":"Feature",\n "id":1,\n "geometry":{ \n "type":"Point",\n "coordinates":[ \n 1.73361,\n 42.54277\n ]\n },\n "properties":{ \n "country":"AD",\n "city":"Pas de la Casa-1",\n "value":0\n }\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n
看起来您需要的是直接访问功能,而不是mapping 所有功能来仅更改一些功能。类似辅助 JavaScript 对象的东西,将功能id作为键,允许您访问该功能以更改其value:
featureById = {}
geojson.features.forEach(f => featureById[f.id] = f) // only once
Run Code Online (Sandbox Code Playgroud)
然后您可以执行此操作(相当于您的示例代码):
featureById[0].properties.value = 1
featureById[1].properties.value = 1
Run Code Online (Sandbox Code Playgroud)
您的原始代码会为每次更改构建一系列新的功能。当然,它是一个指针数组,但它仍然是一个大数组。这样你只需构建featureById一次。每次改变都是 O(k)。
| 归档时间: |
|
| 查看次数: |
2125 次 |
| 最近记录: |