{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data structures\n", "\n", "GeoST uses standardized internal data structures and data validation to ensure that the\n", "functionality it offers can always be applied reliably. Before we discuss the relevant\n", "data structures, we introduce some terminology. Subsurface data come in many different\n", "forms (e.g., boreholes, CPTs, or 3D models), but we distinguish between two main types:\n", "\n", "- **Survey**: the actual measurements (i.e., raw data) of the subsurface. These can include\n", " boreholes, CPTs, well logs, seismic or EM lines, and others.\n", "\n", "- **Model**: for example, a 3D voxel or layer model or a geological map. These are the\n", " result of analyses using **survey** data (e.g., Kriging interpolation) and are therefore\n", " considered an interpretation of the subsurface.\n", "\n", "GeoST provides different data structures to handle these different kinds of data. These\n", "will be introduced in this section of the user guide.\n", "\n", "## Collection\n", "\n", "For **survey** data, the core data structure is a\n", "[`geost.Collection`](../api_reference/collection.rst), which holds the data in a header\n", "and data table. Basically, these tables contain:\n", "\n", "- **header**: metadata and spatial information for each survey.\n", "- **data**: the logged data of the surveys.\n", "\n", "The header and data tables have a one-to-many relationship: one survey (e.g., a borehole)\n", "is represented by one row in the header and multiple rows in the data.\n", "\n", "Typically, available types of subsurface data include point-like data, such as boreholes,\n", "CPTs, and well logs, or line-like data, such as seismic, GPR, and EM data. These can all\n", "be stored in a `Collection`. By default, the available reader functions for different\n", "types of survey data return a `Collection` (see [Survey data](./survey_data.ipynb) section\n", "in this user guide).\n", "\n", "While working with a `Collection`, selections change the contents of the header and data\n", "tables, but the `Collection` automatically maintains alignment between the two. Therefore,\n", "users can safely select and analyse the data while ensuring consistency. It is recommended\n", "to work with a `Collection` by default, unless you specifically need to work with only the\n", "header or data table.\n", "\n", "Let's first look at a `Collection` containing borehole data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geost\n", "\n", "# Load the Utrecht Science Park example borehole data\n", "collection = geost.data.boreholes_usp()\n", "collection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview of attributes and methods\n", "\n", "The [`Collection`](../api_reference/collection.rst) class implements many different attributes\n", "as well as analysis and selection methods. A short summary of the relevant attributes and\n", "methods is presented here. A full overview can be found in the\n", "[`Collection` API reference](../api_reference/collection.rst).\n", "\n", "**Attributes**\n", "\n", "- `data`: Data table of the `Collection`.\n", "- `header`: Header table of the `Collection`.\n", "- `crs`: Current coordinate reference system (CRS) of the `Collection`.\n", "- `vertical_datum`: Current vertical datum of the `Collection`.\n", "\n", "**Reference systems**\n", "\n", "- `set_crs`: Set the CRS of the `Collection`.\n", "- `to_crs`: Convert the current `Collection` CRS to the specified CRS.\n", "- `set_vertical_datum`: Similar to `set_crs`, but for the vertical datum (e.g., NAP =\n", " EPSG:5709).\n", "\n", "\n", "**Analysis**\n", "\n", "- `get_cumulative_thickness`: Return the cumulative thickness for any specified criteria.\n", "- `get_layer_top`: Return the top depth at which a specified layer occurs.\n", "\n", "**Spatial selections**\n", "\n", "- `select_within_bbox`: Select data points in the `Collection` within a bounding box.\n", "- `select_with_points`: Select data points in the `Collection` within a specified distance\n", " of other point geometries.\n", "- `select_with_lines`: Select data points in the `Collection` within a specified distance\n", " of line geometries.\n", "- `select_within_polygons`: Select data points in the `Collection` within polygon geometries.\n", "\n", "**Conditional selections**\n", "\n", "- `select_by_values`: Select data points in the `Collection` based on the presence of\n", " certain values in one or more data columns.\n", "- `select_by_length`: Select data points in the `Collection` based on length requirements.\n", "- `select_by_depth`: Select data points in the `Collection` based on depth constraints.\n", "\n", "**Slicing**\n", "\n", "- `slice_depth_interval`: Slice boreholes in the `Collection` to the specified depth interval.\n", "- `slice_by_values`: Slice boreholes in the `Collection` based on values (e.g., keep only\n", " sand layers and remove all others)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Header table\n", "\n", "In a `Collection`, the header table is always a\n", "[`geopandas.GeoDataFrame`](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.html)\n", "instance, which the `Collection` ensures internally. The header contains the spatial location\n", "of each survey in the `\"geometry\"` column, as well as metadata such as the surface level,\n", "end depth, and other information. Let's take a look at the header:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Type of header: {type(collection.header)}\")\n", "collection.header.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the `Collection` above, the geometry column contains [`shapely.Point`](https://shapely.readthedocs.io/en/stable/reference/shapely.Point.html)\n", "geometries. For 2D line data (e.g., seismic data), this would instead contain\n", "`shapely.LineString` geometries. Each entry in the header (i.e., row in the\n", "`GeoDataFrame`) corresponds to a specific survey, such as a borehole or seismic line.\n", "\n", "The header is not limited to the columns shown above. Any number of columns can be added\n", "to the header to provide additional information about the surveys. Various analysis methods\n", "can be used for this; see, for example, the\n", "[`Collection.spatial_join`](../api_reference/generated/geost.base.Collection.spatial_join.rst)\n", "method.\n", "\n", "If you are only interested in survey locations and/or metadata, it is advisable to work\n", "directly with the header object to avoid the (small) additional overhead of a `Collection`\n", "object. This overhead is caused by checks of the header against the data after operations\n", "to ensure alignment between the two. If you are only working with the header or data table,\n", "GeoST functionality is still available through `DataFrame` and `GeoDataFrame` accessors,\n", "which we will cover in a later [section](#geost-accessor).\n", "\n", "### Data table\n", "\n", "The data table is generally a\n", "[`pandas.DataFrame`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html)\n", "instance, although it can also be a `GeoDataFrame`. The data table holds all the logged\n", "data for each survey. Let's take a look at it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Type of data: {type(collection.data)}\")\n", "collection.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we see that a single survey contains multiple rows, which in this case represent\n", "\"layers\", as we are looking at borehole data. For survey data, we can generally distinguish\n", "between **\"layered\"** and **\"discrete\"** data:\n", "\n", "- **Layered** data contains information that is logged in terms of layers (i.e., depth\n", " intervals over which properties are the same), with **\"top\"** and **\"bottom\"** information\n", " for each layer.\n", "\n", "- **Discrete** data contains information that is logged at discrete intervals (e.g., every\n", " 20 cm), with **\"depth\"** information for each depth interval.\n", "\n", "GeoST can treat both types of data interchangeably, and provides ways to combine one type\n", "with the other. As with the header table, the data table is not limited to the columns shown\n", "above, and any number of columns can be added during analysis.\n", "\n", "If you are only interested in the survey measurements and do not need to work with geometries\n", "or any other additional header data, it is advisable to work directly with the data table to\n", "avoid the overhead of a `Collection` (i.e., maintaining alignment between the header and data\n", "tables). See the [accessor section](#geost-accessor). Several reader functions provide the\n", "option to return a `DataFrame` instead of a `Collection` by setting `as_collection=False`\n", "when using the function.\n", "\n", "\n", "### Positional columns\n", "\n", "GeoST requires certain data columns to be present to ensure that `Collection` methods can\n", "work. These are referred to as \"positional columns\". The required columns differ depending\n", "on the method being used. For example,\n", "[`Collection.slice_depth_interval`](../api_reference/generated/geost.base.Collection.slice_depth_interval.rst)\n", "requires depth information about the surface level of surveys and the depth of layers, while\n", "[`Collection.select_with_points`](../api_reference/generated/geost.base.Collection.select_with_points.rst)\n", "requires valid geometries.\n", "\n", "The presence of depth information or a valid geometry is therefore required for these methods\n", "to work. However, both are optional at the `Collection` level. The `slice_depth_interval`\n", "method does not need geometry to work, while `select_with_points` does not need depth\n", "information. Their presence is therefore only required when you want to use the corresponding\n", "methods.\n", "\n", "This was chosen as a design principle to provide maximum flexibility for users with different\n", "needs. The only mandatory requirement is a column that identifies each individual survey\n", "(e.g., `\"nr\"`) in both the header and data tables.\n", "\n", "```{note}\n", "See the [Positional columns](./survey_data.ipynb#positional-columns) section on the\n", "[Survey data](./survey_data.ipynb) page of this User guide for specific information on\n", "positional columns.\n", "```\n", "\n", "## GeoST accessor\n", "\n", "When you only need to work with either the header or data table, all methods available in\n", "`Collection` are also available when working directly with the header\n", "[`GeoDataFrame`](https://geopandas.org/en/stable/docs/reference/geodataframe.html) or data\n", "[`DataFrame`](https://pandas.pydata.org/docs/reference/frame.html). This is achieved through\n", "a so-called \"accessor\", which [extends](https://pandas.pydata.org/docs/development/extending.html)\n", "the functionality of DataFrames. Under the hood, every `Collection` method also uses this\n", "accessor.\n", "\n", "Below, we briefly demonstrate how this works by comparing the usage of `Collection` methods\n", "with the corresponding accessor methods. The accessor can be accessed by calling\n", "[`.gst`](../api_reference/accessor.rst) on a `GeoDataFrame` or `DataFrame`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create separate `header` and `data` variables for the demonstration.\n", "header = collection.header\n", "data = collection.data\n", "\n", "print(header.gst)\n", "print(data.gst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, for both the header and data table, it prints\n", "`geost.accessor.GeostFrame` objects. This is the object where most GeoST methods are actually\n", "implemented, which is why any method available in a `Collection` is also accessible through\n", "the `.gst` accessor.\n", "\n", "Let's first compare the usage of\n", "[`select_within_bbox`](../api_reference/generated/geost.accessor.GeostFrame.select_within_bbox.rst),\n", "a method that typically operates on the header table because it is a spatial method and the\n", "header contains the spatial information about the surveys. As the name suggests, this method\n", "selects the surveys located within a specific bounding box extent. All we need to do to call\n", "this method on the header `GeoDataFrame` is use `.gst` in between, as shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection_select = collection.select_within_bbox(139_500, 455_000, 140_000, 455_500)\n", "header_select = header.gst.select_within_bbox(139_500, 455_000, 140_000, 455_500)\n", "\n", "print(\"collection_select.data:\", collection_select.header, sep=\"\\n\")\n", "print(\"header_select:\", header_select, sep=\"\\n\")\n", "\n", "header_select.gst # Selection result also has the .gst accessor and methods available" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the selection result is exactly the same. After selecting from the header\n", "table, the `.gst` accessor remains available on the selection result, allowing us to make\n", "further selections or chain several selection methods, for example.\n", "\n", "Methods that would typically operate on the data table, such as\n", "[`slice_by_values`](../api_reference/generated/geost.accessor.GeostFrame.slice_by_values.rst),\n", "work in exactly the same way. All we need to do to call this method on the `\"data\"`\n", "`DataFrame` is use [`.gst`](../api_reference/accessor.rst) in between, as shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select boreholes which contain clay anywhere as the main lithology.\n", "collection_select = collection.slice_by_values(\"lith\", \"K\")\n", "data_select = data.gst.slice_by_values(\"lith\", \"K\")\n", "\n", "print(\"collection_select.data:\", collection_select.data[[\"nr\", \"lith\"]], sep=\"\\n\")\n", "print(\"data_select:\", data_select[[\"nr\", \"lith\"]], sep=\"\\n\")\n", "\n", "data_select.gst # Selection result also has the .gst accessor and methods available" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again the selection result is exactly the same and after the selection, the [`.gst`](../api_reference/accessor.rst) accessor remains available." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model data\n", "\n", "GeoST also supports working with (geological) model data and provides methods for combining\n", "these with survey data. See the [Model data](./model_data.ipynb) section in the user\n", "guide for more detailed information about working with model data in GeoST." ] } ], "metadata": { "kernelspec": { "display_name": "default", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.5" } }, "nbformat": 4, "nbformat_minor": 2 }