| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- else
- return(FALSE)
- }
- aaa <- db_is_shema_exist(schema = "x_matsalu")
- aaa <- db_is_shema_exist(schema = "x_matsalu")
- aaa <- db_is_shema_exist(schema = "xx_matsalu")
- schema = "xx_matsalu"
- schema %in% db_all_schemas()
- schema %in% db_all_schemas()
- if(schema %in% db_all_schemas())
- TRUE
- else
- FALSE
- if(schema %in% db_all_schemas())
- TRUE
- else
- FALSE
- if(schema %in% db_all_schemas())
- TRUE
- if(schema %in% db_all_schemas())
- TRUE
- library(ruut)
- db_all_schemas <- function() {
- source(paste0(getwd(),"/R/db_connect.R"))
- conn <- db_connect()
- all_schemas <- DBI::dbGetQuery(conn, "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA")
- return (all_schemas$schema_name)
- }
- db_all_schemas()
- db_is_shema_exist <- function(schema = NULL) {
- if(schema %in% db_all_schemas())
- TRUE
- else
- FALSE
- }
- db_is_shema_exist(schema = "xx_matsalu")
- db_is_shema_exist(schema = "x_matsalu")
- db_is_shema_exist(schema = "x_matsalu")
- db_create_new_schema <- function(conf) {
- conn <- db_connect()
- all_schemas <- DBI::dbGetQuery(conn,sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user))
- }
- db_create_new_schema(conf)
- styler:::style_active_file()
- db_create_new_schema <- function(conf) {
- conn <- db_connect()
- DBI::dbExecute(conn, sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user
- ))
- }
- conf <- ruut::get_config()
- conf
- db_create_new_schema <- function(conf) {
- conn <- db_connect()
- DBI::dbExecute(conn, sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user
- ))
- }
- db_create_new_schema(conf)
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- source(paste0(getwd(), "/R/db_all_schemas"))
- source(paste0(getwd(), "/R/db_all_schemas.R"))
- paste0(getwd(), "/R/db_all_schemas.R")
- source(paste0(getwd(), "/R/db_all_schemas.R"))
- db_is_shema_exist(schema = "x_matsalu")
- db_is_shema_exist(schema = "x_matsalu")
- db_is_shema_exist(schema = "zx_matsalu")
- styler:::style_active_file()
- styler:::style_active_file()
- db_create_new_schema <- function(conf) {
- source(paste0(getwd(), "/R/db_is_shema_exist.R"))
- if(db_is_shema_exist(schema)) {
- conn <- db_connect()
- DBI::dbExecute(conn, sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user
- ))
- }
- }
- conf <- ruut::get_config()
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf
- conf$schema <- "x_valga1"
- conf
- db_create_new_schema(conf)
- conf$schema
- db_create_new_schema <- function(conf) {
- source(paste0(getwd(), "/R/db_is_shema_exist.R"))
- if(!db_is_shema_exist(conf$schema)) {
- conn <- db_connect()
- DBI::dbExecute(conn, sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user
- ))
- }
- }
- conf <- ruut::get_config()
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- library(ruut)
- conf <- ruut::get_config()
- conf
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- db_create_new_schema <- function(conf) {
- source(paste0(getwd(), "/R/db_is_shema_exist.R"))
- if(!db_is_shema_exist(conf$schema)) {
- conn <- db_connect()
- DBI::dbExecute(conn, sprintf(
- "-- DROP SCHEMA x_matsalu;
- CREATE SCHEMA %s AUTHORIZATION %s;", conf$schema, conf$user
- ))
- }
- }
- db_create_new_schema(conf)
- DBI::dbDisconnect(conn)
- conf <- ruut::get_config()
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga1"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- conf <- ruut::get_config()
- conf$schema <- "x_valga"
- db_create_new_schema(conf)
- styler:::style_active_file()
- library(ruut)
- styler:::style_active_file()
- library(ruut)
- styler:::style_active_file()
- library(ruut)
- library(ruut)
- library(ruut)
- library(ruut)
- library(ruut)
- ogr2ogr --long-usage
- system("ogr2ogr --long-usage")
- library(ruut)
- library(ruut)
- library(ruut)
- styler:::style_active_file()
- styler:::style_active_file()
- system("ogr2ogr --long-usage")
- styler:::style_active_file()
- styler:::style_active_file()
- library(ruut)
- #'
- #' INSPIRE grid map
- #'
- library(dplyr)
- library(ruut)
- # Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2
- # Temp directory
- tmp_dir <- tempdir()
- # Download link
- url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
- # Estonia shapefile (ZIP archive)
- map_shapefile <- "Estonia_shapefile.zip"
- # Download and sav
- saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
- download.file(
- url = download_url,
- destfile = saveTo, method = "curl", extra = "-L"
- )
- # Unzip
- unzip(saveTo, overwrite = T, exdir = tmp_dir)
- # List of files
- ls <- list.files(path = tmp_dir, pattern = ".shp")
- ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
- tbl_names <- strsplit(x = ls, split = ".shp") %>% unlist()
- # Export to postgis
- schema <- "inspire"
- i <- 1
- config <- get_config()
- config$schema <- schema
- # New schema
- db_create_new_schema(conf = config)
- library(dplyr)
- library(ruut)
- # Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2
- # Temp directory
- tmp_dir <- tempdir()
- # Download link
- url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
- # Estonia shapefile (ZIP archive)
- map_shapefile <- "Estonia_shapefile.zip"
- # Download and sav
- saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
- download.file(
- url = url,
- destfile = saveTo, method = "curl", extra = "-L"
- )
- # Unzip
- unzip(saveTo, overwrite = T, exdir = tmp_dir)
- # List of files
- ls <- list.files(path = tmp_dir, pattern = ".shp")
- ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
- tbl_names <- strsplit(x = ls, split = ".shp") %>% unlist()
- # Export to postgis
- schema <- "inspire"
- i <- 1
- config <- get_config()
- config$schema <- schema
- # New schema
- db_create_new_schema(conf = config)
- print(tbl_names[i])
- config$table <- tbl_names[i]
- source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
- crs_source <- "-s_srs EPSG:3035"
- crs_target <- "-t_srs EPSG:4326"
- fid <- "fid"
- ruut::copy_shp_to_db(dir = tmp_dir, layer = tbl_names[i], conf = config,
- id = fid, crs_source = crs_source, crs_target = crs_target,
- geometry_type = 'POLYGON')
- dir = tmp_dir; layer = tbl_names[i]; conf = config;
- id = fid; crs_source = crs_source; crs_target = crs_target;
- geometry_type = 'POLYGON'
- length(list.files(path = dir, pattern = sprintf("%s*", layer)))
- length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0
- source <- sprintf('"%s" "%s"', dir, layer)
- source
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- cmd
- system(cmd)
- dir = tmp_dir; layer = tbl_names[i]; conf = config;
- id = fid; crs_source = crs_source; crs_target = crs_target;
- geometry_type = 'PROMOTE_TO_MULTI'
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- cmd
- ruut::db_create_new_schema(conf)
- system(cmd)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt PROMOTE_TO_MULTI"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target
- )
- cmd
- system(cmd)
- geometry_type = 'POLYGON'
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- cmd
- system(cmd)
- system("ogr2ogr --long-usage")
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs%s -t_srs -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- crs_target
- crs_source
- cmd
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- cmd
- crs_target
- crs_source = "EPSG:4326"
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- source('~/apps/R/packages/ruut/R/copy_shp_to_db.R', echo=TRUE)
- cmd
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- copy_shp_to_db <- function(dir = NULL, layer = NULL, conf = NULL, id = "fid",
- crs_source = "EPSG:4326",
- crs_target = "EPSG:4326",
- geometry_type = "PROMOTE_TO_MULTI") {
- # Command 'ogr2ogr' help
- # system("ogr2ogr --long-usage")
- # Kontrollime kas etteantud kataloogis paiknevad failid
- if (length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0) {
- source <- sprintf('"%s" "%s"', dir, layer)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- ruut::db_create_new_schema(conf)
- Sys.sleep(1)
- system(cmd)
- } else {
- cat("\nEi leitud antud kataloogist faile.")
- return(NULL)
- }
- }
- ruut::copy_shp_to_db(dir = tmp_dir, layer = tbl_names[i], conf = config,
- id = fid, crs_source = crs_source, crs_target = crs_target,
- geometry_type = 'POLYGON')
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode,
- conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target,
- geometry_type
- )
- source <- sprintf('"%s" "%s"', dir, layer)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- cmd
- system(cmd)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "%s %s -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
- crs_source <- "EPSG:3035"
- crs_target <- "EPSG:4326"
- fid <- "fid"
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- config
- conf
- config
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- config$dbname, config$host, config$port, config$user, config$password,
- config$sslmode, config$schema, source, fid, config$schema, config$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, fid, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- system(cmd)
- styler:::style_active_file()
- library(ruut)
- styler:::style_active_file()
- styler:::style_active_file()
- library(ruut)
- styler:::style_active_file()
|