Create a new GRASS database in R with CRS projection

I came up with the following approach which is adopted from R-sig-Geo. First, create a new location via initGRASS using the default PERMANENT mapset. Next, set the desired coordinate reference system (CRS). And finally, create a new mapset which will automatically inherit the CRS from PERMANENT.

library(rgrass7)

## initialize new location and baseline mapset with required epsg code
gisBase <- system("grass72 --config path", intern = TRUE)

initGRASS(gisBase = gisBase, home = raster::tmpDir(), 
          gisDbase = "~/grassdata", location = "tmp", 
          mapset = "PERMANENT", override = TRUE)

execGRASS("g.proj", flags = "c", epsg = 4326)

## initialize new mapset inheriting projection info
execGRASS("g.mapset", flags = "c", mapset = "new_mapset")

Ideally, you should call g.mapset and g.region with print option enabled afterwards to check if everything is running smoothly.

execGRASS("g.mapset", flags = "p")
# new_mapset

execGRASS("g.region", flags = "p")
# projection: 3 (Latitude-Longitude)
# zone:       0
# datum:      wgs84
# ellipsoid:  wgs84
# north:      1N
# south:      0
# west:       0
# east:       1E
# nsres:      1
# ewres:      1
# rows:       1
# cols:       1
# cells:      1

I found that it is possible on rgrass7 but not on spgrass6: https://grasswiki.osgeo.org/wiki/GRASS_Location_Wizard

require(rgrass7)
# use epsg code
system('"C:/Program Files/GRASS GIS 7.0.3/grass70" -c epsg:32651 D:/temp/use_epsg')

# use a georeferenced raster
system('"C:/Program Files/GRASS GIS 7.0.3/grass70" -c D:/folder/srtm30.tif D:/temp/use_raster')

# connect to grass database
initGRASS(gisBase='C:/Program Files/GRASS GIS 7.0.3', gisDbase='D:/temp', location='use_raster', mapset='PERMANENT', override = TRUE)
# gisdbase    D:/temp 
# location    rtss 
# mapset      PERMANENT 
# rows        748 
# columns     944 
# north       1253702 
# south       1230844 
# west        732001.4 
# east        760855.3 
# nsres       30.55878 
# ewres       30.56557 
# projection  +proj=utm +no_defs +zone=51 +a=6378137 +rf=298.257223563 +towgs84=0.000,0.000,0.000 +to_meter=1 

Tags:

R

Raster

Grass