Skip to content

gBuffer solution to invalid objects problem fails

4 messages · Roman Luštrik, Roger Bivand, Kenny Bell

#
Hi all,

I have been getting the below error a few times. I have seen in several
places that a zero-width gBuffer call will fix the topology problem.
However, in this case, I find that the call to gBuffer generates a 4 point
horizontal line, not solving the problem. Is there another solution? Thanks
for any help!

My code:

library(sp)
library(rgeos)
load("r-sig-geo.RData")

gIntersection(grid_poly, new_poly,
              byid = c(TRUE, FALSE),
              id = as.character(grid_poly at data[,1]),
              # if there are coinciding lines, gIntersection
              # returns a cross.
              drop_lower_td = TRUE,
              checkValidity = TRUE)
# new_poly is invalid
# Error in rgeos::gIntersection(grid_poly, new_poly, byid = c(TRUE, FALSE),
 :
#                                 Invalid objects found
#                               In addition: Warning message:
#                                 In RGEOSUnaryPredFunc(spgeom, byid,
"rgeos_isvalid") :
#                                 Self-intersection at or near point
172.23206269997971 -42.13157792003453

# Try using gBuffer as suggested here:
#
http://gis.stackexchange.com/questions/163445/r-solution-for-topologyexception-input-geom-1-is-invalid-self-intersection-er

new_poly1 <- gBuffer(new_poly, width = 0)
plot(new_poly1)
# Generates a line (i.e. kills the polygon)?
?
  r-sig-geo.RData
<https://www.dropbox.com/s/9ivpcmboodpu6zd/r-sig-geo.RData?dl=0>
?
#
Hi Kenny,

can you provide a small, reproducible example which replicates this
behavior?

Cheers,
Roman
On Wed, May 18, 2016 at 7:08 PM, Kenny Bell <kmb56 at berkeley.edu> wrote:

            

  
    
#
There is an example in the message, but Kenny is ignoring the message from 
gBuffer:

Warning message:
In gBuffer(new_poly, width = 0) :
   Spatial object is not projected; GEOS expects planar coordinates

and the differences in geographical coordinates are small. The warning is 
given for a reason. Intersection is projection-agnostic, but buffer is 
not. I don't know whether this is the reason.

Roger
On Thu, 19 May 2016, Roman Lu?trik wrote:

            

  
    
#
Great, this works:

new_poly <- sp::spTransform(new_poly, "+proj=tmerc +lat_0=0 +lon_0=173
+k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +units=m +no_defs")
grid_poly <- sp::spTransform(grid_poly, "+proj=tmerc +lat_0=0 +lon_0=173
+k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +units=m +no_defs")
new_poly1 <- gBuffer(new_poly, width = 0)
plot(new_poly1)
# Works
gIntersection(grid_poly, new_poly1,
              byid = c(TRUE, FALSE),
              id = as.character(grid_poly at data[,1]),
              # if there are coinciding lines, gIntersection
              # returns a cross.
              drop_lower_td = TRUE,
              checkValidity = TRUE)

Thanks so much Roger!
On Thu, May 19, 2016 at 12:02 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote: