0

I am trying to get elevation value in meter using R programming.

I have loaded raster library in rstudio:

elevation <- getData("alt", country = "BD")

How I can get those value with lat lon?

Here is the documentation for the function I used: getData

0

1 Answer 1

1

If you convert the raster to a data frame with xy=TRUE you get a data frame with the coordinates and the altitude:

> ed = as.data.frame(elevation,xy=TRUE)
> head(ed)
         x        y BGD_msk_alt
1 87.90417 26.69583          NA
2 87.91250 26.69583          NA
3 87.92083 26.69583          NA
4 87.92917 26.69583          NA
5 87.93750 26.69583          NA
6 87.94583 26.69583          NA

The NA values are because you have a rectangular grid which is masked to the country boundary - anything outside the boundary is given NA. If you don't want those, remove them by subsetting rows on the third column:

> ed2 = ed[!is.na(ed[,3]),]
> head(ed2)
            x        y BGD_msk_alt
4766 88.41250 26.62917         105
5353 88.40417 26.62083         105
5354 88.41250 26.62083         103
5940 88.39583 26.61250         103
5941 88.40417 26.61250         102
5942 88.41250 26.61250         101

You can then use standard R functions (eg write.table) to produce a CSV, which is well documented elsewhere.

4
  • it is worked. thank you so much. you made my day! would you tell me the 'BGD_msk_alt' is in meter or feet? May 4, 2020 at 12:44
  • I think its metres but I would check that - find a region you know the rough altitude of (from an authoritative source) and compare with the raster - you should be able to see if it is a factor of three out if your assumption about the units is wrong.
    – Spacedman
    May 4, 2020 at 13:24
  • would you tell me how to save it as lat.tif,long.tif and elevation.tif separately? you helped a lot, again, I like to give thank you so much in advance. May 16, 2020 at 17:07
  • What do you mean "save it"? To a file? In what format? And how do you mean "separately"? Do you want to write three text files, or three tiff files with the long, lat, and elevation separately? This sound more like a new question! Make a new one!
    – Spacedman
    May 16, 2020 at 19:07

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.