Skip to content

Commit

Permalink
Update v.in.natura2000
Browse files Browse the repository at this point in the history
Work in progress - dealing with OSGeo#1103
  • Loading branch information
ecodiv committed Sep 28, 2024
1 parent 02330fe commit 21582c5
Showing 1 changed file with 91 additions and 79 deletions.
170 changes: 91 additions & 79 deletions src/vector/v.in.natura2000/v.in.natura2000.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@
import math
import shutil
import tempfile
import grass.script as grass
import grass.script as gs


def main():

n2k_input = options["input"]
n2k_output = options["output"]
pa_sitetype_input = options["sitetype"]
# n2k_input = options["input"]
n2k_input = "/home/paulo/Downloads/Natura2000_end2022.gpkg"
# n2k_output = options["output"]
n2k_output = "Atest"
# pa_sitetype_input = options["sitetype"]
pa_sitetype_input = "A"
habitat_code_input = options["habitat_code"]
species_code_input = options["species_code"]
biogeoreg_long = options["biogeographic_region"]
Expand All @@ -135,7 +138,8 @@ def main():
species_spatial_view = "sv" + species_code_input
biogeoreg_view = "v" + biogeoreg_long2
biogeoreg_spatial_view = "sv" + biogeoreg_long2
ms_input = options["member_state"]
# ms_input = options["member_state"]
ms_input = "NL"
layer_exist = options["existing_layer"]
list_n2k_layer = flags["l"]
list_bg_reg = flags["b"]
Expand All @@ -145,122 +149,132 @@ def main():
list_site_type = flags["t"]
global tmp

# TO ADD
flag_r = "r"

# VARS
naturasites_polygon = "NaturaSite_polygon"

try:
import pyspatialite.dbapi2 as db
import sqlite3 as db
except:
grass.fatal(
"pyspatialite is needed to run this script.\n"
"source: https://pypi.python.org/pypi/pyspatialite \n"
gs.fatal(
"sqlite3 is needed to run this script.\n"
"Please activate/install it in your python stack."
)

if list_n2k_layer:
grass.message("Available data layer(s):")
grass.message("may take some time ...")
grass.message("...")
grass.run_command("v.in.ogr", input=n2k_input, flags="l")
gs.message("Available data layer(s):")
gs.message("may take some time ...")
gs.message("...")
gs.run_command("v.in.ogr", input=n2k_input, flags="l")

if list_bg_reg:
grass.message("Biogeographic regions:")
conn = db.connect("%s" % n2k_input)
gs.message("Biogeographic regions:")
conn = db.connect(n2k_input)
c = conn.cursor()
for row in c.execute(
"SELECT BIOGEFRAPHICREG FROM BIOREGION GROUP BY BIOGEFRAPHICREG"
"SELECT BIOGEOGRAPHICREG FROM BIOREGION GROUP BY BIOGEOGRAPHICREG"
):
grass.message(row)
gs.message(row)
conn.close()

if list_ms:
grass.message("EU member states:")
conn = db.connect("%s" % n2k_input)
gs.message("EU member states:")
conn = db.connect(n2k_input)
c = conn.cursor()
for row in c.execute("SELECT MS FROM Natura2000polygon GROUP BY MS"):
grass.message(row)
for row in c.execute("SELECT MS FROM NaturaSite_polygon GROUP BY MS"):
gs.message(row)
conn.close()

if list_habitats:
grass.message("habitat codes of EU community interest:")
conn = db.connect("%s" % n2k_input)
gs.message("habitat codes of EU community interest:")
conn = db.connect(n2k_input)
c = conn.cursor()
try:
for row in c.execute(
"SELECT HABITATCODE, DESCRIPTION FROM HABITATS GROUP BY HABITATCODE"
):
grass.message(row)
gs.message(row)
except:
pass
grass.warning(
gs.warning(
"Some problems querying habitat code or names occurred."
" Please check columns HABITATCODE and DESCRIPTION of table HABITATS in the sqlite database."
)
conn.close()

if list_species:
grass.message("species codes of EU community interest:")
conn = db.connect("%s" % n2k_input)
gs.message("species codes of EU community interest:")
conn = db.connect(n2k_input)
c = conn.cursor()
try:
for row in c.execute(
"SELECT SPECIESCODE, SPECIESNAME FROM SPECIES GROUP BY SPECIESCODE"
):
grass.message(row)
gs.message(row)
except:
pass
grass.warning(
gs.warning(
"Some problems querying species code or names occurred."
" Please check columns SPECIESCODE and SPECIESNAME of table SPECIES in the sqlite database."
)
conn.close()

if list_site_type:
grass.message("site types:")
conn = db.connect("%s" % n2k_input)
gs.message("site types:")
conn = db.connect(n2k_input)
c = conn.cursor()
for row in c.execute("SELECT SITETYPE FROM NATURA2000SITES GROUP BY SITETYPE"):
grass.message(row)
gs.message(row)
conn.close()

if pa_sitetype_input:
grass.message("importing protected areas of site type: %s" % pa_sitetype_input)
grass.message("may take some time ...")
grass.run_command(
gs.message(f"importing protected areas of site type: {pa_sitetype_input}")
gs.message("may take some time ...")
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="natura2000polygon",
flags=flag_r,
input=n2k_input,
layer=naturasites_polygon,
output=n2k_output,
where="SITETYPE = '%s'" % (pa_sitetype_input),
where=f"SITETYPE = '{pa_sitetype_input}'",
quiet=False,
overwrite=True,
)

if ms_input:
grass.message("importing protected areas of member state: %s" % ms_input)
grass.message("may take some time ...")
grass.run_command(
gs.message(f"importing protected areas of member state: {ms_input}")
gs.message("may take some time ...")
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="natura2000polygon",
input=n2k_input,
layer=naturasites_polygon,
output=n2k_output,
where="MS = '%s'" % (ms_input),
where=f"MS = '{ms_input}'",
quiet=False,
overwrite=True,
)

### HIER GEBLEVEN

if habitat_code_input:
grass.message(
gs.message(
"importing protected areas with habitat (code): %s" % habitat_code_input
)
grass.message("preparing (spatial) views in the sqlite/spatialite database:")
gs.message("preparing (spatial) views in the sqlite/spatialite database:")
conn = db.connect("%s" % n2k_input)
c = conn.cursor()
# create view of defined habitat
grass.message("view: %s" % habitat_view)
gs.message("view: %s" % habitat_view)
sqlhabitat = 'CREATE VIEW "%s" AS ' % (habitat_view)
sqlhabitat += "SELECT * FROM HABITATS "
sqlhabitat += 'WHERE HABITATCODE = "%s" ' % (habitat_code_input)
sqlhabitat += 'ORDER BY "SITECODE"'
grass.message(sqlhabitat)
gs.message(sqlhabitat)
c.execute(sqlhabitat)
# create spatial view of defined habitat - part 1
grass.message("spatial view: %s" % habitat_spatial_view)
gs.message("spatial view: %s" % habitat_spatial_view)
sqlhabitatspatial1 = 'CREATE VIEW "%s" AS ' % (habitat_spatial_view)
sqlhabitatspatial1 += (
'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
Expand All @@ -282,7 +296,7 @@ def main():
sqlhabitatspatial1 += 'FROM "Natura2000polygon" AS "a" '
sqlhabitatspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (habitat_view)
sqlhabitatspatial1 += 'ORDER BY "a"."SITECODE";'
grass.message(sqlhabitatspatial1)
gs.message(sqlhabitatspatial1)
c.execute(sqlhabitatspatial1)
# create spatial view of defined habitat - part 2
sqlhabitatspatial2 = "INSERT INTO views_geometry_columns "
Expand All @@ -291,15 +305,15 @@ def main():
'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);'
% (habitat_spatial_view.lower())
)
grass.message(sqlhabitatspatial2)
gs.message(sqlhabitatspatial2)
# execute spatial vieww
c.execute(sqlhabitatspatial2)
conn.commit()
conn.close()
# import spatial view
grass.message("importing data...")
grass.message("may take some time...")
grass.run_command(
gs.message("importing data...")
gs.message("may take some time...")
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="%s" % (habitat_spatial_view),
Expand All @@ -308,22 +322,22 @@ def main():
)

if species_code_input:
grass.message(
gs.message(
"importing protected areas with species (code): %s" % species_code_input
)
grass.message("preparing (spatial) views in the sqlite/spatialite database:")
gs.message("preparing (spatial) views in the sqlite/spatialite database:")
conn = db.connect("%s" % n2k_input)
c = conn.cursor()
# create view of defined species
grass.message("view: %s" % species_view)
gs.message("view: %s" % species_view)
sqlspecies = 'CREATE VIEW "%s" AS ' % (species_view)
sqlspecies += "SELECT * FROM SPECIES "
sqlspecies += 'WHERE SPECIESCODE = "%s" ' % (species_code_input)
sqlspecies += 'ORDER BY "SITECODE"'
grass.message(sqlspecies)
gs.message(sqlspecies)
c.execute(sqlspecies)
# create spatial view of defined species - part 1
grass.message("spatial view: %s" % species_spatial_view)
gs.message("spatial view: %s" % species_spatial_view)
sqlspeciesspatial1 = 'CREATE VIEW "%s" AS ' % (species_spatial_view)
sqlspeciesspatial1 += (
'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
Expand Down Expand Up @@ -360,7 +374,7 @@ def main():
sqlspeciesspatial1 += 'FROM "Natura2000polygon" AS "a" '
sqlspeciesspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (species_view)
sqlspeciesspatial1 += 'ORDER BY "a"."SITECODE";'
grass.message(sqlspeciesspatial1)
gs.message(sqlspeciesspatial1)
c.execute(sqlspeciesspatial1)
# create spatial view of defined habitat - part 2
sqlspeciesspatial2 = "INSERT INTO views_geometry_columns "
Expand All @@ -369,15 +383,15 @@ def main():
'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);'
% (species_spatial_view.lower())
)
grass.message(sqlspeciesspatial2)
gs.message(sqlspeciesspatial2)
# execute spatial view
c.execute(sqlspeciesspatial2)
conn.commit()
conn.close()
# import spatial view
grass.message("importing data...")
grass.message("may take some time...")
grass.run_command(
gs.message("importing data...")
gs.message("may take some time...")
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="%s" % (species_spatial_view),
Expand All @@ -386,22 +400,22 @@ def main():
)

if biogeoreg_long:
grass.message(
gs.message(
"importing protected areas of biogeographic region: %s" % biogeoreg_long
)
grass.message("preparing (spatial) views in the sqlite/spatialite database:")
gs.message("preparing (spatial) views in the sqlite/spatialite database:")
conn = db.connect("%s" % n2k_input)
c = conn.cursor()
# create view of defined biogeographic region
grass.message("view: %s" % biogeoreg_view)
gs.message("view: %s" % biogeoreg_view)
sqlbioreg = 'CREATE VIEW "%s" AS ' % (biogeoreg_view)
sqlbioreg += "SELECT * FROM BIOREGION "
sqlbioreg += 'WHERE BIOGEFRAPHICREG = "%s" ' % (biogeoreg_long)
sqlbioreg += 'ORDER BY "SITECODE"'
grass.message(sqlbioreg)
gs.message(sqlbioreg)
c.execute(sqlbioreg)
# create spatial view of defined biogeographical region - part 1
grass.message("spatial view: %s" % biogeoreg_spatial_view)
gs.message("spatial view: %s" % biogeoreg_spatial_view)
sqlbioregspatial1 = 'CREATE VIEW "%s" AS ' % (biogeoreg_spatial_view)
sqlbioregspatial1 += 'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
sqlbioregspatial1 += '"a"."RELEASE_DA" AS "RELEASE_DA", "a"."MS" AS "MS", '
Expand All @@ -413,7 +427,7 @@ def main():
sqlbioregspatial1 += 'FROM "Natura2000polygon" AS "a" '
sqlbioregspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (biogeoreg_view)
sqlbioregspatial1 += 'ORDER BY "a"."SITECODE";'
grass.message(sqlbioregspatial1)
gs.message(sqlbioregspatial1)
c.execute(sqlbioregspatial1)
# create spatial view of defined biogeographical region - part 2
sqlbioregspatial2 = "INSERT INTO views_geometry_columns "
Expand All @@ -422,15 +436,15 @@ def main():
'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);'
% (biogeoreg_spatial_view.lower())
)
grass.message(sqlbioregspatial2)
gs.message(sqlbioregspatial2)
# execute spatial view
c.execute(sqlbioregspatial2)
conn.commit()
conn.close()
# import spatial view
grass.message("importing data...")
grass.message("may take some time...")
grass.run_command(
gs.message("importing data...")
gs.message("may take some time...")
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="%s" % (biogeoreg_spatial_view),
Expand All @@ -439,10 +453,8 @@ def main():
)

if layer_exist:
grass.message(
"importing existing spatial layer %s of the dataset" % layer_exist
)
grass.run_command(
gs.message("importing existing spatial layer %s of the dataset" % layer_exist)
gs.run_command(
"v.in.ogr",
input="%s" % (n2k_input),
layer="%s" % (layer_exist),
Expand All @@ -452,5 +464,5 @@ def main():


if __name__ == "__main__":
options, flags = grass.parser()
options, flags = gs.parser()
sys.exit(main())

0 comments on commit 21582c5

Please sign in to comment.