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

    ## Le chargement a nécessité le package : sf

    ## Warning: le package 'sf' a été compilé avec la version R 4.5.3

    ## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is FALSE

    sf <- sf::st_read(file)

    ## Reading layer `example' from data source
    ##   `C:\Users\emman\AppData\Local\Temp\RtmpaEFuPd\Rinstc210270c3360\cleangeo\extdata\example.shp'
    ##   using driver `ESRI Shapefile'
    ## Simple feature collection with 2 features and 1 field
    ## Geometry type: MULTIPOLYGON
    ## Dimension:     XY
    ## Bounding box:  xmin: -120.7856 ymin: 38.40518 xmax: -120.6791 ymax: 38.46756
    ## Geodetic CRS:  WGS 84

    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:1   Mode :logical   GEOM_VALIDITY:1
    ##  NA's               :1   FALSE:1         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:2   Mode:logical   NA's:2
    ##           TRUE:2