在带有R的传单地图上显示SpatialPolygonsDataFrame [英] Display SpatialPolygonsDataFrame on leaflet map with R

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

问题描述

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

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

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

我能够找到加拿大的多边形: http://www.gadm.org/country. 我为R选择了SpatialPolygonsDataFrame格式,但还有其他可用格式(例如Shapefile)

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

如何在地图上显示形状?我假设我必须利用sp软件包,但是我不知道该怎么做. 在此先感谢您的帮助!

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!

推荐答案

您可以按照文档SpatialPolygons*对象传递给addPolygons函数. .io/leaflet/"rel =" noreferrer>此处.

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

例如(请注意,以下内容包括〜11.4 MB下载):

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)

请注意,GADM数据也可以通过raster软件包中的getData功能下载:

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)


编辑

作为对这些评论的回应,我非常喜欢自然地球" 提供的轻型多边形数据集.在下面的示例中,我从自然地球下载了1:50,000,000个国家的shapefile(Admin 0),并将其子集分配给当前的英联邦成员国,然后进行绘制.压缩的shapefile小于1 MB.

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天全站免登陆