[英]Add polygon to map R leaflet
I am trying to display a map with R using the rCharts
package. 我正在尝试使用
rCharts
包显示带有R的地图。 I am starting simple and so I want to add a polygon to my map. 我从简单开始,所以我想向地图添加一个多边形。 But I have no clue how.
但是我不知道如何。 Any ideas?
有任何想法吗?
addPolygon
doesnt work. 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
Add the polygon to your map by converting to geoJSON format, as in example 10 in the rCharts source code: https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/examples/example10.R 通过转换为geoJSON格式将多边形添加到地图中,如rCharts源代码中的示例10: https : //github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/examples/example10.R
Note how lat and long are different between the xy coords in the geoJSON and the setView. 请注意geoJSON和setView中的xy坐标之间的纬度和经度有何不同。 This here code gives me a blue box in the Czech Republic close to Germany.
此处的代码给了我一个靠近德国的捷克共和国的蓝框。
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.
If you have more than one polygon, you need to create several structures of the {"type": "Feature",
and comma-separate them within the square brackets of the "features"
of the "FeatureCollection"
. 如果您有多个多边形,则需要创建
{"type": "Feature",
多个结构{"type": "Feature",
并在"FeatureCollection"
的"features"
的方括号内用逗号分隔。 I've re-indented things a bit to show the structure better. 为了使结构更好,我重新缩进了一点。 Its getting to the point where a templating system like the
brew
package is going to help you... 到
brew
包装等模板系统将帮助您的地步...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.