Convert polygons to points or lines in R

We often need to simplify the geometry of polygons into points or lines. How do you do that in R? Nothing easier than that – we resort to the functions gCentroid or gPointOnSurface from the rgeos library.

Let’s first load the libraries for reading and transforming vector data:

Next, we load our polygons. We will use the boundaries of the municipalities in Mazovia from the Polish Register of Borders (download):

Show loaded layer:

We can use gCentroid to create the centroids of the polygons:

Let’s add the centroids to the previously displayed layer:

In the rgdal library, there is another function for generalizing polygons to points – gPointOnSurface. This function is a bit slower, but it always creates points inside polygons, unlike gCentroid. Let’s create a layer with this tool:

Let’s add the result to the layers shown earlier:

As you can see, the points from both point layers are offset from each other.

Finally, we show how to convert the polygons into lines. To do this, we use the class Projection with the base function R as:

We check the class of the new object:

We have a line layer 🙂

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil