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.









Make gif from images (Linux)

  • Gif

One tool to help with the task is the ImageMagick.

If it is not already installed on a terminal use the following:

To convert a series of images to gif use the following command

or if the folder contains only the png needed in the animation:

The resize option will shrink the gif so it can take up less space.

the delay controls the animation speed. Larger values result in slower animation

As with all Linux commands  convert --help will show all the available options.


  • Movies

The process to make video is also very similar. There is a tool ffmpeg that can be install using:

An example to convert a series of images into an mp4 movie is the following:

I’ll spare the details as there is already a detail description in this link.

Custom mesh visualization with Houdini

Houdini is not the typical software used for scientific visualizations but the possibilities that Houdini offers are truly amazing.

However the first step to use Houdini is to instruct it how to read data. In this guide I’ll describe the steps to read an element mesh. I assume that the mesh data file is in some custom format.

One option is  to convert the custom file into a typical file format like wavefront .obj and then read via a file node.

The option I’ll describe here is to use a python node that reads a custom mesh file. The format of the file is as follows

where np is the number of mesh nodes and nel the number of mesh elements. The next np lines are the coordinates of the nodes followed by the node ids that the elements are made of. Note that the file reads either triangular or quadrilateral elements. The triangular elements are indicated by setting 0 as the fourth id of the node element.

By default Houdini interface is split into 3 main windows. Place the cursor anywhere inside the network view window(bottom right), press tab, start typing the word geometry, press enter to confirm and left click to place a geometry node.

Hou_001 Hou_002

Double click to enter inside the geo1 node. Select the default file node and press delete to… delete it!

Inside the geo1 node, with the cursor still located anywhere inside the network view, press tab, type python and press enter to confirm and left click to place the python node. The window above the network view is where the properties of the selected node are shown. For the python node there is just an almost empty area where one can write python code. I’ll add one more property which is a gui element to select the mesh file.

Find the gear button at the top right corner of the property window. Click once and choose Edit Parameter Interface…


At the left panel select the type file and click the arrow between the 2 panels (the one that points to the right) to add a new parameter.


Highlight the newly created property and rename the Name and label as shown in the figure below


Make sure you spell the Name exactly the same way

Next Apply and Accept to close the window and a new property will appear!


You can now click on the icon after the dropdown menu symbol to chose the mesh file.

Finally paste the following code inside the python Code area and you should be able to read a 2D mesh.

My example is a 2D mesh for the Central Valley groundwater basin


While this doesn’t seem to be something particularly interesting, it’s basic step for using the Houdini Beast!