Cidree / rpostgis

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

ERROR: Geometry type (Polygon) does not match column type (MultiPolygon) #13

Closed JoanViana closed 5 years ago

JoanViana commented 5 years ago

Hi,

Using the command pgInsert I easily create (the table did not exist previously) a table with geom type MultiPolygons (pgi data can be found here) on the database. However, when I perform the same command to insert new polygons (pgi data can be found here), from same source but different extension, it gives me this error:

Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : ERROR:  Geometry type (Polygon) does not match column type (MultiPolygon)
)

There must be a solution using sf package or RPostgreSQL package, but I would appreciate if it can be solved with this excellent interface, directly from sp Spatial Data.

Command I used to perform both inserts is:

pgi = pgInsert(connection, name = c("Tree","tree_valencia"),
               data.obj = polygonTrees, geom = "geom", encoding = c("latin1", "UTF-8"), return.pgi = T)

The sources (shape format) can be found here: Valencia_Trees.zip

Thanks in advance!

dnbucklin commented 5 years ago

Hi @JoanViana , thanks for trying out the package.

This turned out to a be a bug in the test for multi-polygons; it was counting any polygon with a "hole" as a multi-polygon, and thus setting the database table to be multi-polygons as well. The error happens when you try to insert single polygons (without holes) into that table.

A bugfix is applied in dev, install with:

devtools::install_github("mablab/rpostgis", ref = "dev")

I haven't tested extensively so let me know if you have additional issues.

JoanViana commented 5 years ago

Hi @dnbucklin, many thanks for that very quick reply.

I have inserted 260k polygons using dev branch (where bugfix is applied) and everything works perfectly!

Congratulations for the wonderful work you are doing!

dnbucklin commented 5 years ago

Thanks, good to hear that it's working!