# Net2INP

![Net2INP icon](docs/icon.png)

QGIS plugin that turns interconnected line / MultiLineString layers (or XYZ + links CSV data) into ready-to-run **EPANET `.inp`** hydraulic network files.

## Screenshots

### Network selection in QGIS

Select LineString / MultiLineString layers in Net2INP; selected layers are highlighted on the map. Generate the `.inp` with calculated lengths and default (or attribute) properties.

![Net2INP in QGIS — layer selection and highlighted network](docs/screenshot_qgis.png)

### Network loaded in EPANET

Open the generated `.inp` in EPANET — junctions, pipes, lengths, diameters, roughness and other properties are already filled in.

![Network opened in EPANET with properties](docs/screenshot_epanet.png)

## Features

### From line layers
- Lists all LineString / MultiLineString layers in the project
- Select / deselect layers; selected layers are **highlighted in red** on the map
- Automatically detects junctions by snapping endpoints within a user-defined tolerance
- Merges degree-2 connections into continuous pipes (optional, on by default)
- Calculates pipe lengths from GIS geometry (CRS / ellipsoid aware)
- Uses intermediate vertices of MultiLineStrings as EPANET `[VERTICES]`
- Optional attribute mapping for diameter, roughness and pipe ID
- Writes complete `.inp` with `[JUNCTIONS]`, `[PIPES]`, `[COORDINATES]`, `[VERTICES]`, `[OPTIONS]` …
- Export network as CSV (nodes XYZ or pipes)

### From XYZ / CSV
- Load a **nodes CSV** (ID, X, Y, Z/Elev, optional Type = J/R/T, Demand, Head)
- Optionally load a **links CSV** (From, To, Length, Diameter, Roughness)
- Auto-detect common column names
- Designate junctions, reservoirs or tanks via a Type column
- Lengths calculated from coordinates when not supplied

### Defaults & options
- Default pipe diameter, roughness, minor loss, junction elevation & demand
- Default reservoir head and tank geometry (init / min / max level, diameter)
- Flow units (LPS, GPM, CFS, MLD, …) and head-loss formula (H-W / D-W / C-M)
- Optional dummy reservoir when none exists (EPANET requires at least one tank or reservoir)
- Optional preview nodes layer (`Net2INP_Nodes`) on the map

## Requirements

- QGIS 3.16 or later (Qt5 and Qt6 compatible)
- No extra Python packages required

## Installation

1. **Plugins → Manage and Install Plugins → Install from ZIP**
2. Select `Net2INP.zip`
3. Enable the plugin
4. Toolbar button / menu: **Plugins → Net2INP**

## Quick start – line layers

1. Load your pipe network shapefiles / GPKG (LineString or MultiLineString)
2. Open **Net2INP**
3. On the **Line Layers → INP** tab, tick the layers that form the network (they highlight red)
4. Adjust snap tolerance if needed (default 0.5 map units)
5. Optionally map diameter / roughness attribute fields
6. Set defaults and units on the **Defaults & Options** tab
7. Choose an output `.inp` path
8. Click **Generate INP from Line Layers**

## Quick start – CSV

1. Prepare a nodes file, e.g.:

```csv
id,x,y,z,type,demand
J1,100,200,50,J,10
J2,150,200,48,J,5
R1,50,200,55,R,
```

2. (Optional) links file:

```csv
id,from,to,length,diameter,roughness
P1,R1,J1,50,200,100
P2,J1,J2,50,150,100
```

3. On the **XYZ / CSV → INP** tab load the files, click **Load columns**, map fields
4. Generate the `.inp`

## Output `.inp` structure

```
[TITLE]
Network generated by Net2INP

[JUNCTIONS]
;ID                  Elev         Demand
 J1                  50.0000      10.0000
 ...

[RESERVOIRS]
;ID                  Head
 R1                  55.0000

[PIPES]
;ID   Node1  Node2  Length  Diam  Roughness  MinorLoss  Status
 P1   R1     J1     50.0    200   100        0          Open
 ...

[COORDINATES]
;Node  X-Coord  Y-Coord
 ...

[VERTICES]
;Link  X-Coord  Y-Coord
 ...

[OPTIONS]
 Units              LPS
 Headloss           H-W

[END]
```

## Tips

- Snap tolerance should match the precision of your data (a few centimetres to a few metres).
- If lines do not share exact vertices, increase the tolerance so connecting endpoints become the same junction.
- Attribute values override the defaults when a field is mapped and the value is numeric.
- Terminal nodes (pipe ends) are required by EPANET and are written with demand = 0.

## Author

Hobby Bwanali  
email: hobbybwanali@gmail.com  
https://github.com/hobbybwanali/Net2INP

## License

GNU General Public License v2 (or later). See `LICENSE`.
