Skip to content

Commit

Permalink
add 'merge_duplicated_col_names' fn to fix #348
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Sep 25, 2024
1 parent 88ba939 commit 7a5ac5f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: osmdata
Title: Import 'OpenStreetMap' Data as Simple Features or Spatial Objects
Version: 0.2.5.018
Version: 0.2.5.019
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Bob", "Rudis", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Improved `get_bb(..., format_out = "sf_polygon")` to return full metadata
along with geometries (#338 thanks to @RegularnaMatrica)
- Mention key-only feature requests in README (#342 thanks to @joostschouppe)
- Merge any columns in `osmdata_sf()` with mixed-case duplicated names (#348)


0.2.5
Expand Down
24 changes: 24 additions & 0 deletions R/get-osmdata-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ make_sf <- function (..., stringsAsFactors = FALSE) { # nolint
)
}

df <- merge_duplicated_col_names (df)

object <- as.list (substitute (list (...))) [-1L]
arg_nm <- sapply (object, function (x) deparse (x)) # nolint
sfc_name <- make.names (arg_nm [sf_column])
Expand All @@ -138,6 +140,28 @@ make_sf <- function (..., stringsAsFactors = FALSE) { # nolint
return (df)
}

#' Merge any `sf` `data.frame` columns which have mixed-case duplicated names
#' (like "This" and "this"; #348).
merge_duplicated_col_names <- function (df) {

nms_lower <- tolower (names (df))
dups <- which (duplicated (nms_lower))
if (length (dups) > 0L) {
dup_nms <- nms_lower [dups]
cols_to_rm <- NULL
for (nm in dup_nms) {
index <- which (nms_lower == nm)
df [, index [1]] <- apply (df [, index], 1, function (i) {
ifelse (all (is.na (i)), i [1], i [which (!is.na (i)) [1]])
})
cols_to_rm <- c (cols_to_rm, index [2])
}
df <- df [, -(cols_to_rm)]
}

return (df)
}


sf_types <- c ("points", "lines", "polygons", "multilines", "multipolygons")

Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"codeRepository": "https://github.com/ropensci/osmdata/",
"issueTracker": "https://github.com/ropensci/osmdata/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.2.5.018",
"version": "0.2.5.019",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down

0 comments on commit 7a5ac5f

Please sign in to comment.