[R-sig-Geo] Handling invalid geometries from the ‘mapdata’ package

Karl Ove Hufthammer karl at huftis.org
Mon Mar 28 13:07:29 CEST 2011


Roger Bivand wrote:

> Your problem cannot be solved automatically, but here from zooming in on
> the problem area:
> 
> gIsValid(x.sp)
> plot(x.sp, xlim=c(27.9, 28), ylim=c(69.9, 70.1))
> 
> and eyeballing the bounding boxes of the Polygons objects:
> 
> lapply(pls1, bbox)
> 
> it looks as though 26 is the problem:
> 
> plot(x.sp[26], add=TRUE, border="orange")
> 
> x.sp1 <- x.sp[-26]
> gIsValid(x.sp1)
> gPointOnSurface(x.sp1)

Thanks! That helped. ‘Norway:Tana’ is really a very strange polygon ... :)

I also tried running your procedure on the whole ‘worldHires’, to see if 
this is a frequent problem. The idea was to run

  status=gIsValid(x.sp, byid=TRUE)
  status[!status]

to get a list of invalid polygons (though perhaps the SpatialPolygons object 
may be invalid even if all the sub-polygons are valid?)

First I had to remove the proj4string, as some of the polygons are outside 
the [-180, 180] range. And it turned out that I got an error message already 
at the

  pls <- slot(x.sp, "polygons")
  pls1 <- lapply(pls, checkPolygonsHoles)
  slot(x.sp, "polygons") <- pls1

step.

So I guess it’s better to stay away from the ‘worldHires’ data set,
and use GSHHS instead … :-/

-- 
Karl Ove Hufthammer



More information about the R-sig-Geo mailing list