Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Roelofsen, Hans
benb_utils
Commits
914d5f70
Commit
914d5f70
authored
Jan 12, 2022
by
Roelofsen, Hans
Browse files
update to species table and methods for using it
parent
a81c36c8
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
fix_bt.py
View file @
914d5f70
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
eg. 02.01 --> N02.01 if as_mnp==False
...
...
@@ -18,6 +18,8 @@ def fix_bt(code_in, as_mnp=False, verbose=False):
N04.02.00 is equivalent to N04.02
:param code_in: beheertypecode in any format
:param as_mnp: return MNP style beheertypecode, default=False
:param verbose: verbose feedback
:param strict: raise error if bt code is unexpected
:return: beheertypecode
"""
...
...
@@ -30,14 +32,20 @@ def fix_bt(code_in, as_mnp=False, verbose=False):
# Identify parts of code
parts
=
code_in
.
split
(
"."
)
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
:
top
,
sub
=
parts
neer
=
None
elif
len
(
parts
)
==
3
:
top
,
sub
,
neer
=
parts
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
assert
top
[
0
].
upper
()
in
[
'N'
,
'W'
,
'S'
,
'T'
,
'A'
,
'L'
],
"3. Unexpected BT Code: {}"
.
format
(
code_in
)
...
...
mnp_species.py
0 → 100644
View file @
914d5f70
"""
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
})))
resources/mnp_species.csv
View file @
914d5f70
This diff is collapsed.
Click to expand it.
snl_beheertypen.py
View file @
914d5f70
...
...
@@ -4,15 +4,31 @@ Hans Roelofsen, 14/07/2021
"""
import
os
from
pathlib
import
P
ath
import
p
ath
lib
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
:param benb_dir: directory of benb_utils
:return: pandas dataframe
"""
return
pd
.
read_csv
(
os
.
path
.
join
(
benb_dir
,
r
'resources/snl_beheertypen.csv'
),
sep
=
','
,
comment
=
'#'
,
quotechar
=
'"'
)
\ No newline at end of file
mnp_codes
=
df
.
LAND_TYPE_CODE
.
apply
(
fix_bt
,
as_mnp
=
True
)
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment