Skip to content

Commit

Permalink
start with open_geodata_germany
Browse files Browse the repository at this point in the history
  • Loading branch information
anikaweinmann committed Feb 13, 2024
1 parent 145d1e2 commit d6c4175
Show file tree
Hide file tree
Showing 4 changed files with 554 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/grass_gis_helpers/open_geodata_germany/download_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: download_data.py
# AUTHOR(S): Anika Weinmann
#
# PURPOSE: open-geodata-germany: functions for the download
# 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.
#
############################################################################

import os
import grass.script as grass


def check_download_dir(download_dir):
"""Checks if download directory is set. If yes check if folder exists or
creating it. If not set a temporary directory
Args:
download_dir (str): download directory module parameter
Returns:
(str): Path to download directory
"""
if not download_dir:
download_dir = grass.tempdir()
else:
if not os.path.isdir(download_dir):
grass.message(
_(
f"Download folder {download_dir} does not exist and will "
"be created.")
)
os.makedirs(download_dir)
grass.message(f"Download directory: {download_dir}")
return download_dir
99 changes: 99 additions & 0 deletions src/grass_gis_helpers/open_geodata_germany/federal_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: federal_state.py
# AUTHOR(S): Anika Weinmann
#
# PURPOSE: open-geodata-germany: infos and functions to federale states
# 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.
#
############################################################################

import os
import grass.script as grass

FS_ABBREVIATION = {
"Baden-Württemberg": "BW",
"BW": "BW",
"Bayern": "BY",
"BY": "BY",
"Berlin": "BE",
"BE": "BE",
"Brandenburg": "BB",
"BB": "BB",
"Bremen": "HB",
"HB": "HB",
"Hamburg": "HH",
"HH": "HH",
"Hessen": "HE",
"HE": "HE",
"Mecklenburg-Vorpommern": "MV",
"MV": "MV",
"Niedersachsen": "NI",
"NI": "NI",
"Nordrhein-Westfalen": "NW",
"NW": "NW",
"Rheinland-Pfalz": "RP",
"RP": "RP",
"Saarland": "SL",
"SL": "SL",
"Sachsen": "SN",
"SN": "SN",
"Sachsen-Anhalt": "ST",
"ST": "ST",
"Schleswig-Holstein": "SH",
"SH": "SH",
"Thüringen": "TH",
"TH": "TH",
}


def get_federal_states(federal_state, federal_state_file):
"""Get federale state and federal state file module parameters and return
list with federal state abbrevations
Args:
federal_state (str): federal state module parameter
federal_state_file (str): federal state file module parameter
Returns:
(list): list with federale state abbrevations
"""
if federal_state_file:
if not os.path.isfile(federal_state_file):
grass.fatal(
_(
f"Federal state file is given, but file "
"<{federal_state_file}> does not exists."
)
)
with open(federal_state_file) as fs_file:
fs_list_str = fs_file.read().strip()
elif federal_state:
fs_list_str = federal_state.strip()
else:
grass.fatal(
_(
"Neither <federal_state> nor <federal_state_file> are given. "
"Please set one of the two."
)
)
fs_list = []
for fs in fs_list_str.split(","):
fs = fs.strip()
if fs not in FS_ABBREVIATION:
grass.fatal(_(f"Non valid name of federal state: {fs}"))
fs_list.append(FS_ABBREVIATION[fs])
return fs_list
56 changes: 56 additions & 0 deletions src/grass_gis_helpers/open_geodata_germany/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: general.py
# AUTHOR(S): Anika Weinmann
#
# PURPOSE: open-geodata-germany: general functions
# 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.
#
############################################################################

import os
import grass.script as grass


def create_vrt(input_raster_list, output):
"""Create a VRT raster map out of input list, or renaming if only one
raster is inside the list. If the input raster maps are inside other
mapsets they will be copied to the current mapset before the VRT will be
created.
Args:
input_raster_list (list): List with input raster maps
output (str): Name of the output (vrt) raster map
"""
# copy raster maps to current mapset
for rast in input_raster_list:
if "@" in rast:
rast_wo_mapsetname = rast.split("@")[0]
grass.run_command(
"g.copy",
raster=f"{rast},{rast_wo_mapsetname}",
)
input_raster_list = [val.split("@")[0] for val in input_raster_list]
# buildvrt if required + renaming to output name
if len(input_raster_list) > 1:
grass.run_command("g.region", raster=input_raster_list)
grass.run_command(
"r.buildvrt", input=input_raster_list, output=output, quiet=True, overwrite=True
)
else:
grass.run_command(
"g.rename", raster=f"{input_raster_list[0]},{output}", quiet=True, overwrite=True
)
Loading

0 comments on commit d6c4175

Please sign in to comment.