Metropolitan-Council / councilR

A curated collection of commonly used templates, color palettes, functions, and more!
Other
6 stars 1 forks source link

Return data frame if no spatial data available in `import_from_gis()` (v0.2.5) #71

Closed eroten closed 6 months ago

eroten commented 6 months ago

Implement conditional when pulling tables from GISLibrary

import_from_gis() now pulls the query table column information and checks for any "geometry" data types. If there is a geometry column, pull table and return as sf object. If there is no geometry, pull table and return as data frame.

This fix isn't absolutely necessary - we could just update the documentation to show an example of pulling a non-spatial table using gis_connection().

Additional changes

Closes #70

Remedied reprex

library(councilR)

options(councilR.uid = "mc\\rotenle",
        councilR.pwd = keyring::key_get("MetC"))

tracts2010_spatial <- import_from_gis(
  query = "GISLibrary.dbo.Census2010TigerTract",
  .quiet = TRUE
)

# output truncated
head(tracts2010_spatial)
#> Simple feature collection with 6 features and 15 fields
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 458704.8 ymin: 4947713 xmax: 510796.5 ymax: 4996701
#> Projected CRS: NAD83 / UTM zone 15N
#>   OBJECTID STATEFP10 COUNTYFP10 TRACTCE10     GEOID10 NAME10
#> 1        1        27        037    060744 27037060744 607.44
#> 2        2        27        139    080903 27139080903 809.03
#> 3        3        27        037    060738 27037060738 607.38
#> 4        4        27        123    034602 27123034602 346.02
#> 5        5        27        163    070403 27163070403 704.03
#> 6        6        27        163    070909 27163070909 709.09
#>            NAMELSAD10 MTFCC10 FUNCSTAT10  ALAND10 AWATER10  INTPTLAT10
#> 1 Census Tract 607.44   G5020          S  2053432    86758 +44.7861609
#> 2 Census Tract 809.03   G5020          S 28332969  4533634 +44.7225340
#> 3 Census Tract 607.38   G5020          S  2128920    26061 +44.7735895
#> 4 Census Tract 346.02   G5020          S  1572097        0 +44.9570359
#> 5 Census Tract 704.03   G5020          S 63302121  3042082 +45.0743811
#> 6 Census Tract 709.09   G5020          S  4196628    25974 +44.9792561
#>                              wkt
#> 1 POLYGON ((481983.3 4959679,...
#> 2 MULTIPOLYGON (((458707 4947...
#> 3 POLYGON ((479657.5 4956868,...
#> 4 POLYGON ((496397.8 4978603,...
#> 5 POLYGON ((504360 4987370, 5...
#> 6 POLYGON ((504423.9 4980078,...

tracts2010_popHH <- import_from_gis(
  query = "GISLibrary.dbo.Census2010PopulationTract",
  .quiet = TRUE
)

head(tracts2010_popHH)
# output truncated
#>   OBJECTID   GEOG_UNIT GEOG_LEVEL              GEONAME              GEOID
#> 1        1 27001770100      TRACT    Census Tract 7701 14000US27001770100
#> 2        2 27001770200      TRACT    Census Tract 7702 14000US27001770200
#> 3        3 27001770300      TRACT    Census Tract 7703 14000US27001770300
#> 4        4 27001770400      TRACT    Census Tract 7704 14000US27001770400
#> 5        5 27001790501      TRACT Census Tract 7905.01 14000US27001790501
#> 6        6 27001790502      TRACT Census Tract 7905.02 14000US27001790502

Created on 2023-12-22 with reprex v2.0.2