我正在尝试使用rCharts包显示R的地图.我开始简单,所以我想在地图上添加一个多边形.但我不知道如何.有任何想法吗?addPolygon不起作用.
map <- Leaflet$new()
map$tileLayer(provider = 'Stamen.TonerLite')
map$setView(c(48.1, 16.7), zoom = 10)
map$addPolygon(
c(48.99831, 49.08815, 49.08815, 48.99831, 48.99831),
c(13.42666, 13.42666, 13.56383, 13.56358, 13.42666),
layerId=c("1"),
options=opts,
defaultOptions=opts)
map
Run Code Online (Sandbox Code Playgroud)
通过转换为geoJSON格式将多边形添加到地图中,如rCharts源代码中的示例10所示:https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/examples/example10.R
请注意geoJSON和setView中xy坐标之间的lat和long是如何不同的.这里的代码给了我一个捷克共和国附近的蓝色盒子.
xy = cbind(
c(13.42666, 13.42666, 13.56383, 13.56358, 13.42666),
c(48.99831, 49.08815, 49.08815, 48.99831, 48.99831)
)
xyjson = RJSONIO::toJSON(xy)
jsonX = paste(
'{"type":"FeatureCollection","features":[
{"type":"Feature",
"properties":{"region_id":1, "region_name":"My Region"},
"geometry":{"type":"Polygon","coordinates": [ ',xyjson,' ]}}
]
}')
polys = RJSONIO::fromJSON(jsonX)
map = Leaflet$new()
map$tileLayer(provider = 'Stamen.TonerLite')
map$setView(c(49.1,13.5), zoom = 8)
map$geoJson(polys)
map
# or print(map) from a script probably.
Run Code Online (Sandbox Code Playgroud)
如果你有一个以上的多边形,你需要创建的一些结构{"type": "Feature",和它们的方括号的中英文逗号分隔"features"的"FeatureCollection".我重新缩进了一些东西以更好地展示结构.它已经达到像brew包装一样的模板系统将帮助你......
| 归档时间: |
|
| 查看次数: |
2633 次 |
| 最近记录: |