Skip to content

Commit

Permalink
add patch_vector, import_local_vetor_data
Browse files Browse the repository at this point in the history
  • Loading branch information
juleshaas committed Feb 20, 2024
1 parent 1800573 commit fcd35af
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/grass_gis_helpers/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from .cleanup import rm_vects
from .general import communicate_grass_command
from .raster import adjust_raster_resolution, rename_raster
from .vector import patch_vector


def download_and_import_tindex(tindex_url, output, download_dir):
Expand Down Expand Up @@ -389,3 +390,72 @@ def import_local_xyz_files(
if len(all_raster) > 0:
imported_local_data = True
return imported_local_data


def import_local_vector_data(aoi_map, local_data_dir, fs, rm_vectors, output):
"""Import of vector data from local file path
Args:
aoi_map (str): name of vector map defining AOI
local_data_dir (str): path to local data
fs (str): federal state
rm_vectors (list): List with vectors which should be removed
output (str): output map
Returns:
imported_local_data (bool): True if local data imported, otherwise False
"""
imported_local_data = False

# get files (GPKG or SHP)
files = glob.glob(
os.path.join(local_data_dir, fs, "*.gpkg"),
recursive=True,
)
shp_files = glob.glob(
os.path.join(local_data_dir, fs, "*.shp"), recursive=True
)
files.extend(shp_files)

# import data for AOI
imported_list = []
for i, file in enumerate(files):
if aoi_map:
grass.run_command(
"g.region",
vector=aoi_map,
quiet=True,
)
grass.run_command(
"v.import",
input=file,
output=f"{output}_{i}",
extent="region",
quiet=True,
)
imported_list.append(f"{output}_{i}")
rm_vectors.append(f"{output}_{i}")

# patch outputs
patch_vector(imported_list, output)

# check if result is not empty
map_info = grass.parse_command(
"v.info",
map=output,
flags="gt",
)
if int(map_info["centroids"]) == 0 and fs in ["BW"]:
grass.fatal(_("Local data does not overlap with AOI."))
elif int(map_info["centroids"]) == 0:
grass.message(
_(
"Local data does not overlap with AOI. Data will be downloaded"
" from Open Data portal."
)
)
else:
imported_local_data = True

return imported_local_data

42 changes: 42 additions & 0 deletions src/grass_gis_helpers/vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: vector.py
# AUTHOR(S): Anika Weinmann, Julia Haas
#
# PURPOSE: functions for general vector processing
# 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 grass.script as grass


def patch_vector(vector_list, output):
# patch output from several federal states
if len(vector_list) > 1:
grass.run_command(
"v.patch",
input=vector_list,
output=output,
flags="e",
quiet=True,
)
else:
grass.run_command(
"g.rename",
vector=f"{vector_list[0]},{output}",
quiet=True,
)

0 comments on commit fcd35af

Please sign in to comment.