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

Roger Bivand Roger.Bivand at nhh.no
Mon Mar 28 14:12:43 CEST 2011


On Mon, 28 Mar 2011, Karl Ove Hufthammer wrote:

> 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

If the error was:

> pls1 <- lapply(pls, checkPolygonsHoles)
Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, "rgeos_equals") :
   TopologyException: side location conflict at 78.9364 33.4081

then I can reproduce it, and will examine whether it can be handled - the 
checkPolygonsHoles() function also uses GEOS, but probably should warn 
after failing try(gEquals(...)).

Was this your error?

Throwing bad data at maptools/rgeos is a nice way of stress-testing it.

Roger

>
> step.
>
> So I guess it’s better to stay away from the ‘worldHires’ data set,
> and use GSHHS instead … :-/
>
> -- 
> 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