How Can I Generate a Triangular Network with R?

Tony Yen
Tony Yen
Apr 22, 2018 · 5 min read

In this article, we will demonstrate how you can generate a triangular network with the famous open source software R.

Why Do I Need a Triangular Network?

As I have said in my introduction article, I am planning to build a functional principal component analysis on a set of data.

While that does not mean that I will necessary need a triangular network, there are some unique properties I would like to use later. The most important properties of a functional EOF set based on triangular network is that it is continuous and the cross product is easy to compute.

Furthermore, if I want to develop a basic finite element method in the future, I probably will have to generate a triangular network anyway.

The Packages I Used in This Article

library(“tripack”, lib.loc=”~/R/win-library/3.4")
library(“maptools”, lib.loc=”~/R/win-library/3.4")
library(“raster”, lib.loc=”~/R/win-library/3.4")
library(“rgeos”, lib.loc=”~/R/win-library/3.4")

The package “tripack” was used for triangular network generation.

The package “maptools” was used to read the shape file of Taiwan.

“raster” and “rgeos” were used to perform most of the geo-statistical operations such as distance counting or combining polygons. I can’t remember which functions belong to which package specifically.

Anyways, let’s get started.

Reading a Shape File in R

So I downloaded a shape file of Taiwan and found the function to read it.

Reading a shape file in R wasn’t difficult at all. And combining all the polygons into one greater raster object wasn’t hard either. The functions readShapePoly() and aggregate() did their job well.

taiwan.union <- aggregate(taiwan)

The real problem was to know how R read these shape file. It was stored into a raster object. It had a very confusing data structure:

It took me some time to figure out the right way to call the coordinates of a specific polygon.

The coordinate of the k-th point in the j-th polygon of the i-th polygon set in the raster object obj would be:


After applying the raster object to aggregate(), there will only be one polygon set in that object.

However, since there are a lot of islands, I had to find the polygon with the most points to know which of the polygon was the main island.

for(i in 1:length(taiwan.union@polygons[[1]]@Polygons)){

The plot() function on the last line above verified that this searching rationale is valid.

I actually didn’t need a resolution that high, so I reduced the points on the polygon to an amount of 500:


Generating Points within the Polygon Using Geo-distance Contours

To generate a triangular network, you have to have some points within the polygon. I generated the points using contour lines of the geo-distance to the shore.

Unfortunately the gDistance() function only returns the value when the input point is outside the input polygon, so it took me some time to find an algorithm to generate the complement of Taiwan island.

land <- raster(extent(list(x=xlim,y=ylim)))
res(land) <- .1
landpolygon <- rasterToPolygons(land)
land_com <- gDifference(landpolygon,taiwan.union)

Basically the code above is just telling the computer to build a rectangular raster object, and then subtract it with the raster object I had before.

In the graph below, you can see this is a complement to the original polygon by the small gap on the northeastern edge.

Moving on, I generated a regular grid and calculate the distance of each grid point to the shore:

for(i in 1:length(x_in)){
for(j in 1:length(y_in)){
dis[i,j]=gDistance(readWKT(paste(“POINT”,”(“,xlim[1]+(i-1)*.1,” “,ylim[1]+(j-1)*.1,”)”,sep=””)),land_com)

And then I generated the corresponding geo-distance contour lines:

for(i in 1:length(dis.contour)){

Plotting everything together confirmed the validity of our coding so far.

for(i in 1:length(dis.contour)){

Generating the Triangular Network and Delete Irrelevant Triangles

To generate the triangular network, I first had to combine all the points together into one matrix:

for(i in 1:length(dis.contour)){

Then I called the tri.mesh() function to generate a triangular network with the matrix. This process was very straightforward.


But when I plotted the triangular network, something very wrong immediately appeared on the screen.

I had to come up with an algorithm to delete the triangles that were outside the border of the original polygon.

So I turned to another function, triangles(), which turns a triangulation object to a matrix containing the same information.

The matrix would be much easier to compute with, because each row represents a triangle, and the first three columns represents the three points of a specific triangle.

Therefore, I could filter out the triangles with all three border points. This algorithm would also delete some of the triangle on the edges that were still inside the island, but the overall error would still improve.

for(i in 1:nrow(tin_Taiwan_matrix)){

And that was basically it, though to plot the resulting network also required some effort.

for(i in 2:nrow(tin_Taiwan_matrix)){


And at last I got the graph which I have shown at the very beginning of this article:

What is Next?

Now that I get my triangular network, I would like to generate a functional principal component analysis with this network. I will explain what this really means and how I deal with it.

About This Series…

This article is part of the series My Coding Diary with R. To know how the series has been developing, check the intro article out:

Tony Yen

Written by

Tony Yen

A Taiwanese student who studies in the master program of Renewable Energy Engineering and Management in University of Freiburg.