将多边形添加到地图R传单 [英] Add polygon to map R leaflet

查看:88
本文介绍了将多边形添加到地图R传单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rCharts包显示带有R的地图.我从简单开始,所以我想在地图上添加一个多边形.但是我不知道如何.有任何想法吗? addPolygon不起作用.

I am trying to display a map with R using the rCharts package. 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.

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

推荐答案

通过转换为geoJSON格式将多边形添加到地图中,如rCharts源代码中的示例10:

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和setView中的xy坐标之间的纬度和经度有何不同.此代码为我在靠近德国的捷克共和国提供了一个蓝框.

Note how lat and long are different between the xy coords in the geoJSON and the setView. 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.

如果有多个多边形,则需要创建{"type": "Feature",的多个结构,并将它们用逗号分隔在"FeatureCollection""features"的方括号内.我已经重新缩进一些东西,以更好地显示结构.到了像brew软件包这样的模板系统将帮助您的地步...

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". 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...

这篇关于将多边形添加到地图R传单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆