简体   繁体   English

将表格从R导入PostgreSQL DB

[英]Import table from R into PostgreSQL DB

I loaded some spatial data from my PostgreSQL DB into R with the help of the RPostgreSQL-package and ST_AsText : 我借助RPostgreSQL-package和ST_AsText将PostgreSQL数据库中的一些空间数据加载到R中:

dbGetQuery(con, "SELECT id, ST_AsText(geom) FROM table;")

After having done some analyses I want to go back that way. 做完一些分析后,我想回到原来的状态。 My geom column is stil formated as character / WKT. 我的geom列的格式仍为character / WKT。 Unfortunately dbWriteTable doesn't accept similar arguments as dbGetQuery . 不幸的是, dbWriteTable不接受与dbGetQuery类似的参数。

  • What is the best way to import spatial data from R to PostgreSQL? 将空间数据从R导入PostgreSQL的最佳方法是什么?

Up to now the only way I found, is importing the data into the DB and using ST_GeomFromText in an additional step to get my geometry data type. 到目前为止,我发现的唯一方法是将数据导入数据库,并在另一个步骤中使用ST_GeomFromText来获取几何数据类型。

I created a table on my DB by using dbWriteTable() and the postGIStools-package to INSERT the data (it must be a SpatialPolygonsDataFrame). 我使用dbWriteTable()和postGIStools-package在数据库上创建了一个表,用于插入数据(该数据必须是SpatialPolygonsDataFrame)。

## Create an empty table on DB
dbWriteTable(con, name=c("public", "<table>"), value=(dataframe[-c(1:nrow(dataframe)), ]))
require(postGIStools)
## INSERT INTO
postgis_insert(con, df=SpatialPolygonsDataFrame, tbl="table", geom_name="st_astext")
dbSendQuery(con, "ALTER TABLE <table> RENAME st_astext TO geom;")
dbSendQuery(con, "ALTER TABLE <table> ALTER COLUMN geom TYPE geometry;")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM