1

I want to extract values from my RGB raster, which are in the buffer(zones) of my 14 station points data. So far the values are extracted with the raster::extract by taking the mean of some values instead of returning integer values.

For the next step I need the first band values as the original integers from the buffers of my points data. The values vary between 0 and 255. My progress so far: rasterfile_example

# Loading the raster
rasterfile <- raster("rasterfile_example.tif")
rasterfile <- projectRaster(rasterfile, crs = "+init=epsg:3035")
# My station_points:
> head(station_points)
    station_latitude_d station_longitude_d buffer
120           52.54304            13.34933   1000
128           52.48581            13.34877   1000
137           52.39841            13.36810   5000
142           52.47319            13.22514   5000
144           52.48945            13.43084   1000
157           52.64417            13.48308   2500

structure(list(station_latitude_d = c(52.543041, 52.485814, 52.398406, 
52.473192, 52.489451, 52.644169, 52.447697, 52.463611, 52.467534, 
52.481709, 52.514072, 52.5066, 52.513606, 52.438115), station_longitude_d = c(13.349326, 
13.348775, 13.368103, 13.225144, 13.430844, 13.483079, 13.64705, 
13.31825, 13.441654, 13.433967, 13.469931, 13.332972, 13.418833, 
13.38772), buffer = c(1000, 1000, 5000, 5000, 1000, 2500, 5000, 
150, 150, 150, 150, 150, 1000, 150)), row.names = c(120L, 128L, 
137L, 142L, 144L, 157L, 162L, 167L, 169L, 170L, 171L, 173L, 174L, 
175L), class = "data.frame")
# read coordinates
coordinates(station_points) <- ~station_longitude_d+station_latitude_d 

# assign CRS
proj4string(station_points) <- "+init=epsg:4326"  

# make sure raster and station points are in the same projection
station_points <- spTransform(station_points, 
    crs(rasterfile)) 

# The extract function I tried:
rasterfile_extract <- raster::extract( 
  rasterfile, 
  station_points,
  buffer = station_points$buffer,
  method = "simple",
  small = FALSE,
  cellnumbers = TRUE,
  weights = FALSE,
  normalizeWeights = FALSE,
  df = TRUE,
  layer=1,
  nl=3
) %>% as.data.frame()

The results from the extract:

> head(rasterfile_extract)
  ID  cells first_band
1  1 272716   255.0000
2  1 272717   255.0000
3  1 272718   244.3879
4  1 272719   213.4301
5  1 272720   209.0000
6  1 272721   209.0000
  • At first blush, your buffer distances appear to be in a distance unit where your data is in a wgs84 geography projection with decimal degrees as units. Also, you do realize that you need to aggregate the values yourself? In your example results the values all represent the same observation. – Jeffrey Evans May 13 at 15:14
  • Thanks for your comment. The buffer size is in meters, as the buffer in the extract function requires, while the station point coordinates mentioned are reprojected in the next step before the extract. So this seems to be working. By aggregating the values myself you mean I should use the cellnumber to get the values? I don't know what you mean by aggregating them by myself exactly. My results are the first 6 lines of the observations from 14 stations. These are all from the first station. – Eva546312 May 13 at 15:49
  • The buffer values are in the projection units and are not required to be in meters. When you extract raster values by an area you return multiple cell values associated with each feature. The ID column represents the rowname of the vector (points, polygons, lines) so, this is what you would summarize by. I recently provided an answer that breaks this all down. Just think of your buffered points as polygons because, functionally it is the same thing. gis.stackexchange.com/questions/361333/… – Jeffrey Evans May 13 at 16:08
  • Thank you Jeffrey, for your elaborate answer. Unfortunately I still couldn't solve the problem with the rgb value merge in the extract, while working through the code you provided on the other post yesterday. The polygons I could create with gBuffer are one little step forward. Yet I don't know how to access the original data from the raster which are covered by the polygons. – Eva546312 May 13 at 18:09
  • Can you use dput on your station_points variable and link to a sample tif file that would work so this can easily be copy/pasted into R as a reproducible example? stackoverflow.com/questions/5963269/… – Roger-123 May 13 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.