Utility functions for the RGEOS package

getScale()
  setScale(scale=100000000)
  translate(spgeom)
  checkP4S(p4s)
        version_GEOS(runtime = TRUE)
        rgeos_extSoftVersion()
        version_GEOS0()
        set_RGEOS_polyThreshold(value)
        get_RGEOS_polyThreshold()
        set_RGEOS_warnSlivers(value)
        get_RGEOS_warnSlivers()
        set_RGEOS_dropSlivers(value)
        get_RGEOS_dropSlivers()
        get_RGEOS_CheckValidity()
        set_RGEOS_CheckValidity(value)

Arguments

scale

Numeric value determining the precision of translated geometries

spgeom

sp object as defined in package sp

p4s

Either a character string or an object of class CRS

runtime

default TRUE; if FALSE, installation GEOS version

value

the value to be passed to an RGEOS option in its environment

Details

getScale and setScale are used to get and set the scale option in the rgeos environment. This option is used to determine the precision of coordinates when translating to and from GEOS C objects. Precision is defined as 1 / scale. The final example is a use case reported by Mao-Gui Hu, who has also made the objects available, where the default scale defeats an intended line intersection operation; changing the scale temporarily resoves the issue.

In order to permit polygon slivers to be detected, reported and dropped, the user may set a numeric value for polyThreshold and logical values for warnSlivers and dropSlivers. By default, the threshold is 0.0, and warning and dropping are FALSE. To detect slivers, the threshold may be set to a small value and warnSlivers set to TRUE. To drop slivers from returned Polygons and Polygon objects, set dropSlivers to TRUE for a non-zero threshold.

translate is a testing function which translates the sp object into a GEOS C object and then back into an sp object and is used extensively in the translation unit tests. In all cases it is expected that spgeom and translate(spgeom) should be identical.

checkP4S is a validation function for proj4strings and is used in testing.

version_GEOS returns the full runtime version string, and version_GEOS0 only the GEOS version number. set_RGEOS_CheckValidity takes an integer 0:2, 0L is no operation, 1L is check validity, and 2L is check and if invalid try to repair with a zero width buffer.

Author

Roger Bivand & Colin Rundel

Examples

  readWKT("POINT(1.5 1.5)")
#> SpatialPoints:
#>     x   y
#> 1 1.5 1.5
#> Coordinate Reference System (CRS) arguments: NA 

  # With scale set to 1, the following point will be rounded
  setScale(1)
  readWKT("POINT(1.5 1.5)")
#> SpatialPoints:
#>   x y
#> 1 2 2
#> Coordinate Reference System (CRS) arguments: NA 
  
  setScale(10)
  readWKT("POINT(1.5 1.5)")
#> SpatialPoints:
#>     x   y
#> 1 1.5 1.5
#> Coordinate Reference System (CRS) arguments: NA 
  
  getScale()
#> [1] 10
  # Set scale option back to default
  setScale()
  
  
  
  # scale option only affect objects when they are translated through rgeos
  setScale(1)
  library(sp)
  SpatialPoints(data.frame(x=1.5,y=1.5))
#> SpatialPoints:
#>        x   y
#> [1,] 1.5 1.5
#> Coordinate Reference System (CRS) arguments: NA 
  translate( SpatialPoints(data.frame(x=1.5,y=1.5)) )
#> SpatialPoints:
#>   x y
#> 1 2 2
#> Coordinate Reference System (CRS) arguments: NA 
  
  setScale()

        # added example of scale impact on intersection 120905
sline1 <- readWKT(readLines(system.file("wkts/sline1.wkt", package="rgeos")))
sline2 <- readWKT(readLines(system.file("wkts/sline2.wkt", package="rgeos")))
rslt <- gIntersection(sline1, sline2)
#> Warning: GEOS support is provided by the sf and terra packages among others
class(rslt)
#> [1] "SpatialCollections"
#> attr(,"package")
#> [1] "rgeos"
getScale()
#> [1] 1e+08
setScale(1e+6)
rslt <- gIntersection(sline1, sline2)
#> Warning: GEOS support is provided by the sf and terra packages among others
class(rslt)
#> [1] "SpatialLines"
#> attr(,"package")
#> [1] "sp"
sapply(slot(rslt, "lines"), function(x) length(slot(x, "Lines")))
#> [1] 63
rslt <- gLineMerge(rslt, byid=TRUE)
sapply(slot(rslt, "lines"), function(x) length(slot(x, "Lines")))
#> [1] 1
setScale()
get_RGEOS_dropSlivers()
#> [1] FALSE
get_RGEOS_warnSlivers()
#> [1] TRUE
get_RGEOS_polyThreshold()
#> [1] 0
# Robert Hijmans difficult intersection case
load(system.file("test_cases/polys.RData", package="rgeos"))
try(res <- gIntersection(a, b, byid=TRUE))
#> Warning: GEOS support is provided by the sf and terra packages among others
#> output subgeometry 1, row.name: 109 294
#> subsubgeometry 0: Polygon
#> subsubgeometry 1: Polygon
#> subsubgeometry 2: LineString
#> Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false,  : 
#>   Geometry collections may not contain other geometry collections
res <- gIntersection(a, b, byid=TRUE, drop_lower_td=TRUE)
#> Warning: GEOS support is provided by the sf and terra packages among others
sort(unlist(sapply(slot(res, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area"))))
#>  [1] 3.135474e-08 3.926317e-07 5.350546e-07 9.982584e-07 1.434509e-06
#>  [6] 2.087862e-06 1.212778e+03 3.117775e+05 1.424050e+06 1.700212e+06
#> [11] 7.269743e+06
oT <- get_RGEOS_polyThreshold()
oW <- get_RGEOS_warnSlivers()
oD <- get_RGEOS_dropSlivers()
set_RGEOS_polyThreshold(1e-3)
set_RGEOS_warnSlivers(TRUE)
res1 <- gIntersection(a, b, byid=TRUE, drop_lower_td=TRUE)
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: 1: MultiPolygon object 109 294 area 1.02961e-06
#> Warning: 2: MultiPolygon object 202 294 area 3.915e-06
#> Warning: 3: Polygon object 208 294 area 5.35055e-07
#> Warning: Exterior ring 0 of object 109 294 area 3.13547e-08
#> Warning: Exterior ring 1 of object 109 294 area 9.98258e-07
#> Warning: Exterior ring 0 of object 202 294 area 2.08786e-06
#> Warning: Exterior ring 1 of object 202 294 area 1.43451e-06
#> Warning: Exterior ring 2 of object 202 294 area 3.92632e-07
#> Warning: Exterior ring 0 of object 208 294 area 5.35055e-07
sort(unlist(sapply(slot(res, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area"))))
#>  [1] 3.135474e-08 3.926317e-07 5.350546e-07 9.982584e-07 1.434509e-06
#>  [6] 2.087862e-06 1.212778e+03 3.117775e+05 1.424050e+06 1.700212e+06
#> [11] 7.269743e+06
set_RGEOS_dropSlivers(TRUE)
res2 <- gIntersection(a, b, byid=TRUE, drop_lower_td=TRUE)
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: 1: MultiPolygon object 109 294 area 1.02961e-06
#> Warning: 2: MultiPolygon object 202 294 area 3.915e-06
#> Warning: 3: Polygon object 208 294 area 5.35055e-07
sort(unlist(sapply(slot(res, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area"))))
#>  [1] 3.135474e-08 3.926317e-07 5.350546e-07 9.982584e-07 1.434509e-06
#>  [6] 2.087862e-06 1.212778e+03 3.117775e+05 1.424050e+06 1.700212e+06
#> [11] 7.269743e+06
set_RGEOS_dropSlivers(FALSE)
oo <- gUnaryUnion(res1, c(rep("1", 3), "2", "3", "4"), checkValidity=2L)
#> Warning: Too few points in geometry component at or near point 3001878.09776633 2416352.9858511998
#> Warning: Too few points in geometry component at or near point 3000956.1033775401 2413481.790976
#> Warning: Too few points in geometry component at or near point 3000956.1033775401 2413481.790976
#> res1 is invalid
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: 1: Polygon object 109 294 area 9.67871e-07
#> Warning: 2: Polygon object 202 294 area 2.02431e-06
#> Warning: Exterior ring 0 of object 109 294 area 9.67871e-07
#> Warning: Exterior ring 0 of object 202 294 area 2.02431e-06
#> Attempting to make res1 valid by zero-width buffering
#> Warning: Exterior ring 1 of object 1 area 2.99218e-06
unlist(sapply(slot(oo, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area")))
#> [1] 1.700212e+06 2.992178e-06 7.269743e+06 3.117775e+05 1.212778e+03
#> [6] 1.424050e+06
ooo <- gIntersection(b, oo, byid=TRUE, checkValidity=2L)
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: spgeom1 and spgeom2 have different proj4 strings
#> Warning: Exterior ring 1 of object 294 1 area 2.99216e-06
gArea(ooo, byid=TRUE)
#>     294 1     294 2     294 3     294 4 
#> 1700211.9 7269743.1  312990.3 1424050.3 
unlist(sapply(slot(ooo, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area")))
#> [1] 1.700212e+06 2.992165e-06 7.269743e+06 3.117775e+05 1.212778e+03
#> [6] 1.424050e+06
set_RGEOS_dropSlivers(TRUE)
ooo <- gIntersection(b, oo, byid=TRUE, checkValidity=2L)
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: spgeom1 and spgeom2 have different proj4 strings
#> Warning: Exterior ring 1 of object 294 1 area 2.99216e-06
gArea(ooo, byid=TRUE)
#>     294 1     294 2     294 3     294 4 
#> 1700211.9 7269743.1  312990.3 1424050.3 
unlist(sapply(slot(ooo, "polygons"), function(p) sapply(slot(p, "Polygons"), slot, "area")))
#> [1] 1700211.935 7269743.131  311777.515    1212.778 1424050.306
set_RGEOS_polyThreshold(oT)
set_RGEOS_warnSlivers(oW)
set_RGEOS_dropSlivers(oD)