Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1.  
  2. GetGisData <- function (con, pass, name, band = 1, mapid = F, schema = "gisdata")
  3. {
  4. if (!require(rgdal))
  5. stop("rgdal not installed. Please install")
  6. if (!require(raster))
  7. stop("raster not installed. Please install")
  8. if (is.na(name))
  9. stop("You have to set a filename first. Query the available GisData")
  10. cI <- postgresqlConnectionInfo(con)
  11. name <- tolower(name)
  12. if (mapid) {
  13. q = paste0("SELECT mapfile from map where map_id=",
  14. name, ";")
  15. res <- dbGetQuery(con, q)
  16. if (length(res) == 0)
  17. stop("Map_id not found in Map table")
  18. n = tolower(paste0(unlist(str_split(basename(res$mapfile),
  19. "\\."))[1], "__", res$roi_id))
  20. dsn = paste0("PG:dbname='", cI$dbname, "' host='", cI$host,
  21. "' port=", cI$port, " user='", cI$user, "' password='",
  22. pass, "' schema='", schema, "' table='", n, "' mode=2")
  23. try(ras <- readGDAL(dsn), silent = T)
  24. if (exists("ras")) {
  25. print(paste0("Found corresponding raster table in ",
  26. paste0(schema, ".", n)))
  27. print("-------")
  28. res <- raster(ras, band)
  29. }
  30. else {
  31. print("No spatial data with this name found!")
  32. res <- FALSE
  33. }
  34. }
  35. else {
  36. dsn = paste0("PG:dbname='", cI$dbname, "' host='", cI$host,
  37. "' user='", cI$user, "' password='", pass, "'")
  38. ll <- ogrListLayers(dsn)
  39. if (paste0("gisdata.", name) %in% ll) {
  40. print(paste0("Found corresponding shapefile in ",
  41. paste0("gisdata.", name)))
  42. print("-------")
  43. res = readOGR(dsn, paste0("gisdata.", name))
  44. }
  45. else {
  46. dsn = paste0("PG:dbname='", cI$dbname, "' host='",
  47. cI$host, "' port=", cI$port, " user='", cI$user,
  48. "' password='", pass, "' schema='", schema, "' table='",
  49. name, "' mode=2")
  50. try(ras <- readGDAL(dsn), silent = T)
  51. if (exists("ras")) {
  52. print(paste0("Found corresponding raster table in ",
  53. paste0("gisdata.", name)))
  54. print("-------")
  55. res <- raster(ras, band)
  56. }
  57. else {
  58. print("No spatial data with this name found!")
  59. res <- FALSE
  60. }
  61. }
  62. }
  63. return(res)
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement