PolyToRaster: Creates raster file from polygon matching the 'geoFile'

Description Usage Arguments Value See Also Examples

View source: R/PolyToRaster.R

Description

PolyToRaster takes a NetCDF geogrid as well as a polygon and return the attribute requested from polygon as a raster file.

Usage

1
2
3
4
PolyToRaster(geoFile = NULL, useRfc = FALSE, polygon = NULL,
  polygonAddress = NULL, polygonShapeFile = NULL, field = NULL,
  fun = "last", mask = FALSE, maskValue = 1, getCoverMask = FALSE,
  getCover = FALSE, plot.it = TRUE, parallel = FALSE)

Arguments

geoFile

The geogrid NetCDF file.

useRfc

Logical: if TRUE will use the loaded rfc polygon in rwrfhydro. Default is FALSE

polygon

SpatialPolygon* This is used if you want to use a polygon already loaded in memory.

polygonAddress

Character: Address to where polygonShapeFile is located.

polygonShapeFile

Character: Name of a polygon shapefile.

field

Numeric or character: The value(s) to be transferred. This can be a single number, or a vector of numbers that has the same length as the number of spatial features (polygons).

fun

Function or character: To determine what values to assign to cells that are covered by multiple spatial features. You can use functions such as min, max, or mean, or one of the following character values: 'first', 'last', 'count'.

mask

Logical: If TRUE the values of the input Raster object are 'masked' by the spatial features of polygon shapefile. That is, cells that spatially overlap with the spatial features retain their values, the other cells become NA. Default is FALSE.

maskValue

Numeric: The value to be used for creating the mask. Default is 1.

getCoverMask

Logical: If TRUE, the fraction of each grid cell that is covered by the polygons is returned (and the values of field, fun, mask. The fraction covered is estimated by dividing each cell into 100 subcells and determining presence/absence of the polygon in the center of each subcell

getCover

Logical: If TRUE, the fraction of each grid cell that is covered by the each field is returned as a layer in the returned RasterStack. Name of the RasetrLayers in the RasterStacK is obtained from unique values of field. The fraction covered is estimated by dividing each cell into 100 subcells and determining presence/absence of the polygon in the center of each subcell

plot.it

Logical: If TRUE will plot both the raster and the polygon on the same plot

parallel

Logical: will be used only if GetCover is TRUE

Value

return a RasterLayer or a RasterStack.

See Also

Other geospatial: ExportGeogrid, GetGeogridIndex, GetGeogridSpatialInfo, GetPoly, GetProj, GetRfc, GetTimeZone

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## Not run: 

#Example 1:
# To rasterize the rfc SpatialPolygonsDataFrame :

r <- PolyToRaster(geoFile = "/glade/scratch/arezoo/IOC/ESMF/geo_em.d01.nc.conus_1km",
                 useRfc = TRUE, 
                 field ="BASIN_ID")
                 
# You can get the numbres assign to each BAISN_ID like :

# Example 2: 
# To return a mask with default value of 1 inside the polygons and NA outside

r1 <- PolyToRaster(geoFile = "/glade/scratch/arezoo/IOC/ESMF/geo_em.d01.nc.conus_1km",
                  useRfc = TRUE, 
                  field ="BASIN_ID", 
                  mask = TRUE)

# Example 3:
# To return a mask with value of 5 inside the polygons and NA outside

r2 <- PolyToRaster(geoFile = "/glade/scratch/arezoo/IOC/ESMF/geo_em.d01.nc.conus_1km",
                              useRfc = TRUE, 
                              field ="BASIN_ID", 
                              mask = TRUE,
                              maskValue = 5)

# Example 4:
# To return a raster with values to be the BASIN_ID (convert character to integer numbers)
# with fraction of each grid cell that is covered by the polygons

r3 <- PolyToRaster(geoFile = "/glade/scratch/arezoo/IOC/ESMF/geo_em.d01.nc.conus_1km",
                              useRfc = TRUE, 
                              field ="BASIN_ID",
                              getCover = TRUE,
                              parallel = TRUE))
plot(r3)

# Example 5:
# To read a shapefile (polygon) from disk and 
# return a raster with values of field 
# with fraction of each grid cell that is covered by the polygons

r4 <- PolyToRaster(geoFile = "/glade/scratch/arezoo/IOC/ESMF/geo_em.d01.nc.conus_1km",
                  polygonAddress= "/glade/scratch/arezoo/QPF_verification_rwrfhydro/gis",
                  polygonShapeFile= "HUC4",
                  field ="HUC4")
plot(r4)

## End(Not run)

NCAR/rwrfhydro documentation built on Feb. 28, 2021, 12:47 p.m.