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

get_elev_point and related functions may unexpectedly change column names without warning #69

Open
rgzn opened this issue Apr 21, 2022 · 1 comment

Comments

@rgzn
Copy link

rgzn commented Apr 21, 2022

If there are special characters in the column names for an input sfc object, such as "(" or "-", elevatr functions like get_elev_point will convert them to "." without any sort of warning. If unnoticed this can cause major problems trying to process data.

I have not traced down the source of this bug, but I suspect it is caused by a call to the base function as.data.frame which does these column conversions (for some arcane unknown to me reason). If that is the source, it can be fixed by passing the optional = TRUE argument to as.data.frame.

This same issue was present in sf::st_read and was fixed:
r-spatial/sf#1916

Reprex:

library(sf)
library(dplyr)
library(magrittr)
library(elevatr)

# Read a row from an sf example file, cast as points.
# Then put some dashes in the column names:
st_read(system.file("shape/nc.shp", package="sf")) %>% 
  head(1) %>%  
  st_cast("POINT") %>% 
  rename_with(~ gsub("_","-",.x, fixed= TRUE)) ->
  nc_dash
#> Reading layer `nc' from data source 
#>   `C:\Users\jweissman\Documents\R\win-library\4.1\sf\shape\nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> Warning in st_cast.sf(., "POINT"): repeating attributes for all sub-geometries
#> for which they may not be constant

# New sfc object with elevation:
nc_dash %>% 
  get_elev_point(src = "epqs") ->
  nc_dash_elev
#> Downloading point elevations:
#> Note: Elevation units are in meters

# Remove new columns:
nc_dash_elev %>% 
  select(-elevation, 
         -elev_units) ->
  nc_dash_noelev

# Check if the column names have changed:
all(names(nc_dash) == names(nc_dash_noelev))
#> [1] FALSE

Created on 2022-04-21 by the reprex package (v2.0.1)

@jhollist
Copy link
Owner

jhollist commented May 3, 2022

@rgzn thanks for the issue and the reprex. I need to carve out some time soon to work on elevatr. I hope to do that in the next few weeks and I will take a look at this then.

And sorry for the delay in responding.

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