diff --git a/DESCRIPTION b/DESCRIPTION index a57f2ae..5c66a90 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,11 +23,13 @@ Imports: mapview, miniUI, sf (>= 0.5-2), - shiny + shiny, + rstudioapi (>= 0.8), + shinyWidgets (>= 0.4.3) Suggests: crayon Enhances: geojsonio Encoding: UTF-8 LazyData: true -RoxygenNote: 6.1.0 +RoxygenNote: 6.1.1 diff --git a/NAMESPACE b/NAMESPACE index 3f5fd1d..f39ae79 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,9 +13,20 @@ export(editFeatures) export(editMap) export(editMod) export(editModUI) +export(mapeditAddin) export(selectFeatures) export(selectMap) export(selectMod) export(selectModUI) import(htmltools) import(shiny) +importFrom(leaflet,setView) +importFrom(mapview,mapview) +importFrom(miniUI,gadgetTitleBar) +importFrom(miniUI,miniButtonBlock) +importFrom(miniUI,miniContentPanel) +importFrom(miniUI,miniPage) +importFrom(rstudioapi,getActiveDocumentContext) +importFrom(sf,write_sf) +importFrom(shinyWidgets,switchInput) +importFrom(shinyWidgets,updateSwitchInput) diff --git a/R/addin.R b/R/addin.R new file mode 100644 index 0000000..b85c3e5 --- /dev/null +++ b/R/addin.R @@ -0,0 +1,87 @@ + + + +#' @title mapedit Addin +#' @description Create and save spatial objects within the Rstudio IDE +#' +#' @return sf object to .GlobalEnv and/or saved to file +#' @importFrom miniUI miniPage miniContentPanel gadgetTitleBar miniButtonBlock +#' @import shiny +#' @importFrom shinyWidgets switchInput updateSwitchInput +#' @importFrom mapview mapview +#' @importFrom sf write_sf +#' @importFrom leaflet setView +#' @importFrom rstudioapi getActiveDocumentContext +#' @export +#' +mapeditAddin <- function() { + + ui <- miniPage( + gadgetTitleBar("Edit Map"), + miniContentPanel( + editModUI("editor"), + miniButtonBlock( + div(style="display: inline-block;padding-top:22px;padding-left:30px;width:180px;", + switchInput('savefile', 'Save', value = FALSE, onStatus = "success", offStatus = "danger")), + div(style="display: inline-block; width: 400px;", + textInput('filename', '', value = 'saved_geometry.geojson')), + div(style="display: inline-block;padding-top:18px;width: 400px;font-size: 10pt;color: #313844;", + 'You can add folders and change output type.', + 'Created geometry will always save to .GlobalEnv') + ) + ) + ) + + server <- function(input, output, session) { + + # get values from rstudio + ct <- getActiveDocumentContext() + + TEXT <- ct$selection[[1]]$text + OBJECTNAME <- ifelse(TEXT == '', 'geom', TEXT) + FILENAME <- ifelse(TEXT == '', 'saved_geometry.geojson', paste0(TEXT, '.geojson')) + SF_OBJECT <- NULL + + # test selected text an sf object + try({ + SF_OBJECT <- get(TEXT) + if (class(SF_OBJECT) != 'sf') {SF_OBJECT <- NULL} + }) + + # update UI based on inputs + updateTextInput(session, 'filename', value = FILENAME) + if (FILENAME != 'saved_geometry.geojson') { + updateSwitchInput(session, 'savefile', value = TRUE) + } + + + # load mapedit + if (class(SF_OBJECT) == 'sf') { + geo <- callModule(editMod, "editor", mapview(SF_OBJECT)@map) + } else { + geo <- callModule(editMod, "editor", setView(mapview()@map, 80, 0, 3)) + } + + + # return geometry to file and object in .GlobalEnv + observeEvent(input$done, { + geom <- geo()$finished + + if (!is.null(geom) & !is.null(SF_OBJECT)) geom <- rbind(SF_OBJECT, geom) + + if (!is.null(geom)) { + assign(OBJECTNAME, geom, envir = .GlobalEnv) + if (input$savefile) { + sf::write_sf(geom, input$filename, delete_layer = TRUE, delete_dsn = TRUE) + } + } + + stopApp() + }) + + } + + viewer <- paneViewer(600) + runGadget(ui, server, viewer = viewer) + +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf new file mode 100644 index 0000000..6d1c8f0 --- /dev/null +++ b/inst/rstudio/addins.dcf @@ -0,0 +1,4 @@ +Name: Create Spatial Data +Description: Create spatial data using mapedit +Binding: mapeditAddin +Interactive: true diff --git a/man/mapeditAddin.Rd b/man/mapeditAddin.Rd new file mode 100644 index 0000000..df2b0eb --- /dev/null +++ b/man/mapeditAddin.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addin.R +\name{mapeditAddin} +\alias{mapeditAddin} +\title{mapedit Addin} +\usage{ +mapeditAddin() +} +\value{ +sf object to .GlobalEnv and/or saved to file +} +\description{ +Create and save spatial objects within the Rstudio IDE +}