how calculate altitude from coordinates data?

Hi community

Im want to calculate the altitude from coordinates data, with library(raster)
But with some countries, like Puerto Rico, I have problems.


df <- tribble(
  "G10263", 41.70,  -8.17,
  "G22652", 2.69,  -16.84,
  "G22656A", 32.67,-17.05,
  "G22659", 32.73,-16.79,

m <- data.frame(lon =df$DI_LONGITUD_NEW , 
                lat = df$DI_LATITUD_NEW )

x <- getData('alt', country = "PRT") # PRT is  Puerto Rico
###### show this ERROR #####
# trying URL ''
# Content type 'application/zip' length 196373 bytes (191 KB)
# downloaded 191 KB
# returning a list of RasterLayer objects
# Warning message:
#   In getData("alt", country = "PRT") :
#   getData will be removed in a future version of raster
# . Please use the geodata package instead

dfull5_PRT <- cbind(m, alt = extract(x, m))

Any idea how make this calculations, maybe with other library


Hi @M_AcostaCH ,

I think the issue might be that the extract() function is looking for a raster, but in your output it shows that x is actually a list. I think you're getting two raster files out of your query, so extract doesn't know what to do.

Try specifying the filepath to one of the raster layers out of the list:

dfull5_PRT <- cbind(m, alt = extract(x[["/path-to-raster-file/PRT1_msk_alt.grd"]], m))
1 Like

Hi @jonesey441

Im download the file for PRT, and put the path in extract but show this error.
Im try two forms.

# form 1
dfull5_PRT <- cbind(m, alt =extract(x[["C:\\Users\\macosta\\Downloads\\PRT_msk_alt\\PRT1_msk_alt.grd"]], m))

# form 2
dfull5_PRT <- cbind(m, alt = raster::extract(x[["C:\\Users\\macosta\\Downloads\\PRT_msk_alt\\PRT1_msk_alt.grd"]], m))

# both show this 
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘extract’ for signature ‘"NULL", "data.frame"’

So, Im change \ for / but show the same error.

@M_AcostaCH will you try just creating the raster variable x on it's own, outside of the extract function. Then double check that based on the syntax you're trying that x is in fact a raster variable?

Also data.frame being NULL? Maybe double check that m isn't blank? Not sure why that would be the case but I don't think x is a data.frame so m would be the only other option.


1 Like

You could make a dummy example, is my first time with raster.

Im put the path of PRT1_msk_alt.grd and dont know how to extract very well the data.

Because I need make this for others countries.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.