Wednesday, 6 July 2016

raster - Create a new GRASS database in R with CRS projection


How to create a new GRASS database in R with CRS projection? I am able to create a new one using the following code but it is not projected


require(raster)
require(spgrass6)
initGRASS("/Applications/GRASS-6.4.app/Contents/MacOS", gisDbase="/Users/junf/Documents",
location='temp',mapset='PERMANENT', override=TRUE)
# gisdbase /Users/junf/Documents
# location temp
# mapset PERMANENT

# rows 1
# columns 1
# north 1
# south 0
# west 0
# east 1
# nsres 1
# ewres 1
# projection NA


Now I tried using a projected GeoTiff using the following code which is better but the projection is still NA:


r <- as(raster('/Users/junf/Documents/WatershedModel/UpperPulangi/upw_srtm30.tif'), 'SpatialGrid')
initGRASS("/Applications/GRASS-6.4.app/Contents/MacOS", gisDbase="/Users/junf/Documents", location='temp',mapset='PERMANENT', override=TRUE,SG=r)
# gisdbase /Users/junf/Documents
# location temp
# mapset PERMANENT
# rows 3317
# columns 2774
# north 950826
# south 849428.7

# west 690839.6
# east 775638
# nsres 30.56898
# ewres 30.569
# projection NA

Can someone please help me create a new GRASS database with WGS 84 (EPSG: 4326) projection or a projection that matches an existing georeferenced image for import in GRASS?



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...