read_VECT moves one GRASS vector object file with attribute data through a temporary GeoPackage file to a terra "SpatVector" object; write_VECT moves a terra "SpatVector" object through a temporary GeoPackage file to a GRASS vector object file. vect2neigh returns neighbour pairs with shared boundary length as described by Markus Neteler, in https://stat.ethz.ch/pipermail/r-sig-geo/2005-October/000616.html. cygwin_clean_temp can be called to try to clean the GRASS mapset-specific temporary directory under cygwin.

read_VECT(vname, layer, type=NULL, flags="overwrite",
    ignore.stderr = NULL)
write_VECT(x, vname, flags="overwrite", ignore.stderr = NULL)
vInfo(vname, layer, ignore.stderr = NULL)
vColumns(vname, layer, ignore.stderr = NULL)
vDataCount(vname, layer, ignore.stderr = NULL)
vect2neigh(vname, ID=NULL, ignore.stderr = NULL, remove=TRUE, vname2=NULL,
 units="k")

Arguments

vname

A GRASS vector file name

layer

a layer name (string); if missing set to default of “1”

type

override type detection when multiple types are non-zero, passed to v.out.ogr

ignore.stderr

default the value set by set.ignore.stderrOption; NULL, taking the value set by set.ignore.stderrOption, can be set to TRUE to silence system() output to standard error; does not apply on Windows platforms

x

A "SpatVector" object moved to GRASS

flags

Character vector containing additional optional flags and/or options for v.in.ogr, particularly "o" and "overwrite"

ID

A valid DB column name for unique identifiers (optional)

remove

default TRUE, remove copied vectors created in vect2neigh

vname2

If on a previous run, remove was FALSE, the name of the temporary vector may be given to circumvent its generation

units

default "k"; see GRASS 'v.to.db' manual page for alternatives

Value

read_VECT imports a GRASS vector object into a "SpatVector" object.

vect2neigh returns a data frame object with left and right neighbours and boundary lengths, also given class GRASSneigh and spatial.neighbour (as used in spdep). The incantation to retrieve the neighbours list is sn2listw(vect2neigh())$neighbours, and to retrieve the boundary lengths: sn2listw(vect2neigh())$weights. The GRASSneigh object has two other useful attributes: external is a vector giving the length of shared boundary between each polygon and the external area, and total giving each polygon's total boundary length.

Author

Roger S. Bivand, e-mail: Roger.Bivand@nhh.no

Examples

run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
  read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") run <- TRUE
GV <- Sys.getenv("GRASS_VERBOSE")
Sys.setenv("GRASS_VERBOSE"=0)
ois <- get.ignore.stderrOption()
set.ignore.stderrOption(TRUE)
#> [1] FALSE
if (run) {
  execGRASS("v.info", map="schools", layer="1")
}
#>  +----------------------------------------------------------------------------+
#>  | Name:            schools                                                   |
#>  | Mapset:          PERMANENT                                                 |
#>  | Location:        nc_basic_spm_grass7                                       |
#>  | Database:        /home/rsb/topics/grassdata                                |
#>  | Title:           Wake County schools (points map)                          |
#>  | Map scale:       1:1                                                       |
#>  | Name of creator: helena                                                    |
#>  | Organization:    NC OneMap                                                 |
#>  | Source date:     Tue Nov  7 19:34:09 2006                                  |
#>  | Timestamp (first layer): none                                              |
#>  |----------------------------------------------------------------------------|
#>  | Map format:      native                                                    |
#>  |----------------------------------------------------------------------------|
#>  |   Type of map: vector (level: 2)                                           |
#>  |                                                                            |
#>  |   Number of points:       167             Number of centroids:  0          |
#>  |   Number of lines:        0               Number of boundaries: 0          |
#>  |   Number of areas:        0               Number of islands:    0          |
#>  |                                                                            |
#>  |   Map is 3D:              No                                               |
#>  |   Number of dblinks:      1                                                |
#>  |                                                                            |
#>  |   Projection: Lambert Conformal Conic                                      |
#>  |                                                                            |
#>  |               N:   248159.84441077    S:   203559.01136227                 |
#>  |               E:   671714.55110786    W:   619215.13388253                 |
#>  |                                                                            |
#>  |   Digitization threshold: 0                                                |
#>  |   Comment:                                                                 |
#>  |                                                                            |
#>  +----------------------------------------------------------------------------+
#> 
if (run) {
  print(vInfo("schools"))
  schs <- read_VECT("schools")
  print(summary(schs))
}
#>      nodes     points      lines boundaries  centroids      areas    islands 
#>          0        167          0          0          0          0          0 
#> primitives      map3d 
#>        167          0 
#>       cat            TAG             NAMESHORT           NAMELONG        
#>  Min.   :  1.0   Length:167         Length:167         Length:167        
#>  1st Qu.: 42.5   Class :character   Class :character   Class :character  
#>  Median : 84.0   Mode  :character   Mode  :character   Mode  :character  
#>  Mean   : 84.0                                                           
#>  3rd Qu.:125.5                                                           
#>  Max.   :167.0                                                           
#>                                                                          
#>    CORECAPACI       MOBILEUNIT       MOBILECAPA      GLEVEL         
#>  Min.   :   0.0   Min.   : 0.000   Min.   :0.00   Length:167        
#>  1st Qu.: 498.5   1st Qu.: 0.000   1st Qu.:0.75   Class :character  
#>  Median : 586.0   Median : 6.000   Median :1.50   Mode  :character  
#>  Mean   : 763.7   Mean   : 7.318   Mean   :1.75                     
#>  3rd Qu.: 903.0   3rd Qu.:11.000   3rd Qu.:2.50                     
#>  Max.   :2294.0   Max.   :49.000   Max.   :4.00                     
#>  NA's   :23       NA's   :19       NA's   :163                      
#>    LOGRADE            HIGRADE            CALENDAR           HASBASE         
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>    ISMAGNET            PHONE            ADDRNUMBER         ADDRPREFIX       
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>    ADDRROOT           ADDRTYPE          ADDRSUFFIX          ADDRCITY        
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>   ADDRZIPCOD             SPED        STATUS             NODEID         
#>  Length:167         Min.   : NA   Length:167         Length:167        
#>  Class :character   1st Qu.: NA   Class :character   Class :character  
#>  Mode  :character   Median : NA   Mode  :character   Mode  :character  
#>                     Mean   :NaN                                        
#>                     3rd Qu.: NA                                        
#>                     Max.   : NA                                        
#>                     NA's   :167                                        
#>    CAPACITYTO         ESL             BOARDDIS2            PROJ_CAP     
#>  Min.   :   0.0   Length:167         Length:167         Min.   :   0.0  
#>  1st Qu.: 543.0   Class :character   Class :character   1st Qu.: 497.0  
#>  Median : 761.0   Mode  :character   Mode  :character   Median : 722.0  
#>  Mean   : 867.0                                         Mean   : 793.1  
#>  3rd Qu.: 992.5                                         3rd Qu.: 975.0  
#>  Max.   :2294.0                                         Max.   :2390.0  
#>  NA's   :20                                             NA's   :2       
#>     NOTES          
#>  Length:167        
#>  Class :character  
#>  Mode  :character  
#>                    
#>                    
#>                    
#>                    
if (run) {
  write_VECT(schs, "newsch", flags=c("o", "overwrite"))
  execGRASS("v.info", map="newsch", layer="1")
}
#> Warning: GDAL Message 6: dataset /tmp/grass8-rsb-481196/RtmpHUcZJT/file7e462c306a8b.gpkg does not support layer creation option ENCODING
#>  +----------------------------------------------------------------------------+
#>  | Name:            newsch                                                    |
#>  | Mapset:          rsb                                                       |
#>  | Location:        nc_basic_spm_grass7                                       |
#>  | Database:        /home/rsb/topics/grassdata                                |
#>  | Title:                                                                     |
#>  | Map scale:       1:1                                                       |
#>  | Name of creator: rsb                                                       |
#>  | Organization:                                                              |
#>  | Source date:     Tue Jan 23 16:37:51 2024                                  |
#>  | Timestamp (first layer): none                                              |
#>  |----------------------------------------------------------------------------|
#>  | Map format:      native                                                    |
#>  |----------------------------------------------------------------------------|
#>  |   Type of map: vector (level: 2)                                           |
#>  |                                                                            |
#>  |   Number of points:       167             Number of centroids:  0          |
#>  |   Number of lines:        0               Number of boundaries: 0          |
#>  |   Number of areas:        0               Number of islands:    0          |
#>  |                                                                            |
#>  |   Map is 3D:              No                                               |
#>  |   Number of dblinks:      1                                                |
#>  |                                                                            |
#>  |   Projection: Lambert Conformal Conic                                      |
#>  |                                                                            |
#>  |               N:   248159.84441077    S:   203559.01136227                 |
#>  |               E:   671714.55110786    W:   619215.13388253                 |
#>  |                                                                            |
#>  |   Digitization threshold: 0                                                |
#>  |   Comment:                                                                 |
#>  |                                                                            |
#>  +----------------------------------------------------------------------------+
#> 
if (run) {
  nschs <- read_VECT("newsch")
  print(summary(nschs))
}
#>       cat             cat_           TAG             NAMESHORT        
#>  Min.   :  1.0   Min.   :  1.0   Length:167         Length:167        
#>  1st Qu.: 42.5   1st Qu.: 42.5   Class :character   Class :character  
#>  Median : 84.0   Median : 84.0   Mode  :character   Mode  :character  
#>  Mean   : 84.0   Mean   : 84.0                                        
#>  3rd Qu.:125.5   3rd Qu.:125.5                                        
#>  Max.   :167.0   Max.   :167.0                                        
#>                                                                       
#>    NAMELONG           CORECAPACI       MOBILEUNIT       MOBILECAPA  
#>  Length:167         Min.   :   0.0   Min.   : 0.000   Min.   :0.00  
#>  Class :character   1st Qu.: 498.5   1st Qu.: 0.000   1st Qu.:0.75  
#>  Mode  :character   Median : 586.0   Median : 6.000   Median :1.50  
#>                     Mean   : 763.7   Mean   : 7.318   Mean   :1.75  
#>                     3rd Qu.: 903.0   3rd Qu.:11.000   3rd Qu.:2.50  
#>                     Max.   :2294.0   Max.   :49.000   Max.   :4.00  
#>                     NA's   :23       NA's   :19       NA's   :163   
#>     GLEVEL            LOGRADE            HIGRADE            CALENDAR        
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>    HASBASE            ISMAGNET            PHONE            ADDRNUMBER       
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>   ADDRPREFIX          ADDRROOT           ADDRTYPE          ADDRSUFFIX       
#>  Length:167         Length:167         Length:167         Length:167        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>                                                                             
#>    ADDRCITY          ADDRZIPCOD             SPED        STATUS         
#>  Length:167         Length:167         Min.   : NA   Length:167        
#>  Class :character   Class :character   1st Qu.: NA   Class :character  
#>  Mode  :character   Mode  :character   Median : NA   Mode  :character  
#>                                        Mean   :NaN                     
#>                                        3rd Qu.: NA                     
#>                                        Max.   : NA                     
#>                                        NA's   :167                     
#>     NODEID            CAPACITYTO         ESL             BOARDDIS2        
#>  Length:167         Min.   :   0.0   Length:167         Length:167        
#>  Class :character   1st Qu.: 543.0   Class :character   Class :character  
#>  Mode  :character   Median : 761.0   Mode  :character   Mode  :character  
#>                     Mean   : 867.0                                        
#>                     3rd Qu.: 992.5                                        
#>                     Max.   :2294.0                                        
#>                     NA's   :20                                            
#>     PROJ_CAP         NOTES          
#>  Min.   :   0.0   Length:167        
#>  1st Qu.: 497.0   Class :character  
#>  Median : 722.0   Mode  :character  
#>  Mean   : 793.1                     
#>  3rd Qu.: 975.0                     
#>  Max.   :2390.0                     
#>  NA's   :2                          
if (run) {
  print(all.equal(names(nschs), as.character(vColumns("newsch")[,2])))
}
#> [1] TRUE
if (run) {
  print(vInfo("roadsmajor"))
}
#>      nodes     points      lines boundaries  centroids      areas    islands 
#>        266          0        355          0          0          0          0 
#> primitives      map3d 
#>        355          0 
if (run) {
  roads <- read_VECT("roadsmajor")
  print(summary(roads))
}
#>       cat          MAJORRDS_      ROAD_NAME          MULTILANE        
#>  Min.   :  1.0   Min.   :  1.0   Length:355         Length:355        
#>  1st Qu.: 89.5   1st Qu.: 91.5   Class :character   Class :character  
#>  Median :178.0   Median :180.0   Mode  :character   Mode  :character  
#>  Mean   :178.0   Mean   :179.8                                        
#>  3rd Qu.:266.5   3rd Qu.:268.5                                        
#>  Max.   :355.0   Max.   :357.0                                        
#>     PROPYEAR         OBJECTID       SHAPE_LEN       
#>  Min.   :   0.0   Min.   :  1.0   Min.   :   20.36  
#>  1st Qu.:   0.0   1st Qu.: 89.5   1st Qu.:  763.32  
#>  Median :   0.0   Median :178.0   Median : 1601.23  
#>  Mean   : 192.7   Mean   :178.0   Mean   : 4934.15  
#>  3rd Qu.:   0.0   3rd Qu.:266.5   3rd Qu.: 9555.59  
#>  Max.   :2025.0   Max.   :355.0   Max.   :64177.26  
if (run) {
  cen_neig <- vect2neigh("census")
  str(cen_neig)
}
#> WARNING: Values in column <cat> will be overwritten
#> WARNING: Values in column <left> will be overwritten
#> WARNING: Values in column <right> will be overwritten
#> WARNING: Values in column <length> will be overwritten
#> Classes ‘GRASSneigh’, ‘spatial.neighbour’ and 'data.frame':	12128 obs. of  3 variables:
#>  $ left  : int  1 1 1 1 2 2 2 2 2 3 ...
#>  $ right : int  2 18 176 177 1 3 20 176 182 2 ...
#>  $ length: num  0.2814 0.1166 0.0849 0.2747 0.2814 ...
#>  - attr(*, "external")= num [1:2537] 0 0 0 0 0.239 ...
#>  - attr(*, "total")= Named num [1:2537] 0.7577 0.9377 0.753 0.0995 1.7322 ...
#>   ..- attr(*, "names")= chr [1:2537] "1" "2" "3" "4" ...
#>  - attr(*, "n")= int 2537
if (run) {
  execGRASS("g.remove", flags="f", name=c("newsch", "newsch1"), type="vector")
}
Sys.setenv("GRASS_VERBOSE"=GV)
set.ignore.stderrOption(ois)
#> [1] TRUE