Given a bbox woth following values:
f1 <- base::data.frame("left" = 11.46067, "bottom" = 48.05556 ,"right" = 11.69851 ,"top" = 48.21477)
how could i calculate the center point (lat,lon) of this bbox?
I consider that the median should be used to find the center. I hope this code helps you.
library(leaflet)
f1 <- base::data.frame("left" = 11.46067, "bottom" = 48.05556 ,"right" = 11.69851 ,"top" = 48.21477)
point <- c(median(c(f1$left, f1$right)), median(c(f1$bottom, f1$top)))
leaflet() %>%
addTiles() %>%
addRectangles(lng1 = f1$left, lat1 = f1$bottom, lng2 = f1$right, lat2 = f1$top) %>%
addCircleMarkers(lng = point[1], lat = point[2])
I dont know why but the proposed solution does not work for me.
For example given the following bbox (11.360777 48.06162 11.722908 48.24812 ) and the following calculation : median(11.360777, 11.722908) and median(48.06162, 48.24812) accourding to suggestion; it produces a point far away from the center of the bbox.
What did work is to find an intersection of two diagonal lines drawn through the bbox:
geosphere::gcIntersect(p1 = c(xmin, ymin),
p2=c(xmax, ymax),
p3=c(xmax, ymin),
p4=c(xmin, ymax))
Related
I'm creating a flow map in R Leaflet which will eventually go through Shiny. This is a very simplified example of what it looks like:
How can I curve the flow lines?
Reproducible example:
library(leaflet)
library(leaflet.minicharts)
library(tidyverse)
dat <- data.frame(
Origin_lat = c(40.15212, 40.65027),
Origin_lng = c(-74.79037, -74.91990),
Dest_lat = c(40.78749, 40.78749),
Dest_lng = c(-73.96188, -73.96188),
flow = c(237, 84)
)
leaflet() %>%
addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
setView(lat = 40.39650, lng = -74.39541, zoom = 9) %>%
addFlows(
lng0 = dat$Origin_lng,
lat0 = dat$Origin_lat,
lng1 = dat$Dest_lng,
lat1 = dat$Dest_lat,
flow = dat$flow
)
There is a package geosphere which has the functions which you need
This should get you where you need to be, you can pass it either a matrix for start and stop with X,Y in each or a vector for each type of coordinate, as here
library(geosphere) #install as needed
gcIntermediate(c(Origin_lat,Origin_lng), c(Dest_lat,Dest_lng),
n=80, #how many curve points along lines
addStartEnd=TRUE,
sp=TRUE ) %>%
leaflet() %>%
addTiles() %>%
addPolylines()
If it were me, I would restructure the points to be able to pass: origin & destination as two-column matrices of Lat, Lon values, but it should work as vectors too.
I would like to be able to find the centre point between two markers on a map (example below). Is there a function in leaflet or in another package that allows this? Thank you in advance
coor_zone6 <- c(3.16680, 3.16176, 42.46667, 42.46997)
matrice_coord_zone6 <- matrix(coor_zone6, nrow=2, ncol = 2)
colnames(matrice_coord_zone6) <- c("long", "lat")
matrice_coord_zone6 <- data.frame(matrice_coord_zone6)
matrice_coord_zone6$name <- c("M_1","M_3")
leaflet(matrice_coord_zone6) %>%
addMouseCoordinates(epsg = NULL, proj4string = NULL, native.crs = FALSE) %>%
addProviderTiles("Esri.WorldImagery") %>%
addMarkers(lng = ~long, lat = ~lat) %>%
addPolylines(~long, ~lat, popup = ~name)
I have not found any leaflet function that can perform this calculation, but it is not difficult to find the intermediate point between both coordinates.
You must add both longitudes and divide them by 2, you will have to do the same with both latitudes.
In your case, if I have not misunderstood, your first coordinate is (3.16680, 42.46667) and your second coordinate is (3.16176, 42.46997) so the calculation would be as follows:
(3,16680 + 3,16176) / 2 = 3,16428
(42,46667 + 42,46997) / 2 = 42,46832
So the intermediate point would be the following: (3.16428, 42.46832)
I'm trying to add separate images to popups so that as you click on each location, an image specific to that place/popup appears. I've figured out how to get one image in, but it applies to all of the popups on the map instead of just one. I have been trying to use the package leafpop for this, but I can't really figure out how to make it work. Even if I just use one image, nothing appears on the map.
This is what my code looks like for it:
library(leaflet)
library(leafpop)
img = system.file("file/image_name.jpg", package = "jpg")
leaflet(map) %>%
addTiles() %>%
addCircleMarkers(label = map#data$name,
weight = 2,
color = "grey",
fillColor = "red",
fillOpacity = 0.7)%>%
addPopupImages(img, group = "map")
I know there's some bits in there that I'm not quite doing right. At this point, I just want to know if it's even possible to do this the way I'm envisioning. Any help is appreciated.
The images need to be in a vector of the same length as the points passed to leaflet. Here is a reproducible example you can copy paste that will get you started:
library(tidyverse)
library(sf)
library(leaflet)
library(leafpop)
pts <- tibble(x = runif(10, 175, 176),
y = runif(10, -38, -37)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
img <- glue::glue("https://github.com/IQAndreas/sample-images/blob/gh-pages/100-100-color/{11:20}.jpg?raw=true")
pts$img <- img
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = pts, group = "pts") %>%
addPopupImages(pts$img, group = "pts")
Figured it out, with the help of Rich Pauloo! This is the code I ended up using the get local image files. It's a little clunky, but it worked out for me:
data_name <- readOGR("data/map_file.kml")
data_name2 <- data.frame(data_name)
pts <- st_as_sf(data.frame(data_name2),
coords = c("coords.x1", "coords.x2"), crs = 4326)
img <- c("images/picture_name.jpg") ##did this for every image I wanted to use, in the order
##that matched up with the data points I wanted them associated with.
pts$img <- img
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = pts, group = "pts") %>%
addPopupImages(pts$img, group = "pts", width = 300)
Sorry if my conventions for writing out code are not quite right for the website. I just wanted to keep things generic and not include any of my file names or anything.
I need to label several overlapping polygons, but only the label of the biggest one is shown. However when I tested with some simulated data the labels were shown correctly. I compared the data in two cases carefully but cannot find the difference caused the problem.
Here is a minimal example of simulated overlapping polygons:
library(leaflet)
library(sp)
poly_a <- data.frame(lng = c(0, 0.5, 2, 3),
lat = c(0, 4, 4, 0))
poly_b <- data.frame(lng = c(1, 1.5, 1.8),
lat = c(2, 3, 2))
pgons = list(
Polygons(list(Polygon(poly_a)), ID="1"),
Polygons(list(Polygon(poly_b)), ID="2")
)
poly_dat <- data.frame(name = as.factor(c("a", "b")))
rownames(poly_dat) <- c("1", "2")
spgons = SpatialPolygons(pgons)
spgonsdf = SpatialPolygonsDataFrame(spgons, poly_dat, TRUE)
leaflet() %>% addPolygons(data = spgonsdf, label = ~name
# ,
# highlightOptions = highlightOptions(
# color = "red", weight = 2,bringToFront = TRUE)
)
It's working properly:
However it didn't work with my data.
https://github.com/rstudio/leaflet/files/1430888/Gabs.zip
You can drag the zip into this site and use the i button to see it's correctly labeled
library(rgdal)
# download Gabs.zip and extract files to Gabs folder
hr_shape_gabs <- readOGR(dsn = 'Gabs', layer = 'Gabs - OU anisotropic')
hr_shape_gabs_pro <- spTransform(hr_shape_gabs,
CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet(hr_shape_gabs_pro) %>%
addTiles() %>%
addPolygons(weight = 1, label = ~name)
Only the biggest polygon label is shown:
The data in both case are SpatialPolygonsDataFrame, the data slot have proper polygon names.
Change the order of polygons in hr_shape_gabs: polygon in position 3 should be the smaller one.
library(leaflet)
library(sp)
library(rgdal)
hr_shape_gabs <- readOGR(dsn = 'Gabs - OU anisotropic.shp',
layer = 'Gabs - OU anisotropic')
# Change the position of the smaller and wider polygons
# Position 1 = wider polygon, position 3 = smaller polygon
pol3 <- hr_shape_gabs#polygons[[3]]
hr_shape_gabs#polygons[[3]] <- hr_shape_gabs#polygons[[1]]
hr_shape_gabs#polygons[[1]] <- pol3
hr_shape_gabs$name <- rev(hr_shape_gabs$name)
hr_shape_gabs_pro <- spTransform(hr_shape_gabs,
CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet() %>%
addTiles() %>%
addPolygons(data= hr_shape_gabs_pro, weight = 1, label = ~name)
Here's a scalable solution in sf for many layers, based on this answer.
The idea is to order the polygons by decreasing size, such that the smallest polygons plot last.
library(sf)
library(dplyr)
# calculate area of spatial polygons sf object
poly_df$area <- st_area(poly_df)
poly_df <- arrange(poly_df, -area)
# view with labels in leaflet to see that small polygons plot on top
leaflet(poly_df) %>% addTiles() %>% addPolygons(label = ~id)
Apologies for the lack of reproducibility. This is more of a concept answer.
I am able to retrive a Direction map with the below Rscript, but I am unable to find function for making it Interactive.
longitude <- c( 77.605855,77.606800,77.596843,77.575793 )
latitude <- c(12.956580,12.966157, 12.964777,12.964473)
d <- as.data.frame(cbind(longitude,latitude))
map <- get_googlemap(center = c(lon = 77.605855, lat = 12.956580), zoom = 14,
size = c(500, 500), scale = 2,maptype = c("roadmap"),markers = d, path = d)
Below are the functionalities that I need to have in my map.
1. Interactive Zoom.
2. Auto Center so that all the markers are visible.
3. OnClick on the marker I would like to show title -say for Eg. "This is Your Car".
The documentation leaflet referred by #SymbolixAU at the comments helped me to arrive at the solution, below is my code to address my requirements mentioned in the question.
library(leaflet)
longitute <-c(77.605855,77.606800,77.596843,77.596747,77.596296,77.595738,77.594944 )
latitude <- c(12.956580,12.966157, 12.964777,12.964323,12.963570,12.962964, 12.962399)
d <- as.data.frame(cbind(longitute,latitude))
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=d$longitute, lat=d$latitude, popup="New Point",)
m<- addPolylines(m , lng = d$longitute, lat = d$latitude)
m
So this is my Output.