使用 R 在传单地图上显示 SpatialPolygonsDataFrame [英] Display SpatialPolygonsDataFrame on leaflet map with R

查看:13
本文介绍了使用 R 在传单地图上显示 SpatialPolygonsDataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在传单地图上显示加拿大的多边形.

# 创建地图图书馆(传单)m = 传单() %>% addTiles()米

我找到了加拿大的多边形: 提供的轻量级多边形数据集.这是一个示例,我从 Natural Earth 下载了 1:50,000,000 个国家的 shapefile(Admin 0),将其子集到英联邦的当前成员,然后绘制它们.压缩后的 shapefile 小于 1 MB.

库(rgdal)图书馆(传单)download.file(file.path('http://www.naturalearthdata.com/http/','www.naturalearthdata.com/download/50m/culture','ne_50m_admin_0_countries.zip'),f <-临时文件())解压缩(f, exdir=tempdir())世界 <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh",巴巴多斯"、伯利兹"、博茨瓦纳"、文莱"、喀麦隆"、加拿大"、塞浦路斯"、多米尼克"、斐济"、加纳"、格林纳达"、圭亚那"、印度"、牙买加"、肯尼亚"、基里巴斯"、莱索托"、马拉维"、马来西亚"、马尔代夫"、马耳他"、毛里求斯"、莫桑比克"、纳米比亚"、瑙鲁"、新西兰"、尼日利亚"、巴基斯坦"、巴布亚"新几内亚"、卢旺达"、圣.圣基茨和尼维斯"、圣卢西亚"、圣.文.和格林."、萨摩亚"、塞舌尔"、塞拉利昂"、新加坡"、所罗门群岛"、南非"、斯里兰卡"、斯威士兰"、坦桑尼亚"、汤加"、特立尼达和多巴哥"、图瓦卢"、乌干达"、英国"、瓦努阿图"、赞比亚")传单() %>% addTiles() %>%addPolygons(data=subset(world, name %in% commonwealth), weight=2)

I would like to display the polygon of Canada on a leaflet map.

# create map
library(leaflet)
m = leaflet() %>% addTiles()
m

I was able to find the polygon for Canada: http://www.gadm.org/country. I chose the SpatialPolygonsDataFrame format for R, but there are other formats available (such as Shapefile)

# load object in R
load("country_polygons/CAN_adm0.RData")
pol_can <- gadm

How can I display the shape on the map? I assume I have to leverage the sp package but I could not figure out how to do so. Many thanks in advance for your help!

解决方案

You can pass a SpatialPolygons* object to the addPolygons function as per Section 2.2 of the docs here.

For example (note that the following includes a ~11.4 MB download):

library(sp)
download.file('http://biogeo.ucdavis.edu/data/gadm2/R/CAN_adm0.RData', f <- tempfile())
load(f)
leaflet() %>% addTiles() %>% addPolygons(data=gadm, weight=2)

Note that GADM data can also be downloaded with the getData function in the raster package:

library(raster)
can <- getData('GADM', country='VAT', level=0)


EDIT

In response to the comments, I really like the lightweight polygon datasets that Natural Earth provides. Here's an example where I download the 1:50,000,000 countries shapefile (Admin 0) from Natural Earth, subset it to the current members of the Commonwealth of Nations, and plot those. The zipped shapefile is under 1 MB.

library(rgdal)
library(leaflet)

download.file(file.path('http://www.naturalearthdata.com/http/',
                        'www.naturalearthdata.com/download/50m/cultural',
                        'ne_50m_admin_0_countries.zip'), 
              f <- tempfile())
unzip(f, exdir=tempdir())

world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')

commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh", 
  "Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus",
  "Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya",
  "Kiribati", "Lesotho", "Malawi", "Malaysia", "Maldives", "Malta", "Mauritius",
  "Mozambique", "Namibia", "Nauru", "New Zealand", "Nigeria", "Pakistan", "Papua
  New Guinea", "Rwanda", "St. Kitts and Nevis", "Saint Lucia", "St. Vin. and
  Gren.", "Samoa", "Seychelles", "Sierra Leone", "Singapore", "Solomon Is.",
  "South Africa", "Sri Lanka", "Swaziland", "Tanzania", "Tonga", "Trinidad and
  Tobago", "Tuvalu", "Uganda", "United Kingdom", "Vanuatu", "Zamibia")

leaflet() %>% addTiles() %>% 
  addPolygons(data=subset(world, name %in% commonwealth), weight=2)

这篇关于使用 R 在传单地图上显示 SpatialPolygonsDataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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