koenderks / rcityviews

rcityviews is a user-friendly R interface for creating stylized city maps using OpenStreetMap (www.openstreetmap.org) data, implemented as an R package and a Shiny web application.
https://koenderks.github.io/rcityviews/
GNU General Public License v3.0
163 stars 22 forks source link

[Question] Meaning of defaultRadius #18

Closed stalkerGH closed 10 months ago

stalkerGH commented 10 months ago

It's rather a question than the bug. What is defaultRadius meaning in builders.R? What units does it use?

koenderks commented 10 months ago

I think the defaultRadius is measured in geographical coordinates (i.e., degrees lat / long).

stalkerGH commented 10 months ago

So if coordinates are X,Y (with decimal parts), then radius is (X+/- defaultRadius) and the same for Y?

koenderks commented 10 months ago

Basically, but I think it depends on the zoom as well.

We do:

  defaultRadius <- 0.0225
  if (!is.null(zoom)) { # We enter this statement since we are not in the app
    radius <- geosphere::distm(x = c(city[["long"]], city[["lat"]]), y = c(city[["long"]], city[["lat"]] + defaultRadius * (1 / zoom)), fun = geosphere::distHaversine)
  } else {
    radius <- geosphere::distm(x = c(city[["long"]], city[["lat"]]), y = c(city[["long"]], input[["osm_bounds"]][["north"]]), fun = geosphere::distHaversine)
  }
  cropped <- data.frame(lat = city[["lat"]], long = city[["long"]]) |>
    sf::st_as_sf(coords = c("long", "lat"), crs = 4326) |>
    # sf::st_buffer(dist = radius) |> # see https://github.com/r-spatial/sf/issues/1692
    s2::s2_buffer_cells(distance = radius, max_cells = 5000) |>
    sf::st_as_sf()

So in coordinates the view box (cropped) is [xmin: city[["lat"]] - defaultRadius (1 / zoom), xmax: city[["lat"]] + defaultRadius (1 / zoom)] and the same for ymin and ymax but then with long. However, we do not use the default radius directly but compute a radius (I believe in meters) based on the location on earth (geosphere::distHaversine). Then we get the actual view box.

stalkerGH commented 10 months ago

Thank you forn in-depth explanation. Now I understand why zoom > 1 is faster than zoom < 1.

And thanks for pointing to distHaversine function!