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 = "", proxy = FALSE, use_gdal_grass_driver = TRUE, type = NULL,
    flags = "overwrite", Sys_ignore.stdout = FALSE,
    ignore.stderr = get.ignore.stderrOption())
write_VECT(x, vname, flags = "overwrite", ignore.stderr = get.ignore.stderrOption())
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 the first layer will be used

proxy

Default is FALSE. Set as TRUE if you need a SpatVectorProxy object.

use_gdal_grass_driver

Default TRUE. The standalone GDAL-GRASS driver for the vector format will be used if it is installed. The advantage is that no intermediate file needs to be written from GRASS GIS and subsequently read into R; instead the vector layer is read directly from the GRASS GIS database. Please read the Note further below!

type

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

Sys_ignore.stdout

Passed to system.

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 layer into a SpatVector or SpatVectorProxy 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.

Note

Be aware that the GDAL-GRASS driver may have some issues for vector data. In our experience, the error and warning messages for vector data can be ignored. Further, the returned metadata about the coordinate reference system may currently be incomplete, e.g. it may miss the EPSG code.

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) {
  meta <- gmeta()
  location_path <- file.path(meta$GISDBASE, meta$LOCATION_NAME)
  previous_mapset <- meta$MAPSET
  example_mapset <- "RGRASS_EXAMPLES"
  execGRASS("g.mapset", "c", mapset = example_mapset)
}

if (run) {
  execGRASS("v.info", map="schools", layer="1")
}
#>  +----------------------------------------------------------------------------+
#>  | Name:            schools                                                   |
#>  | Mapset:          PERMANENT                                                 |
#>  | Location:        nc_basic_spm_grass7                                       |
#>  | Database:        /home/floris/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 
#> Warning: GDAL Error 1: Cannot reset cursor.
#> Warning: GDAL Error 1: Attributes not found.
#>       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) {
  try({
    write_VECT(schs, "newsch", flags=c("o", "overwrite"))
  })
  schs <- read_VECT("schools", use_gdal_grass_driver = FALSE)
}
#> Error in write_VECT(schs, "newsch", flags = c("o", "overwrite")) : 
#>   This SpatVector already links to layer 'schools' of the data source 'schools' in the GRASS GIS database.
if (run) {
  write_VECT(schs, "newsch", flags=c("o", "overwrite"))
  execGRASS("v.info", map="newsch", layer="1")
}
#> Warning: GDAL Message 6: dataset /tmp/RtmpPXqwfy/file8833110daf77.gpkg does not support layer creation option ENCODING
#>  +----------------------------------------------------------------------------+
#>  | Name:            newsch                                                    |
#>  | Mapset:          RGRASS_EXAMPLES                                           |
#>  | Location:        nc_basic_spm_grass7                                       |
#>  | Database:        /home/floris/grassdata                                    |
#>  | Title:                                                                     |
#>  | Map scale:       1:1                                                       |
#>  | Name of creator: floris                                                    |
#>  | Organization:                                                              |
#>  | Source date:     Tue Jun 18 19:32:18 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))
}
#> Warning: GDAL Error 1: Cannot reset cursor.
#> Warning: GDAL Error 1: Attributes not found.
#>       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))
}
#> Warning: GDAL Error 1: Cannot reset cursor.
#> Warning: GDAL Error 1: Attributes not found.
#>       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 (FALSE) {
  # not run: vect2neigh() currently writes 3 new data sources in the PERMANENT
  # mapset, despite this mapset not being the active one.
  cen_neig <- vect2neigh("census")
  str(cen_neig)
}
if (run) {
  execGRASS("g.remove", flags="f", name=c("newsch", "newsch1"), type="vector")
  execGRASS("g.mapset", mapset = previous_mapset)
  if (example_mapset != previous_mapset) {
    unlink(file.path(location_path, example_mapset), recursive = TRUE)
  }
}
Sys.setenv("GRASS_VERBOSE"=GV)
set.ignore.stderrOption(ois)
#> [1] TRUE