Skip to content

Commit

Permalink
dgm import as tif
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrunn committed Nov 11, 2024
1 parent 29dd933 commit 2d4d58e
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 9 deletions.
5 changes: 3 additions & 2 deletions grass-gis-addons/m.import.rvr/m.import.rvr.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ <h2>SEE ALSO</h2>

<h2>AUTHORS</h2>

Anika Weinmann, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a>
Momen Mawad, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a>
Anika Weinmann, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a></br>
Momen Mawad, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a></br>
Victoria-Leandra Brunn, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a>
18 changes: 13 additions & 5 deletions grass-gis-addons/m.import.rvr/m.import.rvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# MODULE: m.import.rvr
#
# AUTHOR(S): Anika Weinmann and Momen Mawad
# AUTHOR(S): Anika Weinmann, Momen Mawad and Victoria-Leandra Brunn
#
# PURPOSE: Imports data for the processing of buildings analysis,
# green roofs detection and/or trees analysis
Expand Down Expand Up @@ -1470,7 +1470,7 @@ def import_data(data, dataimport_type, output_name, res=None):
elif dataimport_type == "rasterORxyz":
if options[data]:
if os.path.isdir(options[data]):
import_xyz_from_dir(
import_xyz_from_dir(
options[data],
float(options["dtm_resolution"]),
res,
Expand Down Expand Up @@ -1546,12 +1546,20 @@ def compute_data(compute_type, output_name, resolutions=[0.1]):
)
elif compute_type == "ndsm":
for res in resolutions:
dtm = f"dtm_{get_res_str(res)}"
kwargs = {
"dsm": f"dsm_{get_res_str(res)}",
"output_name": output_name,
"dtm": dtm,
}
if options["dtm_file"]:
kwargs["dtm"] = f"dtm_{get_res_str(res)}"
# download DTM
if not options["dtm_file"]:
grass.run_command(
"r.dtm.import.nw",
aoi="study_area",
output=dtm,
flags="r",
)
compute_ndsm(**kwargs)
else:
grass.warning(_(f"Computation of <{compute_type}> not yet supported."))
Expand All @@ -1577,7 +1585,7 @@ def main():

# check if needed addons are installed
check_addon("r.import.ndsm_nrw", "/path/to/r.import.ndsm_nrw")
check_addon("r.import.dtm_nrw", "/path/to/r.import.dtm_nrw")
check_addon("r.dtm.import.nw")

# check if needed paths to data are set
grass.message(_("Checking input parameters ..."))
Expand Down
8 changes: 8 additions & 0 deletions grass-gis-addons/r.dtm.import.nw/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MODULE_TOPDIR = ../..

PGM = r.dtm.import.nw

include $(MODULE_TOPDIR)/include/Make/Script.make
include $(MODULE_TOPDIR)/include/Make/Python.make

default: script
24 changes: 24 additions & 0 deletions grass-gis-addons/r.dtm.import.nw/r.dtm.import.nw.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h2>DESCRIPTION</h2>

<em>r.dtm.import.nw</em> downloads the digital terrain model
(DTM, in German DGM) for Nordrhein-Westfalen (NW) and area of interest.

The data "Digitales Geländemodell - Rasterweite 1 m" is downloaded from
<a href="https://www.opengeodata.nrw.de/produkte/geobasis/hm/dgm1_tiff/dgm1_tiff/">OpenGeodata.NRW</a>
and can be used under the specification of the licence:
<a href="https://www.govdata.de/dl-de/zero-2-0">"Datenlizenz Deutschland - Zero - Version 2.0"</a> licence.


<h2>EXAMPLE</h2>

<h3>Import DTM</h3>

Import DTM with native resolution:
<div class="code"><pre>
r.dtm.import.nw aoi=aoi_NW output=dtm_NW -r
</pre></div>


<h2>AUTHORS</h2>

Victoria-Leandra Brunn, <a href="https://www.mundialis.de/">mundialis GmbH &amp; Co. KG</a>
179 changes: 179 additions & 0 deletions grass-gis-addons/r.dtm.import.nw/r.dtm.import.nw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: r.dtm.import.nw
# AUTHOR(S): Victoria-Leandra Brunn
#
# PURPOSE: Downloads DTM for Nordrhein-Westfalen and aoi
# COPYRIGHT: (C) 2024 by mundialis GmbH & Co. KG and the GRASS
# Development Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
############################################################################

# %module
# % description: Downloads DTM for Nordrhein-Westfalen and aoi.
# % keyword: raster
# % keyword: import
# % keyword: DTM
# % keyword: DGM
# % keyword: open-geodata-germany
# %end

# %option G_OPT_V_INPUT
# % key: aoi
# % description: Polygon of the area of interest to set region
# % required: no
# %end

# %option
# % key: download_dir
# % label: Path to output folder
# % description: Path to download folder
# % required: no
# % multiple: no
# %end

# %option G_OPT_R_OUTPUT
# % description: Name for output raster map
# %end

# %flag
# % key: k
# % label: Keep downloaded data in the download directory
# %end

# %flag
# % key: r
# % label: Use native data resolution
# %end

# %rules
# % requires_all: -k,download_dir
# %end

import atexit
import os
import grass.script as grass

from grass_gis_helpers.cleanup import general_cleanup
from grass_gis_helpers.data_import import (
download_and_import_tindex,
get_list_of_tindex_locations,
)
from grass_gis_helpers.open_geodata_germany.download_data import (
check_download_dir,
)
from grass_gis_helpers.raster import adjust_raster_resolution, create_vrt

# set constant variables
TINDEX = (
"https://github.com/vbrunn/tile-indices/raw/NW_DTM_tileidx/DTM/NW/"
"nw_dtm_tindex_proj.gpkg.gz"
)
DATA_BASE_URL = (
"https://www.opengeodata.nrw.de/produkte/geobasis/hm/dgm1_tiff/"
"dgm1_tiff/"
)

ID = grass.tempname(12)
ORIG_REGION = f"original_region_{ID}"

# set global variables
keep_data = False
download_dir = None
rm_rasters = []
rm_vectors = []


def cleanup():
"""Cleaning up function"""
rm_dirs = []
if not keep_data:
if download_dir:
rm_dirs.append(download_dir)
general_cleanup(
orig_region=ORIG_REGION,
rm_rasters=rm_rasters,
rm_vectors=rm_vectors,
rm_dirs=rm_dirs,
)


def main():
"""Main function of r.dtm.import.nw"""
global rm_rasters, rm_vectors, keep_data, download_dir

aoi = options["aoi"]
download_dir = check_download_dir(options["download_dir"])
output = options["output"]
keep_data = flags["k"]
native_res = flags["r"]

# save original region
grass.run_command("g.region", save=ORIG_REGION, quiet=True)
ns_res = grass.region()["nsres"]

# set region if aoi is given
if aoi:
grass.run_command("g.region", vector=aoi, flags="a")

# get tile index
tindex_vect = f"dtm_tindex_{ID}"
rm_vectors.append(tindex_vect)
download_and_import_tindex(TINDEX, tindex_vect, download_dir)

# get download urls which overlap with aoi
url_tiles = get_list_of_tindex_locations(tindex_vect, aoi)

# import DTM directly
grass.message(_("Importing DTM..."))
all_dtm = []
for url in url_tiles:
dtm_name = os.path.splitext(os.path.basename(url))[0].replace("-", "")
if "/vsicurl/" not in url:
url = f"/vsicurl/{url}"
grass.run_command(
"r.import",
input=url,
output=dtm_name,
extent="region",
overwrite=True,
quiet=True,
)
all_dtm.append(dtm_name)

# resample / interpolate whole VRT (because interpolating single files leads
# to empty rows and columns)
# check resolution and resample / interpolate data if needed
if not native_res:
# create VRT
vrt = f"vrt_dtm_{ID}"
rm_rasters.append(vrt)
create_vrt(all_dtm, vrt)

grass.message(_("Resampling / interpolating data..."))
grass.run_command("g.region", raster=vrt, res=ns_res, flags="a")
adjust_raster_resolution(vrt, output, ns_res)
rm_rasters.extend(all_dtm)
else:
# create VRT
create_vrt(all_dtm, output)

grass.message(_(f"DTM raster map <{output}> is created."))


if __name__ == "__main__":
options, flags = grass.parser()
atexit.register(cleanup)
main()
4 changes: 2 additions & 2 deletions grass-gis-addons/r.import.dtm_nrw/r.import.dtm_nrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def get_required_tiles():
required_tiles_raw = list(product(required_ew_tiles, required_ns_tiles))
required_tiles = []
for tile in required_tiles_raw:
tilename = "dgm1_32_{}_{}_1_nw.xyz.gz".format(tile[0], tile[1])
tilename = "dgm1_32_{}_{}_1_nw_2022.tif.gz".format(tile[0], tile[1])
required_tiles.append(tilename)
return required_tiles

Expand Down Expand Up @@ -246,7 +246,7 @@ def main():
required_tiles = get_required_tiles()
baseurl = (
"https://www.opengeodata.nrw.de/produkte/geobasis/hm/"
"dgm1_xyz/dgm1_xyz/"
"dgm1_tiff/dgm1_tiff/"
)
# check if tiles exist
dl_urls = []
Expand Down

0 comments on commit 2d4d58e

Please sign in to comment.