cleangeo quickstart guide

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 http://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.

Install cleangeo

For the moment, cleangeo can be installed from its development repository hosted in Github. For this, you will need the devtools package and run:

devtools::install_github("eblondel/cleangeo")

It is planned to publish soon cleangeo to CRAN.

Load cleangeo

To load rsdmx in R, do the following:

library(cleangeo)

Work with cleangeo

Let's load the package and read some some test spatial objects.

file <- system.file("extdata", "example.shp", package = "cleangeo")

require(maptools)
## Loading required package: maptools
## Loading required package: sp
## Checking rgeos availability: TRUE
sp <- readShapePoly(file)

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                           
##                     NA's :0

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 gBuffer(polygon, id = ID, width = 0): Polygons object missing
## comment attribute ignoring hole(s). See function createSPComment.
## Warning in gBuffer(polygon, id = ID, width = 0): Polygons object missing
## comment attribute ignoring hole(s). See function createSPComment.

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                   
##           NA's:0

Spatial objects do not have geometry error anymore! To double check, we can try to check the geometry validity with rgeos:

require(rgeos)
## Loading required package: rgeos
## rgeos version: 0.3-13, (SVN revision 508)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Linking to sp version: 1.2-0 
##  Polygon checking: TRUE
sapply(slot(sp.clean, "polygons"), function(x){
  gIsValid(SpatialPolygons(Srl = list(x)))
})
## [1] TRUE TRUE TRUE

And that's it!