Skip to content

Commit

Permalink
Merge pull request #71 from Metropolitan-Council/70-add-helper-for-co…
Browse files Browse the repository at this point in the history
…nnecting-to-enterprise-geodatabase-tables-in-databases-vignette

Return data frame if no spatial data available in `import_from_gis()` (v0.2.5)
  • Loading branch information
LimerickSam authored Jan 2, 2024
2 parents b5a6eb2 + 8871f46 commit e620aeb
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 28 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: councilR
Title: Functions and Templates for the Metropolitan Council
Version: 0.2.4
Date: 2023-10-05
Version: 0.2.5
Date: 2023-12-22
Authors@R: c(
person("Metropolitan Council", role = "cph"),
person("Liz", "Roten", , "[email protected]", role = c("cre", "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ importFrom(data.table,.NGRP)
importFrom(data.table,.SD)
importFrom(data.table,data.table)
importFrom(dplyr,case_when)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,transmute)
importFrom(fs,dir_create)
Expand Down Expand Up @@ -74,6 +75,7 @@ importFrom(ggspatial,annotation_scale)
importFrom(ggspatial,north_arrow_fancy_orienteering)
importFrom(glue,glue)
importFrom(magrittr,"%>%")
importFrom(magrittr,extract2)
importFrom(odbc,odbc)
importFrom(plotly,layout)
importFrom(purrr,flatten)
Expand Down
8 changes: 4 additions & 4 deletions R/db_emissions.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
#' # get specific tables
#' t_electricity_residential_ctu <- import_from_emissions(
#' table_name = "metro_energy.vw_electricity_residential_ctu"
#' )
#' )
#'
#' t_eia_energy_consumption_state <- import_from_emissions(
#' table_name = "state_energy.eia_energy_consumption_state"
#' )
#' )
#'
# t_utility_natural_gas_by_ctu <- import_from_emissions(
#' t_utility_natural_gas_by_ctu <- import_from_emissions(
#' table_name = "metro_energy.vw_utility_natural_gas_by_ctu"
#' )
#' )
#' }
#'
#' @importFrom DBI dbCanConnect dbConnect
Expand Down
48 changes: 40 additions & 8 deletions R/db_gis.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#' care to use [DBI::dbDisconnect()] once you are done.
#' - `import_from_gis()` imports a given table from GISLibrary and
#' converts it into a [sf::sf()] object. The connection will
#' be automatically closed after the table is imported.
#' be automatically closed after the table is imported. If the table
#' does not have any spatial data, the table will be returned as a
#' data.frame.
#'
#' Further examples can be found in `vignette("Databases", package = "councilR")`.
#'
Expand Down Expand Up @@ -127,11 +129,13 @@ gis_connection <- function(
#'
#' @rdname gis
#'
#' @return `import_from_gis()` - A [sf::sf()] object
#' @return `import_from_gis()` - A [sf::sf()] object or a data frame
#' @export
#' @importFrom sf st_as_sf
#' @importFrom DBI dbGetQuery dbDisconnect
#' @importFrom tictoc tic toc
#' @importFrom magrittr extract2
#' @importFrom dplyr filter
import_from_gis <- function(query,
dbname = "GISLibrary",
uid = getOption("councilR.uid"),
Expand All @@ -147,15 +151,43 @@ import_from_gis <- function(query,
pwd = pwd
)

que <- DBI::dbGetQuery(
# fetch query table column names
column_names <- DBI::dbGetQuery(
conn,
paste0("SELECT *, Shape.STAsText() as wkt FROM ", query)
paste0(
"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '",
# remove GISLibrary.dbo. to get just the table name
gsub(pattern = "GISLibrary.dbo.", replacement = "", x = query), "'"
)
)

sf_df <- sf::st_as_sf(
que,
wkt = "wkt", crs = 26915
)
# if there are any geometry columns,
# pull as wkt
if ("geometry" %in% column_names$DATA_TYPE) {

# fetch column name with geometry
geo_column <- column_names %>%
dplyr::filter(DATA_TYPE == "geometry") %>%
magrittr::extract2("COLUMN_NAME")

# fetch query
que <- DBI::dbGetQuery(
conn,
paste0("SELECT *, ", geo_column, ".STAsText() as wkt FROM ", query)
)

# convert wkt to sf
sf_df <- sf::st_as_sf(
que,
wkt = "wkt", crs = 26915
)
} else {
# otherwise, just pull the table
sf_df <- DBI::dbGetQuery(
conn,
paste0("SELECT * FROM ", query)
)
}

DBI::dbDisconnect(conn)

Expand Down
6 changes: 3 additions & 3 deletions R/scale_fill_council.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @param n numeric, value between 1-8
#' @param name character, starting color name. One of `councilR::colors`.
#'
#' @description Modified [RColorBrewer::brewer.pal()]
#' @description Adapted from [RColorBrewer::brewer.pal()]
#' @examples
#' \dontrun{
#' library(scales)
Expand All @@ -31,7 +31,7 @@ council.pal <- function(n, name) {

#' @title Discrete Council palettes
#'
#' @description Modified from [scales::brewer_pal()].
#' @description Adapted from [scales::pal_brewer()].
#' Stripped down all arguments, just to show the core
#'
#' @family aesthetics
Expand Down Expand Up @@ -74,7 +74,7 @@ scale_fill_council <- function(..., aesthetics = "fill") {

#' @title Color scale
#'
#' @description Modified from [ggplot2::scale_fill_brewer()]
#' @description Adapted from [ggplot2::scale_fill_brewer()]
#'
#' @inheritParams scale_fill_council
#'
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Please see our contribution guidelines [here](.github/CONTRIBUTING.md).
Thanks to all the folks who have contributed to this project in one way or another

[&#x0040;ashleyasmus](https://github.com/ashleyasmus), [&#x0040;ehesch](https://github.com/ehesch), [&#x0040;eroten](https://github.com/eroten),
[&#x0040;velicknd](https://github.com/velicknd), [&#x0040;Brandon-Whited](https://github.com/Brandon-Whited), and [&#x0040;leonx075](https://github.com/leonx075).
[&#x0040;velicknd](https://github.com/velicknd), [&#x0040;Brandon-Whited](https://github.com/Brandon-Whited), [&#x0040;LimerickSam](https://github.com/LimerickSam), and [&#x0040;leonx075](https://github.com/leonx075).



Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ or another
[@ehesch](https://github.com/ehesch),
[@eroten](https://github.com/eroten),
[@velicknd](https://github.com/velicknd),
[@Brandon-Whited](https://github.com/Brandon-Whited), and
[@Brandon-Whited](https://github.com/Brandon-Whited),
[@LimerickSam](https://github.com/LimerickSam), and
[@leonx075](https://github.com/leonx075).
2 changes: 1 addition & 1 deletion man/council.pal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/council_pal2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/emissions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/gis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/scale_color_council.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-db_gis.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ testthat::test_that("airports spatial dataset", {
# test that object returned is an sf object
testthat::expect_equal(class(airport)[[1]], "sf")
})


testthat::test_that("county ctu lookup table", {
lookup_table <- import_from_gis(
uid = httr2::secret_decrypt("QUHBRb_yoy2RRj59qno8NVXA7mW402xkins", "COUNCILR_KEY"),
pwd = httr2::secret_decrypt("rQHk4S39pjfJ6yoKWUUNpQUDk2i9XA3d", "COUNCILR_KEY"),
query = "GISLibrary.dbo.CountyCTULookupTable",
dbname = "GISLibrary",
.quiet = TRUE
)

# test that all airports are included
# there should be 14 airports
testthat::expect_equal(nrow(lookup_table), 220)

# test that object returned is an sf object
testthat::expect_equal(class(lookup_table)[[1]], "data.frame")
})
10 changes: 9 additions & 1 deletion vignettes/Databases.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ head(import_from_FRED(table_name = "GQ_UNIT", prod = FALSE))

## GISLibrary

GISLibrary is an ESRI SQL database that contains essential geospatial datasets.
GISLibrary is an ESRI SQL database that contains essential geospatial datasets and complementary tables.

Generally, if the Council has published a dataset on [Minnesota Geospatial Commons](https://gisdata.mn.gov/), it is available in GISLibrary.

Expand All @@ -94,10 +94,18 @@ DBI::dbDisconnect(gis)

```{r gis-table, eval=FALSE}
# import a specific table as an sf object, with no additional SQL
# returns an sf object
airport <- import_from_gis(
query = "GISLibrary.dbo.AIRPORTS",
.quiet = TRUE
)
# if a table does not have any spatial data,
# it will return a data frame
lookup_table <- import_from_gis(
query = "GISLibrary.dbo.CountyCTULookupTable",
.quiet = TRUE
)
```

## Emissions
Expand Down

0 comments on commit e620aeb

Please sign in to comment.