Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GetGisData <- function (con, pass, name, band = 1, mapid = F, schema = "gisdata")
- {
- if (!require(rgdal))
- stop("rgdal not installed. Please install")
- if (!require(raster))
- stop("raster not installed. Please install")
- if (is.na(name))
- stop("You have to set a filename first. Query the available GisData")
- cI <- postgresqlConnectionInfo(con)
- name <- tolower(name)
- if (mapid) {
- q = paste0("SELECT mapfile from map where map_id=",
- name, ";")
- res <- dbGetQuery(con, q)
- if (length(res) == 0)
- stop("Map_id not found in Map table")
- n = tolower(paste0(unlist(str_split(basename(res$mapfile),
- "\\."))[1], "__", res$roi_id))
- dsn = paste0("PG:dbname='", cI$dbname, "' host='", cI$host,
- "' port=", cI$port, " user='", cI$user, "' password='",
- pass, "' schema='", schema, "' table='", n, "' mode=2")
- try(ras <- readGDAL(dsn), silent = T)
- if (exists("ras")) {
- print(paste0("Found corresponding raster table in ",
- paste0(schema, ".", n)))
- print("-------")
- res <- raster(ras, band)
- }
- else {
- print("No spatial data with this name found!")
- res <- FALSE
- }
- }
- else {
- dsn = paste0("PG:dbname='", cI$dbname, "' host='", cI$host,
- "' user='", cI$user, "' password='", pass, "'")
- ll <- ogrListLayers(dsn)
- if (paste0("gisdata.", name) %in% ll) {
- print(paste0("Found corresponding shapefile in ",
- paste0("gisdata.", name)))
- print("-------")
- res = readOGR(dsn, paste0("gisdata.", name))
- }
- else {
- dsn = paste0("PG:dbname='", cI$dbname, "' host='",
- cI$host, "' port=", cI$port, " user='", cI$user,
- "' password='", pass, "' schema='", schema, "' table='",
- name, "' mode=2")
- try(ras <- readGDAL(dsn), silent = T)
- if (exists("ras")) {
- print(paste0("Found corresponding raster table in ",
- paste0("gisdata.", name)))
- print("-------")
- res <- raster(ras, band)
- }
- else {
- print("No spatial data with this name found!")
- res <- FALSE
- }
- }
- }
- return(res)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement