The goal of this document is to get you up and running with cleangeo as quickly as possible.
cleangeo was initially born from some assistance provided to users that were facing issues in processing spatial data in R (see the original post at https://gis.stackexchange.com/questions/113964/fixing-orphaned-holes-in-r).
The main problem with their data is that some spatial objects did not have valid geometries, exposing different types of geometry errors, preventing any easy spatial data processing with rgeos. Then, cleangeo was built in order to facilitate handling and catching rgeos geometry issues, and provide an utility to clean the spatial objects.
This short document shows you how to inspect spatial objects, and clean them with cleangeo.
cleangeo
is available for download from CRAN. To get the latest cleangeo
you can install it from its development repository hosted in Github. For this, you will need the devtools
package and run:
devtools::install_github("eblondel/cleangeo")
To load the package in R, do the following:
library(cleangeo)
## Loading required package: rgeos
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.3
## Warning: multiple methods tables found for 'plot'
## rgeos version: 0.5-1, (SVN revision 614)
## GEOS runtime version: 3.6.1-CAPI-1.10.1
## Linking to sp version: 1.3-1
## Polygon checking: TRUE
## Loading required package: maptools
## Checking rgeos availability: TRUE
## Warning: replacing previous import 'sp::plot' by 'rgeos::plot' when loading
## 'cleangeo'
Let's load the package and read some some test spatial objects.
file <- system.file("extdata", "example.shp", package = "cleangeo")
require(maptools)
sp <- readShapePoly(file)
## Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
The next step is to inspect these spatial objects, in order to detect potential geometry issues, and make a summary
:
report <- clgeo_CollectionReport(sp)
clgeo_SummaryReport(report)
## type valid issue_type
## rgeos_validity:2 Mode :logical GEOM_VALIDITY:2
## NA's :1 FALSE:2 NA's :1
## TRUE :1
By analysing this report, you will see that 2 of the 3 spatial objects are not valid. The issues deal with a problem of geometry validity. Quite interesting to have such comprehensive report, but how to fix these issues? This is where cleangeo can really help you! so let's try to clean these spatial objects.
The below one-line code uses clgeo_Clean
on our spatial objects.
sp.clean <- clgeo_Clean(sp)
## Warning in Polygon(cc[, 1:2], hole = FALSE): less than 4 coordinates in polygon
And now? Well, let's check the new spatial objects!
report.clean <- clgeo_CollectionReport(sp.clean)
clgeo_SummaryReport(report.clean)
## type valid issue_type
## NA's:3 Mode:logical NA's:3
## TRUE:3
Spatial objects do not have geometry error anymore! To double check, we can try to check the geometry validity with rgeos
:
require(rgeos)
sapply(slot(sp.clean, "polygons"), function(x){
gIsValid(SpatialPolygons(Srl = list(x)))
})
## [1] TRUE TRUE TRUE
And that's it!