Writing and reading files

brodata provides functions to store downloaded data in files. This saves the original CSV or XML files exactly as downloaded from BRO or DINO, so the saved files do not depend on the brodata version.

import os
import brodata
download_path = "download"
if not os.path.isdir(download_path):
    os.makedirs(download_path)

Single files

To a single file

to_file = os.path.join(download_path, "GMW000000049567.xml")
brodata.gmw.GroundwaterMonitoringWell.from_bro_id("GMW000000049567", to_file=to_file)
GroundwaterMonitoringWell(broId='GMW000000049567', x=117892.6, y=439651.9)

From a single file

Read the previously downloaded file by supplying its filename to brodata.gmw.GroundwaterMonitoringWell.

fname = to_file
brodata.gmw.GroundwaterMonitoringWell(fname)
GroundwaterMonitoringWell(broId='GMW000000049567', x=117892.6, y=439651.9)

Zip-files

For methods that result in a GeoDataFrame of multiple objects, there is a to_zip parameter, with which you can save the downloaded objects to a zip-file.

To a zip-file

extent = [117700, 118700, 439400, 440400]
to_zip = os.path.join(download_path, "bro_gws.zip")
brodata.gmw.get_data_in_extent(extent, kind=None, to_zip=to_zip, redownload=True);
The default of `combine=False` will change to True in a future version of brodata. Pass combine=False to retain current behavior or combine=True to adopt the future default and silence this warning.

From a zip-file

fname = os.path.join(download_path, "bro_gws.zip")
gdf = brodata.gmw.get_data_in_extent(fname, kind=None)
print("Read {} groundwater monitoring tubes from this zip-file".format(len(gdf)))
Read 20 groundwater monitoring tubes from this zip-file
The default of `combine=False` will change to True in a future version of brodata. Pass combine=False to retain current behavior or combine=True to adopt the future default and silence this warning.

You can also read just part of the locations within a zip-file by still supplying an extent.

gdf = brodata.gmw.get_data_in_extent([117700, 118000, 439400, 440400], kind=None, to_zip=fname)
print("Read {} groundwater monitoring tubes from this zip-file".format(len(gdf)))
Read 8 groundwater monitoring tubes from this zip-file
The default of `combine=False` will change to True in a future version of brodata. Pass combine=False to retain current behavior or combine=True to adopt the future default and silence this warning.

From a zip file downloaded from BROloket or DINOloket

Data can be read from a zip file downloaded from DINOloket or BROloket. The function returns a dictionary with folder names as keys and dictionaries of object types as values.

fname = os.path.join(
    "..", "..", "tests", "data", "r-calje@artesia-water-nl_2024-06-04-12-35-07.zip"
)
brodata.util.read_zipfile(fname)
{'BRO_Grondwatergebruiksysteem': {'GUF000000016723': GroundwaterUtilisationFacility(broId='GUF000000016723')},
 'DINO_GeotechnischSondeeronderzoek': {'S38B00277_00': <PIL.TiffImagePlugin.TiffImageFile image mode=1 size=3808x3396>,
  'S38B00030_00': <PIL.TiffImagePlugin.TiffImageFile image mode=1 size=3810x3400>}}