How to keep the attributes when rasterizing, correspondence between field and numbers used in rasterize

1k Views Asked by At

I want to rasterize a polygon, if I define the field, it is not returning the attribute. For examle, the following polygon has 22 polygon and 13 unique BASIN_ID. If I rasterize based on BASIN_ID which are type factor/character, it will assign a numeric to each. but how can I find out which number refers to which polygon?

polyg
class       : SpatialPolygonsDataFrame
features    : 22
extent      : -2207945, 2235431, -1667188, 1584226  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=30 +lat_2=60 +lat_0=40.0000076293945 +lon_0=-97 +x_0=0 +y_0=0 +a=6370000 +b=6370000 +units=m +no_defs
variables   : 5
 names       : SITE_ID, STATE,           RFC_NAME, RFC_CITY, BASIN_ID
 min values  :     ALR,    CA, Arkansas-Red Basin,  Atlanta,    ABRFC
 max values  :     TUA,    UT,          West Gulf,    Tulsa,    WGRFC

This is the command used to rasterize:

>   rnew <- raster::rasterize(x = polyg,
+                               y = r,
+                               field = field)

I will get:

> rnew
class       : RasterLayer
dimensions  : 3253, 4444, 14456332  (nrow, ncol, ncell)
resolution  : 1000, 1000  (x, y)
extent      : -2208000, 2236000, -1668001, 1584999  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=30 +lat_2=60 +lat_0=40.0000076293945 +lon_0=-97 +x_0=0 +y_0=0 +a=6370000 +b=6370000 +units=m +no_defs
data source : in memory
names       : layer
values      : 1, 13  (min, max)

enter image description here

I guess I could find out the correspondence using the following (not sure), but I am looking for an option in rasterize which does this for me.

   polyg@data$BASIN_ID as.numeric(polyg@data$BASIN_ID)
  1                MBRFC                               7
  2                CBRFC                               3
  3                ABRFC                               1
  4                CNRFC                               4
  5                LMRFC                               5
  6                MARFC                               6
  7                NCRFC                               8
  8                NERFC                               9
  9                NWRFC                              10
  10               OHRFC                              11
  11               SERFC                              12
  12               WGRFC                              13
  13               WGRFC                              13
  14               WGRFC                              13
  15               WGRFC                              13
  16               WGRFC                              13
  17               WGRFC                              13
  18               WGRFC                              13
  19               WGRFC                              13
  20               WGRFC                              13
  21               WGRFC                              13
  22               WGRFC                              13

Thanks,

 levels(rfc[['BASIN_ID']])
   [1] "ABRFC" "AKRFC" "CBRFC" "CNRFC" "LMRFC" "MARFC" "MBRFC" "NCRFC" "NERFC"
   [10] "NWRFC" "OHRFC" "SERFC" "WGRFC"
0

There are 0 best solutions below