4

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"
newbie
  • 757
  • 1
  • 9
  • 19
  • 1
    Perhaps have a look at `levels(polyg[[field]])`. – Josh O'Brien Feb 18 '16 at 17:05
  • I have added the results of levels, which gives a list of all the unique BASIN_IDs. I guess my question is that if it always use the level (the integer number assigned to it) for rasterizing ? – newbie Feb 18 '16 at 17:13
  • 1
    Yes, it should always use that (and has for me in the past). As always, it's worth double checking at first, and then doing a spot check from time to time – Josh O'Brien Feb 18 '16 at 17:18
  • @JoshO'Brien Do you know how to add this information to the raster file? something like creating a raster attribute table (RAT)? – newbie Feb 18 '16 at 20:42
  • 1
    Yes, I'd add a RAT, [as for example here](http://stackoverflow.com/a/20321664/980833). And then, if you're doing much of this, bundle the several steps involved into a small wrapper function for rasterizing factor attributes and attaching the factor information to the resultant raster as a RAT. Best of luck. – Josh O'Brien Feb 18 '16 at 21:46

0 Answers0