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

Reimporting draw toolbar features #234

Open
oliverbeagley-pgg opened this issue Nov 29, 2024 · 1 comment
Open

Reimporting draw toolbar features #234

oliverbeagley-pgg opened this issue Nov 29, 2024 · 1 comment

Comments

@oliverbeagley-pgg
Copy link

I've been using leaflet.extras in shiny to export the features drawn with the toolbar to then be able to reimport them later. I have been having issues with getting it to reimport in the same way as when it was exported.

When I export a circle from the _draw_new_feature event (or similar) using jsonlite::toJSON(<feature>, auto_unbox = TRUE) then put this in a addGeoJSONv2 it won't recreate it as a circle but rather a point/marker, It is similar story for the other feature types that can be created through the draw tool. Is there anyway that I can get this back as original type drawn?

I also have some code that will store a user entered name by a lookup on the _leaflet_id from each feature. When the features are added back the _leaflet_id of course changes. Is there a nice way to programmatically get the new _leaflet_ids out in some way such that I can update a lookup without using the draw tools manually?

@trafficonese
Copy link
Owner

I am using this function to create Simple Feature objects from drawn features..
Maybe that is what you're looking for?

I don't understand the last part about the _leaflet_id. Maybe you can create a minimal example?

library(sf)
library(sfheaders)

get_poly_from_drawing <- function(drawn) {
  coords <- matrix(unlist(drawn$geometry$coordinates),
                   ncol = 2, byrow = T)
  if (drawn$properties$feature_type == "circle") {
    pts <- sfheaders::sf_point(coords)
    st_crs(pts) <- 4326
    poly <- st_buffer(st_transform(pts, 3835), drawn$properties$radius)
    poly <- st_transform(poly, 4326)
  } else {
    poly <- sfheaders::sf_polygon(coords)
    st_crs(poly) <- 4326
  }
  poly
}

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

No branches or pull requests

2 participants