【R】cleangeo
2021年8月15日
					1. はじめに
cleangeoは、geospatialデータを解析してエラーがあれば修正してくれるパッケージです。
2. インストール
Githubからインストールできます。
devtools::install_github("eblondel/cleangeo")
3. つかってみる
Rにある例のshpデータを使ってみます。
library(cleangeo)
require(maptools)
file <- system.file("extdata", "example.shp", package = "cleangeo")
sp <- readShapePoly(file)
sp
An object of class "SpatialPolygonsDataFrame"
Slot "data":
  id
0  1
1  2
2  3
Slot "polygons":
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -120.68001   38.40576
Slot "area":
[1] 7.167663e-07
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
          [,1]     [,2]
[1,] -120.6791 38.40589
[2,] -120.6792 38.40584
[3,] -120.6808 38.40518
[4,] -120.6804 38.40604
[5,] -120.6791 38.40608
[6,] -120.6791 38.40589
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -120.68001   38.40576
Slot "ID":
[1] "0"
Slot "area":
[1] 7.167663e-07
[[2]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -120.79351   38.34549
Slot "area":
[1] 0.0001487693
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
           [,1]     [,2]
 [1,] -120.7961 38.35437
 [2,] -120.7954 38.35427
 [3,] -120.7954 38.35427
レポートを見てみると2件のエラーがあることがわかります。
sp.report <- clgeo_CollectionReport(sp) sp.summary <- clgeo_SummaryReport(sp.report) sp.summary
> sp.summary
             type     valid                 issue_type
 rgeos_validity:2   Mode :logical   GEOM_VALIDITY:2   
 NA's          :1   FALSE:2         NA's         :1   
                    TRUE :1     
clgeo_Clean()関数でクリーニングできます。
sp.fixed <- clgeo_Clean(sp, verbose = TRUE) sp.fixed.report <- clgeo_CollectionReport(sp.fixed) sp.fixed.summary <- clgeo_SummaryReport(sp.fixed.report) sp.fixed.summary
> sp.fixed.summary
   type    valid         issue_type
 NA's:3   Mode:logical   NA's:3    
          TRUE:3 
4. さいごに
shape fileはちょくちょく使うので、今後使ってみたいと思います。