R/read_files.R

Defines functions readFiles

Documented in readFiles

.top_env <- new.env(parent = emptyenv())
#' @title Reads GeoNames data
#' @description
#' This function reads toponym data for the package.  
#' @details
#' This function accesses the data saved by \code{getData()}, reads it as data frame and stores it in the package environment. [Here](http://download.geonames.org/export/dump/readme.txt) is further information on the used column names.
#' Parameter \code{countries} accepts all designations found in \code{country(query = "country table")}.
#' @param countries character string vector with country designations (names or ISO-codes).
#' @param feat.class character string vector. Selects data only of those feature classes (check \url{http://download.geonames.org/export/dump/readme.txt} for the list of all feature classes). By default, it is \code{P}.
#' @param toponym_path character string. Path name for downloaded data.
#' @keywords internal
#' @return A data frame with GeoNames data.
readFiles <- function(countries, feat.class = "P", toponym_path = NULL) {
  toponym_path <- checkPath(toponym_path = toponym_path)
  countries <- unlist(lapply(country(query = countries, toponym_path = toponym_path), function(x) x[, 1]))
  filename <- list()
  for (i in 1:length(countries)) { # store filename for data downloaded by getData()
      filename[[i]] <- paste0(toponym_path, "/", countries, ".txt")[i]
  }

  L <- list()
  for (i in 1:length(countries)) {
    if (tolower(countries[i]) %in% ls(.top_env) == FALSE) { # if not in .top_env, read data
      geonames_content <- utils::read.table(
        file = filename[[i]], # reads country data of parameter "countries"
        head = FALSE, sep = "\t", quote = "", na.strings = "",
        comment.char = "", encoding = "utf8"
      )
      Encoding(geonames_content[, 2]) <- "UTF-8" # set encoding to UTF-8 in case the local encoding of the OS reads it wrong
      Encoding(geonames_content[, 4]) <- "UTF-8" # set encoding to UTF-8 in case the local encoding of the OS reads it wrong

      # add column names, which were received from the geonames readme file
      colnames(geonames_content) <- c(
        "geonameid", "name", "asciiname", "alternatenames",
        "latitude", "longitude", "feature class", "feature code",
        "country code", "cc2", "admin1 code", "admin2 code", "admin3 code",
        "admin4 code", "population", "elevation", "dem", "timezone",
        "modification date"
      )


      L[[i]] <- assign(tolower(countries[i]), geonames_content, envir = .top_env) # saves in pkg env for later use
    } else { #else, retrieve data from .top_env
      L[[i]] <- .top_env[[tolower(countries[i])]]
    }
  }
  if (length(L) > 1) {
    gn <- L[[1]]
    for (j in 2:length(L)) {
      gn <- rbind(gn, L[[j]])
    }
  } else {
    gn <- L[[1]]
  }

  # select only specified features
  gn <- gn[which(gn$"feature class" %in% feat.class), ]
}

Try the toponym package in your browser

Any scripts or data that you put into this service are public.

toponym documentation built on April 13, 2026, 5:06 p.m.