## tools.DemandConnectionTool


The connection tool from MIKE+ for demand allocations.


Usage

``` python
tools.DemandConnectionTool(database)
```


Connects the points of the demand allocations table (`mw_DemAlloc`) in a water distribution model to junctions or pipes. Each connected demand allocation gets its `ConnectionTypeNo` and its `JunctionID` or `PipeID` set, and a line in the demand allocation connections table (`mw_DemAllocConn`).


## Examples

Connect every demand allocation to the nearest junction within 50 metres.

``` python
>>> from mikeplus import Database
>>> from mikeplus.tools import DemandConnectionTool
>>> db = Database("path/to/model.sqlite")
>>> DemandConnectionTool(db).run("nearest_junction", max_distance=50.0)
>>> db.close()
```


## Methods

| Name | Description |
|----|----|
| [__init__()](#__init__) | Initialize the DemandConnectionTool with the given Database. |
| [run()](#run) | Connect demand allocations to the network. |

------------------------------------------------------------------------


#### \_\_init\_\_()


Initialize the DemandConnectionTool with the given Database.


Usage

``` python
__init__(database)
```


##### Parameters


`database: Database`  
A Database object for a MIKE+ water distribution model.


------------------------------------------------------------------------


#### run()


Connect demand allocations to the network.


Usage

``` python
run(
    method="nearest_junction",
    *,
    demand_ids=None,
    max_distance=None,
    max_diameter=None,
    junction_ids=None,
    pipe_ids=None,
    where=None
)
```


A demand allocation connected to a junction afterwards has `ConnectionTypeNo` 1 and no `PipeID`; one connected to a pipe has `ConnectionTypeNo` 2 and no `JunctionID`. A demand allocation for which no target is found loses any connection it had: it gets `ConnectionTypeNo` 1, no `JunctionID`, no `PipeID` and no connection line.


##### Parameters


`method: str = ``"nearest_junction"`  
How to choose the target, by default "nearest_junction":

- "nearest_junction": the junction nearest to the demand allocation.
- "junction_by_nearest_pipe": the end junction, nearest to the demand allocation, of the nearest pipe.
- "junction_by_pipe_id": the end junction, nearest to the demand allocation, of the pipe whose MUID equals the demand allocation's MUID. Demand allocations without such a pipe, or whose pipe does not meet [where](queries.BaseQuery.md#mikeplus.queries.BaseQuery.where), are skipped and left as they are.
- "nearest_pipe": the pipe nearest to the demand allocation.

`demand_ids: Iterable[str] = None`  
MUIDs of the demand allocations to connect. By default all of them; an empty iterable connects none.

`max_distance: float = None`  
Search distance, in the model's length unit, for "nearest_junction", "junction_by_nearest_pipe" and "nearest_pipe". By default the search is not limited.

`max_diameter: float = None`  
Only pipes with at most this diameter, in the model's diameter unit, are candidates for "junction_by_nearest_pipe" and "nearest_pipe". By default all pipes are.

`junction_ids: Iterable[str] = None`  
MUIDs of the junctions that "nearest_junction" may connect to. By default all junctions.

`pipe_ids: Iterable[str] = None`  
MUIDs of the pipes that "junction_by_nearest_pipe" and "nearest_pipe" may connect to. By default all pipes.

`where: str = None`  
SQL condition the targets must meet: on `mw_Junction` for "nearest_junction", otherwise on `mw_Pipe`. For "junction_by_pipe_id" it is a .NET format string: `{0}` is replaced by the pipe's MUID, and literal braces must be doubled.


##### Raises


`TypeError`  
If `demand_ids`, `junction_ids` or `pipe_ids` is a single string instead of an iterable of MUIDs.

`ValueError`  
If `method` is not one of the values above, an argument is given that does not apply to `method`, a distance or diameter limit is not a positive finite number, `junction_ids` or `pipe_ids` is empty, or `demand_ids`, `junction_ids` or `pipe_ids` names a feature that does not exist.

`DatabaseError`  
If MIKE+ fails to connect the demand allocations. If it fails partway through, the demand allocations it had already processed keep their new connections, possibly alongside a stale `JunctionID` or `PipeID`.
