Cidree / rpostgis

rpostgis: R Interface to a 'PostGIS' Database
http://cidree.github.io/rpostgis/
77 stars 14 forks source link

pgInsert: No column name matches found in database table. #17

Closed mrahiz closed 4 years ago

mrahiz commented 5 years ago

I received the following error when trying to import an ESRI shapefile using pgInsert into a table in my database:

Error in pgInsertize(data.obj, create.table, force.match, conn, new.id, : No column name matches found in database table. Error in pgInsert(connection, name = , c("master", "bounding_box"), geom = "geom", : Table preparation failed. No changes made to database.

My table has the following fields: id, description and geom, whereas my shapefile has the following: FID, shape and id.

Am I missing an argument or a step that triggers the error message?

My code is as follows:

library(rgdal)
library(rpostgis)
location <- "D:/workings" 
shape <- readOGR(dsn=location,layer="input_shapefile")
shape.df <- as(shape,"data.frame") # convert to dataframe
pgi <- pgInsert(con,name=c("master","bounding_box"),data.obj=shape.df,new.id="gid")
dnbucklin commented 5 years ago

You could use the argument partial.match = TRUE to insert only on the columns that have matching names between the existing PostGIS table and the data frame. Note that matching is case-sensitive. In your example, only the column id would transfer.

Also, if your existing PostGIS table has a geometry column, you can just insert the shape Spatial*DataFrame directly, e.g.:

shape <- readOGR(dsn=location,layer="input_shapefile")
pgInsert(con,name=c("master","bounding_box"),data.obj=shape, partial.match = TRUE)
basille commented 4 years ago

Closing it, as there is no activity, and it seems the issue was addressed.