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 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. Then, cleangeo was built in order to facilitate handling and catching 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

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:

remotes::install_github("eblondel/cleangeo")

Load cleangeo

To load the package 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(sf)
## Loading required package: sf
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is FALSE
sf <- sf::st_read(file)
## Reading layer `example' from data source 
##   `/tmp/RtmpHK2cuM/Rinsta744aab1e4a/cleangeo/extdata/example.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 3 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -120.7992 ymin: 38.33745 xmax: -120.6791 ymax: 38.46756
## CRS:           NA
sp <- as(sf, "Spatial")

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
##  geos_error_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)

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