Skip to content

Instantly share code, notes, and snippets.

@datagistips
Created October 17, 2022 07:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datagistips/ad7e5ad9da4b94f3a7f5d1b9f652cf66 to your computer and use it in GitHub Desktop.
Save datagistips/ad7e5ad9da4b94f3a7f5d1b9f652cf66 to your computer and use it in GitHub Desktop.
Get Birds Eye Imagery for Mount St Michel (Brittany, France)
library(magick)
library(rjson)
library(glue)
birdseye <- function(coords, key) {
# This script is an R adaptation from python # https://gist.github.com/WvanWaas/b0e60d88f22d13a9d431a53208c8ed7c
# You must first have a Bing Maps account : https://learn.microsoft.com/en-us/bingmaps/getting-started/bing-maps-dev-center-help/getting-a-bing-maps-key
# Get latlon as text
long <- coords[1]
lat <- coords[2]
latlon <- paste(lat, long, sep=",")
# Give latlon to URL. The URL also contains the key
url = glue("https://dev.virtualearth.net/REST/v1/Imagery/Metadata/Birdseye?centerPoint={latlon}&key={key}")
js <- jsonlite::fromJSON(url)
if (js[["resourceSets"]][["estimatedTotal"]] > 0) {
for (res in js[["resourceSets"]][["resources"]]) {
# Get parameters
tile_url = res[["imageUrl"]]
subdomain = res[["imageUrlSubdomains"]][[1]][2]
zoom = res[["zoomMax"]]
xtile = res[["tilesX"]]
ytile = res[["tilesY"]]
tiles = xtile * ytile # total tiles: 0 - tileX * tileY
tile_width = res[["imageWidth"]]
tile_height = res[["imageHeight"]]
# Get Images
imgs <- c()
for (tileId in 0:(tiles-1)) {
print(tileId)
ctile = glue(tile_url)
img_content <- image_read(ctile)
imgs <- c(imgs, img_content)
}
# Merge Images
mat <- matrix(1:tiles, ncol=xtile, nrow=ytile, byrow=TRUE)
out <- vector(mode="list")
for (i in 1:nrow(mat)) {
v <- mat[i, ]
imgs2 <- image_join(imgs[v])
left_to_right <- image_append(imgs2)
img <- image_background(left_to_right, "white", flatten = TRUE)
img
out[[i]] <- img
}
full_img <- image_join(out)
top_to_bottom <- image_append(full_img, stack = TRUE)
img <- image_background(top_to_bottom, "white", flatten = TRUE)
return(img)
}
}
}
# Get Birds Eye Imagery for Mount St Michel (Brittany, France)
lat <- 48.6359017
long <- -1.5113132
img <- birdseye(coords = c(long, lat), key)
image_write(img, "le_mont_saint_michel.jpeg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment