Convert polyline river network to polygon river network

At large scales, rivers are delineated as lines. Typically river network data are in the form of polyline shapefile. To convert polyline network to polygon nework that is consistent is a quite challenging task. In GIS, the buffer approach is not returning the desired results as it returns a rounded buffer around the end polyline points.

For example, using the fixed distance buffer in QGIS with one segment the result is the following:

where each polyline segment creates a buffer polygon that overlaps with other.

The proposed workflow is not ideal but tackles some of the issues, while it returns a continuous representation of the river using polygons.

However the method is quite involved as requires some Matlab coding, and some experience with Houdini. A great advantage of the workflow is that it is non destructive, which means that modifications of the original input shapefile propagated at the end result without additional work.

Starting with Matlab, first we read the polyline shapefile:

Each river consists of multiple polyline segments. Therefore we have to group them. Here we use the field NAME to group the rivers.

The grouped rivers are stored into a temporary CVHMSTRM variable. Each polyline segment has shared nodes with other polyline segments, which are supposed to be connected. Here we make a unique list of river nodes.

Now that we have a list of unique river nodes we can create a graph, which is a structure that identifies the connections between nodes.

Graphs are powerful data classes that provide many functionalities. An example is the ability to plot them.

With the river graphs at our disposal we are able to identify the main rivers and separate them from the tributaries. The following lengthy code does two things. First Identifies the longest paths which are supposed to be the main rivers and the remaining paths connected to the longest one. Secondly calculates normal vectors for each river node. For the nodes that are connected to only one segment, the normal is equal to the segment normal. For the nodes that are connected to two segments the normal is the average of the two segment normals. Normals will be used later to create the river polygons.

Finally for each river a width and flow rate is added. While the width is a required information for the method the flow rate could be omitted. However when I export the river polygons I would like to have the flow rate associated with each polygon. The code to add width depends on the application and is not shown here.

Last we write the river information into files for reading into Houdini. Note that the coordinate values are divided by a large constant 100000. This is because software like Houdini, blender etc do not handle very well large coordinate values.

The format of the files is very simple

Now its time to launch Houdini.

Place a python node inside a geometry node. (See this post on how to achieve this).

Add a new parameter to the python interface and make sure that the name of the parameter is input_file . Then paste the following code to the python code area:

Next we are going to build a small graph to achieve the desired result.

Right after python code we have to remove duplicated nodes using the Fuse node. Then we use the Point-Old node to set the normals:

I have also added a Null node that does nothing other than improving visibility and organizing the graph.

If we turn on the display points and display normals options we can see the river polyline, the points and the point normals.

Next we lay down 2 Attribute VOP nodes. These nodes offset the main river by the width amount along the normal direction. In practice I created the positive offset and then duplicated to modify the negative offset.

By double clicking the attribute VOP (dive inside the node) we can modify the geometries at a different level. In addition we can see that the available nodes now are very different.

First we lay down a Get Attribute node to read the width information. For the Get Attribute to work we have to set float as Signature, First Input as Input method, Point as Attribute Class and type width for Attribute. As shown above the ptnum have to be connected to i1. In addition we lay down a constant float equal to 2 and a divide node to calculate the half width.

There is also a normalize node that is connected with the N value of the geometry. This is normalizing the normals of the nodes. The following figure shows the continuation of the Attribute VOP graph

The outcome of half width is further divided by the same large constant we use when writing the houdini files, which is then multiplied by the point normalized normals and added to point Position values. Finally the result of add operation is connected to P geometry output node which shifts the nodes by half width amount. The negative offset node is identical with the exception that between the divide1 and multiply1 we have added a negate node.

Next outside the Attribute VOP node we merge the two offset nodes and then add a skin node. The following figures show the result of the merge and the result of the skin node. For the skin to work properly we have to set some rules about which lines to connect. Set Quadrilaterals for connectivity. For Skin pick Skip every Nth primitive, and for N value use the expression   detail("../python2/","Nseg",0)  which sets as N value the number of stream segments. Note that in the initial python code we set a global attribute Nseg.

We can see in areas where the river has sharp bends some weird artifacts may be produced. These will be taken care later. However note how the river width changes smoothly between segments of different width.


Somehow the Skin operator does not transfer the Qrate attribute to the polygons and sets all Qrates to 0. A simple attribute VOP takes care of that. A get and a set attribute node does exactly that.

Then the fuse node with a certain tolerance resolve the artifacts displayed above

Finally we use a python node to write the polygons into a file to export them into a suitable format using the following python code.









Custom Colormaps in Matlab

Matlab colormaps are usefull to display continuous spatial information. More about colormap usage and options can be found under Matlab help file

However there are cases where the default colormaps are not sufficient. For example the figure below shows the hydraulic head trend over an 80-year simulation. For the areas with positive values the groundwater table is rising while in the areas with negative values the water table drops. However using the default colormap is not easy to distinguish these areas.



To improve the figure we are going to modify the colormap so that the color at value 0 is white, while the positive values will have a blue gradient from deep blue to white, and the negative values a gradient from white to deep red.

After we have plotted the figure we can obtain information about the color axis as follows

In my example cmax = 7.2 and cmin = -5.4.  We can also set our own custom limits for the colors by setting  ax.CLim = [-5 8]; . This will make the cmin = -5 and cmax = 8. This is very useful if one wants consistent colormaps between different figures with different range values.

Next we’ll create a variable that has values that range between cmin and cmax and identify the last row that is negative. In my case that was the 28th row (id = 28).

Next, for each row of the variable lcol we have to assign a color. The lcol(1) corresponds to cmin therefore we want to assign deep red (130,0,0) while the row id corresponds to 0 values which should have white color (255, 255, 255). The last row corresponds to cmax and we will assign deep blue (0, 0, 130). For the remaining row we will interpolate their values as follows:

custom_map is a 64×3 matrix. The columns correspond to the R, G and B normalized color values.

Next we set our custom map to be our colormap

Finally we display it and assign an explanatory text


In the above map we can easily distinguish between the areas where the water table rises or drops.

The full code for the above figure is given below with a minor change that controls the color resolution: