First of all, we will declare the folder where the shape and csv files are located. More data will be found in this same folder, so this variable will be used multiple times along the tutorial.

Declare paths
rootPath = r"D:\Workshop\data"

We now start creating the very basic network (made of branches and nodes). This information can be found in the GIS shape file network_Branches.shp. The required importer can be found in the library called SOBEKTutorialLibrary in the Libraries folder of the toolbox. You can gain access to all the methods included in that library by adding (and then running) the following code.

Import all functionality from a library file
from Libraries.SOBEKTutorialLibrary import *

The mask * (asterisk) written after the keyword import will load and make all the functions contained in the SOBEKTutorialLibrary library available for use. All of them will now also appear in the code completion dialog when you hit CTRL + SPACE.

At this point, it is important to make an observation. It is a good practice to place the lines of code used to import libraries at the top of your script, in a separate region. This will help you keep your code tidy and easier to understand, update and maintain. Therefore, place the line above completely at the top of your script, select it and then create a region for it (pressing CTRL+R). You can call it Import libraries.
The other line of code can be analogously integrated is a different region (for example one called Create network). The rest of the main code in this section can also be placed in this same region Create network. The code that you type in the future corresponding to imports would then go to the first region Import libraries. And the specific for each section to its respective region.

Let's now continue with the script. In this just imported library we have a function called CreateNetworkFromBranchShapeFile    which we will use to create the basic network.

Create basic network
network = CreateNetworkFromBranchShapeFile(rootPath + r"\network_Branches.shp")

When you run this line of the script, the network will be created using the indicated shape file and assigned to the variable network.

Now that we have a network with branches and nodes, we can continue with the import of the cross-sections. This is done with the function AddCrossSectionsToNetwork    . The shape file contains the location of the different cross sections as well as some generic properties. The tables specifying the cross section profiles are stored in the csv file.

Add cross-sections to network
AddCrossSectionsToNetwork(rootPath + r"\network_Cross Sections.shp", rootPath + r"\CrossSectionProfiles.csv", network)

Anagolously, for the bridges we use the function AddBridgesToNetwork  . As in the previous case, the shape file contains the location of the different bridges, as well as some other generic properties. The tables specifying the cross sections at the bridges are stored in the csv file.

Add bridges to network
AddBridgesToNetwork(rootPath + r"\network_Bridges.shp", rootPath + r"\BridgeProfiles.csv", network)

We will import the lateral sources and the weirs with one single function AddBranchObjectsFromShapeFile  . In this case, there is no table associated (this is an assumption we make for the sake of simplicity). Therefore, no associated csv file will need to be imported.

Add lateral sources and weirs to network
from Libraries.NetworkFunctions import BranchObjectType # Import libraries region

AddBranchObjectsFromShapeFile(BranchObjectType.LateralSource, rootPath + r"\network_Lateral Sources.shp", network)
AddBranchObjectsFromShapeFile(BranchObjectType.Weir , rootPath + r"\network_Weirs.shp", network)

 

The network is complete now that we have finished adding the structures to it. We can next create a new waterflow model and add this network to it.

First, we need to import two more libraries: SobekWaterFlowFunctions to be able to create a WaterFlowModel1D  and StandardFunctions for adding the model to the project with the AddToProject   function.

Create waterflow model and add network
from Libraries.SobekWaterFlowFunctions import *  # Import libraries region
from Libraries.StandardFunctions import *  # Import libraries region

flowModel = WaterFlowModel1D()
flowModel.Network = network
flowModel.Name = "Flow model"

AddToProject(flowModel)

 

Now, we will be able to see the model we have created so far by opening the corresponding view for it using the OpenModelMapViewWithBackground method.

Open map for model
OpenModelMapViewWithBackground(flowModel)

You should get something like this :

 

Note: The icon  indicates functions that you might find interesting to see how they have been built. You can do this by opening the corresponding library (another normal script python file on its own) in the toolbox and checking the code which defines the function or method you are interested in.

 

 

 

  • No labels