{ "cells": [ { "cell_type": "markdown", "id": "dcef197e-b2e7-4484-b83e-92d51176f385", "metadata": {}, "source": [ "# Inputting Data" ] }, { "cell_type": "markdown", "id": "7ce4abaa-3b40-41b8-8976-b8fdb264df25", "metadata": {}, "source": [ "This notebook demonstrates the various ways to provide input data to the `OptiWindNet`'s Network/Router API by initializing a `WindFarmNetwork()` instance.\n", "\n", "> **Note:** Many of the Jupyter notebooks provided include SVG figures as output. To ensure these visuals are displayed correctly in JupyterLab or Jupyter Notebook, make sure the notebook is marked as **trusted**.\n", "> *In JupyterLab, you can do this by pressing* `Ctrl + Shift + C` *and selecting* **Trust Notebook**." ] }, { "cell_type": "markdown", "id": "06194dbd", "metadata": {}, "source": [ "Import required modules" ] }, { "cell_type": "code", "execution_count": 1, "id": "fa29e2ac-26f7-4927-bebf-eeef30a5ab37", "metadata": {}, "outputs": [], "source": [ "from optiwindnet.api import WindFarmNetwork, load_repository" ] }, { "cell_type": "code", "execution_count": 2, "id": "e9caed29", "metadata": {}, "outputs": [], "source": [ "# Display figures as SVG in Jupyter notebooks\n", "%config InlineBackend.figure_formats = ['svg']" ] }, { "cell_type": "markdown", "id": "fca2771e-61e8-4f29-a6ae-8bbce26c4762", "metadata": {}, "source": [ "## Options for initializing `wfn`" ] }, { "cell_type": "markdown", "id": "fda312c3", "metadata": {}, "source": [ "There are 3 alternatives to initialize `WindFarmNetwork()` instance within **optiwindnet**:\n", "- From raw data or a preconstructed graph\n", "- From a `.yaml` file\n", "- From a `.osm.pbf` file" ] }, { "cell_type": "markdown", "id": "1bcfeda4-e9da-4305-b7ab-22c38f96f199", "metadata": {}, "source": [ "### Option 0: From raw data or a preconstructed graph" ] }, { "cell_type": "markdown", "id": "42397346", "metadata": {}, "source": [ "This option is used when you already have the wind farm layout and parameters defined in a Python data structure or *networkx.Graph*.\n", "\n", "You can directly pass either:\n", "\n", "* A: Raw wind farm layout data via coordinates: turbinesC, substationsC, borderC, etc.,\n", "which will be converted into a graph internally, or\n", "\n", "* B: A prebuilt networkx.Graph via the L parameter (e.g., loaded from external sources or computed elsewhere)." ] }, { "cell_type": "markdown", "id": "1086f5d4", "metadata": {}, "source": [ "#### A: Initialize via coordinates:" ] }, { "cell_type": "code", "execution_count": null, "id": "e93a9280-594e-4840-8ea6-ec2a0bd800b3", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# all coordinates are sequences of (x, y) pairs\n", "# if input coordinates are in arrays X and Y, use `np.hstack((X, Y))`\n", "borderC = np.array( # coordinate sequence defines the polygon, last-first segment implicit\n", " [[1951, 200], [1951, 1383], [386, 1383], [650, 708], [624, 678],\n", " [4, 1036], [4, 3], [1152, 3], [917, 819], [957, 854]],\n", " dtype=float)\n", "obstaclesC = [\n", " # first obstacle\n", " np.array([[1540, 920], [1600, 940], [1600, 1150], [1400, 1200]]),\n", " # [second obstacle] ...\n", "]\n", "substationsC = np.array([[696, 1063],], dtype=float)\n", "turbinesC = np.array(\n", " [[1940, 279], [1920, 703], [1475, 696], [1839, 1250],\n", " [1277, 1296], [442, 1359], [737, 435], [1060, 26],\n", " [522, 176], [87, 35], [184, 417], [71, 878]],\n", " dtype=float)\n", "cables = [(2, 1500.0), (5, 1800.0)]" ] }, { "cell_type": "code", "execution_count": 4, "id": "06fae7b8", "metadata": {}, "outputs": [], "source": [ "wfn01 = WindFarmNetwork(turbinesC=turbinesC, substationsC=substationsC, cables=cables, borderC=borderC, obstaclesC=obstaclesC)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5539b8f8-a4d6-4aa6-b1c3-faf04331f38a", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn01" ] }, { "cell_type": "markdown", "id": "7442e039", "metadata": {}, "source": [ "#### B: Initialize via the `L` parameter:\n", "\n", "Pass a prebuilt *Networkx.Graph* of the location geometry (`L`) to `WindFarmNetwork()`.\n", "\n", "> Note: `load_repository()` loads locations from all files in the given directory. If no path is given, it loads the locations distributed with *OptiWindNet*. For more details, see [Load repositories containing location data](a03_load_repositories.ipynb)." ] }, { "cell_type": "code", "execution_count": 6, "id": "72fe2805", "metadata": {}, "outputs": [], "source": [ "locations = load_repository()\n", "L_prebuilt = locations.seagreen\n" ] }, { "cell_type": "markdown", "id": "b410179a", "metadata": {}, "source": [ "Initialize `WindFarmNetwork()` instance with the loaded `L_prebuilt`:" ] }, { "cell_type": "code", "execution_count": 7, "id": "ec5d7fc6", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn02 = WindFarmNetwork(L=L_prebuilt, cables=7)\n", "wfn02" ] }, { "cell_type": "markdown", "id": "ac064931-e789-467f-8378-0e64d098d46a", "metadata": {}, "source": [ "### Option 1: From a YAML file:" ] }, { "cell_type": "markdown", "id": "57d65ab1", "metadata": {}, "source": [ "This option loads the location geometry from a `.yaml` file, which is useful for working with saved or external project files in this format. A sample code for generating `.yaml` files with proper structure are provided below." ] }, { "cell_type": "code", "execution_count": 8, "id": "501eb6be-c087-46c2-85cd-11b5de328c57", "metadata": {}, "outputs": [], "source": [ "with open('data/example_location.yaml', 'w') as yaml_file:\n", " yaml_file.write('''\n", "# coordinate format can be \"planar\" or \"latlon\"\n", "# - for \"latlon\" examples, see `optiwindnet/data/*.yaml`\n", "# - this field is optional, default is \"latlon\"\n", "# - coordinates are converted to floats, so floats may be used as well\n", "COORDINATE_FORMAT: planar\n", "\n", "# extents define a polygon:\n", "# - do not repeat the initial vertex at the end\n", "# - line breaks are optional\n", "EXTENTS: [\n", " [1951, 200],\n", " [1951, 1383],\n", " [386, 1383],\n", " [650, 708],\n", " [624, 678],\n", " [4, 1036],\n", " [4, 3],\n", " [1152, 3],\n", " [917, 819],\n", " [957, 854]\n", "]\n", "\n", "# obstacles is optional and must be a list of polygons (even if 1 obstacle)\n", "OBSTACLES: [\n", " [ # first obstacle\n", " [1540, 920],\n", " [1600, 940],\n", " [1600, 1150],\n", " [1400, 1200],\n", " ],\n", " # [second obstacle]\n", "]\n", "\n", "SUBSTATIONS: [\n", " [696, 1063],\n", "]\n", "\n", "TURBINES: [\n", " [1940, 279],\n", " [1920, 703],\n", " [1475, 696],\n", " [1839, 1250],\n", " [1277, 1296],\n", " [442, 1359],\n", " [737, 435],\n", " [1060, 26],\n", " [522, 176],\n", " [87, 35],\n", " [184, 417],\n", " [71, 878],\n", "]\n", "''')" ] }, { "cell_type": "markdown", "id": "d4f1fcec", "metadata": {}, "source": [ "Initialize `WindFarmNetwork()` instance with an exisiting `.yaml` file:" ] }, { "cell_type": "code", "execution_count": 9, "id": "a42be570-4222-4faf-a247-60611d55a09a", "metadata": {}, "outputs": [], "source": [ "wfn1 = WindFarmNetwork.from_yaml(filepath='data/example_location.yaml', cables=cables)" ] }, { "cell_type": "code", "execution_count": 10, "id": "5dc83b7b-d9be-48e0-9255-fabcef9cc7c2", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn1" ] }, { "cell_type": "markdown", "id": "c468e88c", "metadata": {}, "source": [ "### Option 2: From OSM.PBF input file\n", "> `osm.pbf` stands for *OpenStreetMap protobuffer format*." ] }, { "cell_type": "markdown", "id": "aa8ed17c", "metadata": {}, "source": [ "This option loads the location geometry from a `.osm.pbf` file, which is useful for working with saved or external project files in this format." ] }, { "cell_type": "markdown", "id": "1cb621d8", "metadata": {}, "source": [ ">The JOSM open-source map editor is recommended if using this format: . In addition, the JOSM plugin **pbf** is required to save in the `.osm.pbf` format. The plugin **opendata** is useful for importing many common GIS file formats.\n", ">\n", ">The OpenStreetMaps objects used for representing a windfarm location geometry are:\n", ">- *nodes*\n", ">- *ways*\n", ">- *multipolygons* (relation between closed ways)\n", ">\n", ">Wind turbines are represented by *nodes* with the tag `power=generator`. Substations are represented either by *nodes* or by closed *ways* tagged `power=substation` or `power=transformer`. Substations based on *ways* will be reduced to the point at the centroid of the polygon defined by the *way*.\n", ">\n", ">The border of the windfarm can be a closed *way* tagged `power=plant`. If obstacles are required, then the closed *way* for the border must be combined with the closed *ways* for the obstacles in a *multipolygon* with the tag `power=plant` (in which case the *ways* themselves should not be tagged).\n", ">\n", ">See `optiwindnet/data/*.osm.pbf` for more examples.\n", "\n", "Initialize `WindFarmNetwork()` instance with an existing `.osm.pbf` file:" ] }, { "cell_type": "code", "execution_count": 11, "id": "0baae019-4743-40cd-b7d9-bfbd49dc3137", "metadata": {}, "outputs": [], "source": [ "wfn2 = WindFarmNetwork.from_pbf('data/example_location.osm.pbf', cables=cables)" ] }, { "cell_type": "code", "execution_count": 12, "id": "45f77ec2-53e9-4858-8771-d6fa22cc032e", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn2" ] } ], "metadata": { "kernelspec": { "display_name": "OptiWindNet", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }