[GIS] Conversion from CGIAR GEOTIFFs to SRTM3 like HGTs

geotiff-tiffhgtsrtm

Im writing a C program for querying height information from SRTM data. Since reading directly GeoTIFF format is tough job, I decided to load HGT format, which serves great for the purpose.

Since SRTM3 data contains void areas, I found the CGIAR preprocesed data which looks much better. But they only offer geotiff download for 5×5° altogether saved in continous numbered filenames (thus not compatible with degree number naming).

I would like to convert these TIF files (ie srtm_39_03.tif) to HGT and name them properly. Or alternatively please suggest another download site.


So far I realised, that I can convert 1° TIF to HGT easily, but 5° fails:

$ gdal_translate -of SRTMHGT srtm_39_02.tif srtm_39_02.hgt
Input file size is 6001, 6001
0Warning 1: The corner coordinates of the source are not properly aligned on plain latitude/longitude boundaries.
ERROR 1: Image dimensions should be 1201x1201 or 3601x3601.

As of the splitting, it could be probably achieved with gdal_translate -srcwin 0 0 1201 1201 srtm_39_02.tif out.tif, but Im really confused how to split 6001 in 5 parts each 1201 pixels 🙂

Thanks,
zbycz


Update: according to SRTM manual, HGT files overlap with the 1 upmost/rightmost line, so the splitting is clear now. But still would be happy for consultation.


Update2: finally I used SRTM hgt data and wrote my own ANSI-C SRTM parser.

Best Answer

The below R code might be helpful to you. Given a folder of CGIAR SRTM files, the below script will unzip them and rename the files so the filename will include the longitude and latitude of the pixel in the upper left corner (for example 'cgiar_srtm_W155_S05.tif' for upper left corner long=-155, lat=-5).

library(tools) # for file_path_sans_ext

zip_files <- dir('.', pattern='.zip$')

for (zip_file in zip_files) {
    print(zip_file)
    file_base <- file_path_sans_ext(zip_file)
    unzip(zip_file)
    zip_list <- unzip(zip_file, list=TRUE)$Name
    header_file <- zip_list[grepl('.tfw$', zip_list)]
    coords <- round(read.table(header_file))
    ll <- list(coords[5,1], coords[6,1])
    if (ll[[1]] < 0) {
        ll[[1]] <- paste0('W', sprintf('%03i', abs(ll[[1]])))
    } else {
        ll[[1]] <- paste0('E', sprintf('%03i', ll[[1]]))
    }
    if (ll[[2]] < 0) {
        ll[[2]] <- paste0('S', sprintf('%02i', abs(ll[[2]])))
    } else {
        ll[[2]] <- paste0('N', sprintf('%02i', ll[[2]]))
    }
    new_file_base <- paste('cgiar_srtm', ll[[1]], ll[[2]], sep='_')
    for (zipped_file in zip_list) {
        file.rename(zipped_file, gsub(file_base, new_file_base, zipped_file))
    }
}
Related Question