Skip to content

Commit

Permalink
Merge pull request #21 from mundialis/administrative_boundaries
Browse files Browse the repository at this point in the history
Add import_administrative_boundaries
  • Loading branch information
anikaweinmann committed Feb 26, 2024
2 parents beb3a1e + 77adb0c commit 350a45f
Showing 1 changed file with 61 additions and 0 deletions.
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

0 comments on commit 350a45f

Please sign in to comment.