用传单绘制特定国家的地图
Draw a map of a specific country with leaflet
我想用 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
是否足以完成这项任务(即使我没有找到放大的方法)?
maps
包将 shapefile 数据作为顶点发送。 Afaik 没有像这样的东西包含在传单中。因此,您将不得不从其他地方获取数据。这是我的建议:
# 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)
您可以只使用从 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
####### 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;
感谢@fdetsch for his suggestion!
我想用 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
是否足以完成这项任务(即使我没有找到放大的方法)?
maps
包将 shapefile 数据作为顶点发送。 Afaik 没有像这样的东西包含在传单中。因此,您将不得不从其他地方获取数据。这是我的建议:
# 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)
您可以只使用从 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
####### 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;
感谢@fdetsch for his suggestion!