Commit 914d5f70 authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

update to species table and methods for using it

parent a81c36c8
import numbers import numbers
def fix_bt(code_in, as_mnp=False, verbose=False): def fix_bt(code_in, as_mnp=False, verbose=False, strict=True):
""" """
Parser for Beheertype codes to repair abbreviated codes and/or MNP extension Parser for Beheertype codes to repair abbreviated codes and/or MNP extension
eg. 02.01 --> N02.01 if as_mnp==False eg. 02.01 --> N02.01 if as_mnp==False
...@@ -18,6 +18,8 @@ def fix_bt(code_in, as_mnp=False, verbose=False): ...@@ -18,6 +18,8 @@ def fix_bt(code_in, as_mnp=False, verbose=False):
N04.02.00 is equivalent to N04.02 N04.02.00 is equivalent to N04.02
:param code_in: beheertypecode in any format :param code_in: beheertypecode in any format
:param as_mnp: return MNP style beheertypecode, default=False :param as_mnp: return MNP style beheertypecode, default=False
:param verbose: verbose feedback
:param strict: raise error if bt code is unexpected
:return: beheertypecode :return: beheertypecode
""" """
...@@ -30,14 +32,20 @@ def fix_bt(code_in, as_mnp=False, verbose=False): ...@@ -30,14 +32,20 @@ def fix_bt(code_in, as_mnp=False, verbose=False):
# Identify parts of code # Identify parts of code
parts = code_in.split(".") parts = code_in.split(".")
if len(parts) == 1: if len(parts) == 1:
raise AssertionError("1. Unexpected BT code: {}".format(code_in)) if strict:
raise AssertionError("1. Unexpected BT code: {}".format(code_in))
else:
return None
elif len(parts) == 2: elif len(parts) == 2:
top, sub = parts top, sub = parts
neer = None neer = None
elif len(parts) == 3: elif len(parts) == 3:
top, sub, neer = parts top, sub, neer = parts
else: else:
raise AssertionError("2. Unexpected BT code: {}".format(code_in)) if strict:
raise AssertionError("2. Unexpected BT code: {}".format(code_in))
else:
return None
# Verify TOP and add "N" is required # Verify TOP and add "N" is required
assert top[0].upper() in ['N', 'W', 'S', 'T', 'A', 'L'], "3. Unexpected BT Code: {}".format(code_in) assert top[0].upper() in ['N', 'W', 'S', 'T', 'A', 'L'], "3. Unexpected BT Code: {}".format(code_in)
......
"""
Module to read MNP species CSV tab and provide several helper dictionaries
"""
import pandas as pd
import os
import pathlib
filepath = pathlib.Path(__file__)
# De volledige Species tabel als Pandas dataframe
species_tab = pd.read_csv(os.path.join(os.path.dirname(filepath), r'resources\mnp_species.csv'), sep=',', comment='#')
# Dictionaries tussen code, lokale naam, wetenschappelijke naam
code2local = dict(zip(species_tab.Species_code, species_tab.Local_name))
local2code = dict(zip(species_tab.Local_name, species_tab.Species_code))
code2scientific = dict(zip(species_tab.Species_code, species_tab.Scientific_name))
scientific2code = dict(zip(species_tab.Scientific_name, species_tab.Species_code))
local2scientific = dict(zip(species_tab.Local_name, species_tab.Scientific_name))
scientific2local = dict(zip(species_tab.Scientific_name, species_tab.Local_name))
code2taxon = dict(zip(species_tab.Species_code, species_tab.taxon))
# Dictionaries to self, is usefull sometimes. Trust me...
local2local = dict(zip(species_tab.Local_name, species_tab.Local_name))
code2code = dict(zip(species_tab.Species_code, species_tab.Species_code))
scientific2scientific = dict(zip(species_tab.Scientific_name, species_tab.Scientific_name))
# Dict between species code and boolean species list
code2sel468 = dict(zip(species_tab.Species_code, species_tab.sel468.map({1: True, 0: False})))
code2sel281 = dict(zip(species_tab.Species_code, species_tab.sel281.map({1: True, 0: False})))
code2sel146 = dict(zip(species_tab.Species_code, species_tab.sel146.map({1: True, 0: False})))
code2selall = dict(zip(species_tab.Species_code, species_tab.selall.map({1: True, 0: False})))
This diff is collapsed.
...@@ -4,15 +4,31 @@ Hans Roelofsen, 14/07/2021 ...@@ -4,15 +4,31 @@ Hans Roelofsen, 14/07/2021
""" """
import os import os
from pathlib import Path import pathlib
import pandas as pd import pandas as pd
import sys
from benb_utils.fix_bt import fix_bt # this only works if the dir containing benb_utils has already been appended to
# sys.path
filepath = pathlib.Path(__file__)
# TODO: dit moet vast handiger kunnen! Idee is natuurlijk om een Pandas DF te kunnen importeren vanuit een andere repo
def get_snl_beheertypen_list(benb_dir): df = pd.read_csv(os.path.join(os.path.dirname(filepath), r'resources/snl_beheertypen.csv'), sep=',', comment='#', quotechar='"')
def get_snl_beheertypen_list(df=df, of='sparse'):
""" """
return snl beheertypen list as pandas df return snl beheertypen list as pandas df
:param benb_dir: directory of benb_utils :param benb_dir: directory of benb_utils
:return: pandas dataframe :return: pandas dataframe
""" """
return pd.read_csv(os.path.join(benb_dir, r'resources/snl_beheertypen.csv'), sep=',', comment='#', quotechar='"') mnp_codes = df.LAND_TYPE_CODE.apply(fix_bt, as_mnp=True)
\ No newline at end of file return {'full': df.assign(mnp_code=mnp_codes),
'sparse': df}[of]
def btcode2description(code):
# Note: dit hoeft niet perse in een functie te staan.
try:
return dict(zip(df.LAND_TYPE_CODE.apply(fix_bt, as_mnp=True),
df.LST_NAME))[fix_bt(code, as_mnp=True)]
except KeyError as e:
print('\n{} is not a valid beheertype\n'.format(code))
sys.exit(0)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment