{ "cells": [ { "cell_type": "markdown", "id": "f10ff34c-1e9b-499c-bd78-b7d3275d9dc2", "metadata": {}, "source": [ "# Writing and reading files\n", "`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." ] }, { "cell_type": "code", "execution_count": null, "id": "77a7313e-20a3-4a1c-87b2-4d9d199c1751", "metadata": {}, "outputs": [], "source": [ "import os\n", "import brodata" ] }, { "cell_type": "code", "execution_count": null, "id": "d395de05-189e-4c7b-9b3e-4f9471cebbf1", "metadata": {}, "outputs": [], "source": [ "download_path = \"download\"\n", "if not os.path.isdir(download_path):\n", " os.makedirs(download_path)" ] }, { "cell_type": "markdown", "id": "c6d7a352-cafa-4cfb-8498-0a337436ec8f", "metadata": {}, "source": [ "## Single files" ] }, { "cell_type": "markdown", "id": "ed8b7432-c7a8-4bb8-ba81-6dc2b3ba11f4", "metadata": {}, "source": [ "### To a single file" ] }, { "cell_type": "code", "execution_count": null, "id": "f3214ab6-0d0f-4756-b98b-7666072e0bb3", "metadata": {}, "outputs": [], "source": [ "to_file = os.path.join(download_path, \"GMW000000049567.xml\")\n", "brodata.gmw.GroundwaterMonitoringWell.from_bro_id(\"GMW000000049567\", to_file=to_file)" ] }, { "cell_type": "markdown", "id": "ad489e5d-9393-4f80-851f-148593ccdee4", "metadata": {}, "source": [ "### From a single file\n", "Read the previously downloaded file by supplying its filename to `brodata.gmw.GroundwaterMonitoringWell`." ] }, { "cell_type": "code", "execution_count": null, "id": "9d38d5bb-41c6-4604-a094-e302cab7932f", "metadata": {}, "outputs": [], "source": [ "fname = to_file\n", "brodata.gmw.GroundwaterMonitoringWell(fname)" ] }, { "cell_type": "markdown", "id": "e53eb2ee-a638-474c-93ae-1dfcf0240fd1", "metadata": {}, "source": [ "## Zip-files\n", "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." ] }, { "cell_type": "markdown", "id": "9112c274-7dff-4641-a8fb-97a57db07df2", "metadata": {}, "source": [ "### To a zip-file" ] }, { "cell_type": "code", "execution_count": null, "id": "842bb90a-5a5f-4725-8406-d1c901e70a03", "metadata": {}, "outputs": [], "source": [ "extent = [117700, 118700, 439400, 440400]\n", "to_zip = os.path.join(download_path, \"bro_gws.zip\")\n", "brodata.gmw.get_data_in_extent(extent, kind=None, to_zip=to_zip, redownload=True);" ] }, { "cell_type": "markdown", "id": "7ef7ed62-892f-49bf-8b20-6c53aa8e4041", "metadata": {}, "source": [ "### From a zip-file" ] }, { "cell_type": "code", "execution_count": null, "id": "e822a542-c2ad-4d3c-b287-c57b8cbe3c50", "metadata": {}, "outputs": [], "source": [ "fname = os.path.join(download_path, \"bro_gws.zip\")\n", "gdf = brodata.gmw.get_data_in_extent(fname, kind=None)\n", "print(\"Read {} groundwater monitoring tubes from this zip-file\".format(len(gdf)))" ] }, { "cell_type": "markdown", "id": "74e86c47-9db1-4bbf-83a4-75002cb03aa3", "metadata": {}, "source": [ "You can also read just part of the locations within a zip-file by still supplying an extent." ] }, { "cell_type": "code", "execution_count": null, "id": "2c51a99d-668b-4096-beef-7d241242407e", "metadata": {}, "outputs": [], "source": [ "gdf = brodata.gmw.get_data_in_extent([117700, 118000, 439400, 440400], kind=None, to_zip=fname)\n", "print(\"Read {} groundwater monitoring tubes from this zip-file\".format(len(gdf)))" ] }, { "cell_type": "markdown", "id": "c1023a60-c15a-4ac4-b563-a69aa438f18f", "metadata": {}, "source": [ "## From a zip file downloaded from BROloket or DINOloket\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "d0940612-fb8c-46d9-8339-8167e368ca5c", "metadata": {}, "outputs": [], "source": [ "fname = os.path.join(\n", " \"..\", \"..\", \"tests\", \"data\", \"r-calje@artesia-water-nl_2024-06-04-12-35-07.zip\"\n", ")\n", "brodata.util.read_zipfile(fname)" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }