Skip to content

add_sf() create a _bbox atribute in data causing mapbox to auto fly to it #12

Description

@raphidoc

This behaviour makes the map display unpleasant (moving too much) and more difficult to use.
The add_sf function could provide an argument to disable the creation of _bbox.

add_sf <- {
    try_library("sf", "add_sf")
    dat <- plotly_data(add_data(p, data))
    if (!is_sf(dat)) {
        stop("The `data` for an `add_sf()` layer must be an sf object, ", 
            "not an object of class: ", class(dat)[1], call. = FALSE)
    }
    if (is_mapbox(p) || is_geo(p)) 
        dat <- st_cast_crs(dat)
    bbox <- sf::st_bbox(dat)
    set <- attr(dat, "set")
    d <- to_basic.GeomSf(dat)
    d <- if (is.data.frame(d)) 
        list(d)
    attrz <- if (inherit) 
        modify_list(unclass(p$x$attrs[[1]]), list(...))
    else list(...)
    for (i in seq_along(d)) {
        attrs <- modify_list(sf_default_attrs(d[[i]]), attrz)
        if ("z" %in% names(attrs)) 
            attrs$fill <- NULL
        args <- list(p = p, class = "plotly_sf", x = x, y = y, 
            `_bbox` = bbox, set = set, data = if ("group" %in% 
                names(d[[i]])) group_by_add(d[[i]], !!rlang::sym("group")) else d[[i]], 
            inherit = inherit)
        args <- modify_list(args, attrs)
        p <- do.call(add_trace_classed, args)
    }
    p
}

Workaround with add_polygons and sfheaders::sf_to_df()

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestinvalidThis doesn't seem rightquestionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions