Skip to content

Commit

Permalink
add soil distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 25, 2022
1 parent 3cbeb99 commit d3379fc
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
87 changes: 87 additions & 0 deletions pygef/broxml/mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import annotations

from functools import lru_cache
import polars as pl


class _MappingParameters:
@lru_cache(1)
def dist_table(self) -> pl.DataFrame:
mapping = self.bro_to_dict()
return pl.DataFrame(
{
"geotechnical_soil_name": list(mapping.keys()),
"soil_dist": list(mapping.values()),
}
).sort("geotechnical_soil_name")

@lru_cache(1)
def bro_to_dict(self) -> dict[str, pl.Series]:
# ["rocks", "gravel", "sand", "silt, "clay", "peat"]
BRO_TO_DIST = {
"keien": [1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"keienMetGrind": [0.7, 0.3, 0.0, 0.0, 0.0, 0.0],
"keienMetZand": [0.7, 0.0, 0.3, 0.0, 0.0, 0.0],
"keienMetSilt": [0.7, 0.0, 0.0, 0.3, 0.0, 0.0],
"keienMetKlei": [0.7, 0.0, 0.0, 0.0, 0.3, 0.0],
"keitjes": [1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"keitjesMetGrind": [0.7, 0.3, 0.0, 0.0, 0.0, 0.0],
"keitjesMetZand": [0.7, 0.0, 0.3, 0.0, 0.0, 0.0],
"keitjesMetSilt": [0.7, 0.0, 0.0, 0.3, 0.0, 0.0],
"keitjesMetKlei": [0.7, 0.0, 0.0, 0.0, 0.3, 0.0],
"grind": [0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
"grindMetKeien": [0.3, 0.7, 0.0, 0.0, 0.0, 0.0],
"grindMetKeitjes": [0.3, 0.7, 0.0, 0.0, 0.0, 0.0],
"zwakZandigGrid": [0.0, 0.8, 0.2, 0.0, 0.0, 0.0],
"sterkZandigGrid": [0.0, 0.6, 0.4, 0.0, 0.0, 0.0],
"siltigGrind": [0.0, 0.7, 0.0, 0.3, 0.0, 0.0],
"keiigGrind": [0.3, 0.7, 0.0, 0.0, 0.0, 0.0],
"zand": [0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
"zandMetKeien": [0.3, 0.0, 0.7, 0.0, 0.0, 0.0],
"zandMetKeitjes": [0.3, 0.0, 0.7, 0.0, 0.0, 0.0],
"zwakGrindigZand": [0.2, 0.0, 0.8, 0.0, 0.0, 0.0],
"sterkGrindigZand": [0.4, 0.0, 0.6, 0.0, 0.0, 0.0],
"siltigZand": [0.0, 0.0, 0.7, 0.3, 0.0, 0.0],
"siltigZandMetGrind": [0.0, 0.15, 0.7, 0.15, 0.0, 0.0],
"kleiigZand": [0.0, 0.0, 0.7, 0.0, 0.3, 0.0],
"kleiigZandMetGrind": [0.0, 0.15, 0.7, 0.0, 0.15, 0.0],
"silt": [0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
"siltMetKeien": [0.3, 0.0, 0.0, 0.7, 0.0, 0.0],
"siltMetKeitjes": [0.3, 0.0, 0.0, 0.7, 0.0, 0.0],
"zwakGrindigSilt": [0.0, 0.2, 0.0, 0.8, 0.0, 0.0],
"sterkGrindigSilt": [0.0, 0.4, 0.0, 0.6, 0.0, 0.0],
"zwakZandigSilt": [0.0, 0.0, 0.2, 0.8, 0.0, 0.0],
"zwakZandigSiltMetGrind": [0.0, 0.1, 0.1, 0.8, 0.0, 0.0],
"sterkZandigSilt": [0.0, 0.0, 0.4, 0.6, 0.0, 0.0],
"sterkZandigSiltMetGrind": [0.0, 0.2, 0.2, 0.6, 0.0, 0.0],
"klei": [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
"kleiMetKeien": [0.3, 0.0, 0.0, 0.0, 0.7, 0.0],
"kleiMetKeitjes": [0.3, 0.0, 0.0, 0.0, 0.7, 0.0],
"zwakGrindigeKlei": [0.0, 0.2, 0.0, 0.0, 0.8, 0.0],
"sterkGrindigeKlei": [0.0, 0.4, 0.0, 0.0, 0.6, 0.0],
"zwakZandigeKlei": [0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
"zwakZandigeKleiMetGrind": [0.0, 0.1, 0.1, 0.0, 0.8, 0.0],
"sterkZandigeKlei": [0.0, 0.0, 0.4, 0.0, 0.6, 0.0],
"sterkZandigeKleiMetGrind": [0.0, 0.2, 0.2, 0.0, 0.6, 0.0],
"detritus": [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
"zwakZandigeDetritus": [0.0, 0.0, 0.2, 0.0, 0.0, 0.8],
"sterkZandigeDetritus": [0.0, 0.0, 0.4, 0.0, 0.0, 0.6],
"siltigeDetritus": [0.0, 0.0, 0.0, 0.2, 0.0, 0.8],
"kleiigeDetritus": [0.0, 0.0, 0.0, 0.0, 0.2, 0.8],
"humus": [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
"zwakZandigeHumus": [0.0, 0.0, 0.2, 0.0, 0.0, 0.8],
"sterkZandigeHumus": [0.0, 0.0, 0.4, 0.0, 0.0, 0.6],
"siltigeHumus": [0.0, 0.0, 0.0, 0.2, 0.0, 0.8],
"kleiigeHumus": [0.0, 0.0, 0.0, 0.0, 0.2, 0.8],
"veen": [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
"zwakZandigVeen": [0.0, 0.0, 0.2, 0.0, 0.0, 0.8],
"sterkZandigVeen": [0.0, 0.0, 0.4, 0.0, 0.0, 0.6],
"siltigVeen": [0.0, 0.0, 0.0, 0.2, 0.0, 0.8],
"kleiigVeen": [0.0, 0.0, 0.0, 0.2, 0.0, 0.8],
"gyttja": [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
"bruinkool": [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
}
return {k: pl.Series(v) for k, v in BRO_TO_DIST.items()}


MAPPING_PARAMETERS = _MappingParameters()
20 changes: 18 additions & 2 deletions pygef/broxml/parse_bore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from pygef.broxml import resolvers
from pygef.broxml.xml_parser import read_xml
from pygef.bore import BoreData
from pygef.broxml.mapping import MAPPING_PARAMETERS
from lxml import etree
import polars as pl


# maps keyword argument to:
Expand Down Expand Up @@ -123,7 +125,9 @@
}


def read_bore(file: io.BytesIO | Path | str) -> list[BoreData]:
def read_bore(
file: io.BytesIO | Path | str, include_soil_dist: bool = True
) -> list[BoreData]:
root = etree.parse(file).getroot()
match = re.compile(r"xsd/.*/(\d\.\d)")
matched = match.search(root.nsmap["bhrgtcom"])
Expand All @@ -133,4 +137,16 @@ def read_bore(file: io.BytesIO | Path | str) -> list[BoreData]:
else:
if 3.0 >= float(matched.group(1)) < 2.0:
raise ValueError("only bhrgtcom/2.x is supported ")
return read_xml(root, BoreData, BORE_ATTRIBS_V2, "sourceDocument")
all_bd = read_xml(root, BoreData, BORE_ATTRIBS_V2, "sourceDocument")

if include_soil_dist:
out = []
for bore_data in all_bd:
tbl = MAPPING_PARAMETERS.dist_table()
bore_data.data = bore_data.data.join(
tbl, on="geotechnical_soil_name", how="left"
)
out.append(bore_data)
return out
else:
return all_bd
1 change: 1 addition & 0 deletions pygef/tests/gef/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import polars as pl
import polars.testing

import pygef.gef.geo as geo
import pygef.gef.utils as utils
Expand Down
27 changes: 26 additions & 1 deletion pygef/tests/xml/test_bore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

from pygef import broxml
from pygef.broxml import Location
from pygef.broxml.mapping import MAPPING_PARAMETERS
from datetime import date
import polars as pl


def test_bore_percentages() -> None:
# all soil distributions should be a distribution e.g.
# sum to 100 %
# be all positive
for dist in MAPPING_PARAMETERS.bro_to_dict().values():
assert dist.sum() == 1.0
assert (dist < 0.0).sum() == 0


def test_bore_attributes(bore_xml_v2: str) -> None:
parsed = broxml.read_bore(bore_xml_v2)
assert len(parsed) == 1
Expand Down Expand Up @@ -131,7 +141,22 @@ def test_bore_attributes(bore_xml_v2: str) -> None:
"middelgrof200tot300um",
"middelgrof300tot420um",
],
}
"soil_dist": [
[0.2, 0.0, 0.8, 0.0, 0.0, 0.0],
[0.2, 0.0, 0.8, 0.0, 0.0, 0.0],
[0.0, 0.1, 0.1, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.2, 0.0, 0.8, 0.0],
[0.0, 0.0, 0.7, 0.3, 0.0, 0.0],
[0.0, 0.0, 0.7, 0.3, 0.0, 0.0],
[0.0, 0.0, 0.7, 0.3, 0.0, 0.0],
[0.0, 0.15, 0.7, 0.15, 0.0, 0.0],
[0.0, 0.15, 0.7, 0.15, 0.0, 0.0],
],
},
)
assert bore_data.data.frame_equal(expected, null_equal=True)

Expand Down

0 comments on commit d3379fc

Please sign in to comment.