[R-sig-Geo] Handling invalid geometries from the ‘mapdata’ package
Roger Bivand
Roger.Bivand at nhh.no
Mon Mar 28 12:06:09 CEST 2011
On Mon, 28 Mar 2011, Karl Ove Hufthammer wrote:
> I have discovered what looks like a geometry error in data from ‘worldHires’
> data set. Here’s an illustration:
>
> library(rgeos)
> library(rgdal)o
> library(mapdata)
> library(maptools)
>
> x.map = map("worldHires", "Norway", fill=TRUE, col="transparent", plot=FALSE, xlim=c(0,40), ylim=c(50,73))
> x.sp = map2SpatialPolygons(x.map, x.map$names, proj4string=CRS("+init=epsg:4326"))
>
> plot(x.sp) # *Looks* OK
> gPointOnSurface(x.sp) # Error: TopologyException: side location conflict at 3.97911 0.0769463
> gIsValid(x.sp) # Error: Self-intersection at or near point 27.9364 70.0864
> points(27.9364, 70.0864, col="red", pch=19)
>
> I guess this is to be be expected, as the ‘worldHires’ data isn’t of very
> high quality, and is not guaranteed to be consistent. My questions are
> 1) is it possible to automatically ‘fix’ this problem inside R, and
> 2) what other problems may I expect from continuing to use the invalid data?
To start with, it is always advisible to promote the sp SpatialPolygons
object Polygons components to GEOS compatibility:
pls <- slot(x.sp, "polygons")
pls1 <- lapply(pls, checkPolygonsHoles)
slot(x.sp, "polygons") <- pls1
which adds a comment attribute to each Polygons object assigning hole
interior rings to their containing exterior rings.
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)
Fresh releases of rgeos and maptools are on their way to CRAN, but source
may be checked out from R-Forge now (the package builds there are of
yesterday's version).
Hope this helps,
Roger
>
> --
> Regards,
> Karl Ove Hufthammer
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list