7

I try to display an arrow on the map using R and the leaflet package,

code of map see my related question:

# install packages
install.packages(c("rgdal", "maptools", "leaflet", "htmlwidgets"), dependencies = TRUE)

# load libraries
library("rgdal")
library("maptools")
library("leaflet")
library("htmlwidgets")

# load france as shape
FRA=readShapePoly("data/france.shp")

# Initialsation
m <- leaflet(padding = 0)

# Add country
m <- addPolygons(map = m, data = FRA, opacity = 100, 
                 color = "#FAFCFA", 
                 weight = 0.25,popup = NULL,
                 options = list(clickable = FALSE), 
                 fill = T, fillColor = "#B3C4B3", 
                 fillOpacity = 100)

# Dimention of the map
m$width <- 874
m$height <- 700

# Export as HTML file
saveWidget(m, 'mapfrance.html', selfcontained = FALSE)

code of arrow :

x<-c(2.484722,-8.445833)
y<-c(51.5085300,10.50306)
i <- order(x, y); x <- x[i]; y <- y[i]
## draw arrows from point to point :
s <- seq(length(x)-1)  # one shorter than data
plot(FRA)
points(x,y)
arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3,angle = 20)

how can I display a simple arrow on the map?

2

You can't just write the r plots into your map output. Check leaflet for R for more details.

The best solution would be to use the json arrow as said from @nebi, but I also don't know how to implement it into r.


Another solution is to write a function which returns you the coordinates of an arrowhead and then just draw some lines (see example below). The arrowhead doesn't look too nice 'cuz of the use of longitudes and latitudes for the calculation. But anyway it works.

# install packages
install.packages(c("rgdal", "maptools", "leaflet", "htmlwidgets"), dependencies = TRUE)

# load libraries
library("rgdal")
library("maptools")
library("leaflet")
library("htmlwidgets")

# function get arrowhead() returns coordinates of a the arrowhead of a line
get_arrowhead <- function (fromPoint, toPoint){

  # dx,dy = arrow line vector
  dx <- toPoint$x - fromPoint$x;
  dy <- toPoint$y - fromPoint$y;

  # normalize
  length <- sqrt(dx * dx + dy * dy);
  unitDx <- dx / length;
  unitDy <- dy / length;

  # increase this to get a larger arrow head
  arrowHeadBoxSize = 1;

  arrowPoint1 <- list(x = (toPoint$x - unitDx * arrowHeadBoxSize - unitDy * arrowHeadBoxSize),
                      y = (toPoint$y - unitDy * arrowHeadBoxSize + unitDx * arrowHeadBoxSize));
  arrowPoint2 <- list(x = (toPoint$x - unitDx * arrowHeadBoxSize + unitDy * arrowHeadBoxSize),
                      y = (toPoint$y - unitDy * arrowHeadBoxSize - unitDx * arrowHeadBoxSize));

  return( mapply(c, arrowPoint1, toPoint, arrowPoint2) )

}

# load france as 
FRA=readShapePoly("france.shp")

m <- leaflet(padding = 0)

# Add country
m <- addPolygons(map = m, data = FRA, opacity = 100, 
                 color = "#FAFCFA", 
                 weight = 0.25,popup = NULL,
                 options = list(clickable = FALSE), 
                 fill = T, fillColor = "#B3C4B3", 
                 fillOpacity = 100)

x<-c(2.484722,-8.445833)
y<-c(51.5085300,10.50306)
i <- order(x, y); x <- x[i]; y <- y[i]

fromPoint <- list (x = x[1], y = y[1])
toPoint <- list (x = x[2], y = y[2])

# get coordinates of arrowhead
arrowhead <- get_arrowhead (fromPoint, toPoint)

# draw points
m <- addCircles(map = m, lng = x, lat = y, weight = 1, radius = 1000)

# draw polyline
m <- addPolylines(map = m, lng = x, lat = y ) # arrow line
m <- addPolylines(map = m, arrow_data[,"x"], lat = arrow_data[,"y"] ) # arrow head

# Dimention of the map
m$width <- 874
m$height <- 700

# Export as HTML file
saveWidget(m, 'mapfrance.html', selfcontained = FALSE)

map france with arrow example

  • @witich : how to add the angle to show the direction of arrows , it's possible? – zina_GIS Jan 2 '16 at 19:30
1

I can't add a comment, but you could create an icon and use addMarker

check this out : https://rstudio.github.io/leaflet/markers.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.