Cidree / rpostgis

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

pgInsert hang with SPDF #12

Closed imprompt closed 5 years ago

imprompt commented 5 years ago

I have RStudio running R v3.5.0 with rpostgis 1.4.1 running under CentOS 7.5 connecting to a postgres DB:

PostgreSQL 10.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28), 64-bit

Problem: pgInsert() hangs with no feedback when using spatial data.

What works: test <- data.frame(x=0, y=0, a=1) pgInsert(conn, name=c("public", "test_table1"), data.obj=test)

What doesn't work (hangs or crashes R): coordinates(test) <- ~x + y pgInsert(conn, name=c("public", "test_table2"), data.obj=test)

The database has the postgis extension What would be the best way to diagnose the problem?

imprompt commented 5 years ago

This works: test <- data.frame(x=0, y=0, a=1) pgInsert(conn, name=c("public", "test_table1"), data.obj=test) pgMakePts(conn, name = c("public", "test_table1"), srid = 4326)

dnbucklin commented 5 years ago

Hi Malcolm,

I'm not able to replicate the issue. Do you see any messages printed in the R console prior to the hang/crash?

imprompt commented 5 years ago

Running using Rscript in a terminal gives the following:

Creating new table...
Using writeWKT from rgeos package...

 *** caught segfault ***
address 0x1, cause 'memory not mapped'

Traceback:
 1: rgeos::writeWKT(data.obj, byid = TRUE)
 2: pgInsertizeGeom(data.obj, geom, create.table, force.match, conn,     new.id, row.names, alter.names, partial.match, df.mode, geog)
 3: doTryCatch(return(expr), name, parentenv, handler)
 4: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 5: tryCatchList(expr, classes, parentenv, handlers)
 6: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        sm <- strsplit(conditionMessage(e), "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && isTRUE(getOption("show.error.messages"))) {        cat(msg, file = outFile)        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
 7: try(pgi <- pgInsertizeGeom(data.obj, geom, create.table, force.match,     conn, new.id, row.names, alter.names, partial.match, df.mode,     geog))
 8: pgInsert(conn, name = c("public", "test_table2"), data.obj = test)
An irrecoverable exception occurred. R is aborting now ...
Segmentation fault (core dumped)
imprompt commented 5 years ago

Same crash when writing spatial polygons when running:

e <- extent(c(0, 100, 0, 100))
e <- as(e, 'SpatialPolygons')
e <- SpatialPolygonsDataFrame(e, data=data.frame(a=1, b=2))
pgInsert(conn, name=c("public", "test_table3"), data.obj=e)
imprompt commented 5 years ago

For points I can use the pgMakePts workaround, but for polygons I can't see a way to add a polygons column after writing a non-geometric data frame.

dnbucklin commented 5 years ago

Could it be an issue with your rgeos package? Try:

library(rgeos)

test <- data.frame(x=0, y=0, a=1)
coordinates(test) <- ~x + y
writeWKT(test)
[1] "POINT (0.0000000000000000 0.0000000000000000)"

If so you may need to check/re-install your system GEOS package.

imprompt commented 5 years ago

Thanks. It looks like regos is the problem, but reinstalling did not fix it. So not a problem with rpostgis. Thanks for your help.

dnbucklin commented 5 years ago

If you install the package wkb, that would allow for limited use of pgInsert (e.g. for single-part points and polygons). But for all other geometry types, it needs the rgeos package and you'd still get the error. Good luck!

imprompt commented 5 years ago

Working after building the latest system geos from source. Thanks for the package.

basille commented 5 years ago

Good to see that the problem is solved! @imprompt, for the record, could your give your previous and current versions of geos here (I understand the problem was not rgeos but the geos library directly)? Thanks!

imprompt commented 5 years ago

yum install geos: installed v3.4.1, which caused problem installed v3.7.0 from source to fix.

basille commented 5 years ago

Thanks!