Draw a map of a specific country with a brochure

I would like to use the leaflet

R package to draw a map of specific countries like Italy, Spain, etc.

I checked basic examples with a function setView()

and I tried to give a vector of two values ​​for the latitude and longitutde argument:

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 just a test)

      

But I can never have a specific country on my screen like the result of this function:

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

      

In the end, I just want to draw some points by geographically placing them on my maps (with latitude and longitude)

Is this possible or is the package maps

suitable for the task (although I haven't found a way to zoom in)?

+3


source to share


2 answers


The package maps

submits the form file data as vertices. Afaik is not included in the leaflet. Therefore, you will have to get your data elsewhere. Here's my suggestion:

# 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()

      

enter image description here

ADD-ON:



You can look at the vertices making up italy as saved in the package maps

with the code below.

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

      

enter image description here

+2


source


You can just use polygons derived from maps

. Any other suitable source can of course be used, as @JanLauGe mentioned .

Once you have country-specific polygons, you can submit them to Leafet after converting them to SpatialPolygonsDataFrame

. You can also create a mask if you want to display only the area of ​​interest.

Naturally, after that you can easily add any point or marker with standard Leaflet methods, for example 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 with plugins

####### 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 Plejons

Thanks for @fdetsch for his suggestion

+3


source







All Articles