[TUTORIAL & tool] DEM : convert GeoTiff to HGT

Started by yip, April 06, 2016, 11:28:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yip

--> Or how to get the Europe DEM into Oruxmaps.



(Please excuse my English)

Hello

For those of us who doesn-t live in Andalucia and cannot enjoy a 5mx5m DEM ;) we need to rely on global DEM sources.

Some years ago, I wrote a tutorial to port ASTER GDEM (30mx30m) to OruxMaps (http://oruxmaps.forosactivos.net/t2757-altitude-and-3d-with-aster-gdem-tutorial">//http://oruxmaps.forosactivos.net/t2757-altitude-and-3d-with-aster-gdem-tutorial)



Recently, Europe has released a comparable dataset, EUDEM (also based on ASTER  :lol: ):

http://www.eea.europa.eu/data-and-maps/data/eu-dem">//http://www.eea.europa.eu/data-and-maps/data/eu-dem , download the ones you want.



Here is a tool to convert these files to HGT so you can use them in Oruxmaps.

-install GDAL tools

-copy the following snippet in a new file and name it "tif2hgt.bat"

-configure line 17 of the script with your GDAL install path

-in Windows, drag&drop (multiple) tif files on the script, they will be converted to HGT.

rem
rem  TIF 2 HGT
rem
rem Purpose: convert TIF DEM into HGT DEM files (Digital Elevation Model)
rem
rem Usage: Drag&Drop multiple TIFs file on this script (but NO directory!)
rem
rem Prerequisites : GDAL utilities and a bit of configuration below
rem warning : no error checking !
rem warning : AVOID whitespaces/specialchars in filenames AND working paths
rem author: yip 15 june 2016

@echo off
setlocal enableextensions enabledelayedexpansion

rem CONFIGURATION
set gdalpath="c:Program FilesGDAL"
rem END CONFIG

rem should not be changed. HGT is almost always this size. only deal with square HGTs. I don't know about poles.
set hgtsize=3600
rem dirty hack for the FOR loops
set /A hgtone=%hgtsize%+1

rem only for displaying progress
for %%z in (%*) do set /A totalfiles+=1

rem MAIN LOOP
rem process each drag&dropped file
for %%F in (%*) do (

set /A currentfile+=1
echo ========Processing [%%~nxF]========

echo [Phase 0] Get info about the TIF with GDAL ...
rem get the size of the tif (eg. 18000x18000)
%gdalpath%gdalinfo.exe %%F | findstr /c:"Size is" > tempdem.out
set /p tifsize=<tempdem.out
for /f "tokens=3,4 delims=, " %%g in (tempdem.out) do (
set xtifsize=%%g & set /A xmax=%%g-2
set ytifsize=%%h & set /A ymax=%%h-2
)
set /A totalsize= !xtifsize! / %hgtsize% * !ytifsize! / %hgtsize%

rem loop for each row of that TIFF
for /l %%X in (0, %hgtsize% , !xmax!) do (

rem loop for each column of that TIFF
for /l %%Y in (0, %hgtsize% , !ymax!) do (

echo [Phase 1] Gdal-translate for window %%X %%Y ,ignore HGT name error ...
%gdalpath%gdal_translate.exe -strict -q -r cubic -eco -of SRTMHGT -srcwin %%X %%Y %hgtone% %hgtone% %%F N99E999.HGT 2>&1 | findstr "Expected" > tempdem.out
set /p goodhgt=<tempdem.out
set goodhgt=!goodhgt:~-12,-1!

echo [Phase 2] rename N99E999.HGT to !goodhgt! and clean the temp files ...
rename N99E999.HGT !goodhgt! && del N99E999.HGT.aux.xml tempdem.out

set /A progress+=1
echo --Done--[!goodhgt!]-[HGT:!progress!/!totalsize!]-[TIF:!currentfile!/%totalfiles%]-----
@title tif2hgt : [TIF:!currentfile!/%totalfiles%] [HGT:!progress!/!totalsize!] Processing ... [%%~nxF]
)
)
)
echo =========== END ( check your files, NO error checking ! ) ==========
@title tif2hgt : END
pause


It should work on Windows 7+. Yes it is a dirty little script. I take no responsability if something goes wrong.



About the mentionned EU-DEM dataset, each TIF transforms into 25 HGT. the whole europe 20GB tif give birth to 4000 .HGT (100GB) . each HGT is 25MB.

Be sure to have loads of free space on your device !!



(About my considerations about HGT format among the other DEMS, see: http://www.oruxmaps.com/foro/viewtopic.php?f=7&t=3611">//http://www.oruxmaps.com/foro/viewtopic.php?f=7&t=3611)



yip



edit : 15 june 2016 :

-"Programs Files" to "Program Files"

-add quotes around Program Files, thanks Maki !

- changed boundaries from TiffSize - 1 to TiffSize -2 to address an issue with ASTER GDEM. I would not be surprised if the HGT generated would be shifted right 1 pixel as a side effect now...

Maki

#1
Well, I don't live in Andalucia (but I thoroughly enjoyed it when I traveled there) but I have 5x5 DTM for my area.



Thanks for your tutorial, I'll try it as soon as possible.



Also, beware of the EUDEM in very rugged areas. I compared with high quality LIDAR scans and while generally fine those DEMs can locally be very off. Example: the Monviso summit is 100 m lower than than the real thing...

Maki

#2
Thanks a bunch, it worked very well with my custom data!



There is only a little glitch, in line 17 you have

set gdalpath=c:Programs FilesGDAL



but it actually should be

set gdalpath="c:Programs FilesGDAL"

with the path within quotes since there is a space in it. At least that's for Windows 10, I didn't try with other Windows versions.



Also, as a side note, thanks for the tips in the other thread you linked, I'll add there some comments about the HDR+BIL format.

yip

#3
thanks Maki !



I've modified the script to fix this quotes issue; there was a typo there also.

There was also a problem with Geotiff sized 3601 , 7201 ... instead of 3600 ... 7200...

should be fixed now. the ASTER GDEM v2 were impacted by this issue.



and I'm glad to know it worked for custom geotiff as well !

Maki

#4
I tried the second version of the script. I checked the results against the previous version and the original file, and it's pixel perfect, no misalignment at all. Also note that my custom dataset is not a geotiff, but a .vrt file that mixes different geotiffs. All the source data however was already reprojected to the right resolution and projection. Trying with different data with other projections/resolution gives bogus results.