How can you extract an attribute value from a raster from a point (lat/long)?

Example data: Raster with attribute table - value of interest is temperature

I have a list of points with lat/long values, which I want to extract the temperature values for. The raster contains multiple attribute values, temp is not the "pixel" value

  • 1
    What software or language are you using? Also, have you searched this question? It's a very common/basic one and it's likely it's already been answered for you. – Jon Jan 17 '19 at 22:06
  • Well I am trying to do this in R using the raster library. I've also tried georasters and rasterio. I can get the values, but there are other attributes in the raster I want to return. That is the issue. Yes I have searched. – Jim Jan 17 '19 at 22:09
  • BTW - The values I want to capture are in the associated VAT.dbf table. Source file is a GeoTiff created in ArcGIS. Thanks. – Jim Jan 17 '19 at 22:40
  • Assuming that your rasters are also in a geographic projection, all you have to do is pass a matrix of lat,long coordinates to the raster extract function. This will a vector of values corresponding to each row in the matrix. Or, in the case of a raster stack, a data.frame where columns represent each rasters values, still organized by row. – Jeffrey Evans Oct 18 '19 at 16:01

As it is you do not include a reproducible example. But assuming you're using R and you have a single layer raster:

location = data.frame(x = -68.8901643,
                      y = 44.9142272)
coordinates(location) <- ~x+y
mypoints = SpatialPoints(location,proj4string = CRS("+init=epsg:4326"))

myproj = CRS(myraster)
points.proj = spTransform(mypoints, myproj)
myvalues = extract(raster, points.proj)

If you have more than one lat/long pair you can extract everything at once by using a data.frame with more rows, e.g.:

location = data.frame(x = c(-68.8901643,-70,-72,-74),
                      y = c(44.9142272,45,47,49))
  • I am new to R so I don't don't know whether to use extract or some other command for selecting by lat/long values. Your example doesn't provide lat long parameters. I just want to return an attribute value for a given lat/long location in a raster. Thanks. – Jim Jan 18 '19 at 15:18
  • I tried val = cellFromXY(data_tif, c(-68.8901643,44.9142272)) but it doesn't return the value from the VAT. – Jim Jan 18 '19 at 15:31
  • @Jim ok see the updated answer – JepsonNomad Jan 18 '19 at 17:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.