Skip to content

Commit

Permalink
Improvements to BroApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik-Geo committed Jul 10, 2024
1 parent af08ffd commit f9f2f64
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
55 changes: 33 additions & 22 deletions geost/bro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
from lxml import etree

from geost.bro.bro_utils import divide_bbox, get_bbox_criteria
from geost.bro.bro_utils import get_bbox_criteria
from geost.projections import xy_to_ll

Coordinate = TypeVar("Coordinate", int, float)
Expand All @@ -28,6 +28,7 @@ def __init__(self, server_url=r"https://publiek.broservices.nl"):
self.server_url = server_url
self.objects_url = "/objects"
self.search_url = "/characteristics/searches"
self.object_list = []

def get_objects(
self, bro_ids: Union[str, Iterable], object_type: str = "CPT"
Expand Down Expand Up @@ -120,33 +121,43 @@ def search_objects_in_bbox(
)
if response.status_code == 200 and "rejection" not in response.text:
etree_root = etree.fromstring(response.text.encode("utf-8"))
bro_objects = self.__objects_from_etree(etree_root)
self.object_list += self.__objects_from_etree(etree_root)
elif response.status_code == 400 or "groter dan 2000" in response.text:
bro_objects = []
division_levels = int(((xmax - xmin + ymax - ymin)) / 1000)
division_x = (xmax - xmin) / division_levels
for division_level in range(division_levels):
print(
f"More than 2000 object requests in API call, dividing calls. Current call {division_level+1}/{division_levels}"
)
xmin_divided = xmin + (division_level * division_x)
xmax_divided = xmin + ((division_level + 1) * division_x)
response = self.__response_to_bbox(
xmin_divided,
xmax_divided,
ymin,
ymax,
epsg=epsg,
object_type=object_type,
)
etree_root = etree.fromstring(response.text.encode("utf-8"))
bro_objects += self.__objects_from_etree(etree_root)
self.__search_objects_in_divided_bbox(
xmin, xmax, ymin, ymax, epsg=epsg, object_type=object_type
)
else:
raise Warning(
"Selection is invalid and could not be retrieved from the database"
)

return bro_objects
def __search_objects_in_divided_bbox(
self,
xmin: Coordinate,
xmax: Coordinate,
ymin: Coordinate,
ymax: Coordinate,
epsg: str = "28992",
object_type: str = "CPT",
) -> List[str]:
division_levels = int(((xmax - xmin + ymax - ymin)) / 1000)
division_x = (xmax - xmin) / division_levels
for division_level in range(division_levels):
print(
f"More than 2000 object requests in API call, dividing calls. Current call {division_level+1}/{division_levels}"
)
xmin_divided = xmin + (division_level * division_x)
xmax_divided = xmin + ((division_level + 1) * division_x)
response = self.__response_to_bbox(
xmin_divided,
xmax_divided,
ymin,
ymax,
epsg=epsg,
object_type=object_type,
)
etree_root = etree.fromstring(response.text.encode("utf-8"))
self.object_list += self.__objects_from_etree(etree_root)

def __response_to_bbox(
self,
Expand Down
18 changes: 0 additions & 18 deletions geost/bro/bro_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,3 @@ def get_bbox_criteria(xmin, xmax, ymin, ymax):
}
}
return json_line


def divide_bbox(xmin, xmax, ymin, ymax, level=1):
# order: upper left, lower left, upper right, lower right
# xmins = np.zeros(level**level)
# xmaxs = np.zeros(level**level)
# ymins = np.zeros(level**level)
# ymaxs = np.zeros(level**level)

# xmin_coors = np.linspace(xmin, xmax, level + 1)[:level]
# xmax_coors = np.linspace(xmax, xmin, level + 1)[:level][::-1]
# ymin_coors = np.linspace(ymin, ymax, level + 1)[:level]
# ymax_coors = np.linspace(ymax, ymin, level + 1)[:level][::-1]

# for i in range(level):
# xmins[(i + 1) * level : (i + 1) * level + 1] = xmin_coors

return xmin, xmax, ymin, ymax
20 changes: 11 additions & 9 deletions geost/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def get_bro_objects_from_bbox(
ymax=ymax,
object_type=object_type,
)
bro_objects = api.get_objects(bro_ids, object_type=object_type)
bro_objects = api.get_objects(api.object_list, object_type=object_type)
bro_parsed_objects = []
# TODO: The below has to be adjusted for different BRO objects
for bro_object in bro_objects:
Expand All @@ -336,10 +336,11 @@ def get_bro_objects_from_bbox(
collection = BoreholeCollection(
dataframe,
vertical_reference="depth",
horizontal_reference=horizontal_reference,
horizontal_reference=28992,
is_inclined=False,
)
collection.change_vertical_reference(vertical_reference)
collection.change_horizontal_reference(horizontal_reference)

return collection

Expand Down Expand Up @@ -392,30 +393,31 @@ def get_bro_objects_from_geometry(
ymax=geometry.bounds.maxy.values[0],
object_type=object_type,
)
bro_objects = api.get_objects(bro_ids, object_type=object_type)
bro_objects = api.get_objects(api.object_list, object_type=object_type)
bro_parsed_objects = []
# TODO: The below has to be adjusted for different BRO objects
for bro_object in bro_objects:
for i, bro_object in enumerate(bro_objects):
try:
object = SoilCore(bro_object)
except (TypeError, AttributeError): # as err:
pass
# print("Cant read a soil core") # supressed for demo
# print(err)
print(i)
except (TypeError, AttributeError) as err:
print("Cant read a soil core")
print(err)
bro_parsed_objects.append(object.df)

dataframe = pd.concat(bro_parsed_objects).reset_index()

collection = BoreholeCollection(
dataframe,
vertical_reference="depth",
horizontal_reference=horizontal_reference,
horizontal_reference=28992,
is_inclined=False,
)
collection = getattr(collection, geometry_to_selection_function[geometry.type[0]])(
geometry, buffer
)
collection.header = collection.header.reset_index()
collection.change_vertical_reference(vertical_reference)
collection.change_horizontal_reference(horizontal_reference)

return collection
30 changes: 15 additions & 15 deletions tests/test_broapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ def test_search_and_get_cpts_in_bbox(self):
# Note: if test fails, check if BRO database was updated first
api = BroApi()
# Find CPTs
cpts = api.search_objects_in_bbox(
112400, 112500, 442750, 442850, object_type="CPT"
)
api.search_objects_in_bbox(112400, 112500, 442750, 442850, object_type="CPT")
minimum_present_objects = ["CPT000000000787", "CPT000000029403"]
present_objects = [obj for obj in cpts if obj in minimum_present_objects]
present_objects = [
obj for obj in api.object_list if obj in minimum_present_objects
]
assert len(present_objects) == len(minimum_present_objects)
# Get CPTs
cpt_datas = api.get_objects(cpts, object_type="CPT")
cpt_datas = api.get_objects(api.object_list, object_type="CPT")
for cpt_data in cpt_datas:
assert isinstance(cpt_data, _Element)

Expand All @@ -97,19 +97,19 @@ def test_search_and_get_bhrps_in_bbox(self):
# Note: if test fails, check if BRO database was updated first
api = BroApi()
# Find CPTs
bhrps = api.search_objects_in_bbox(
141500, 141700, 455100, 455300, object_type="BHR-P"
)
api.search_objects_in_bbox(141500, 141700, 455100, 455300, object_type="BHR-P")
minimum_present_objects = [
"BHR000000085497",
"BHR000000247842",
"BHR000000120513",
"BHR000000206176",
]
present_objects = [obj for obj in bhrps if obj in minimum_present_objects]
present_objects = [
obj for obj in api.object_list if obj in minimum_present_objects
]
assert len(present_objects) == len(minimum_present_objects)
# Get CPTs
bhr_datas = api.get_objects(bhrps, object_type="BHR-P")
bhr_datas = api.get_objects(api.object_list, object_type="BHR-P")
for bhr_data in bhr_datas:
assert isinstance(bhr_data, _Element)

Expand All @@ -118,19 +118,19 @@ def test_search_and_get_bhrgts_in_bbox(self):
# Note: if test fails, check if BRO database was updated first
api = BroApi()
# Find CPTs
bhrgts = api.search_objects_in_bbox(
141300, 142300, 452700, 453500, object_type="BHR-GT"
)
api.search_objects_in_bbox(141300, 142300, 452700, 453500, object_type="BHR-GT")
minimum_present_objects = [
"BHR000000353592",
"BHR000000353583",
"BHR000000353598",
"BHR000000353600",
]
present_objects = [obj for obj in bhrgts if obj in minimum_present_objects]
present_objects = [
obj for obj in api.object_list if obj in minimum_present_objects
]
assert len(present_objects) == len(minimum_present_objects)
# Get CPTs
bhr_datas = api.get_objects(bhrgts, object_type="BHR-GT")
bhr_datas = api.get_objects(api.object_list, object_type="BHR-GT")
for bhr_data in bhr_datas:
assert isinstance(bhr_data, _Element)

Expand Down

0 comments on commit f9f2f64

Please sign in to comment.