绘制传单图层与加载传单图层时,Geojson 结构不同



我的 Web 应用程序让用户使用 Leaflet.Draw 控件绘制形状以及从 geojson 文件加载形状。但是,似乎这两种方法都会生成不同的 geojson 对象。

当我从 geojson 加载形状时。

{  
"options":{  
},
"_layers":{  
"2998":{  
"options":{  
"pane":"overlayPane",
"attribution":null,
"bubblingMouseEvents":true,
"fill":true,
"smoothFactor":1,
"noClip":false,
"stroke":true,
"color":"#3388ff",
"weight":3,
"opacity":1,
"lineCap":"round",
"lineJoin":"round",
"dashArray":null,
"dashOffset":null,
"fillColor":null,
"fillOpacity":0.2,
"fillRule":"evenodd",
"interactive":true
},
"_bounds":{  
"_southWest":{  
"lat":3.668372,
"lng":101.873785
},
"_northEast":{  
"lat":3.670492,
"lng":101.875974
}
},
"_latlngs":[  
[  
{  
"lat":3.668372,
"lng":101.873785
},
{  
"lat":3.670492,
"lng":101.873785
},
{  
"lat":3.670492,
"lng":101.875974
},
{  
"lat":3.668372,
"lng":101.875974
}
]
],
"_initHooksCalled":true,
"_events":{  
"revert-edited":[  
{  
"ctx":{  
"latlngs":[  
null
],
"_initHooksCalled":true
}
}
],
"add":[  
{  
}
],
"remove":[  
{  
}
]
},
"feature":{  
"properties":{  
"Name":"rectangle-xugcym9x4i",
"Description":"",
"Tag":"",
"id":"rectangle-xugcym9x4i"
},
"geometry":{  
"type":"Polygon",
"coordinates":[  
[  
[  
101.873785,
3.668372
],
[  
101.873785,
3.670492
],
[  
101.875974,
3.670492
],
[  
101.875974,
3.668372
],
[  
101.873785,
3.668372
]
]
]
},
"type":"Feature"
},
"defaultOptions":{  
"pane":"overlayPane",
"attribution":null,
"bubblingMouseEvents":true
},
"_leaflet_id":2998,
"_eventParents":{  
}
}
},
"_leaflet_id":2999,
"_initHooksCalled":true
}leaflet.js:1125:17

当我用传单画形状时。

{  
"options":{  
"stroke":true,
"color":"#3388ff",
"weight":4,
"opacity":0.5,
"fill":true,
"fillColor":null,
"fillOpacity":0.2,
"clickable":true
},
"_bounds":{  
"_southWest":{  
"lat":3.668382508900863,
"lng":101.87454700469972
},
"_northEast":{  
"lat":3.6702567212235673,
"lng":101.87684297561646
}
},
"_latlngs":[  
[  
{  
"lat":3.668382508900863,
"lng":101.87454700469972
},
{  
"lat":3.6702567212235673,
"lng":101.87454700469972
},
{  
"lat":3.6702567212235673,
"lng":101.87684297561646
},
{  
"lat":3.668382508900863,
"lng":101.87684297561646
}
]
],
"_initHooksCalled":true,
"_events":{  
"revert-edited":[  
{  
"ctx":{  
"latlngs":[  
null
],
"_initHooksCalled":true
}
}
],
"add":[  
{  
}
],
"remove":[  
{  
}
]
},
"editing":{  
"options":{  
},
"_initHooksCalled":true
},
"feature":{  
"properties":{  
"Name":"rectangle-9d3jtxlr99",
"Description":"",
"Tag":"",
"id":"rectangle-9d3jtxlr99"
}
}
}    

由于这两种形状都是 Leaflet geojson 形状,我希望它们具有相同的数据结构。由于绘制的形状和加载的形状具有不同的结构,因此我无法始终如一地为它们编制索引以获取所需的属性。我的函数将适用于一种类型的形状,但不适用于另一种形状。

此外,我可以使用 Leaflet.draw 控件编辑绘制的图层,但不能编辑加载的图层。有没有办法标准化绘制和加载形状的 geojson 结构?

两种形状都是 Leaflet geojson 形状

从您显示的数据结构来看,第一个对应于传单 GeoJSON 图层,而第二个对应于传单矩形矢量图层。

如果我们仔细观察,我们会在第一个"_layers"字典对象中找到与第二个类似的结构,它显示了 Leaflet 如何在图层组中嵌套各个图层。

如果您尝试从您绘制的图层中获取 GeoJSON 数据(例如,获得它们的"几何"(,那么只需对它们使用toGeoJSON()方法即可。

我可以使用 Leaflet.draw 控件编辑绘制的图层,但不能编辑加载的图层。

您可能会尝试将第一个图层组直接嵌套到您提供给edit.featureGroup选项的图层组中L.Control.Draw.在这种情况下,请参阅传单绘制"无法读取未定义的属性'启用'"将控件添加到 geoJSON 层

最新更新