Преглед на файлове

Lisatud 'copy_shp_to_db'.

Ardo Kubjas преди 5 години
родител
ревизия
d5ee960678
променени са 8 файла, в които са добавени 153 реда и са изтрити 62 реда
  1. 2 1
      NAMESPACE
  2. 17 12
      R/copy_qgis_object_to_db.R
  3. 47 0
      R/copy_shp_to_db.R
  4. 1 5
      R/pk_boundary_box.R
  5. 0 37
      man/copy_polygon_to_db.Rd
  6. 41 0
      man/copy_qgis_object_to_db.Rd
  7. 44 0
      man/copy_shp_to_db.Rd
  8. 1 7
      man/pk_bbox.Rd

+ 2 - 1
NAMESPACE

@@ -2,7 +2,8 @@
 
 export(cat_function)
 export(convert_sf2qgis)
-export(copy_polygon_to_db)
+export(copy_qgis_object_to_db)
+export(copy_shp_to_db)
 export(db_all_schemas)
 export(db_connect)
 export(db_create_new_schema)

+ 17 - 12
R/copy_polygon_to_db.R → R/copy_qgis_object_to_db.R

@@ -1,36 +1,41 @@
-#' Polügoni kopeerimine andmebaasi
+#' QGIS objekti kopeerimine andmebaasi
 #'
-#' Funktsioon kopeerib sf polügooni postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
-#' @param x	object of "qgis_result" spatial polygon.
+#' Funktsioon kopeerib QGIS objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
+#' @param x	object of "qgis_result" spatial object.
 #' @param conf	list database configuration. Default get_config()
 #' @param id field andmebaasi unikaalne id veerg. Default 'fid'
 #' @param crs_source CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.
 #' @param crs_target CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.
-#' @keywords postgis, polygon
+#' @param geometry_type str Force a geometry type for new layer.  One of: NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR.  Add "25D" for 3D layers.
+#' @keywords postgis, QGIS object
 #' @export
 #' @examples
 #' ## Not run:
 #' ##
-#' ## copy_polygon_to_db()
+#' ## copy_qgis_object_to_db()
 #' ##
 #' ## End(**Not run**)
-copy_polygon_to_db <- function(x = NULL, conf = NULL, id = "fid",
-                               crs_source = "EPSG:4326",
-                               crs_target = "EPSG:4326") {
+copy_qgis_object_to_db <- function(x = 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")
   if (!is.null(x) && class(x) == "qgis_result") {
     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 POLYGON ",
+        "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 POLYGON"
+        "-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
+      conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target,
+      geometry_type
     )
     ruut::db_create_new_schema(conf)
-    Sys.sleep(2)
+    Sys.sleep(1)
     system(cmd)
   } else {
     NULL

+ 47 - 0
R/copy_shp_to_db.R

@@ -0,0 +1,47 @@
+#' ESRI Shapefile kopeerimine andmebaasi
+#'
+#' Funktsioon kopeerib ESRI Shapefile objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat conf <- ruut::get_conf().
+#' @param dir	path directory of the shape files.
+#' @param layer	str layer name.
+#' @param conf	list database confuration. Default get_conf()
+#' @param id field andmebaasi unikaalne id veerg. Default 'fid'
+#' @param crs_source CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.
+#' @param crs_target CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.
+#' @param geometry_type str Force a geometry type for new layer.  One of: NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR.  Add "25D" for 3D layers.
+#' @keywords postgis, ESRI Shapefile
+#' @export
+#' @examples
+#' ## Not run:
+#' ##
+#' ## copy_qgis_object_to_db()
+#' ##
+#' ## End(**Not run**)
+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 --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, 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)
+  }
+}

+ 1 - 5
R/pk_boundary_box.R

@@ -2,11 +2,7 @@
 #'
 #' Funktsioon leiab etteantud piirkonnale piirikasti (boundary box). Tulemuseks on qgis_vektor.
 #' @param x	object of class sf, sfc or sfg.
-#' @param type num select value: 1-3
-#' - 0: Envelope (Bounding Box
-#' - 1: Minimum Oriented Rectangle
-#' - 2: Minimum Enclosing Circle
-#' - 3: Convex Hull
+#' @param type num select value: 1-3. 0: Envelope (Bounding Box), 1: Minimum Oriented Rectangle, 2: Minimum Enclosing Circle, 3: Convex Hull.
 #' @keywords piirkond, boundarybox
 #' @export
 #' @examples

+ 0 - 37
man/copy_polygon_to_db.Rd

@@ -1,37 +0,0 @@
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/copy_polygon_to_db.R
-\name{copy_polygon_to_db}
-\alias{copy_polygon_to_db}
-\title{Polügoni kopeerimine andmebaasi}
-\usage{
-copy_polygon_to_db(
-  x = NULL,
-  conf = NULL,
-  id = "fid",
-  crs_source = "EPSG:4326",
-  crs_target = "EPSG:4326"
-)
-}
-\arguments{
-\item{x}{object of "qgis_result" spatial polygon.}
-
-\item{conf}{list database configuration. Default get_config()}
-
-\item{id}{field andmebaasi unikaalne id veerg. Default 'fid'}
-
-\item{crs_source}{CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.}
-
-\item{crs_target}{CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.}
-}
-\description{
-Funktsioon kopeerib sf polügooni postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
-}
-\examples{
-## Not run:
-##
-## copy_polygon_to_db()
-##
-## End(**Not run**)
-}
-\keyword{polygon}
-\keyword{postgis,}

+ 41 - 0
man/copy_qgis_object_to_db.Rd

@@ -0,0 +1,41 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/copy_qgis_object_to_db.R
+\name{copy_qgis_object_to_db}
+\alias{copy_qgis_object_to_db}
+\title{QGIS objekti kopeerimine andmebaasi}
+\usage{
+copy_qgis_object_to_db(
+  x = NULL,
+  conf = NULL,
+  id = "fid",
+  crs_source = "EPSG:4326",
+  crs_target = "EPSG:4326",
+  geometry_type = "PROMOTE_TO_MULTI"
+)
+}
+\arguments{
+\item{x}{object of "qgis_result" spatial object.}
+
+\item{conf}{list database configuration. Default get_config()}
+
+\item{id}{field andmebaasi unikaalne id veerg. Default 'fid'}
+
+\item{crs_source}{CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.}
+
+\item{crs_target}{CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.}
+
+\item{geometry_type}{str Force a geometry type for new layer.  One of: NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR.  Add "25D" for 3D layers.}
+}
+\description{
+Funktsioon kopeerib QGIS objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
+}
+\examples{
+## Not run:
+##
+## copy_qgis_object_to_db()
+##
+## End(**Not run**)
+}
+\keyword{QGIS}
+\keyword{object}
+\keyword{postgis,}

+ 44 - 0
man/copy_shp_to_db.Rd

@@ -0,0 +1,44 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/copy_shp_to_db.R
+\name{copy_shp_to_db}
+\alias{copy_shp_to_db}
+\title{ESRI Shapefile kopeerimine andmebaasi}
+\usage{
+copy_shp_to_db(
+  dir = NULL,
+  layer = NULL,
+  conf = NULL,
+  id = "fid",
+  crs_source = "EPSG:4326",
+  crs_target = "EPSG:4326",
+  geometry_type = "PROMOTE_TO_MULTI"
+)
+}
+\arguments{
+\item{dir}{path directory of the shape files.}
+
+\item{layer}{str layer name.}
+
+\item{conf}{list database confuration. Default get_conf()}
+
+\item{id}{field andmebaasi unikaalne id veerg. Default 'fid'}
+
+\item{crs_source}{CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.}
+
+\item{crs_target}{CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.}
+
+\item{geometry_type}{str Force a geometry type for new layer.  One of: NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR.  Add "25D" for 3D layers.}
+}
+\description{
+Funktsioon kopeerib ESRI Shapefile objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat conf <- ruut::get_conf().
+}
+\examples{
+## Not run:
+##
+## copy_qgis_object_to_db()
+##
+## End(**Not run**)
+}
+\keyword{ESRI}
+\keyword{Shapefile}
+\keyword{postgis,}

+ 1 - 7
man/pk_bbox.Rd

@@ -9,13 +9,7 @@ pk_bbox(x = NULL, type = 0)
 \arguments{
 \item{x}{object of class sf, sfc or sfg.}
 
-\item{type}{num select value: 1-3
-\itemize{
-\item 0: Envelope (Bounding Box
-\item 1: Minimum Oriented Rectangle
-\item 2: Minimum Enclosing Circle
-\item 3: Convex Hull
-}}
+\item{type}{num select value: 1-3. 0: Envelope (Bounding Box), 1: Minimum Oriented Rectangle, 2: Minimum Enclosing Circle, 3: Convex Hull.}
 }
 \description{
 Funktsioon leiab etteantud piirkonnale piirikasti (boundary box). Tulemuseks on qgis_vektor.