|
| 1 | +""" |
| 2 | +Exporting trace data with, e.g., length and azimuth attributes included |
| 3 | +======================================================================= |
| 4 | +
|
| 5 | +``fractopo`` provides ready-to-use calculations for attributes such as the |
| 6 | +length and orientation of fracture traces or branches. These attributes, |
| 7 | +calculated with ``fractopo``, might be useful for use elsewhere. |
| 8 | +
|
| 9 | +Two examples are provided: |
| 10 | +
|
| 11 | +1. Call of ``fractopo`` provided functions to calculate length and |
| 12 | + orientation into new columns in a GeoDataFrame. |
| 13 | +2. Use of builtin ``Network`` functionality to realize these attributes |
| 14 | + into the ``trace_gdf`` used by the network and then exporting it |
| 15 | + afterwards. |
| 16 | +""" |
| 17 | + |
| 18 | +# %% |
| 19 | +# Initializing and loading of example trace and area data |
| 20 | +# ------------------------------------------------------- |
| 21 | + |
| 22 | +from pathlib import Path |
| 23 | +from tempfile import TemporaryDirectory |
| 24 | + |
| 25 | +import geopandas as gpd |
| 26 | +import matplotlib.pyplot as plt |
| 27 | + |
| 28 | +from fractopo.analysis.network import Network |
| 29 | +from fractopo.general import Col, determine_azimuth |
| 30 | + |
| 31 | +BASE_DIR_PATH = Path("..") |
| 32 | + |
| 33 | +KB11_TRACE_GDF = gpd.read_file( |
| 34 | + BASE_DIR_PATH.joinpath( |
| 35 | + Path("tests/sample_data/KB11/KB11_traces.geojson") |
| 36 | + ) |
| 37 | +) |
| 38 | +KB11_AREA_GDF = gpd.read_file( |
| 39 | + BASE_DIR_PATH.joinpath( |
| 40 | + Path("tests/sample_data/KB11/KB11_area.geojson") |
| 41 | + ) |
| 42 | +) |
| 43 | + |
| 44 | +# %% |
| 45 | +# 1. Use of builtin ``Network`` functionality |
| 46 | +# ------------------------------------------------------------------ |
| 47 | +# This method will not modify the trace geometries in any way. |
| 48 | +# Only the attributes are calculated. |
| 49 | + |
| 50 | +# Create copy so that the original KB11_TRACE_GDF is not modified |
| 51 | +kb11_trace_gdf_1 = KB11_TRACE_GDF.copy() |
| 52 | + |
| 53 | +# %% |
| 54 | +# Calculate length and azimuth |
| 55 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +# ``geopandas``, through ``shapely``, already provides easy |
| 58 | +# calculation of geometry lengths using the length attribute |
| 59 | +kb11_trace_gdf_1["length"] = kb11_trace_gdf_1.geometry.length |
| 60 | + |
| 61 | +# How to calculate the azimuth, i.e. orientation, is more subjective. |
| 62 | +# ``fractopo`` defaults to a simple approach, where the orientation |
| 63 | +# is defined solely by the start and end points of a trace. |
| 64 | +kb11_trace_gdf_1["azimuth"] = [ |
| 65 | + determine_azimuth(line=trace, halved=True) |
| 66 | + for trace in kb11_trace_gdf_1.geometry.values |
| 67 | +] |
| 68 | + |
| 69 | +# %% |
| 70 | +# Export trace GeoDataFrame |
| 71 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +# Do not use TemporaryDirectory yourself if you want to persist the data! |
| 74 | +with TemporaryDirectory() as tmp_dir: |
| 75 | + output_path_1 = Path(tmp_dir).joinpath("KB11_traces_with_length_and_azimuth.gpkg") |
| 76 | + kb11_trace_gdf_1.to_file(output_path_1, driver="GPKG") |
| 77 | + |
| 78 | + |
| 79 | +# %% |
| 80 | +# 2. Call of ``fractopo`` provided functions |
| 81 | +# ------------------------------------------------------------------ |
| 82 | +# This method can modify the traces by, e.g., truncating them to |
| 83 | +# the target area (``truncate_traces=True``). |
| 84 | + |
| 85 | + |
| 86 | +KB11_NETWORK = Network( |
| 87 | + name="KB11", |
| 88 | + trace_gdf=KB11_TRACE_GDF, |
| 89 | + area_gdf=KB11_AREA_GDF, |
| 90 | + truncate_traces=True, |
| 91 | + circular_target_area=False, |
| 92 | + determine_branches_nodes=True, |
| 93 | + snap_threshold=0.001, |
| 94 | + azimuth_set_names=("N-S", "E-W"), |
| 95 | + azimuth_set_ranges=((135, 45), (45, 135)), |
| 96 | +) |
| 97 | + |
| 98 | +# %% |
| 99 | +# Check that length and azimuth columns do not pre-exist in network |
| 100 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +assert ( |
| 103 | + len( |
| 104 | + set(KB11_NETWORK.trace_gdf.columns).intersection( |
| 105 | + {Col.LENGTH.value, Col.AZIMUTH.value} |
| 106 | + ) |
| 107 | + ) |
| 108 | + == 0 |
| 109 | +) |
| 110 | + |
| 111 | +# %% |
| 112 | +# Access trace length and orientation arrays |
| 113 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 114 | + |
| 115 | +KB11_NETWORK.trace_length_array |
| 116 | +KB11_NETWORK.trace_azimuth_array |
| 117 | + |
| 118 | +# Check that underlying GeoDataFrame now contains columns for length and |
| 119 | +# azimuths |
| 120 | +assert ( |
| 121 | + len( |
| 122 | + set(KB11_NETWORK.trace_gdf.columns).intersection( |
| 123 | + {Col.LENGTH.value, Col.AZIMUTH.value} |
| 124 | + ) |
| 125 | + ) |
| 126 | + == 2 |
| 127 | +) |
| 128 | + |
| 129 | +# %% |
| 130 | +# Export trace GeoDataFrame |
| 131 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 132 | + |
| 133 | +# Do not use TemporaryDirectory yourself if you want to persist the data! |
| 134 | +with TemporaryDirectory() as tmp_dir: |
| 135 | + output_path_2 = Path(tmp_dir).joinpath("KB11_traces_with_network_attributes.gpkg") |
| 136 | + KB11_NETWORK.trace_gdf.to_file(output_path_2, driver="GPKG") |
| 137 | + |
| 138 | +# %% |
| 139 | +# Show a preview of the exported data as a table |
| 140 | +# ----------------------------------------------------------- |
| 141 | + |
| 142 | +preview_df = KB11_NETWORK.trace_gdf[[Col.LENGTH.value, Col.AZIMUTH.value]].head(5) |
| 143 | + |
| 144 | +fig, ax = plt.subplots(figsize=(12, 4)) |
| 145 | +ax.axis("off") |
| 146 | +tbl = ax.table(cellText=preview_df.values, colLabels=preview_df.columns, loc="center") |
| 147 | +tbl.set_fontsize(16) |
| 148 | +tbl.scale(1.2, 1.2) |
| 149 | +ax.set_title("Preview of exported trace attributes") |
0 commit comments