Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import_administrative_boundaries #21

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/grass_gis_helpers/open_geodata_germany/federal_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
############################################################################

import os
import requests
import grass.script as grass

FS_ABBREVIATION = {
Expand Down Expand Up @@ -60,6 +61,66 @@
}


def import_administrative_boundaries(output, aoi=None, level="KRS"):
"""Import administrative boundaries for AOI/region

Args:
output (str): The name for the output vector map with the imported
administrative boundaries
aoi (str): The name of the area of interest for which the
administrative boundaries should be imported; if aoi is set
to None the boundaries will be imported for the current
region
level (str): The level of the administrative boundaries;
STA - Staat
LAN - Länder
RBZ - Regierungsbezirke
KRS - Kreise
VWG - Verwaltungsgemeinschaften
GEM - Gemeinden
"""
# save current region and set region to AOI
if aoi:
reg = f"tmp_region_{grass.tempname(8)}"
grass.run_command("g.region", save=reg)
grass.run_command("g.region", vector=aoi, quiet=True)

# url of administrative boundaries
URL = (
"https://daten.gdz.bkg.bund.de/produkte/vg/vg5000_0101/"
"aktuell/vg5000_01-01.utm32s.shape.ebenen.zip"
)
# file of administrative boundaries in zip
filename = os.path.join(
"vg5000_01-01.utm32s.shape.ebenen",
"vg5000_ebenen_0101",
f"VG5000_{level}.shp",
)
try:
# check if URL is reachable
response = requests.get(URL)
if not response.status_code == 200:
grass.fatal(
(
"The data import of the administrative boundaries are "
"currently not available."
)
)

# download and import administrative boundaries
vsi_url = f"/vsizip/vsicurl/{URL}/{filename}"
grass.run_command(
"v.import",
input=vsi_url,
output=output,
extent="region",
quiet=True,
)
finally:
if aoi:
grass.run_command("g.region", region=reg)


def get_federal_states(federal_state, federal_state_file):
"""Get federal state and federal state file module parameters and return
list with federal state abbreviations
Expand Down