使用 Leaflet 绘制特定国家的地图。

6
我想要使用R中的leaflet包来绘制特定国家(如意大利、西班牙等)的地图。我已查看了使用setView()函数的基本示例,并尝试给出一个由两个值组成的向量作为纬度和经度参数:
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  setView(lng=c(46.00,48.00), lat=c(2.00,6.00), zoom = 4)
m  # Print the map (map is not centered on a country, it's just a test)

但是我永远无法在屏幕上看到特定的国家,就像这个函数的结果一样:

library(maps)
map('italy', fill = TRUE, col = 1:10)

最终,我只想通过在地图上标出它们的经纬度来指出一些要点。

是否有可能实现呢?或者maps包已经足够完成这个任务了(尽管我没有找到放大的方法)?

2个回答

5

maps包提供了shapefile数据作为顶点。据我所知,leaflet中没有类似的功能。因此,您需要从其他地方获取数据。这是我的建议:

# Get an Italy shapefile
download.file(url = 'http://biogeo.ucdavis.edu/data/diva/adm/ITA_adm.zip', 
              destfile = 'italy.zip')
unzip(zipfile = 'italy.zip')

# Load libraries
library(sp)
library(rgdal)
italy <- readOGR('ITA_adm0.shp')

library(leaflet)
leaflet(italy) %>%
  addPolygons() %>%
  addTiles()

这里输入图片描述

附加内容:

您可以通过以下代码查看组成意大利的顶点,这些顶点保存在maps包中。

library(maps)
italy <- map('italy', fill = TRUE, col = 1:10)
italy_coords <- cbind(italy$x, italy$y)
plot(italy_coords)

enter image description here


没问题,很高兴它有用! - JanLauGe

5

你可以直接使用从maps获取的多边形。当然,任何其他合适的来源都可以使用,就像@JanLauGe提到的那样。

在获取了特定国家的多边形后,您可以将其转换为SpatialPolygonsDataFrame并将其提供给Leafet。如果您只想显示感兴趣的区域,则还可以创建掩码。

自然地,在此之后,您可以使用标准的Leaflet方法(如addCircleMarkers( lng, lat ))轻松添加任何点或标记。

library(ggmap)
library(leaflet)
library(magrittr)
library(maps)
library(maptools)
library(raster)
library(rgeos)
library(sp)

country   <- 'italy';
zoomLevel <- 6;

# Get the map ( class is map )
ita.map <- map( country, fill = TRUE, col = 1, plot = F );

# Get the geo center for lazyness
ita.center <- geocode( "italy" );

# Extract the names from ita.map.
# e.g. "Trapani:I. Le Egadi:I. Marettimo" -> "Trapani"
# note: any other solution is fine, because we don't really need them, but they
# can be useful later
ita.map.ids <- sapply( strsplit( ita.map$names, ':' ), function(x) x[1] );
# Convert our map object to SpatialPolygons
ita.sp <- map2SpatialPolygons( ita.map, IDs=ita.map.ids,
    proj4string=CRS("+proj=longlat +datum=WGS84"))

# Note: if you only need a unified polygon, it can be achieved by fortify
# ita.sp.df <- fortify( ita.sp );

# Finally convert our SpatialPolygons to SpatialPolygonsDataFrame
tmp.id.df <- data.frame( ID = names(ita.sp) );
rownames( tmp.id.df ) <- names( ita.sp );
ita.spdf <- SpatialPolygonsDataFrame( ita.sp, tmp.id.df );

# Visualize
l.ita.map <- leaflet( ita.spdf ) %>% 
    setView(lng = ita.center$lon, lat = ita.center$lat, zoom = zoomLevel ) %>%
    addTiles() %>%
    addPolygons( data = ita.spdf, weight = 1, fillColor = "blue", fillOpacity = 0.5 );

l.ita.map

Map of Italy from from ploygons

####### Alternatively if a mask if needed #######

# Get a plane of the world
wld.sp <- rasterToPolygons( raster(ncol = 1, nrow = 1, crs = proj4string(ita.sp) ) );
# Cut our country polygon from the plane to get our target mask
ita.sp.mask <- gDifference( wld.sp, ita.sp );

# Convert our ita.sp.mask (SpatialPolygons) to SpatialPolygonsDataFrame
tmp.id.df <- data.frame( ID = "1" );
rownames( tmp.id.df ) <- names( ita.sp.mask );
ita.mask.spdf <- SpatialPolygonsDataFrame( ita.sp.mask, tmp.id.df );

# Coordinates of Rome
ita.rome.center <- geocode( "Rome, italy" );

# Visualize
l.ita.mask.map <- leaflet( ita.mask.spdf ) %>% 
    setView( lng = ita.center$lon, lat = ita.center$lat, zoom = zoomLevel ) %>%
    addTiles() %>%
    addPolygons( data = ita.mask.spdf, fillColor = "white", fillOpacity = 1.0, color = "black", weight = 1 ) %>%
addCircleMarkers(lng = ita.rome.center$lon, lat = ita.rome.center$lat );

l.ita.mask.map;

Masked map of Italy from from ploygons

感谢@fdetsch提供的建议


非常好的扩展回答,谢谢!我之前不知道map2SpatialPolygons函数。 - JanLauGe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接