Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating interactive and static maps #97

Open
HenrikBengtsson opened this issue May 25, 2024 · 1 comment
Open

Generating interactive and static maps #97

HenrikBengtsson opened this issue May 25, 2024 · 1 comment

Comments

@HenrikBengtsson
Copy link
Member

Here's an example illustrating how to generate a topology map showing the 300' and the 600' windsocks on Ed Levin using R:

library(leaflet)

m <- leaflet()
m <- m |> addTiles(urlTemplate = "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png")
m <- m |> setView(lat = 37.4611, lng = -121.8625, zoom = 17)

## 300' windsock
m <- m |> addMarkers(lat = 37.4611738, lng = -121.8646301, label = "300ft windsock", labelOptions = labelOptions(noHide = TRUE))

## 600' windsock
m <- m |> addMarkers(lat = 37.4612652, lng = -121.8604359, label = "600ft windsock", labelOptions = labelOptions(noHide = TRUE))

## Open (sic!) interactive leaflet map in web browser
print(m)

To save the same view as a static PNG file, use:

mapview::mapshot2(m, file = "edlevin.png")

This produces:

edlevin

Installation instructions

Assuming modern version of R, install packages using:

$ R
...
> install.packages(c("mapview", "webshot2"))
@HenrikBengtsson
Copy link
Member Author

Here's how to pull free-flying and windsock information from OpenStreetMap and generate an standalone, interactive map;

library(osmdata)
library(leaflet)

## https://fontawesome.com/search?o=r&m=free
icons <- list(
        windsock = awesomeIcons(icon = "flag", markerColor = "gray",   iconColor = "white"),
          launch = awesomeIcons(icon = "play", markerColor = "green",  iconColor = "white"),
              lz = awesomeIcons(icon = "stop", markerColor = "red",    iconColor = "white"),
        training = awesomeIcons(icon = "play", markerColor = "orange", iconColor = "white"),
  groundhandling = awesomeIcons(icon = "user", markerColor = "blue",   iconColor = "white")
)

## Ed Levin & Mission Area
center <- c(lat = 37.465, lng = -121.86)
pad <- c(-1, -1, +1, +1) * 1/10
q <- opq(bbox = rep(center[c("lng", "lat")], times = 2) + pad)
q <- q |> add_osm_features(features = list(
    "sport" = "free_flying",
  "aeroway" = "windsock"
))
data <- q |> osmdata_sf()
print(data)

m <- leaflet(data)
m <- m |> addTiles(urlTemplate = "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png")

for (kk in seq_len(nrow(data$osm_points))) {
  point <- data$osm_points[kk, ]
  pos <- point$geometry[[1]]
  names(pos) <- c("lng", "lat")
  label <- point$name
  icon <- NULL

  site <- point[["free_flying:site"]]
  if (identical(site, "takeoff")) {
    icon <- icons$launch
  } else if (identical(site, "landing")) {
    icon <- icons$lz
  } else if (identical(site, "training")) {
    icon <- icons$training
  }
  
  type <- point[["free_flying:ground_handling"]]
  if (identical(type, "yes")) {
    icon <- icons$groundhandling
  }

  if (identical(point[["aeroway"]], "windsock")) {
    icon <- icons$windsock
  }

  m <- m |> addAwesomeMarkers(lat = pos["lat"], lng = pos["lng"], label = label, icon = icon)
}

m <- m |> setView(lat = center["lat"], lng = center["lng"], zoom = 15)
print(m)

To save as PNG;

mapview::mapshot2(m, file = "edlevin.png")

edlevin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant